Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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