Skip to content

Instantly share code, notes, and snippets.

@hendrixroa
Created January 18, 2020 21:57
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 hendrixroa/9ace028316fe07e6bd2ed8c910d08f25 to your computer and use it in GitHub Desktop.
Save hendrixroa/9ace028316fe07e6bd2ed8c910d08f25 to your computer and use it in GitHub Desktop.
Pull aws secrets values given keys.
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