Skip to content

Instantly share code, notes, and snippets.

@apsamuel
Created April 3, 2022 18:06
Show Gist options
  • Select an option

  • Save apsamuel/32c9a9b5e16556cd4baa0f237b5afae6 to your computer and use it in GitHub Desktop.

Select an option

Save apsamuel/32c9a9b5e16556cd4baa0f237b5afae6 to your computer and use it in GitHub Desktop.
returning_sharedini_creds
export class AWSAuthenticator {
#disableAssumeRole = false
#region = awsDefaultRegion
#profile = awsDefaultProfile
#configFile = `${process.env.HOME}/.aws/config`
#sharedCredentialsFile = `${process.env.HOME}/.aws/credentials`
constructor(params = {}) {
Object.assign(this, {
...params,
disableAssumeRole: params.disableAssumeRole || this.#disableAssumeRole,
filename: process.env.AWS_SHARED_CREDENTIALS_FILE || params.filename || this.#sharedCredentialsFile,
region: process.env.AWS_REGION || params.region || this.#region,
profile: process.env.AWS_PROFILE || params.profile || this.#profile
})
// ensure env
process.env.AWS_PROFILE = this.profile
process.env.AWS_SDK_LOAD_CONFIG = this.filename
? 1
: 0
process.env.AWS_REGION = this.region
process.env.AWS_SHARED_CREDENTIALS_FILE = this.filename
this.environment = Object.fromEntries(Object.entries(process.env).filter(entry => {
let k = entry[0]
// let v = entry[1]
if (k.startsWith('AWS_')) return entry
}))
}
async load(params = {}, callback) {
if (Object.keys(this.environment).includes('AWS_PROFILE') && process.env.AWS_SDK_LOAD_CONFIG === "1") {
try {
this.credentials = new SharedProfileCredential({
disableAssumeRole: params.disableAssumeRole || this.#disableAssumeRole,
filename: params.filename || this.filename,
region: params.region || this.region,
profile: params.profile || this.profile,
callback: (err) => {
if (!err) {
// console.log(AWS.config.credentials)
console.log(`successfully loaded token code`);
} else {
console.log(`failed to load token code`)
}
},
tokenCodeFn: function (serial, cb) {
// return new Promise(async function (resolve, reject) {
try {
const prompt = inquirer.createPromptModule()
const questions = [
{
name: "token",
type: "input",
default: "",
message: `enter token for MFA ${serial}`,
}
]
prompt(questions)
.then((answers) => {
cb(null, answers.token)
})
.catch((errors) => {
cb(errors, null)
})
// callback(null, this.credentials)
} catch (e) {
cb(e, null);
}
},
});
this.credentials.get((err) => {
if (!err) {
console.log(`refreshed ${JSON.stringify(this.credentials)}`)
// this.credentials.get()
if (typeof(callback)==='function') {
callback(null, this)
} else {
return this
}
}
})
} catch (e) {
console.log(`whoops ${e}`)
}
}
// handle addtnl use-cases, new AWS.Credentials, etc.
// AWS.credentials user passes plain text creds or sets AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY
}
use(callback) {
if (callback) {
callback(null, this)
} else {
console.log('no callback defined')
}
}
}
class SharedProfileCredential extends AWS.SharedIniFileCredentials {
constructor(options) {
super(options)
Object.assign(this, options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment