| | | 1 | | import { GetParametersCommand, type SSMClient } from '@aws-sdk/client-ssm'; |
| | | 2 | | import type { SecretExistence } from '../../domain/contract-validation-result.js'; |
| | | 3 | | import { |
| | | 4 | | ExpiredCredentialsError, |
| | | 5 | | isExpiredCredentialsError, |
| | | 6 | | } from '../../domain/expired-credentials-error.js'; |
| | | 7 | | import type { ISecretExistenceChecker } from '../../domain/ports/secret-existence-checker.js'; |
| | | 8 | | import type { ISecretProvider } from '../../domain/ports/secret-provider.js'; |
| | | 9 | | import { |
| | | 10 | | isSsoSessionExpiredError, |
| | | 11 | | SsoSessionExpiredError, |
| | | 12 | | } from '../../domain/sso-session-expired-error.js'; |
| | | 13 | | import { describeError } from '../describe-error.js'; |
| | | 14 | | |
| | 5 | 15 | | const SSM_BATCH_SIZE = 10; |
| | | 16 | | |
| | 5 | 17 | | const AUTHENTICATION_ERROR_NAMES = new Set([ |
| | | 18 | | 'CredentialsProviderError', |
| | | 19 | | 'UnrecognizedClientException', |
| | | 20 | | 'InvalidClientTokenId', |
| | | 21 | | 'InvalidSignatureException', |
| | | 22 | | 'IncompleteSignatureException', |
| | | 23 | | ]); |
| | | 24 | | |
| | | 25 | | /** |
| | | 26 | | * Errors that fail a whole `GetParameters` call because of one of its names, |
| | | 27 | | * so each name is checked on its own to tell which one. |
| | | 28 | | */ |
| | 5 | 29 | | const PER_NAME_ERROR_NAMES = new Set([ |
| | | 30 | | 'AccessDeniedException', |
| | | 31 | | 'ValidationException', |
| | | 32 | | ]); |
| | | 33 | | |
| | | 34 | | /** |
| | | 35 | | * {@link ISecretProvider} backed by AWS SSM Parameter Store. |
| | | 36 | | * |
| | | 37 | | * Parameters are retrieved with decryption enabled so that |
| | | 38 | | * SecureString values are returned in plain text. |
| | | 39 | | * |
| | | 40 | | * SSM supports fetching up to 10 parameters per request, |
| | | 41 | | * so names are chunked into batches automatically. |
| | | 42 | | */ |
| | | 43 | | export class AwsSsmSecretProvider |
| | | 44 | | implements ISecretProvider, ISecretExistenceChecker |
| | | 45 | | { |
| | | 46 | | private readonly ssmClient: SSMClient; |
| | | 47 | | private readonly profile?: string; |
| | | 48 | | |
| | | 49 | | constructor(ssmClient: SSMClient, profile?: string) { |
| | 24 | 50 | | if (!ssmClient) { |
| | 1 | 51 | | throw new Error('ssmClient cannot be null'); |
| | | 52 | | } |
| | 23 | 53 | | this.ssmClient = ssmClient; |
| | 23 | 54 | | this.profile = profile; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | async getSecrets(names: string[]): Promise<Map<string, string>> { |
| | 15 | 58 | | const result = new Map<string, string>(); |
| | 15 | 59 | | if (names.length === 0) { |
| | 1 | 60 | | return result; |
| | | 61 | | } |
| | | 62 | | |
| | 14 | 63 | | for (const name of names) { |
| | 28 | 64 | | if (!name?.trim()) { |
| | 1 | 65 | | throw new Error('Secret name cannot be null or whitespace'); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 13 | 69 | | for (let i = 0; i < names.length; i += SSM_BATCH_SIZE) { |
| | 14 | 70 | | const batch = names.slice(i, i + SSM_BATCH_SIZE); |
| | 14 | 71 | | try { |
| | 14 | 72 | | const response = await this.ssmClient.send( |
| | | 73 | | new GetParametersCommand({ |
| | | 74 | | Names: batch, |
| | | 75 | | WithDecryption: true, |
| | | 76 | | }), |
| | | 77 | | ); |
| | | 78 | | |
| | 8 | 79 | | for (const param of response.Parameters ?? []) { |
| | 6 | 80 | | if (param.Name && param.Value != null) { |
| | 6 | 81 | | result.set(param.Name, param.Value); |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | } catch (error) { |
| | 6 | 85 | | if (isSsoSessionExpiredError(error)) { |
| | 1 | 86 | | throw new SsoSessionExpiredError(this.profile, error); |
| | | 87 | | } |
| | 5 | 88 | | if (isExpiredCredentialsError(error)) { |
| | 1 | 89 | | throw new ExpiredCredentialsError(error); |
| | | 90 | | } |
| | 4 | 91 | | throw error; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 7 | 95 | | return result; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /** |
| | | 99 | | * Uses `GetParameters` in batches of 10 with `WithDecryption: false`: the |
| | | 100 | | * names it cannot find come back in `InvalidParameters`, and the values it |
| | | 101 | | * returns are never read. Needs `ssm:GetParameters` on the parameters. |
| | | 102 | | */ |
| | | 103 | | async checkSecretsExist( |
| | | 104 | | names: readonly string[], |
| | | 105 | | ): Promise<ReadonlyMap<string, SecretExistence>> { |
| | 12 | 106 | | const results = new Map<string, SecretExistence>(); |
| | 12 | 107 | | const distinct = [...new Set(names)]; |
| | 12 | 108 | | for (let i = 0; i < distinct.length; i += SSM_BATCH_SIZE) { |
| | 14 | 109 | | const batch = distinct.slice(i, i + SSM_BATCH_SIZE); |
| | 14 | 110 | | for (const [name, existence] of await this.checkBatch(batch)) { |
| | 41 | 111 | | results.set(name, existence); |
| | | 112 | | } |
| | | 113 | | } |
| | 12 | 114 | | return results; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private async checkBatch( |
| | | 118 | | batch: readonly string[], |
| | | 119 | | ): Promise<Array<[string, SecretExistence]>> { |
| | 16 | 120 | | try { |
| | 16 | 121 | | const { InvalidParameters = [] } = await this.ssmClient.send( |
| | | 122 | | new GetParametersCommand({ |
| | | 123 | | Names: [...batch], |
| | | 124 | | WithDecryption: false, |
| | | 125 | | }), |
| | | 126 | | ); |
| | 8 | 127 | | const missing = new Set(InvalidParameters); |
| | 30 | 128 | | return batch.map((name) => [ |
| | | 129 | | name, |
| | | 130 | | { status: missing.has(name) ? 'missing' : 'exists' }, |
| | | 131 | | ]); |
| | | 132 | | } catch (error) { |
| | 8 | 133 | | if (batch.length > 1 && PER_NAME_ERROR_NAMES.has(errorName(error))) { |
| | 1 | 134 | | const perName = await Promise.all( |
| | 2 | 135 | | batch.map((name) => this.checkBatch([name])), |
| | | 136 | | ); |
| | 1 | 137 | | return perName.flat(); |
| | | 138 | | } |
| | 7 | 139 | | const existence = this.toExistence(error); |
| | 11 | 140 | | return batch.map((name) => [name, existence]); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private toExistence(error: unknown): SecretExistence { |
| | | 145 | | // Expired credentials are a credentials problem: reported, not thrown, |
| | | 146 | | // with the same remediation a load gives. |
| | 7 | 147 | | if (isSsoSessionExpiredError(error)) { |
| | 1 | 148 | | return { |
| | | 149 | | status: 'authentication_failed', |
| | | 150 | | reason: new SsoSessionExpiredError(this.profile, error).message, |
| | | 151 | | }; |
| | | 152 | | } |
| | 6 | 153 | | if (isExpiredCredentialsError(error)) { |
| | 1 | 154 | | return { |
| | | 155 | | status: 'authentication_failed', |
| | | 156 | | reason: new ExpiredCredentialsError(error).message, |
| | | 157 | | }; |
| | | 158 | | } |
| | 5 | 159 | | const name = errorName(error); |
| | 5 | 160 | | const reason = describeError(error); |
| | 5 | 161 | | if (name === 'AccessDeniedException') { |
| | 1 | 162 | | return { status: 'access_denied', reason }; |
| | | 163 | | } |
| | 4 | 164 | | if (AUTHENTICATION_ERROR_NAMES.has(name)) { |
| | 3 | 165 | | return { status: 'authentication_failed', reason }; |
| | | 166 | | } |
| | 1 | 167 | | return { status: 'provider_error', reason }; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | function errorName(error: unknown): string { |
| | 10 | 172 | | return typeof error === 'object' && error !== null && 'name' in error |
| | | 173 | | ? String(error.name) |
| | | 174 | | : ''; |
| | | 175 | | } |