Skip to content

Commit 299cbbb

Browse files
authored
fix(middleware-sdk-rds): stop throw when source id key is optional (#2770)
1 parent b09ba9a commit 299cbbb

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

packages/middleware-sdk-rds/src/index.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,10 @@ import { formatUrl } from "@aws-sdk/util-format-url";
1717

1818
const regARN = /arn:[\w+=/,.@-]+:[\w+=/,.@-]+:([\w+=/,.@-]*)?:[0-9]+:[\w+=/,.@-]+(:[\w+=/,.@-]+)?(:[\w+=/,.@-]+)?/;
1919

20-
const sourceIds: string[] = [
21-
"SourceDBSnapshotIdentifier",
22-
"SourceDBInstanceIdentifier",
23-
"ReplicationSourceIdentifier",
24-
"SourceDBClusterSnapshotIdentifier",
25-
"SourceDBInstanceArn",
26-
];
27-
2820
const sourceIdToCommandKeyMap: { [key: string]: string } = {
2921
SourceDBSnapshotIdentifier: "CopyDBSnapshot",
3022
SourceDBInstanceIdentifier: "CreateDBInstanceReadReplica",
31-
ReplicationSourceIdentifier: "CreateDBCluster",
23+
ReplicationSourceIdentifier: "CreateDBCluster", // This key is optional.
3224
SourceDBClusterSnapshotIdentifier: "CopyDBClusterSnapshot",
3325
SourceDBInstanceArn: "StartDBInstanceAutomatedBackupsReplication",
3426
};
@@ -47,24 +39,20 @@ interface PreviouslyResolved {
4739
* Config of the middleware to automatically add presigned URL to request.
4840
* The presigned URL is generated by sigV4
4941
*/
50-
5142
export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved): InitializeMiddleware<any, any> {
5243
return <Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
5344
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
5445
const { input } = args;
5546
const region = await options.region();
56-
let command, sourceId;
57-
for (const id of sourceIds) {
58-
if (input.hasOwnProperty(id)) {
59-
sourceId = id;
60-
command = sourceIdToCommandKeyMap[id];
61-
}
62-
}
63-
if (!sourceId) {
64-
throw new Error("Source identifier key not set");
65-
}
66-
if (!input.PreSignedUrl && isARN(input[sourceId]) && region !== getEndpointFromARN(input[sourceId])) {
67-
const sourceRegion = getEndpointFromARN(input[sourceId]);
47+
const sourceIdKey = Object.keys(sourceIdToCommandKeyMap).filter((sourceKeyId) =>
48+
input.hasOwnProperty(sourceKeyId)
49+
)[0];
50+
// Source id is optional.
51+
if (!sourceIdKey) return next(args);
52+
53+
const command = sourceIdToCommandKeyMap[sourceIdKey];
54+
if (!input.PreSignedUrl && isARN(input[sourceIdKey]) && region !== getEndpointFromARN(input[sourceIdKey])) {
55+
const sourceRegion = getEndpointFromARN(input[sourceIdKey]);
6856
const resolvedEndpoint = await options.endpoint();
6957
resolvedEndpoint.hostname = `rds.${sourceRegion}.amazonaws.com`;
7058
const request = new HttpRequest({
@@ -78,7 +66,7 @@ export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved):
7866
Version: version,
7967
KmsKeyId: input.KmsKeyId,
8068
DestinationRegion: region,
81-
[sourceId]: input[sourceId],
69+
[sourceIdKey]: input[sourceIdKey],
8270
},
8371
});
8472
const signer = new SignatureV4({

0 commit comments

Comments
 (0)