Created
January 18, 2020 21:57
-
-
Save hendrixroa/9ace028316fe07e6bd2ed8c910d08f25 to your computer and use it in GitHub Desktop.
Pull aws secrets values given keys.
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
import AWS = require('aws-sdk'); | |
import minimist = require('minimist'); | |
const args: any = minimist(process.argv); | |
const secretsManager = new AWS.SecretsManager({ | |
region: process.env.AWS_DEFAULT_REGION || 'us-east-2', | |
}); | |
export class SecretByKey { | |
public getSecretByKey(secretId: string, keys: string[]): Promise<any> { | |
const params: any = { | |
SecretId: secretId, | |
}; | |
return new Promise((resolve: any, reject: any) => { | |
secretsManager.getSecretValue(params, (err: any, data: any) => { | |
if (err) { | |
reject(err); | |
} | |
const valueSecret: any = JSON.parse(data.SecretString); | |
// tslint:disable-next-line: prefer-const | |
let result: any = {}; | |
for (const key of keys) { | |
result[key] = valueSecret[key]; | |
} | |
resolve(result); | |
}); | |
}); | |
} | |
public async getSecret() { | |
let keys: string[] = []; | |
try { | |
keys = JSON.parse(args.keys); | |
} catch (error) { | |
// tslint:disable-next-line: no-console | |
console.error(null); | |
process.exit(1); | |
} | |
const value: any = await this.getSecretByKey(args.secretId, keys); | |
// tslint:disable-next-line: no-console | |
console.log(JSON.stringify(value)); | |
} | |
} | |
const secret: SecretByKey = new SecretByKey(); | |
secret | |
.getSecret() | |
.then() | |
.catch((err: any) => { | |
// tslint:disable-next-line: no-console | |
console.error(null); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment