Last active
August 26, 2020 08:32
-
-
Save jhecking/26ea1f4cdbedfba55367a2547f8c6650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { SharedIniFileCredentials, STS } = require('aws-sdk') | |
const { execSync } = require('child_process') | |
const OK_RESPONSE = /button returned:OK, text returned:(?<answer>\S+)/ | |
async function tokenCodeAppleScript (serialno, callback) { | |
try { | |
const cmd = `osascript -e 'display dialog "Enter MFA code for ${serialno}:" default answer ""'` | |
const response = execSync(cmd).toString() | |
const match = response.match(OK_RESPONSE) | |
if (match) { | |
const { answer } = match.groups | |
callback(null, answer) | |
} | |
callback(null, "") | |
} catch (error) { | |
callback(error) | |
} | |
} | |
const credentials = new SharedIniFileCredentials({ | |
profile: process.env.AWS_PROFILE, | |
tokenCodeFn: tokenCodeAppleScript | |
}) | |
const sts = new STS({ credentials }) | |
sts.getCallerIdentity().promise() | |
.then((data) => { | |
console.log('Successfully assumed role:', data) | |
}).catch((error) => { | |
console.error(error.message) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment