Skip to content

Commit

Permalink
fix(core): add error metadata indicating clock skew correction (#5830)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Feb 27, 2024
1 parent e1ba507 commit 46e5e8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AwsSdkSigV4Signer } from "./AwsSdkSigV4Signer";

describe(AwsSdkSigV4Signer.name, () => {
it("sets clockSkewCorrected metadata in error handler if systemClockOffset was updated", async () => {
const signer = new AwsSdkSigV4Signer();

let error: Error | any;
try {
signer.errorHandler({
config: {
systemClockOffset: 30 * 60 * 1000,
},
})(
Object.assign(new Error("uh oh"), {
$metadata: {},
$response: {
headers: {
date: new Date().toISOString(),
},
statusCode: 500,
},
})
);
} catch (e) {
error = e as Error;
}

expect((error as any).$metadata.clockSkewCorrected).toBe(true);
});
});
18 changes: 10 additions & 8 deletions packages/core/src/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ interface AwsSdkSigV4AuthSigningProperties {
*/
interface AwsSdkSigV4Exception extends ServiceException {
ServerTime?: string;
$metadata: ServiceException["$metadata"] & {
clockSkewCorrected?: boolean;
};
}

/**
Expand Down Expand Up @@ -104,11 +107,13 @@ export class AwsSdkSigV4Signer implements HttpSigner {
const serverTime: string | undefined =
(error as AwsSdkSigV4Exception).ServerTime ?? getDateHeader((error as AwsSdkSigV4Exception).$response);
if (serverTime) {
const config = throwSigningPropertyError(
"config",
signingProperties.config as AwsSdkSigV4Config | undefined
);
const config = throwSigningPropertyError("config", signingProperties.config as AwsSdkSigV4Config | undefined);
const initialSystemClockOffset = config.systemClockOffset;
config.systemClockOffset = getUpdatedSystemClockOffset(serverTime, config.systemClockOffset);
const clockSkewCorrected = config.systemClockOffset !== initialSystemClockOffset;
if (clockSkewCorrected && (error as AwsSdkSigV4Exception).$metadata) {
(error as AwsSdkSigV4Exception).$metadata.clockSkewCorrected = true;
}
}
throw error;
};
Expand All @@ -117,10 +122,7 @@ export class AwsSdkSigV4Signer implements HttpSigner {
successHandler(httpResponse: HttpResponse | unknown, signingProperties: Record<string, unknown>): void {
const dateHeader = getDateHeader(httpResponse);
if (dateHeader) {
const config = throwSigningPropertyError(
"config",
signingProperties.config as AwsSdkSigV4Config | undefined
);
const config = throwSigningPropertyError("config", signingProperties.config as AwsSdkSigV4Config | undefined);
config.systemClockOffset = getUpdatedSystemClockOffset(dateHeader, config.systemClockOffset);
}
}
Expand Down

0 comments on commit 46e5e8d

Please sign in to comment.