From a482da54e947c3381ed68d9ec5b17de876ea2075 Mon Sep 17 00:00:00 2001 From: AllanZhengYP Date: Thu, 9 Sep 2021 07:07:24 +0000 Subject: [PATCH] fix(middleware-sdk-rds): stop throw when source id key is optional --- packages/middleware-sdk-rds/src/index.ts | 34 ++++++++---------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/packages/middleware-sdk-rds/src/index.ts b/packages/middleware-sdk-rds/src/index.ts index a371ae395cdf..ef53ebceadeb 100644 --- a/packages/middleware-sdk-rds/src/index.ts +++ b/packages/middleware-sdk-rds/src/index.ts @@ -17,18 +17,10 @@ import { formatUrl } from "@aws-sdk/util-format-url"; const regARN = /arn:[\w+=/,.@-]+:[\w+=/,.@-]+:([\w+=/,.@-]*)?:[0-9]+:[\w+=/,.@-]+(:[\w+=/,.@-]+)?(:[\w+=/,.@-]+)?/; -const sourceIds: string[] = [ - "SourceDBSnapshotIdentifier", - "SourceDBInstanceIdentifier", - "ReplicationSourceIdentifier", - "SourceDBClusterSnapshotIdentifier", - "SourceDBInstanceArn", -]; - const sourceIdToCommandKeyMap: { [key: string]: string } = { SourceDBSnapshotIdentifier: "CopyDBSnapshot", SourceDBInstanceIdentifier: "CreateDBInstanceReadReplica", - ReplicationSourceIdentifier: "CreateDBCluster", + ReplicationSourceIdentifier: "CreateDBCluster", // This key is optional. SourceDBClusterSnapshotIdentifier: "CopyDBClusterSnapshot", SourceDBInstanceArn: "StartDBInstanceAutomatedBackupsReplication", }; @@ -47,24 +39,20 @@ interface PreviouslyResolved { * Config of the middleware to automatically add presigned URL to request. * The presigned URL is generated by sigV4 */ - export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved): InitializeMiddleware { return (next: InitializeHandler): InitializeHandler => async (args: InitializeHandlerArguments): Promise> => { const { input } = args; const region = await options.region(); - let command, sourceId; - for (const id of sourceIds) { - if (input.hasOwnProperty(id)) { - sourceId = id; - command = sourceIdToCommandKeyMap[id]; - } - } - if (!sourceId) { - throw new Error("Source identifier key not set"); - } - if (!input.PreSignedUrl && isARN(input[sourceId]) && region !== getEndpointFromARN(input[sourceId])) { - const sourceRegion = getEndpointFromARN(input[sourceId]); + const sourceIdKey = Object.keys(sourceIdToCommandKeyMap).filter((sourceKeyId) => + input.hasOwnProperty(sourceKeyId) + )[0]; + // Source id is optional. + if (!sourceIdKey) return next(args); + + const command = sourceIdToCommandKeyMap[sourceIdKey]; + if (!input.PreSignedUrl && isARN(input[sourceIdKey]) && region !== getEndpointFromARN(input[sourceIdKey])) { + const sourceRegion = getEndpointFromARN(input[sourceIdKey]); const resolvedEndpoint = await options.endpoint(); resolvedEndpoint.hostname = `rds.${sourceRegion}.amazonaws.com`; const request = new HttpRequest({ @@ -78,7 +66,7 @@ export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved): Version: version, KmsKeyId: input.KmsKeyId, DestinationRegion: region, - [sourceId]: input[sourceId], + [sourceIdKey]: input[sourceIdKey], }, }); const signer = new SignatureV4({