Skip to content

Commit

Permalink
fix(license-list): upgrade to AWS SDK v3 (#1458)
Browse files Browse the repository at this point in the history
Refactor the license-list client to use AWS SDK v3. Tests updated
accordingly.

Also fixes a mock function for the deny-list integ test to use SDK v3.


----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain committed Sep 13, 2024
1 parent 622464b commit b9e135d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 87 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/__snapshots__/construct-hub.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -32,12 +32,12 @@ async function getAllObjectKeys(bucket: string) {
let continuationToken;
const objectKeys = new Array<string>();
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;

Expand Down
77 changes: 18 additions & 59 deletions src/__tests__/backend/license-list/client.lambda.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof requireEnv>;
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:
Expand All @@ -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<AWS.S3.GetObjectOutput>) => {
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();
Expand All @@ -58,18 +46,9 @@ test('basic use', async () => {

test('empty list', async () => {
// GIVEN
AWSMock.mock(
'S3',
'getObject',
(req: AWS.S3.GetObjectRequest, cb: Response<AWS.S3.GetObjectOutput>) => {
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();
Expand All @@ -80,18 +59,9 @@ test('empty list', async () => {

test('absent list', async () => {
// GIVEN
AWSMock.mock(
'S3',
'getObject',
(req: AWS.S3.GetObjectRequest, cb: Response<AWS.S3.GetObjectOutput>) => {
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();
Expand All @@ -102,23 +72,12 @@ test('absent list', async () => {

test('broken list', async () => {
// GIVEN
AWSMock.mock(
'S3',
'getObject',
(req: AWS.S3.GetObjectRequest, cb: Response<AWS.S3.GetObjectOutput>) => {
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<T> = (err: AWS.AWSError | null, data?: T) => void;
2 changes: 1 addition & 1 deletion src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/backend/license-list/client.lambda-shared.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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}/${
Expand All @@ -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}/${
Expand Down
10 changes: 5 additions & 5 deletions test/integ.deny-list.ts.snapshot/DenyListInteg.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,28 @@
}
}
},
"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"
},
"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}"
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/integ.deny-list.ts.snapshot/DenyListInteg.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -2018,7 +2018,7 @@
]
},
"HandlerArn": {
"Ref": "PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e"
"Ref": "PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa"
},
"InvocationType": "RequestResponse",
"Timeout": "120000",
Expand Down Expand Up @@ -2049,7 +2049,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e": {
"PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
Expand Down
4 changes: 2 additions & 2 deletions test/integ.deny-list.ts.snapshot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -391,7 +391,7 @@
"/DenyListInteg/PruneTest/CurrentVersion/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "PruneTestCurrentVersion332993F8f7b80c6aafda6dabc234ef63b21d065e"
"data": "PruneTestCurrentVersion332993F8757f75b11b0a281bf4ee6c5a433807fa"
}
],
"/DenyListInteg/PruneTest/Policy/Resource": [
Expand Down
2 changes: 1 addition & 1 deletion test/integ.deny-list.ts.snapshot/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit b9e135d

Please sign in to comment.