11// In order to avoid the layer adding the 40mb aws-sdk to a deployment, (which is always available
22// in the Lambda environment anyway), we use require to import the SDK.
33
4+ import { logDebug } from "../utils" ;
5+
46export class KMSService {
57 private encryptionContext ;
68
@@ -12,6 +14,19 @@ export class KMSService {
1214 const buffer = Buffer . from ( ciphertext , "base64" ) ;
1315 let kms ;
1416
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+
1530 // Explicitly try/catch this require to appease esbuild and ts compiler
1631 // otherwise users would need to mark this as `external`
1732 // see https://github.com/DataDog/datadog-lambda-js/pull/409
@@ -20,11 +35,12 @@ export class KMSService {
2035 } catch ( err ) {
2136 if ( ( err as any ) . code === "MODULE_NOT_FOUND" ) {
2237 // Node 18
23- return this . decryptV3 ( buffer ) ;
38+ return this . decryptV3 ( buffer , kmsClientParams ) ;
2439 }
2540 }
2641 try {
27- const kmsClient = new kms ( ) ;
42+ // Configure KMS client to use FIPS endpoint
43+ const kmsClient = new kms ( kmsClientParams ) ;
2844
2945 // When the API key is encrypted using the AWS console, the function name is added as an encryption context.
3046 // When the API key is encrypted using the AWS CLI, no encryption context is added.
@@ -50,7 +66,7 @@ export class KMSService {
5066 }
5167
5268 // Node 18 or AWS SDK V3
53- public async decryptV3 ( buffer : Buffer ) : Promise < string > {
69+ public async decryptV3 ( buffer : Buffer , kmsClientParams : any ) : Promise < string > {
5470 // tslint:disable-next-line: variable-name one-variable-per-declaration
5571 let KMSClient , DecryptCommand ;
5672 // Explicitly try/catch this require to appease esbuild and ts compiler
@@ -61,7 +77,8 @@ export class KMSService {
6177 } catch ( e ) {
6278 throw Error ( "Can't load AWS SDK v2 or v3 to decrypt KMS key, custom metrics may not be sent" ) ;
6379 }
64- const kmsClient = new KMSClient ( ) ;
80+
81+ const kmsClient = new KMSClient ( kmsClientParams ) ;
6582 let result ;
6683 try {
6784 const decryptCommand = new DecryptCommand ( { CiphertextBlob : buffer } ) ;
0 commit comments