Skip to content
Merged
Changes from 3 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
51 changes: 33 additions & 18 deletions src/utils/configure-aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import type { Telemetry } from "./telemetry.ts";
// TODO: add a test for this.

const LOCALSTACK_CONFIG_PROFILE_NAME = "profile localstack";
const VALID_ENDPOINT_URLS = [
"http://localhost.localstack.cloud:4566", // default
"http://127.0.0.1:4566",
"http://localhost:4566",
const VALID_HOSTNAMES = [
"localhost.localstack.cloud",
"127.0.0.1",
"localhost",
];
const DEFAULT_PORT = "4566";
const LOCALSTACK_CONFIG_PROPERTIES = {
region: "us-east-1",
output: "json",
endpoint_url: VALID_ENDPOINT_URLS,
};

// https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html
Expand All @@ -40,16 +40,31 @@ async function overrideSelection(
override: boolean = false,
): Promise<OverrideDecision | undefined> {
if (override === true) {
return "override";
return "Override";
}
const fileList = filesToModify.join(" and ");
const selection = await window.showWarningMessage(
`The "localstack" AWS profile in ${fileList} exists, but does not match the expected properties. Do you want to override it?`,
"override",
"Override",
);
return selection;
}

function isValidEndpointUrl(url: string | undefined): boolean {
if (!url) return false;
try {
const parsed = new URL(url);
return (
(parsed.protocol === "http:" || parsed.protocol === "https:") &&
VALID_HOSTNAMES.includes(parsed.hostname) &&
parsed.port !== "" && // port must be present
(parsed.port === DEFAULT_PORT || /^\d+$/.test(parsed.port))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition parsed.port !== "" is unnecessary because later we check for (parsed.port === DEFAULT_PORT || /^\d+$/.test(parsed.port)

Copy link
Copy Markdown
Contributor

@skyrpex skyrpex Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, are we just trying to make sure there is a port, whatever it is? In that case, having parsed.port !== "" or just parsed.port only should be enough.

Comment thread
anisaoshafi marked this conversation as resolved.
Outdated
);
} catch {
return false;
}
}

function checkIfConfigNeedsOverride(section: IniSection | undefined): boolean {
if (!section) {
return true; // profile doesn't exist
Expand All @@ -58,7 +73,7 @@ function checkIfConfigNeedsOverride(section: IniSection | undefined): boolean {
return !(
section.properties.region &&
section.properties.endpoint_url &&
VALID_ENDPOINT_URLS.includes(section.properties.endpoint_url)
isValidEndpointUrl(section.properties.endpoint_url)
);
}

Expand Down Expand Up @@ -93,7 +108,7 @@ async function dnsResolveCheck(): Promise<boolean> {
}
}

type OverrideDecision = "override" | "do_not_override";
type OverrideDecision = "Override" | "do_not_override";

async function configureAwsConfigProfile(
awsConfigFilename: string,
Expand All @@ -110,14 +125,14 @@ async function configureAwsConfigProfile(
try {
if (section) {
// LocalStack profile exists, but does not match the expected properties
if (overrideDecision === "override") {
if (overrideDecision === "Override") {
// User chose to override the existing profile.

// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck();
const endpointUrl = isDnsResolved
? "http://localhost.localstack.cloud:4566"
: VALID_ENDPOINT_URLS[1];
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;

const updatedIniFile = updateIniSection(
iniFile,
Expand All @@ -143,8 +158,8 @@ async function configureAwsConfigProfile(
// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck();
const endpointUrl = isDnsResolved
? "http://localhost.localstack.cloud:4566"
: VALID_ENDPOINT_URLS[1];
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;

const updatedIniFile = updateIniSection(
iniFile,
Expand Down Expand Up @@ -186,7 +201,7 @@ async function configureCredentialsProfile(
try {
// LocalStack profile exists, but does not match the expected properties
if (section) {
if (overrideDecision === "override") {
if (overrideDecision === "Override") {
// User chose to override the existing profile.
const updatedIniFile = updateIniSection(
iniFile,
Expand Down Expand Up @@ -321,7 +336,7 @@ export async function configureAwsProfiles(options: {
// profiles are there but need adjustment
// in testing, we always override
if (options?.forceOverride) {
overrideDecision = "override";
overrideDecision = "Override";
} else {
// check which files need override
const filesToModify = [];
Expand All @@ -333,7 +348,7 @@ export async function configureAwsProfiles(options: {
}
} else {
// if any of the profiles don't exist, we need to create it
overrideDecision = "override";
overrideDecision = "Override";
}

if (overrideDecision === undefined) {
Expand All @@ -350,7 +365,7 @@ export async function configureAwsProfiles(options: {
},
});
return;
} else if (overrideDecision === "override") {
} else if (overrideDecision === "Override") {
if (configNeedsOverride && credentialsNeedsOverride) {
[configModified, credentialsModified] = await Promise.all([
configureAwsConfigProfile(
Expand Down
Loading