import AWS from 'aws-sdk' ; import config from '../../config' ; import { Dictionary } from '../../domain/declarations' ; const getSecretValue = ( client , secretName ) : Promise < Dictionary < string > > => { return new Promise ( ( resolve , reject ) => { client . getSecretValue ( { SecretId : secretName } , ( err , data ) => { if ( err ) { if ( err . code === 'DecryptionFailureException' ) { return reject ( err ) ; } else if ( err . code === 'InternalServiceErrorException' ) { return reject ( err ) ; } else if ( err . code === 'InvalidParameterException' ) { return reject ( err ) ; } else if ( err . code === 'InvalidRequestException' ) { return reject ( err ) ; } else if ( err . code === 'ResourceNotFoundException' ) { return reject ( err ) ; } else { return reject ( err ) ; } } if ( ! data || ! data . SecretString ) { throw new Error ( `No value available for secret ${ secretName }` ) ; } resolve ( JSON . parse ( data . SecretString ) ) ; } ) ; } ) ; } ; param { boolean } fetchSecretsFromAWS return export const loadSecrets = async ( fetchSecretsFromAWS = true ) => { const region = process . env . REGION ; const client = new AWS . SecretsManager ( { region , } ) ; if ( fetchSecretsFromAWS ) { const secretName = config . secret ; const secrets = await getSecretValue ( client , secretName ) ; Object . keys ( secrets ) . forEach ( ( key ) => { process . env [ key ] = secrets [ key ] ; } ) ; return true ; } } ;