| | | 1 | | import { SSM } from '@aws-sdk/client-ssm'; |
| | | 2 | | import { STS } from '@aws-sdk/client-sts'; |
| | | 3 | | import type { MapFileConfig } from '../../domain/MapFileConfig.js'; |
| | | 4 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 5 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 6 | | import { AwsSsmSecretProvider } from './AwsSsmSecretProvider.js'; |
| | | 7 | | |
| | | 8 | | export async function resolveRegionWithFallback( |
| | | 9 | | profile?: string, |
| | | 10 | | ): Promise<string> { |
| | 4 | 11 | | if (process.env.AWS_REGION) { |
| | 1 | 12 | | return process.env.AWS_REGION; |
| | | 13 | | } |
| | 3 | 14 | | if (process.env.AWS_DEFAULT_REGION) { |
| | 1 | 15 | | return process.env.AWS_DEFAULT_REGION; |
| | | 16 | | } |
| | 2 | 17 | | try { |
| | 2 | 18 | | return await new SSM(profile ? { profile } : {}).config.region(); |
| | | 19 | | } catch { |
| | 1 | 20 | | return 'us-east-1'; |
| | | 21 | | } |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | export function createAwsSecretProvider( |
| | | 25 | | config: MapFileConfig, |
| | | 26 | | logger: ILogger, |
| | | 27 | | ): ISecretProvider { |
| | 26 | 28 | | const { profile } = config; |
| | 26 | 29 | | const region = () => resolveRegionWithFallback(profile); |
| | 26 | 30 | | const clientConfig = profile ? { profile, region } : { region }; |
| | 26 | 31 | | const ssm = new SSM(clientConfig); |
| | 26 | 32 | | const sts = new STS({ |
| | | 33 | | ...clientConfig, |
| | | 34 | | maxAttempts: 1, |
| | | 35 | | requestHandler: { requestTimeout: 2000 }, |
| | | 36 | | }); |
| | 26 | 37 | | return new AwsSsmSecretProvider(ssm, logger, sts, profile); |
| | | 38 | | } |