Skip to content

Instantly share code, notes, and snippets.

@jhecking
Last active August 26, 2020 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhecking/26ea1f4cdbedfba55367a2547f8c6650 to your computer and use it in GitHub Desktop.
Save jhecking/26ea1f4cdbedfba55367a2547f8c6650 to your computer and use it in GitHub Desktop.
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