-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcredentials.ts
More file actions
168 lines (145 loc) · 4.87 KB
/
Copy pathcredentials.ts
File metadata and controls
168 lines (145 loc) · 4.87 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
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as AWS from 'aws-sdk';
import { prompt } from 'inquirer';
import { debug, error } from '../logger';
/**
* Ask user for MFA token for given serial
*
* Result is send to callback function for SDK to authorize the request
*/
async function tokenCodeFn(serialArn: string, cb: (err?: Error, token?: string) => void): Promise<void> {
debug('Require MFA token for serial ARN', serialArn);
try {
const { token } = await prompt({
type: 'input',
name: 'token',
message: `MFA token for ${serialArn}: `,
default: '',
});
debug('Successfully got MFA token from user');
cb(undefined, token);
} catch (err) {
debug('Failed to get MFA token', err);
cb(err);
}
}
/**
* Get home directory
*/
function homeDir(): string {
return (
process.env.HOME ||
process.env.USERPROFILE ||
(process.env.HOMEPATH ? (process.env.HOMEDRIVE || 'C:/') + process.env.HOMEPATH : null) ||
os.homedir()
);
}
/**
* Get path to aws credentials file
*/
function credentialsFileName(): string {
return process.env.AWS_SHARED_CREDENTIALS_FILE || path.join(homeDir(), '.aws', 'credentials');
}
/**
* Get path to aws config file
*/
function configFileName(): string {
return process.env.AWS_CONFIG_FILE || path.join(homeDir(), '.aws', 'config');
}
/**
* Check if file is readable
*/
async function canRead(filePath: string): Promise<boolean> {
try {
await fs.promises.access(filePath, fs.constants.R_OK);
return true;
} catch (err) {
return false;
}
}
/**
* Set AWS region
*/
export async function setAWSRegion(profile?: string, regionCustom?: string): Promise<void> {
if (regionCustom) {
debug('Setting region to custom:', regionCustom);
AWS.config.update({ region: regionCustom });
return;
}
profile = profile || process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE || 'default';
debug('Find region for profile:', profile);
let region =
process.env.AWS_REGION ||
process.env.AMAZON_REGION ||
process.env.AWS_DEFAULT_REGION ||
process.env.AMAZON_DEFAULT_REGION;
// Try different region configs to find correct region
// 1. From credentials file with given profile
// 2. From config file with given profile
// 3. From config file with default profile
const toCheck = [
{ filename: credentialsFileName(), profile },
{ isConfig: true, filename: configFileName(), profile },
{ isConfig: true, filename: configFileName(), profile: 'default' },
];
for (let i = 0; !region && i < toCheck.length; i++) {
const options = toCheck[i];
try {
const contents = await fs.promises.readFile(options.filename);
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const config = (AWS as any).util.ini.parse(contents.toString());
const profileIndex =
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
profile !== (AWS as any).util.defaultProfile && options.isConfig ? 'profile ' + profile : profile;
region = config[profileIndex || '']?.region;
} catch (err) {
error('Failed to parse config', options, err);
}
}
if (!region) {
const usedProfile = !profile ? '' : ` (profile: "${profile}")`;
region = 'us-east-1'; // This is what the AWS CLI does
debug(
`Unable to determine AWS region from environment or AWS configuration${usedProfile}, defaulting to '${region}'`,
);
}
debug('Set region to:', region);
AWS.config.update({ region });
}
/**
* Wrapper function to get better types for environment credenatials
*/
function environmentCredentials(prefix: string): () => AWS.EnvironmentCredentials {
return (): AWS.EnvironmentCredentials => new AWS.EnvironmentCredentials(prefix);
}
/**
* Set credentials and region to AWS from config and env variables
*/
export async function setAWSCredentials(profile?: string, region?: string): Promise<void> {
try {
const sources: (() => AWS.Credentials)[] = [environmentCredentials('AWS'), environmentCredentials('AMAZON')];
profile = profile || process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE || 'default';
if (await canRead(credentialsFileName())) {
sources.push(() => new AWS.SharedIniFileCredentials({ filename: credentialsFileName(), profile, tokenCodeFn }));
}
if (await canRead(configFileName())) {
sources.push(() => new AWS.SsoCredentials({ filename: configFileName(), profile }));
}
const credentials = await new AWS.CredentialProviderChain(sources).resolvePromise();
AWS.config.update({ credentials });
} catch (err) {
error(err);
}
// If region is defined, then set that up as well
await setAWSRegion(profile, region);
}
/**
* Throws error if credentials are missing
*/
export function validateCredentials(): void {
if (!AWS.config.credentials) {
throw new Error('AWS credentials not set');
}
}