Skip to content

fix(core): add error metadata indicating clock skew correction #5830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
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
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