Skip to content

Commit

Permalink
chore: rename expiration to expiresIn in RequestPresigningArgum… (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 24, 2020
1 parent 236a808 commit 97ebfda
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 53 deletions.
6 changes: 1 addition & 5 deletions packages/middleware-sdk-ec2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function copySnapshotPresignedUrlMiddleware(
uriEscapePath: options.signingEscapePath
});
const presignedRequest = await signer.presign(request, {
expiration: expirationTime(3600)
expiresIn: 3600
});

args = {
Expand All @@ -80,10 +80,6 @@ export function copySnapshotPresignedUrlMiddleware(
};
}

function expirationTime(durationSeconds: number): DateInput {
return Math.round((new Date().valueOf() + durationSeconds * 1000) / 1000);
}

export const copySnapshotPresignedUrlMiddlewareOptions: InitializeHandlerOptions = {
step: "initialize",
tags: ["CROSS_REGION_PRESIGNED_URL"],
Expand Down
6 changes: 1 addition & 5 deletions packages/middleware-sdk-rds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function crossRegionPresignedUrlMiddleware(
uriEscapePath: options.signingEscapePath
});
const presignedRequest = await signer.presign(request, {
expiration: expirationTime(3600)
expiresIn: 3600
});
args = {
...args,
Expand Down Expand Up @@ -129,10 +129,6 @@ export const getCrossRegionPresignedUrlPlugin = (
}
});

function expirationTime(durationSeconds: number): DateInput {
return Math.round((new Date().valueOf() + durationSeconds * 1000) / 1000);
}

function isARN(id: string | undefined): boolean {
if (!id) return false;
return regARN.test(id);
Expand Down
9 changes: 3 additions & 6 deletions packages/s3-request-presigner/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ describe("s3 presigner", () => {
sha256: Hash.bind(null, "sha256"),
signingName: "s3"
};
const expiration = Math.floor(
(new Date("2000-01-01T00:00:00.000Z").valueOf() + 60 * 60 * 1000) / 1000
);
const presigningOptions = {
expiration,
expiresIn: 1800,
signingDate: new Date("2000-01-01T00:00:00.000Z")
};
const minimalRequest = new HttpRequest({
Expand Down Expand Up @@ -61,7 +58,7 @@ describe("s3 presigner", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "akid/20000101/us-bar-1/s3/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER
});
expect(minimalRequest).toMatchObject(originalRequest);
Expand All @@ -85,7 +82,7 @@ describe("s3 presigner", () => {
});
});

it("should default expiration to 900 seconds if not explicitly passed", async () => {
it("should default expiresIn to 900 seconds if not explicitly passed", async () => {
const signer = new S3RequestPresigner(s3ResolvedConfig);
const signed = await signer.presign(minimalRequest);
expect(signed.query).toMatchObject({
Expand Down
2 changes: 1 addition & 1 deletion packages/s3-request-presigner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class S3RequestPresigner implements RequestPresigner {
unsignableHeaders.add("content-type");
requestToSign.headers[SHA256_HEADER] = UNSIGNED_PAYLOAD;
return this.signer.presign(requestToSign, {
expiration: new Date(Date.now() + 900 * 1000),
expiresIn: 900,
unsignableHeaders,
...options
});
Expand Down
41 changes: 18 additions & 23 deletions packages/signature-v4/src/SignatureV4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ const credentials: Credentials = {

describe("SignatureV4", () => {
describe("#presignRequest", () => {
const expiration = Math.floor(
(new Date("2000-01-01T00:00:00.000Z").valueOf() + 60 * 60 * 1000) / 1000
);
const presigningOptions = {
expiration,
expiresIn: 1800,
signingDate: new Date("2000-01-01T00:00:00.000Z")
};

Expand All @@ -60,10 +57,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"477e1bb76b04bd9973b28d67a6307e43934ec8327fc17950539eb47573db3c4a"
"46f0091f3e84cbd4552a184f43830a4f8b42fd18ceaefcdc2c225be1efd9e00e"
});
});

Expand All @@ -86,10 +83,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"2e27ee66efe81b085eea0aa53948bb49b76efc90d285ae6b4960f6072608f495"
"3a7fc2cef9cab09384d0ef7a69bab0d942996846422bd041da5e52cae82612c3"
});
});

Expand All @@ -105,10 +102,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"0b13a0f33c2e949b565b61209478951f809bd6943310d44814c9526100047ea7"
"bd1427cfdc9a3b0a55609b0114d1dab4dfebca81a9496d6c47dedf65a3ec3bcb"
});
});

Expand All @@ -129,10 +126,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"60f0eb0b56c453974f0980ac8004c117e5f70f5720288c5fca0180cd6073fb95"
"457d44313f7b225c3523ddfc0ca161dfd010269b98c837a7a6f1b26ceb87ae4c"
});
});

Expand All @@ -153,10 +150,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"3663461416873c62951f3d97a93620d11f2b9bf584bb6790586cc8184aa2efd8"
"1b57912615b8e7ae78790ba713193d34baa793d6be2a1b18370dd27dce2d05a7"
});
});

