Skip to content

Commit 8ceae9b

Browse files
committed
Refactor aws profile code
1 parent 1cd146c commit 8ceae9b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/utils/configure-aws.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ import type { Telemetry } from "./telemetry.ts";
1414
// TODO: add a test for this.
1515

1616
const LOCALSTACK_CONFIG_PROFILE_NAME = "profile localstack";
17-
const VALID_ENDPOINT_URLS = [
18-
"http://localhost.localstack.cloud:4566",
19-
"https://localhost.localstack.cloud:4566",
20-
"http://127.0.0.1:4566",
21-
"https://127.0.0.1:4566",
22-
"http://localhost:4566",
23-
"https://localhost:4566",
17+
const VALID_HOSTNAMES = [
18+
"localhost.localstack.cloud",
19+
"127.0.0.1",
20+
"localhost",
2421
];
22+
const DEFAULT_PORT = "4566";
2523
const LOCALSTACK_CONFIG_PROPERTIES = {
2624
region: "us-east-1",
2725
output: "json",
28-
endpoint_url: VALID_ENDPOINT_URLS,
2926
};
3027

3128
// https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html
@@ -53,6 +50,21 @@ async function overrideSelection(
5350
return selection;
5451
}
5552

53+
function isValidEndpointUrl(url: string | undefined): boolean {
54+
if (!url) return false;
55+
try {
56+
const parsed = new URL(url);
57+
return (
58+
(parsed.protocol === "http:" || parsed.protocol === "https:") &&
59+
VALID_HOSTNAMES.includes(parsed.hostname) &&
60+
parsed.port !== "" && // port must be present
61+
(parsed.port === DEFAULT_PORT || /^\d+$/.test(parsed.port))
62+
);
63+
} catch {
64+
return false;
65+
}
66+
}
67+
5668
function checkIfConfigNeedsOverride(section: IniSection | undefined): boolean {
5769
if (!section) {
5870
return true; // profile doesn't exist
@@ -61,7 +73,7 @@ function checkIfConfigNeedsOverride(section: IniSection | undefined): boolean {
6173
return !(
6274
section.properties.region &&
6375
section.properties.endpoint_url &&
64-
VALID_ENDPOINT_URLS.includes(section.properties.endpoint_url)
76+
isValidEndpointUrl(section.properties.endpoint_url)
6577
);
6678
}
6779

@@ -119,8 +131,8 @@ async function configureAwsConfigProfile(
119131
// check if dnsResolveCheck is successful
120132
const isDnsResolved = await dnsResolveCheck();
121133
const endpointUrl = isDnsResolved
122-
? "http://localhost.localstack.cloud:4566"
123-
: VALID_ENDPOINT_URLS[1];
134+
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
135+
: `http://127.0.0.1:${DEFAULT_PORT}`;
124136

125137
const updatedIniFile = updateIniSection(
126138
iniFile,
@@ -146,8 +158,8 @@ async function configureAwsConfigProfile(
146158
// check if dnsResolveCheck is successful
147159
const isDnsResolved = await dnsResolveCheck();
148160
const endpointUrl = isDnsResolved
149-
? "https://localhost.localstack.cloud:4566"
150-
: "https://127.0.0.1:4566";
161+
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
162+
: `http://127.0.0.1:${DEFAULT_PORT}`;
151163

152164
const updatedIniFile = updateIniSection(
153165
iniFile,

0 commit comments

Comments
 (0)