Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/plugins/configure-aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { commands } from "vscode";
import { createPlugin } from "../plugins.ts";
import { configureAwsProfiles } from "../utils/configure-aws.ts";

export default createPlugin("configure-aws", ({ context, telemetry }) => {
context.subscriptions.push(
commands.registerCommand("localstack.configureAwsProfiles", async () => {
await configureAwsProfiles({
telemetry: telemetry,
notifyNoChangesMade: true,
});
}),
);
});
export default createPlugin(
"configure-aws",
({ context, telemetry, outputChannel }) => {
context.subscriptions.push(
commands.registerCommand("localstack.configureAwsProfiles", async () => {
await configureAwsProfiles({
telemetry: telemetry,
notifyNoChangesMade: true,
outputChannel,
});
}),
);
},
);
16 changes: 13 additions & 3 deletions src/utils/configure-aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as os from "node:os";
import * as path from "node:path";

import { window } from "vscode";
import type { LogOutputChannel } from "vscode";

import { parseIni, serializeIni, updateIniSection } from "./ini-parser.ts";
import type { IniFile, IniSection } from "./ini-parser.ts";
Expand Down Expand Up @@ -98,11 +99,16 @@ async function getProfile(filename: string, profileName: string) {
return { contents, iniFile, section };
}

async function dnsResolveCheck(): Promise<boolean> {
async function dnsResolveCheck(
outputChannel: LogOutputChannel | undefined,
): Promise<boolean> {
try {
const addresses = await resolve("test.localhost.localstack.cloud");
return addresses.includes("127.0.0.1");
} catch (error) {
outputChannel?.debug(
`[aws-profile]: Could not resolve "test.localhost.localstack.cloud". Falling back to "http://127.0.0.1:4566" for the endpoint_url in AWS profile "localstack". Your system may have DNS Rebind Protection enabled, which can block custom DNS names like "localhost.localstack.cloud"`,
);
return false;
}
}
Expand All @@ -115,6 +121,7 @@ async function configureAwsConfigProfile(
iniFile: IniFile,
section: IniSection | undefined,
overrideDecision: OverrideDecision | undefined = undefined,
outputChannel: LogOutputChannel | undefined,
): Promise<boolean | undefined> {
const awsConfigFilenameReadable = awsConfigFilename.replace(
os.homedir(),
Expand All @@ -128,7 +135,7 @@ async function configureAwsConfigProfile(
// User chose to override the existing profile.

// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck();
const isDnsResolved = await dnsResolveCheck(outputChannel);
const endpointUrl = isDnsResolved
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;
Expand All @@ -155,7 +162,7 @@ async function configureAwsConfigProfile(

// LocalStack profile does not exist: create it.
// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck();
const isDnsResolved = await dnsResolveCheck(outputChannel);
const endpointUrl = isDnsResolved
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;
Expand Down Expand Up @@ -276,6 +283,7 @@ export async function configureAwsProfiles(options: {
forceOverride?: boolean; // for testing purposes
notifyNoChangesMade?: boolean;
origin?: "manual_trigger" | "extension_startup";
outputChannel?: LogOutputChannel;
}) {
const trigger = options.origin ?? "manual_trigger";
const startedAt = new Date().toISOString();
Expand Down Expand Up @@ -373,6 +381,7 @@ export async function configureAwsProfiles(options: {
configIniFile,
configSection,
overrideDecision,
options.outputChannel,
),
configureCredentialsProfile(
awsCredentialsFilename,
Expand Down Expand Up @@ -403,6 +412,7 @@ export async function configureAwsProfiles(options: {
configIniFile,
configSection,
overrideDecision,
options.outputChannel,
);
window.showInformationMessage(
'Successfully added the AWS profile named "localstack" to "~/.aws/config".',
Expand Down
Loading