Expand Down Expand Up @@ -185,10 +182,10 @@ describe("SignatureV4", () => {
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
[CREDENTIAL_QUERY_PARAM]: "foo/20000101/us-bar-1/foo/aws4_request",
[AMZ_DATE_QUERY_PARAM]: "20000101T000000Z",
[EXPIRES_QUERY_PARAM]: "3600",
[EXPIRES_QUERY_PARAM]: presigningOptions.expiresIn.toString(),
[SIGNED_HEADERS_QUERY_PARAM]: HOST_HEADER,
[SIGNATURE_QUERY_PARAM]:
"f098880292426cf244a8bf628c20eb6a1836f5e65acf7640193f0ff755592437"
"04ccc7891757c0ca3811d0e018e4655919ef11fa7b956fe9b782f273cec2374f"
});
});

Expand All @@ -212,11 +209,11 @@ describe("SignatureV4", () => {
expect(headersAsSigned).toEqual(headers);
});

it("should return a rejected promise if the expiration is more than one week in the future", async () => {
it("should return a rejected promise if the expiresIn is more than one week in the future", async () => {
await expect(
signer.presign(minimalRequest, {
...presigningOptions,
expiration: new Date()
expiresIn: 7 * 24 * 60 * 60 + 1
})
).rejects.toMatch(/less than one week in the future/);
});
Expand Down Expand Up @@ -279,7 +276,7 @@ describe("SignatureV4", () => {
presigningOptions
);
expect(query[SIGNATURE_QUERY_PARAM]).toBe(
"a70857a0dfb14773d814465001c5f0c0e708cc9a79609541fc22f57a70bdce12"
"6267d8b6f44d165d2b9f4d2c2b45fd6971de0962820243669bf685818c9c7849"
);
});

Expand Down Expand Up @@ -312,7 +309,7 @@ describe("SignatureV4", () => {
presigningOptions
);
expect(query[SIGNATURE_QUERY_PARAM]).toBe(
"474f2588e722ea064242d050c2ce6bf45deed6a2e9ce887ffbc6fa4dac9da620"
"d1a68eff5d8d5be581f20c7793a67a6cd2e561a5b818055b21ad064139eb83b1"
);
});
});
Expand Down Expand Up @@ -649,9 +646,7 @@ describe("SignatureV4", () => {

it("should use the current date for presigning if no signing date was supplied", async () => {
const date = new Date();
const { query } = await signer.presign(minimalRequest, {
expiration: Math.floor((date.valueOf() + 60 * 60 * 1000) / 1000)
});
const { query } = await signer.presign(minimalRequest);
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(
iso8601(date).replace(/[\-:]/g, "")
);
Expand Down
15 changes: 4 additions & 11 deletions packages/signature-v4/src/SignatureV4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from "@aws-sdk/types";
import { toHex } from "@aws-sdk/util-hex-encoding";
import { hasHeader } from "./hasHeader";
import { toDate, iso8601 } from "./utilDate";
import { iso8601 } from "./utilDate";

export interface SignatureV4Init {
/**
Expand Down Expand Up @@ -139,14 +139,13 @@ export class SignatureV4

const {
signingDate = new Date(),
expiration = new Date(Date.now() + 3600 * 1000),
expiresIn = 3600,
unsignableHeaders,
signableHeaders
} = options;

const { longDate, shortDate } = formatDate(signingDate);
const ttl = getTtl(signingDate, expiration);
if (ttl > MAX_PRESIGNED_TTL) {
if (expiresIn > MAX_PRESIGNED_TTL) {
return Promise.reject(
"Signature version 4 presigned URLs" +
" must have an expiration date less than one week in" +
Expand All @@ -165,7 +164,7 @@ export class SignatureV4
CREDENTIAL_QUERY_PARAM
] = `${credentials.accessKeyId}/${scope}`;
request.query[AMZ_DATE_QUERY_PARAM] = longDate;
request.query[EXPIRES_QUERY_PARAM] = ttl.toString(10);
request.query[EXPIRES_QUERY_PARAM] = expiresIn.toString(10);

const canonicalHeaders = getCanonicalHeaders(
request,
Expand Down Expand Up @@ -388,9 +387,3 @@ function getCanonicalHeaderList(headers: object): string {
.sort()
.join(";");
}

function getTtl(start: DateInput, expiration: DateInput): number {
return Math.floor(
(toDate(expiration).valueOf() - toDate(start).valueOf()) / 1000
);
}
4 changes: 2 additions & 2 deletions packages/types/src/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export interface RequestSigningArguments extends SigningArguments {

export interface RequestPresigningArguments extends RequestSigningArguments {
/**
* The time at which the signed request should no longer be honored.
* The number of seconds before the presigned URL expires
*/
expiration?: DateInput;
expiresIn?: number;
}

export interface EventSigningArguments extends SigningArguments {
Expand Down

0 comments on commit 97ebfda

Please sign in to comment.