1
1
// In order to avoid the layer adding the 40mb aws-sdk to a deployment, (which is always available
2
2
// in the Lambda environment anyway), we use require to import the SDK.
3
3
4
+ import { logDebug } from "../utils" ;
5
+
4
6
export class KMSService {
5
7
private encryptionContext ;
6
8
@@ -12,6 +14,19 @@ export class KMSService {
12
14
const buffer = Buffer . from ( ciphertext , "base64" ) ;
13
15
let kms ;
14
16
17
+ const region = process . env . AWS_REGION ;
18
+ const isGovRegion = region !== undefined && region . startsWith ( "us-gov-" ) ;
19
+ if ( isGovRegion ) {
20
+ logDebug ( "Govcloud region detected. Using FIPs endpoints for secrets management." ) ;
21
+ }
22
+ let kmsClientParams = { } ;
23
+ if ( isGovRegion ) {
24
+ // Endpoints: https://docs.aws.amazon.com/general/latest/gr/kms.html
25
+ kmsClientParams = {
26
+ endpoint : `https://kms-fips.${ region } .amazonaws.com` ,
27
+ } ;
28
+ }
29
+
15
30
// Explicitly try/catch this require to appease esbuild and ts compiler
16
31
// otherwise users would need to mark this as `external`
17
32
// see https://github.com/DataDog/datadog-lambda-js/pull/409
@@ -20,11 +35,12 @@ export class KMSService {
20
35
} catch ( err ) {
21
36
if ( ( err as any ) . code === "MODULE_NOT_FOUND" ) {
22
37
// Node 18
23
- return this . decryptV3 ( buffer ) ;
38
+ return this . decryptV3 ( buffer , kmsClientParams ) ;
24
39
}
25
40
}
26
41
try {
27
- const kmsClient = new kms ( ) ;
42
+ // Configure KMS client to use FIPS endpoint
43
+ const kmsClient = new kms ( kmsClientParams ) ;
28
44
29
45
// When the API key is encrypted using the AWS console, the function name is added as an encryption context.
30
46
// When the API key is encrypted using the AWS CLI, no encryption context is added.
@@ -50,7 +66,7 @@ export class KMSService {
50
66
}
51
67
52
68
// Node 18 or AWS SDK V3
53
- public async decryptV3 ( buffer : Buffer ) : Promise < string > {
69
+ public async decryptV3 ( buffer : Buffer , kmsClientParams : any ) : Promise < string > {
54
70
// tslint:disable-next-line: variable-name one-variable-per-declaration
55
71
let KMSClient , DecryptCommand ;
56
72
// Explicitly try/catch this require to appease esbuild and ts compiler
@@ -61,7 +77,8 @@ export class KMSService {
61
77
} catch ( e ) {
62
78
throw Error ( "Can't load AWS SDK v2 or v3 to decrypt KMS key, custom metrics may not be sent" ) ;
63
79
}
64
- const kmsClient = new KMSClient ( ) ;
80
+
81
+ const kmsClient = new KMSClient ( kmsClientParams ) ;
65
82
let result ;
66
83
try {
67
84
const decryptCommand = new DecryptCommand ( { CiphertextBlob : buffer } ) ;
0 commit comments