diff --git a/src/__tests__/__snapshots__/construct-hub.test.ts.snap b/src/__tests__/__snapshots__/construct-hub.test.ts.snap index 374dc9f11..9b4028bee 100644 --- a/src/__tests__/__snapshots__/construct-hub.test.ts.snap +++ b/src/__tests__/__snapshots__/construct-hub.test.ts.snap @@ -9398,7 +9398,7 @@ Direct link to Lambda function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[Test/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { @@ -22397,7 +22397,7 @@ Direct link to Lambda function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[Test/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { @@ -34968,7 +34968,7 @@ Direct link to Lambda function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[Test/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { @@ -47688,7 +47688,7 @@ Direct link to Lambda function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[Test/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { @@ -60389,7 +60389,7 @@ Direct link to Lambda function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[Test/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { diff --git a/src/__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts b/src/__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts index e443f7ce2..d631be34c 100644 --- a/src/__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts +++ b/src/__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts @@ -1,7 +1,7 @@ -import * as AWS from 'aws-sdk'; +import { ListObjectsV2CommandInput, S3 } from '@aws-sdk/client-s3'; import { requireEnv } from '../../../../backend/shared/env.lambda-shared'; -const s3 = new AWS.S3(); +const s3 = new S3(); export async function handler() { const bucketName = requireEnv('BUCKET_NAME'); @@ -32,12 +32,12 @@ async function getAllObjectKeys(bucket: string) { let continuationToken; const objectKeys = new Array(); do { - const listRequest: AWS.S3.ListObjectsV2Request = { + const listRequest: ListObjectsV2CommandInput = { Bucket: bucket, ContinuationToken: continuationToken, }; console.log(JSON.stringify({ listRequest })); - const listResponse = await s3.listObjectsV2(listRequest).promise(); + const listResponse = await s3.listObjectsV2(listRequest); console.log(JSON.stringify({ listResponse })); continuationToken = listResponse.NextContinuationToken; diff --git a/src/__tests__/backend/license-list/client.lambda.test.ts b/src/__tests__/backend/license-list/client.lambda.test.ts index 64446c782..d0e147172 100644 --- a/src/__tests__/backend/license-list/client.lambda.test.ts +++ b/src/__tests__/backend/license-list/client.lambda.test.ts @@ -1,18 +1,20 @@ -import * as AWS from 'aws-sdk'; -import * as AWSMock from 'aws-sdk-mock'; +import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; +import { mockClient } from 'aws-sdk-client-mock'; import * as Case from 'case'; import { LicenseListClient } from '../../../backend/license-list/client.lambda-shared'; import { EnvironmentVariables } from '../../../backend/license-list/constants'; -import { reset } from '../../../backend/shared/aws.lambda-shared'; import { requireEnv } from '../../../backend/shared/env.lambda-shared'; +import { stringToStream } from '../../streams'; jest.mock('../../../backend/shared/env.lambda-shared'); const mockRequireEnv = requireEnv as jest.MockedFunction; const mockBucketName = 'fake-bucket'; const mockObjectKey = 'object/key'; +const mockS3 = mockClient(S3Client); + beforeEach(() => { - AWSMock.setSDKInstance(AWS); + mockS3.reset(); mockRequireEnv.mockImplementation((name: string) => { switch (name) { case EnvironmentVariables.BUCKET_NAME: @@ -27,26 +29,12 @@ beforeEach(() => { }); }); -afterEach(() => { - reset(); - AWSMock.restore(); -}); - test('basic use', async () => { // GIVEN const mockLicense = 'MockLicense-1.0'; - AWSMock.mock( - 'S3', - 'getObject', - (req: AWS.S3.GetObjectRequest, cb: Response) => { - try { - expect(req).toEqual({ Bucket: mockBucketName, Key: mockObjectKey }); - cb(null, { Body: JSON.stringify([mockLicense]) }); - } catch (e: any) { - cb(e); - } - } - ); + mockS3 + .on(GetObjectCommand, { Bucket: mockBucketName, Key: mockObjectKey }) + .resolves({ Body: stringToStream(JSON.stringify([mockLicense])) }); // WHEN const licenseList = await LicenseListClient.newClient(); @@ -58,18 +46,9 @@ test('basic use', async () => { test('empty list', async () => { // GIVEN - AWSMock.mock( - 'S3', - 'getObject', - (req: AWS.S3.GetObjectRequest, cb: Response) => { - try { - expect(req).toEqual({ Bucket: mockBucketName, Key: mockObjectKey }); - cb(null, { Body: JSON.stringify([]) }); - } catch (e: any) { - cb(e); - } - } - ); + mockS3 + .on(GetObjectCommand, { Bucket: mockBucketName, Key: mockObjectKey }) + .resolves({ Body: stringToStream(JSON.stringify([])) }); // WHEN const licenseList = await LicenseListClient.newClient(); @@ -80,18 +59,9 @@ test('empty list', async () => { test('absent list', async () => { // GIVEN - AWSMock.mock( - 'S3', - 'getObject', - (req: AWS.S3.GetObjectRequest, cb: Response) => { - try { - expect(req).toEqual({ Bucket: mockBucketName, Key: mockObjectKey }); - cb(null, {}); - } catch (e: any) { - cb(e); - } - } - ); + mockS3 + .on(GetObjectCommand, { Bucket: mockBucketName, Key: mockObjectKey }) + .resolves({}); // WHEN const licenseList = await LicenseListClient.newClient(); @@ -102,23 +72,12 @@ test('absent list', async () => { test('broken list', async () => { // GIVEN - AWSMock.mock( - 'S3', - 'getObject', - (req: AWS.S3.GetObjectRequest, cb: Response) => { - try { - expect(req).toEqual({ Bucket: mockBucketName, Key: mockObjectKey }); - cb(null, { Body: JSON.stringify('{}', null, 2) }); - } catch (e: any) { - cb(e); - } - } - ); + mockS3 + .on(GetObjectCommand, { Bucket: mockBucketName, Key: mockObjectKey }) + .resolves({ Body: stringToStream(JSON.stringify('{}', null, 2)) }); // THEN return expect(LicenseListClient.newClient()).rejects.toThrowError( /Invalid format/ ); }); - -type Response = (err: AWS.AWSError | null, data?: T) => void; diff --git a/src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap b/src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap index 28a04fe1b..762f8531e 100644 --- a/src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap +++ b/src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap @@ -10680,7 +10680,7 @@ Direct link to the function: /lambda/home#/functions/", "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "455dc8070938b6c14e85478c54597fbcdd4f5b1fe07673be6f0b8918329ff2c6.zip", + "S3Key": "58bb2023f45f37abe4943309d10c7ece45a905ce10b9b3bd07b5d5e8dd5651c5.zip", }, "Description": "[dev/ConstructHub/Sources/NpmJs] Periodically query npmjs.com index for new packages", "Environment": { diff --git a/src/__tests__/package-sources/__snapshots__/code-artifact.test.ts.snap b/src/__tests__/package-sources/__snapshots__/code-artifact.test.ts.snap index 159fd40c4..16fd69d4a 100644 --- a/src/__tests__/package-sources/__snapshots__/code-artifact.test.ts.snap +++ b/src/__tests__/package-sources/__snapshots__/code-artifact.test.ts.snap @@ -243,7 +243,7 @@ exports[`default configuration 1`] = ` "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "cef554c64ae6cb679b47371ce4759963a74eabba08cc65b570afce94c1821f0e.zip", + "S3Key": "5377ecd434fd63dc93637b4a750a5d5cc04a66cb172f5888980ee60ec82e004b.zip", }, "DeadLetterConfig": { "TargetArn": { @@ -727,7 +727,7 @@ exports[`user-provided staging bucket 1`] = ` "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "cef554c64ae6cb679b47371ce4759963a74eabba08cc65b570afce94c1821f0e.zip", + "S3Key": "5377ecd434fd63dc93637b4a750a5d5cc04a66cb172f5888980ee60ec82e004b.zip", }, "DeadLetterConfig": { "TargetArn": { diff --git a/src/backend/license-list/client.lambda-shared.ts b/src/backend/license-list/client.lambda-shared.ts index 1f4f4f3ff..ffafb011f 100644 --- a/src/backend/license-list/client.lambda-shared.ts +++ b/src/backend/license-list/client.lambda-shared.ts @@ -1,5 +1,6 @@ +import { GetObjectCommand } from '@aws-sdk/client-s3'; import { EnvironmentVariables } from './constants'; -import { s3 } from '../shared/aws.lambda-shared'; +import { S3_CLIENT } from '../shared/aws.lambda-shared'; import { requireEnv } from '../shared/env.lambda-shared'; /** @@ -43,9 +44,9 @@ export class LicenseListClient { if (this.#map != null) { throw new Error('init() cannot be called twice'); } - const { Body: body } = await s3() - .getObject({ Bucket: this.#bucketName, Key: this.#objectKey }) - .promise(); + const { Body: body } = await S3_CLIENT.send( + new GetObjectCommand({ Bucket: this.#bucketName, Key: this.#objectKey }) + ); if (!body) { console.log( `WARNING: license list is empty at ${this.#bucketName}/${ @@ -56,7 +57,7 @@ export class LicenseListClient { return; } - const licenseIds = JSON.parse(body.toString('utf-8')); + const licenseIds = JSON.parse(await body.transformToString('utf-8')); if (!Array.isArray(licenseIds)) { throw new Error( `Invalid format in license list file at ${this.#bucketName}/${ diff --git a/test/integ.deny-list.ts.snapshot/DenyListInteg.assets.json b/test/integ.deny-list.ts.snapshot/DenyListInteg.assets.json index 749f71ae7..0ce78d2f0 100644 --- a/test/integ.deny-list.ts.snapshot/DenyListInteg.assets.json +++ b/test/integ.deny-list.ts.snapshot/DenyListInteg.assets.json @@ -131,20 +131,20 @@ } } }, - "48d993869418a3dd49aa6559ab0ebdd0dd35dc070685d051d22e14c67485a75b": { + "6be4bf22009eec0ea992793595949f624067525cedfd25947ac148a371b0159d": { "source": { - "path": "asset.48d993869418a3dd49aa6559ab0ebdd0dd35dc070685d051d22e14c67485a75b.bundle", + "path": "asset.6be4bf22009eec0ea992793595949f624067525cedfd25947ac148a371b0159d.bundle", "packaging": "zip" }, "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "48d993869418a3dd49aa6559ab0ebdd0dd35dc070685d051d22e14c67485a75b.zip", + "objectKey": "6be4bf22009eec0ea992793595949f624067525cedfd25947ac148a371b0159d.zip", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } }, - "3493864ccb154186e013addf6c6cc9d5f5811c0ea6b078d3d2d4507dc4c43371": { + "7bac9aa53fb2d35bac1f52f95821d12b446d626cc8281364f450fe0ac9a6508f": { "source": { "path": "DenyListInteg.template.json", "packaging": "file" @@ -152,7 +152,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "3493864ccb154186e013addf6c6cc9d5f5811c0ea6b078d3d2d4507dc4c43371.json", + "objectKey": "7bac9aa53fb2d35bac1f52f95821d12b446d626cc8281364f450fe0ac9a6508f.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/test/integ.deny-list.ts.snapshot/DenyListInteg.template.json b/test/integ.deny-list.ts.snapshot/DenyListInteg.template.json index 13c096587..92cd4a2f3 100644 --- a/test/integ.deny-list.ts.snapshot/DenyListInteg.template.json +++ b/test/integ.deny-list.ts.snapshot/DenyListInteg.template.json @@ -1981,7 +1981,7 @@ "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" }, - "S3Key": "48d993869418a3dd49aa6559ab0ebdd0dd35dc070685d051d22e14c67485a75b.zip" + "S3Key": "6be4bf22009eec0ea992793595949f624067525cedfd25947ac148a371b0159d.zip" }, "Description": "__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts", "Environment": { @@ -2018,7 +2018,7 @@ ] }, "HandlerArn": { - "Ref": "PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e" + "Ref": "PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa" }, "InvocationType": "RequestResponse", "Timeout": "120000", @@ -2049,7 +2049,7 @@ "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Delete" }, - "PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e": { + "PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa": { "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { diff --git a/test/integ.deny-list.ts.snapshot/manifest.json b/test/integ.deny-list.ts.snapshot/manifest.json index 199e1700f..5b27ffe27 100644 --- a/test/integ.deny-list.ts.snapshot/manifest.json +++ b/test/integ.deny-list.ts.snapshot/manifest.json @@ -18,7 +18,7 @@ "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/3493864ccb154186e013addf6c6cc9d5f5811c0ea6b078d3d2d4507dc4c43371.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7bac9aa53fb2d35bac1f52f95821d12b446d626cc8281364f450fe0ac9a6508f.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -391,7 +391,7 @@ "/DenyListInteg/PruneTest/CurrentVersion/Resource": [ { "type": "aws:cdk:logicalId", - "data": "PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e" + "data": "PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa" } ], "/DenyListInteg/PruneTest/Policy/Resource": [ diff --git a/test/integ.deny-list.ts.snapshot/tree.json b/test/integ.deny-list.ts.snapshot/tree.json index e43ae1cce..46a7fd5b0 100644 --- a/test/integ.deny-list.ts.snapshot/tree.json +++ b/test/integ.deny-list.ts.snapshot/tree.json @@ -3020,7 +3020,7 @@ "s3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" }, - "s3Key": "48d993869418a3dd49aa6559ab0ebdd0dd35dc070685d051d22e14c67485a75b.zip" + "s3Key": "6be4bf22009eec0ea992793595949f624067525cedfd25947ac148a371b0159d.zip" }, "description": "__tests__/backend/deny-list/mocks/trigger.prune-test.lambda.ts", "environment": {