Skip to content

Commit f07a3d0

Browse files
authored
chore: log DNS resolution failure (#43)
Show warning message to guide user if we detect DNS rebind protection.
1 parent 4528033 commit f07a3d0

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/plugins/configure-aws.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { commands } from "vscode";
33
import { createPlugin } from "../plugins.ts";
44
import { configureAwsProfiles } from "../utils/configure-aws.ts";
55

6-
export default createPlugin("configure-aws", ({ context, telemetry }) => {
7-
context.subscriptions.push(
8-
commands.registerCommand("localstack.configureAwsProfiles", async () => {
9-
await configureAwsProfiles({
10-
telemetry: telemetry,
11-
notifyNoChangesMade: true,
12-
});
13-
}),
14-
);
15-
});
6+
export default createPlugin(
7+
"configure-aws",
8+
({ context, telemetry, outputChannel }) => {
9+
context.subscriptions.push(
10+
commands.registerCommand("localstack.configureAwsProfiles", async () => {
11+
await configureAwsProfiles({
12+
telemetry: telemetry,
13+
notifyNoChangesMade: true,
14+
outputChannel,
15+
});
16+
}),
17+
);
18+
},
19+
);

src/utils/configure-aws.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as os from "node:os";
44
import * as path from "node:path";
55

66
import { window } from "vscode";
7+
import type { LogOutputChannel } from "vscode";
78

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

101-
async function dnsResolveCheck(): Promise<boolean> {
102+
async function dnsResolveCheck(
103+
outputChannel: LogOutputChannel | undefined,
104+
): Promise<boolean> {
102105
try {
103106
const addresses = await resolve("test.localhost.localstack.cloud");
104107
return addresses.includes("127.0.0.1");
105108
} catch (error) {
109+
outputChannel?.debug(
110+
`[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"`,
111+
);
106112
return false;
107113
}
108114
}
@@ -115,6 +121,7 @@ async function configureAwsConfigProfile(
115121
iniFile: IniFile,
116122
section: IniSection | undefined,
117123
overrideDecision: OverrideDecision | undefined = undefined,
124+
outputChannel: LogOutputChannel | undefined,
118125
): Promise<boolean | undefined> {
119126
const awsConfigFilenameReadable = awsConfigFilename.replace(
120127
os.homedir(),
@@ -128,7 +135,7 @@ async function configureAwsConfigProfile(
128135
// User chose to override the existing profile.
129136

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

156163
// LocalStack profile does not exist: create it.
157164
// check if dnsResolveCheck is successful
158-
const isDnsResolved = await dnsResolveCheck();
165+
const isDnsResolved = await dnsResolveCheck(outputChannel);
159166
const endpointUrl = isDnsResolved
160167
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
161168
: `http://127.0.0.1:${DEFAULT_PORT}`;
@@ -276,6 +283,7 @@ export async function configureAwsProfiles(options: {
276283
forceOverride?: boolean; // for testing purposes
277284
notifyNoChangesMade?: boolean;
278285
origin?: "manual_trigger" | "extension_startup";
286+
outputChannel?: LogOutputChannel;
279287
}) {
280288
const trigger = options.origin ?? "manual_trigger";
281289
const startedAt = new Date().toISOString();
@@ -373,6 +381,7 @@ export async function configureAwsProfiles(options: {
373381
configIniFile,
374382
configSection,
375383
overrideDecision,
384+
options.outputChannel,
376385
),
377386
configureCredentialsProfile(
378387
awsCredentialsFilename,
@@ -403,6 +412,7 @@ export async function configureAwsProfiles(options: {
403412
configIniFile,
404413
configSection,
405414
overrideDecision,
415+
options.outputChannel,
406416
);
407417
window.showInformationMessage(
408418
'Successfully added the AWS profile named "localstack" to "~/.aws/config".',

0 commit comments

Comments
 (0)