-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawssopilot.js
131 lines (104 loc) · 4.47 KB
/
awssopilot.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
#!/usr/bin/env node --no-warnings=ExperimentalWarning
import puppeteer from 'puppeteer';
import { readFileSync } from 'fs';
import { setTimeout } from 'timers/promises';
import { execa } from 'execa';
import os from 'os';
import pkg from './package.json' with {type: 'json'};
import { checkUpdate } from './update-notifier.js';
const updateAvailable = await checkUpdate({
author: pkg.author,
repository: pkg.repository.name,
name: pkg.name,
version: pkg.version
})
if (updateAvailable) { process.exit(); }
init().catch((error) => {
console.error(error.message);
process.exit(1);
});
async function init() {
// config
const data = readFileSync(os.homedir() + "/awssopilot.config", "utf8");
const config = JSON.parse(data);
// check aws installed
await execa`aws --version`;
// check yawsso installed
await execa`yawsso --version`;
for (const profile of config.profiles) {
console.log(`Setting profile: ${profile}`);
const execaProcess = execa({ reject: false })`aws sso login --profile ${profile} --no-browser`;
for await (const line of execaProcess) {
if (!line.includes('?user_code')) {
continue;
}
const url = line;
// browser
const browser = await puppeteer.launch({ headless: true, args: ['--lang=en'] });
const page = (await browser.pages())[0];
// load page
console.log(` Loading url: ${url}`);
await page.goto(url);
// user
console.log(' Logging user...');
await page.waitForSelector('input[type="email"]');
await page.type('input[type="email"]', config.email);
await page.click('input[type="submit"]');
// password
await page.waitForSelector('input[type="password"]');
await setTimeout(1000);
await page.type('input[type="password"]', config.password);
await page.click('input[type="submit"]');
// call
if (config.type === 'call') {
// login with call
console.log(' Logging with phone call...');
await page.waitForSelector('#signInAnotherWay');
await page.click('#signInAnotherWay');
// select phone
await page.waitForSelector('#idDiv_SAOTCS_Title');
await page.locator(`div ::-p-text("Call +XX XXXXXXX${config.phone}")`).click();
console.log(' Awaiting approval call...');
}
// app
if (config.type === 'app') {
// show code
console.log(' Loading app code...');
const element = await page.waitForSelector('#idRichContext_DisplaySign');
const text = await page.evaluate(element => element.textContent, element);
console.log(` Awaiting approval of code: ${text}`);
}
// submit stay logged
await page.waitForSelector('#KmsiDescription', { delay: 60 });
await page.click('input[type="submit"]');
// allow cookies
await page.waitForSelector('button[aria-label="Accept all cookies"]');
await page.click('button[aria-label="Accept all cookies"]');
// confirm code
console.log(' Approving code...');
await page.waitForSelector('#cli_verification_btn');
await page.click('#cli_verification_btn');
// allow access
console.log(' Approving access...');
await page.locator(`span ::-p-text("Allow access")`).wait();
await page.locator(`span ::-p-text("Allow access")`).click();
await page.locator(`div ::-p-text("Request approved")`).wait();
// close browser
await browser.close();
// close process
console.log(' Awaiting graceful time...');
await setTimeout(3000);
execaProcess.kill();
// yawsso
console.log(' Executing YAWSSO...');
await execa`yawsso -p ${profile}:${profile}-iam`;
// logs
const logMessage = ` IAM profile '${profile}-iam' configured `;
console.log('-'.repeat(logMessage.length));
console.log(` SSO profile '${profile}' token renewed`);
console.log(logMessage);
console.log('-'.repeat(logMessage.length));
break;
}
}
}