forked from ruimarinho/gsts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·312 lines (248 loc) · 10.4 KB
/
index.js
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
#!/usr/bin/env node
/**
* Module dependencies.
*/
import * as configManager from './config-manager.js';
import { CredentialsManager } from './credentials-manager.js';
import { Logger, PLAYWRIGHT_LOG_LEVELS } from './logger.js';
import { ProfileNotFoundError, RoleNotFoundError } from './errors.js';
import { generateCliParameters } from './parameters.js';
import { fileURLToPath, parse as urlparse } from 'node:url';
import { format as formatOutput } from './formatter.js';
import { hideBin } from 'yargs/helpers';
import { join } from 'node:path';
import { spawn } from 'node:child_process';
import { openApp } from 'open';
import envpaths from 'env-paths';
import playwright from 'playwright';
import prompts from 'prompts';
import trash from 'trash';
import yargs from 'yargs';
const paths = envpaths('gsts', { suffix: '' });
/**
* Always return control to the terminal in case an unhandled rejection occurs.
*/
process.on('unhandledRejection', e => {
logger.stop();
logger.error(e);
process.exit(1);
});
/**
* Generate CLI parameters based on dynamic paths.
*/
const cliParameters = generateCliParameters(paths);
/**
* Parse command line parameters via yargs.
*
* At the .middleware() stage, `gsts` supported environment variables
* have already been populated, so testing for undefined `argv`
* properties means both a command line parameter as well as an
* environment variable value are not present, so we can safely proceed
* to the `aws` cli configuration settings parsing in the same order as it does.
*/
const argv = await yargs(hideBin(process.argv))
.usage('gsts')
.middleware(async (argv) => {
return configManager.processConfig(cliParameters, argv, process.env, process.stdout.isTTY);
}, true)
.env('GSTS')
.command('console', 'Authenticate via SAML and open Amazon AWS console in the default browser')
.options(cliParameters)
.strictCommands()
.wrap(150)
.argv;
/**
* Custom logger instance to support `-v` or `--verbose` output and non-TTY
* detailed logging with timestamps.
*/
const logger = new Logger(argv.verbose, process.stdout.isTTY, process.stderr);
/**
* The SAML URL to be used for authentication.
*/
const SAML_URL = `https://accounts.google.com/o/saml2/initsso?idpid=${argv.idpId}&spid=${argv.spId}&forceauthn=false`;
/**
* Create instance of CredentialsManager with logger.
*/
const credentialsManager = new CredentialsManager(logger, argv.awsRegion, argv['credentials-cache'] ? argv.cacheDir : null);
/**
* Main execution routine which handles command-line flags.
*/
(async () => {
if (argv._[0] === 'console') {
logger.debug('Opening url %s', SAML_URL);
return await openApp(SAML_URL);
}
if (argv.clean) {
logger.debug('Cleaning directory %s', paths.data)
await trash(paths.data);
}
if (!argv.headful) {
logger.start('Logging in');
}
let isAuthenticated = false;
if (!argv.headful && argv['credentials-cache'] && !argv.force) {
try {
let session = await credentialsManager.loadCredentials(argv.awsProfile, argv.awsRoleArn);
if (session.isValid()) {
logger.info('Session is valid until %s. Use --force to ignore', session.expiresAt);
logger.stop();
process.stdout.write(formatOutput(session, argv.output));
return;
} else {
logger.info('Session has expired on %s, refreshing credentials...', session.expiresAt);
}
} catch (e) {
// Credentials file may not yet exist or not contain session information for the requested profile.
if (e.code !== 'ENOENT' && !(e instanceof ProfileNotFoundError)) {
throw e;
}
}
}
const playwrightOptions = {
headless: !argv.headful,
userDataDir: paths.data,
logger: {
isEnabled: () => argv.verbose >= 3,
log: (name, severity, message, args) => logger[PLAYWRIGHT_LOG_LEVELS[severity]](`Playwright: ${name} ${message}`, args)
},
channel: argv.playwrightEngineChannel,
executablePath: argv.playwrightEngineExecutablePath,
};
const context = await playwright[argv.playwrightEngine].launchPersistentContext(join(paths.data, argv.playwrightEngine), playwrightOptions);
const page = await context.newPage();
page.setDefaultTimeout(0);
await page.route('**/*', async (route) => {
if (route.request().url() === 'https://signin.aws.amazon.com/saml') {
isAuthenticated = true;
try {
let { availableRoles, roleToAssume, samlAssertion } = await credentialsManager.prepareRoleWithSAML(route.request().postDataJSON(), argv.awsRoleArn);
if (!roleToAssume && availableRoles.length > 1) {
logger.stop();
if (process.stdout.isTTY) {
const choices = availableRoles.reduce((accumulator, role) => {
accumulator.push({ title: role.roleArn })
return accumulator;
}, []);
const response = await prompts({
type: 'select',
name: 'arn',
message: 'Select a role to authenticate with:',
choices
});
if (!response.hasOwnProperty('arn')) {
logger.error('You must choose one of the available role ARNs to authenticate or, alternatively, set one directly using the --aws-role-arn option');
route.abort();
return;
}
roleToAssume = availableRoles[response.arn];
logger.info(`You may skip this step by invoking gsts with --aws-role-arn=${roleToAssume.roleArn}`);
} else {
logger.debug(`Assuming role "${roleToAssume.roleArn}" from the list of available roles %o due to non-interactive mode`, availableRoles);
}
}
const session = await credentialsManager.assumeRoleWithSAML(samlAssertion, roleToAssume, argv.awsProfile, argv.awsSessionDuration);
logger.debug(`Initiating request to "${route.request().url()}"`);
route.continue();
// AWS presents an account selection form when multiple roles are available
// before redirecting to the console. If we see this form, then we know we
// are logged in.
if (availableRoles.length > 1) {
await page.waitForSelector('#saml_form');
await context.close();
}
logger.succeed('Login successful!');
process.stdout.write(formatOutput(session, argv.output));
} catch (e) {
// Passthrough STSServiceException from AWS SDK.
if (e.Code === 'ValidationError') {
throw e;
}
logger.debug('An error has ocurred while authenticating', e);
if (e instanceof RoleNotFoundError) {
logger.error(`Role ARN "${argv.awsRoleArn}" not found in the list of available roles ${JSON.stringify(e.roles)}`);
route.abort();
return;
}
if (['ValidationError', 'InvalidIdentityToken'].includes(e.code)) {
logger.error(`A remote error ocurred while assuming role: ${e.message}`);
route.abort();
return;
}
logger.error(`An unknown error has ocurred with message "${e.message}". Please try again with --verbose`)
route.abort();
return;
}
return;
}
if (/google|gstatic|youtube|googleusercontent|googleapis|gvt1|okta/.test(route.request().url())) {
logger.debug(`Allowing request to "${route.request().url()}"`);
route.continue();
return;
}
logger.debug(`Aborting request to "${route.request().url()}"`);
// Abort with a specific error so we can tag these requests as being blocked by gsts
// instead of a configuration issue (like a custom ARN not being available).
route.abort('blockedbyclient');
});
page.on('requestfailed', async request => {
// Requests tagged with this specific error were made by gsts and should result
// in a program termination.
if (request.failure().errorText === 'net::ERR_BLOCKED_BY_CLIENT') {
logger.debug(`Request to "${request.url()}" has has been successfully blocked`);
await context.close();
logger.debug(`Closed context of "${request.url()}"`);
return;
}
logger.debug(`Request to "${request.url()}" has failed with ${request.failure().errorText}`);
// The request to the AWS console is aborted on successful login for performance reasons,
// so in this particular case it's actually an expected outcome.
const parsedURL = urlparse(request.url());
if (parsedURL.host.endsWith('console.aws.amazon.com') && parsedURL.pathname === '/console/home') {
logger.debug(`Request to "${request.url()}" matches AWS console which means authentication was successful`);
await context.close();
return;
}
});
try {
const ssoPage = await page.goto(SAML_URL, { waitUntil: 'load' })
if (!ssoPage.ok()) {
throw new Error(`Got status code "${ssoPage.status()}" while requesting "${SAML_URL}"`);
}
if (/ServiceLogin|InteractiveLogin|AccountChooser/.test(ssoPage.url())) {
if (!isAuthenticated && !argv.headful) {
logger.warn('User is not authenticated, spawning headful instance');
const args = [fileURLToPath(import.meta.url), '--headful', ...process.argv.slice(2)];
const ui = spawn(process.execPath, args, { stdio: 'inherit' });
ui.on('close', code => {
logger.debug(`Headful instance has exited with code ${code}`);
});
await context.close();
}
}
} catch (e) {
// The request to the AWS console is aborted on successful login for performance reasons,
// so in this particular case closing the browser instance is actually an expected outcome.
if (/browser has disconnected/.test(e.message) || /browser has been closed/.test(e.message) || /Navigation failed because page was closed/.test(e.message)) {
return;
}
logger.debug('Error caught while browsing to the initsso page', e);
throw e;
}
if (argv.headful) {
try {
if (argv.username) {
logger.debug(`Pre-filling email with ${argv.username}`);
await page.fill('input[type=email]', argv.username)
}
await page.waitForResponse('https://signin.aws.amazon.com/saml');
} catch (e) {
if (/Target closed/.test(e.message)) {
logger.debug('Browser closed outside running context, exiting');
return;
}
logger.debug('Error while authenticating in headful mode', e);
logger.error(`An unknown error has ocurred with message "${e.message}". Please try again with --verbose`)
process.exit(1);
}
}
})();