Skip to content

Commit

Permalink
fix(middleware-sdk-rds): stop throw when source id key is optional (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored Sep 10, 2021
1 parent b09ba9a commit 299cbbb
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions packages/middleware-sdk-rds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand All @@ -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<any, any> {
return <Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
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({
Expand All @@ -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({
Expand Down

0 comments on commit 299cbbb

Please sign in to comment.