Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 70x 70x 70x 70x 70x 7x 7x 7x 283x 283x 263x 20x 20x 283x 261x 22x 22x 70x 70x | import type { CustomerVariableProvider, CustomerVariableName, CustomerVariableReturn } from '../types.ts'
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager'
import type { CloudFrontRequest } from 'aws-lambda'
import { getHeaderValue } from '../../headers.ts'
import { retrieveSecret } from './retrieve-secret.ts'
import type { NonNullableObject } from '../../types.ts'
import { DEFAULT_REGION, SECRET_NAME_HEADER_KEY } from '../defaults.ts'
import { isTruthy } from '../../is-truthy.ts'
interface SecretsInfo {
secretName: string | null
secretRegion: string | null
}
export class SecretsManagerVariables implements CustomerVariableProvider {
readonly name = 'SecretsManagerVariables'
private secretsInfo?: SecretsInfo
private validSecretsInfo?: NonNullableObject<SecretsInfo>
private readonly secretsManager?: SecretsManagerClient
constructor(
private readonly request: CloudFrontRequest,
private readonly cacheTtlMs?: number
) {
this.readSecretsInfoFromHeaders()
if (SecretsManagerVariables.isValidSecretInfo(this.secretsInfo)) {
this.validSecretsInfo = this.secretsInfo
try {
this.secretsManager = new SecretsManagerClient({ region: this.secretsInfo.secretRegion })
} catch (error) {
console.error('Failed to create secrets manager', {
error,
secretsInfo: this.secretsInfo,
})
}
}
}
async getVariable(variable: CustomerVariableName): Promise<CustomerVariableReturn> {
const secretsObject = await this.retrieveSecrets()
if (secretsObject === null) {
return null
}
const value = secretsObject[variable]
return value === null || value === undefined ? null : value.toString()
}
private async retrieveSecrets() {
if (!this.secretsManager || !this.validSecretsInfo) {
return null
}
try {
return await retrieveSecret(this.secretsManager, this.validSecretsInfo.secretName, this.cacheTtlMs)
} catch (error) {
console.error('Error retrieving secret from secrets manager', {
error,
secretsInfo: this.secretsInfo,
})
return null
}
}
private readSecretsInfoFromHeaders() {
this.secretsInfo ??= {
secretName: getHeaderValue(this.request, SECRET_NAME_HEADER_KEY),
secretRegion: DEFAULT_REGION,
}
}
private static isValidSecretInfo(secretsInfo?: SecretsInfo): secretsInfo is NonNullableObject<SecretsInfo> {
return secretsInfo !== undefined && isTruthy(secretsInfo.secretRegion) && isTruthy(secretsInfo.secretName)
}
}
|