-
Notifications
You must be signed in to change notification settings - Fork 17
/
awsConfig.ts
86 lines (74 loc) · 2.33 KB
/
awsConfig.ts
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
85
86
import { fromTokenFile } from '@aws-sdk/credential-providers'
import type { ConfigScope } from '@lokalise/node-core'
import { generateWildcardSnsArn, generateWildcardSqsArn } from '@message-queue-toolkit/sqs'
import type { AwsCredentialIdentity, Provider } from '@smithy/types'
function ensureWildcard(value?: string) {
if (!value) {
return value
}
if (!value.endsWith('*')) {
return `${value}*`
}
return value
}
export const awsSnsPrefixTransformer = (value?: string) => {
const valueWithWildCard = ensureWildcard(value)
if (!valueWithWildCard) {
return
}
return generateWildcardSnsArn(valueWithWildCard)
}
export const awsSqsPrefixTransformer = (value?: string) => {
const valueWithWildCard = ensureWildcard(value)
if (!valueWithWildCard) {
return
}
return generateWildcardSqsArn(valueWithWildCard)
}
export type AwsAwareDependencies = {
config: {
aws: AwsConfig
}
}
export type AwsConfig = {
region: string
kmsKeyId: string
allowedSourceOwner: string
sns: {
endpoint?: string
topicArnPattern?: string
}
sqs: {
endpoint?: string
queueArnPattern?: string
}
credentials?: AwsCredentialIdentity | Provider<AwsCredentialIdentity>
}
export function getAwsConfig(configScope: ConfigScope): AwsConfig {
const accessKeyId = configScope.getOptionalNullable('AWS_ACCESS_KEY_ID', undefined)
const secretAccessKey = configScope.getOptionalNullable('AWS_SECRET_ACCESS_KEY', undefined)
const resolvedCredentials =
accessKeyId && secretAccessKey ? { accessKeyId, secretAccessKey } : fromTokenFile()
return {
region: configScope.getMandatory('AWS_REGION'),
kmsKeyId: configScope.getOptionalNullable('AWS_KMS_KEY_ID', ''),
allowedSourceOwner: configScope.getOptionalNullable('AWS_ALLOWED_SOURCE_OWNER', ''),
sns: {
endpoint: configScope.getOptionalNullable('AWS_SNS_ENDPOINT', undefined),
topicArnPattern: configScope.getOptionalNullableTransformed(
'AWS_SNS_TOPIC_NAME_PATTERN',
undefined,
awsSnsPrefixTransformer,
),
},
sqs: {
endpoint: configScope.getOptionalNullable('AWS_SQS_ENDPOINT', undefined),
queueArnPattern: configScope.getOptionalNullableTransformed(
'AWS_SQS_QUEUE_NAME_PATTERN',
undefined,
awsSqsPrefixTransformer,
),
},
credentials: resolvedCredentials,
}
}