Skip to content
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

chore(middleware-flexible-checksums): use RequestChecksumCalculation and ResponseChecksumValidation #6492

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

trivikr
Copy link
Member

@trivikr trivikr commented Sep 18, 2024

Issue

Description

  • Uses values in RequestChecksumCalculation and ResponseChecksumValidation to decide flexible checksums algorithm.
  • Adds flexibleChecksumsInterceptorMiddleware to populate input[requestValidationModeMember]
  • Deprecates MD5, and sets CRC32 as default.

Testing

Unit testing in CI

Also, verified that checksums are computed by default

import { S3 } from "../aws-sdk-js-v3/clients/client-s3/dist-cjs/index.js";
import { NodeHttpHandler } from "@smithy/node-http-handler";
import { equal } from "assert";

// Prints checksum headers for request and response.
class CustomHandler extends NodeHttpHandler {
  constructor() {
    super();
  }

  printChecksumHeaders(prefix, headers) {
    for (const [header, value] of Object.entries(headers)) {
      if (header.startsWith("x-amz-checksum-") || header.startsWith("x-amz-sdk-checksum-")) {
        console.log(`${prefix}['${header}']: '${value}'`);
      }
    }
  }

  async handle(request, options) {
    this.printChecksumHeaders("request", request.headers);
    const response = await super.handle(request, options);
    this.printChecksumHeaders("response", response.response.headers);
    return response;
  }
}

// WHEN_SUPPORTED is default.
const client = new S3({
  requestHandler: new CustomHandler(),
});
const Bucket = "test-flexible-checksums-v2"; // Replace with your test bucket name.
const Key = "hello-world.txt";
const Body = "Hello World"; // Replace with the content you want to test.

console.log("Put Object");
// ChecksumAlgorithm is not explicitly required to be specified.
await client.putObject({ Bucket, Key, Body });

console.log("\nGet Object");
// ChecksumMode is not explicitly required to be specified.
const response = await client.getObject({ Bucket, Key });

equal(Body, await response.Body.transformToString());

Output

Put Object
request['x-amz-sdk-checksum-algorithm']: 'CRC32'
request['x-amz-checksum-crc32']: 'ShexVg=='
response['x-amz-checksum-crc32']: 'ShexVg=='
response['x-amz-checksum-type']: 'FULL_OBJECT'

Get Object
request['x-amz-checksum-mode']: 'ENABLED'
response['x-amz-checksum-crc32']: 'ShexVg=='
response['x-amz-checksum-type']: 'FULL_OBJECT'

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr changed the title chore(middleware-flexible-checksums): add flexibleChecksumsInterceptorMiddleware chore(middleware-flexible-checksums): use RequestChecksumCalculation and ResponseChecksumValidation Sep 18, 2024
@trivikr
Copy link
Member Author

trivikr commented Sep 19, 2024

The PR is ready for review. It's marked as draft, as it shouldn't be merged at this time.

@trivikr
Copy link
Member Author

trivikr commented Sep 20, 2024

The default checksum is changed from CRC32 to SHA256 based on benchmarks provided in #6499

@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from 28c1222 to aad6e6f Compare September 20, 2024 19:59
@trivikr
Copy link
Member Author

trivikr commented Oct 4, 2024

The default checksum is switched back to CRC32 based on internal discussions with other SDK maintainers

@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from fe9e256 to d81266b Compare November 1, 2024 22:37
@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from 40a8f9e to f9c40dd Compare November 12, 2024 16:51
@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from f9c40dd to cc3e793 Compare November 12, 2024 22:01
@trivikr
Copy link
Member Author

trivikr commented Nov 12, 2024

Feature flags were added in d7c329a (#6492)

@trivikr
Copy link
Member Author

trivikr commented Nov 13, 2024

Code to skip checksum computation when user has set checksum header field was added in bc74a9a (#6492)

@trivikr
Copy link
Member Author

trivikr commented Nov 13, 2024

The checksum computation is not skipped for x-amz-checksum-algorithm in 9204de2 (#6492)

Behavior reverted in internal communications

@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from c808a7a to bcffb1b Compare November 19, 2024 02:10
@trivikr
Copy link
Member Author

trivikr commented Nov 19, 2024

Commit to set input[requestAlgorithmMember] to default checksum algorithm if it's not set and either requestChecksumCalculation is supported or requestChecksumRequired is set to true: 317926c (#6492)

@trivikr
Copy link
Member Author

trivikr commented Nov 19, 2024

Commit to remove the case from flexibleChecksumsMiddleware where the input[requestAlgorithmMember] is not set, as that case is now handled in flexibleChecksumsInputMiddleware: dc5b5ae (#6492)

@trivikr
Copy link
Member Author

trivikr commented Nov 19, 2024

The tests are failing because the input is processed from middlewareConfig sent from and not args.
Since it's not related to using RequestChecksumCalculation and ResponseChecksumValidation, I'll fix it outside of PR and rebase.

@trivikr
Copy link
Member Author

trivikr commented Nov 22, 2024

100+ integration tests were added in ae9d5e9 (#6492)

We also fixed an edge case to skip checksum if input[requestAlgorithmMember] is not set

@trivikr
Copy link
Member Author

trivikr commented Nov 22, 2024

An edge case to leave input.requestAlgorithmMember if checksum header is set was implemented in 969be81 (#6492)

Reverted in 2d70aed (#6492) since request is not available before serialization.

@trivikr
Copy link
Member Author

trivikr commented Nov 25, 2024

To handle previous ask, the input[requestAlgorithmMember] (if added by SDK) was removed in 5f86077 (#6492) if checksum header is provided by the user.

const { requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;

if (hasHeaderWithPrefix("x-amz-checksum-", headers)) {
// Remove input[requestAlgorithmMember] and header, if it was added by flexibleChecksumsInputMiddleware
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should state the condition more completely, since

Remove input[requestAlgorithmMember] and header, if it was added by flexibleChecksumsInputMiddleware

sounds like an unconditional removal

it should perhaps say "... and the user has set the checksum via a modeled field"?

This reverts commit ba24558.
The ResponseChecksumValidation should be consumed in request middleware.
It sets input[requestAlgorithmMember] to default checksum algorithm
if it's not set and either requestChecksumCalculation is supported
or requestChecksumRequired is set to true.
Removes the case from flexibleChecksumsMiddleware where the
input[requestAlgorithmMember] is not set, as that case is now
checked in flexibleChecksumsInputMiddleware.

Also removes check for isS3Express and it's default algorithm.
…ddleware

This is because the request is not available pre-serialization.
This reverts commit 969be81.
@trivikr trivikr force-pushed the middleware-flexcheck-interceptor branch from 17a718e to f2d74ac Compare December 17, 2024 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants