-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetSecret.ts
More file actions
47 lines (37 loc) · 1.13 KB
/
getSecret.ts
File metadata and controls
47 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
SecretsManagerClient,
paginateListSecrets
} from '@aws-sdk/client-secrets-manager';
import inquirer from 'inquirer';
import prettyoutput from 'prettyoutput';
import {
AWSConfig
} from './types';
export default async function getSecret(awsConfig: AWSConfig): Promise<string> {
const secretsClient = new SecretsManagerClient(awsConfig);
const paginator = paginateListSecrets({ client: secretsClient }, {});
const secretNames: string[] = [];
for await (const { SecretList: secrets } of paginator) {
if (secrets) {
secretNames.push(...secrets.map(secret => secret.Name as string));
}
}
if (secretNames.length === 0) {
console.error('No secrets found in AWS Secrets Manager');
process.exit(1);
}
if (secretNames.length === 1) {
console.log(prettyoutput({ 'Found only one secret in AWS Secrets Manager': secretNames[0] }).trim());
return secretNames[0];
}
const prompt = inquirer.createPromptModule();
const { secretName } = await prompt([
{
type: 'list',
name: 'secretName',
message: 'Which secret?',
choices: secretNames
}
]);
return secretName;
}