-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfigure-aws.ts
More file actions
503 lines (458 loc) · 14.5 KB
/
configure-aws.ts
File metadata and controls
503 lines (458 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import { resolve } from "node:dns/promises";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { window } from "vscode";
import type { LogOutputChannel } from "vscode";
import { readAuthToken } from "./authenticate.ts";
import { parseIni, serializeIni, updateIniSection } from "./ini-parser.ts";
import type { IniFile, IniSection } from "./ini-parser.ts";
import type { Telemetry } from "./telemetry.ts";
// Important: Add a newline to the beginning of the config and credentials files
// to avoid issues files without a newline at the end of the file.
// TODO: add a test for this.
const LOCALSTACK_CONFIG_PROFILE_NAME = "profile localstack";
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",
};
// https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html
// Do not use the word profile when creating an entry in the credentials file.
const LOCALSTACK_CREDENTIALS_PROFILE_NAME = "localstack";
const LOCALSTACK_CREDENTIALS_PROPERTIES = {
aws_access_key_id: "test",
aws_secret_access_key: "test",
};
export const AWS_DIRECTORY = path.join(os.homedir(), ".aws");
export const AWS_CONFIG_FILENAME = path.join(AWS_DIRECTORY, "config");
export const AWS_CREDENTIALS_FILENAME = path.join(AWS_DIRECTORY, "credentials");
async function overrideSelection(
filesToModify: string[],
override: boolean = false,
): Promise<OverrideDecision | undefined> {
if (override === true) {
return "Override";
}
const fileList = filesToModify.join(" and ");
const selection = await window.showWarningMessage(
`The AWS profile named "localstack" in ${fileList} exists, but does not match the expected properties. Do you want to override it?`,
"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
);
} catch {
return false;
}
}
async function checkIfConfigNeedsOverride(
section: IniSection | undefined,
): Promise<boolean> {
if (!section) {
return true; // profile doesn't exist
}
if (
section.properties.endpoint_url === "http://localhost.localstack.cloud:4566"
) {
const isDnsResolved = await dnsResolveCheck(undefined);
if (!isDnsResolved) {
// if DNS is not resolved, we need to override the endpoint_url
return true;
}
}
return !(
section.properties.region &&
section.properties.endpoint_url &&
isValidEndpointUrl(section.properties.endpoint_url)
);
}
function checkIfCredentialsNeedsOverride(
section: IniSection | undefined,
): boolean {
if (!section) {
return true; // profile doesn't exist
}
return !(
section.properties.aws_access_key_id ===
LOCALSTACK_CREDENTIALS_PROPERTIES.aws_access_key_id &&
section.properties.aws_secret_access_key ===
LOCALSTACK_CREDENTIALS_PROPERTIES.aws_secret_access_key
);
}
async function getProfile(filename: string, profileName: string) {
const contents = await readFile(filename);
const iniFile = parseIni(contents);
const section = iniFile.sections.find((s) => s.name === profileName);
return { contents, iniFile, section };
}
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?.warn(
`[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;
}
}
type OverrideDecision = "Override" | "do_not_override";
async function configureAwsConfigProfile(
awsConfigFilename: string,
contents: string,
iniFile: IniFile,
section: IniSection | undefined,
overrideDecision: OverrideDecision | undefined = undefined,
outputChannel: LogOutputChannel | undefined,
): Promise<boolean | undefined> {
const awsConfigFilenameReadable = awsConfigFilename.replace(
os.homedir(),
"~",
);
try {
if (section) {
// LocalStack profile exists, but does not match the expected properties
if (overrideDecision === "Override") {
// User chose to override the existing profile.
// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck(outputChannel);
const endpointUrl = isDnsResolved
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;
const updatedIniFile = updateIniSection(
iniFile,
LOCALSTACK_CONFIG_PROFILE_NAME,
{
region: LOCALSTACK_CONFIG_PROPERTIES.region,
output: LOCALSTACK_CONFIG_PROPERTIES.output,
endpoint_url: endpointUrl,
},
);
const updatedContents = `${serializeIni(updatedIniFile)}\n`;
await fs.writeFile(awsConfigFilename, updatedContents);
return true;
} else if (overrideDecision === undefined) {
// User cancelled the selection.
return undefined;
}
return false;
}
// LocalStack profile does not exist: create it.
// check if dnsResolveCheck is successful
const isDnsResolved = await dnsResolveCheck(outputChannel);
const endpointUrl = isDnsResolved
? `http://localhost.localstack.cloud:${DEFAULT_PORT}`
: `http://127.0.0.1:${DEFAULT_PORT}`;
const updatedIniFile = updateIniSection(
iniFile,
LOCALSTACK_CONFIG_PROFILE_NAME,
{
region: LOCALSTACK_CONFIG_PROPERTIES.region,
output: LOCALSTACK_CONFIG_PROPERTIES.output,
endpoint_url: endpointUrl,
},
);
const updatedContents =
contents.trim() === ""
? `${serializeIni(updatedIniFile).trim()}\n`
: `${serializeIni(updatedIniFile)}\n`;
await fs.writeFile(awsConfigFilename, updatedContents);
return true;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
window.showErrorMessage(
`Failed to configure the AWS profile named "localstack" in [${awsConfigFilenameReadable}]: ${errorMessage}`,
);
}
}
async function configureCredentialsProfile(
awsCredentialsFilename: string,
contents: string,
iniFile: IniFile,
section: IniSection | undefined,
overrideDecision: string | undefined = undefined,
): Promise<boolean | undefined> {
const awsCredentialsFilenameReadable = awsCredentialsFilename.replace(
os.homedir(),
"~",
);
try {
// LocalStack profile exists, but does not match the expected properties
if (section) {
if (overrideDecision === "Override") {
// User chose to override the existing profile.
const updatedIniFile = updateIniSection(
iniFile,
LOCALSTACK_CREDENTIALS_PROFILE_NAME,
LOCALSTACK_CREDENTIALS_PROPERTIES,
);
const updatedContents = `${serializeIni(updatedIniFile)}\n`;
await fs.writeFile(awsCredentialsFilename, updatedContents);
return true;
} else if (overrideDecision === undefined) {
// User cancelled the selection.
return undefined;
}
return false;
}
// LocalStack profile does not exist: create it.
const updatedIniFile = updateIniSection(
iniFile,
LOCALSTACK_CREDENTIALS_PROFILE_NAME,
LOCALSTACK_CREDENTIALS_PROPERTIES,
);
// If the file is empty, we need to add a newline at the end.
// Otherwise, we just write the updated INI file.
// This is important to avoid issues with files without a newline at the end.
const updatedContents =
contents.trim() === ""
? `${serializeIni(updatedIniFile).trim()}\n`
: `${serializeIni(updatedIniFile)}\n`;
await fs.writeFile(awsCredentialsFilename, updatedContents);
return true;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
window.showErrorMessage(
`Failed to configure the AWS profile named "localstack" in [${awsCredentialsFilenameReadable}]: ${errorMessage}`,
);
}
return false;
}
function isErrorWithCode(error: unknown, code: string): boolean {
return (
typeof error === "object" &&
error !== null &&
"code" in error &&
(error as { code?: unknown }).code === code
);
}
/**
* Reads the contents of a file.
*
* If the file doesn't exist, returns an empty string.
*/
async function readFile(fileName: string) {
try {
const credentials = await fs.readFile(fileName, "utf-8");
return credentials;
} catch (error) {
if (isErrorWithCode(error, "ENOENT")) {
return "";
}
throw error;
}
}
export async function configureAwsProfiles(options: {
telemetry?: Telemetry; // for testing purposes
awsDirectory?: string;
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();
const awsDirectory = options.awsDirectory ?? AWS_DIRECTORY;
await fs.mkdir(awsDirectory, { recursive: true });
const awsConfigFilename = path.join(awsDirectory, "config");
const awsCredentialsFilename = path.join(awsDirectory, "credentials");
// Get profile sections for both files
const [
{
contents: configContents,
iniFile: configIniFile,
section: configSection,
},
{
contents: credentialsContents,
iniFile: credentialsIniFile,
section: credentialsSection,
},
] = await Promise.all([
getProfile(awsConfigFilename, LOCALSTACK_CONFIG_PROFILE_NAME),
getProfile(awsCredentialsFilename, LOCALSTACK_CREDENTIALS_PROFILE_NAME),
]);
let overrideDecision: OverrideDecision | undefined;
let configModified: boolean | undefined;
let credentialsModified: boolean | undefined;
const configNeedsOverride = await checkIfConfigNeedsOverride(configSection);
const credentialsNeedsOverride =
checkIfCredentialsNeedsOverride(credentialsSection);
const authToken = await readAuthToken();
// means sections exist, but we need to check what's inside
if (credentialsSection && configSection) {
if (!configNeedsOverride && !credentialsNeedsOverride) {
// if everything in place, show user that no changes were made and return
if (options?.notifyNoChangesMade) {
window.showInformationMessage(
'The AWS profile named "localstack" was already present, so no changes were made.',
);
}
options.telemetry?.track({
name: "aws_profile_configured",
payload: {
namespace: "onboarding",
origin: trigger,
step_order: 4,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "COMPLETED",
auth_token: authToken,
},
});
return;
} else {
// profile is there but needs adjustment
// in testing, we always override
if (options?.forceOverride) {
overrideDecision = "Override";
} else {
// check which files need override
const filesToModify = [];
if (configNeedsOverride) filesToModify.push("~/.aws/config");
if (credentialsNeedsOverride) filesToModify.push("~/.aws/credentials");
// ask user if they want to override
overrideDecision = await overrideSelection(filesToModify, false);
}
}
} else {
// we need to create it
overrideDecision = "Override";
}
if (overrideDecision === undefined) {
// user cancelled pop-up, so return early
options.telemetry?.track({
name: "aws_profile_configured",
payload: {
namespace: "onboarding",
origin: trigger,
step_order: 4,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "SKIPPED",
auth_token: authToken,
},
});
return;
} else if (overrideDecision === "Override") {
if (configNeedsOverride && credentialsNeedsOverride) {
[configModified, credentialsModified] = await Promise.all([
configureAwsConfigProfile(
awsConfigFilename,
configContents,
configIniFile,
configSection,
overrideDecision,
options.outputChannel,
),
configureCredentialsProfile(
awsCredentialsFilename,
credentialsContents,
credentialsIniFile,
credentialsSection,
overrideDecision,
),
]);
window.showInformationMessage(
'Successfully added the AWS profile named "localstack" to "~/.aws/config" and "~/.aws/credentials".',
);
options.telemetry?.track({
name: "aws_profile_configured",
payload: {
namespace: "onboarding",
origin: trigger,
step_order: 4,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "COMPLETED",
auth_token: authToken,
},
});
} else if (configNeedsOverride) {
configModified = await configureAwsConfigProfile(
awsConfigFilename,
configContents,
configIniFile,
configSection,
overrideDecision,
options.outputChannel,
);
window.showInformationMessage(
'Successfully added the AWS profile named "localstack" to "~/.aws/config".',
);
options.telemetry?.track({
name: "aws_profile_configured",
payload: {
namespace: "onboarding",
origin: trigger,
step_order: 4,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "COMPLETED",
auth_token: authToken,
},
});
} else if (credentialsNeedsOverride) {
credentialsModified = await configureCredentialsProfile(
awsCredentialsFilename,
credentialsContents,
credentialsIniFile,
credentialsSection,
overrideDecision,
);
window.showInformationMessage(
'Successfully added the AWS profile named "localstack" to "~/.aws/credentials".',
);
options.telemetry?.track({
name: "aws_profile_configured",
payload: {
namespace: "onboarding",
origin: trigger,
step_order: 4,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "COMPLETED",
auth_token: authToken,
},
});
}
}
}
export async function checkIsProfileConfigured(): Promise<boolean> {
try {
const [{ section: configSection }, { section: credentialsSection }] =
await Promise.all([
getProfile(AWS_CONFIG_FILENAME, LOCALSTACK_CONFIG_PROFILE_NAME),
getProfile(
AWS_CREDENTIALS_FILENAME,
LOCALSTACK_CREDENTIALS_PROFILE_NAME,
),
]);
const [configNeedsOverride, credentialsNeedsOverride] = await Promise.all([
checkIfConfigNeedsOverride(configSection),
checkIfCredentialsNeedsOverride(credentialsSection),
]);
if (configNeedsOverride || credentialsNeedsOverride) {
return false;
}
return true; // profile exists in both files and is properly configured
} catch (error) {
return false;
}
}