Skip to content

Commit

Permalink
fix: Fix signer to allow streaming unsigned payload without checksum …
Browse files Browse the repository at this point in the history
…& add integration test (#1671)

* Refactor determineSignedBodyValue logic and fix it.

* Add integration test for stremaing payload with unsigned chunks.

* Resolve PR comments

---------

Co-authored-by: Sichan Yoo <[email protected]>
Co-authored-by: Josh Elkins <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent c2fd40b commit cafc52c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Smithy
import XCTest
import AWSS3
import SmithyHTTPAPI
@testable import ClientRuntime
import class SmithyStreams.BufferedStream
import class SmithyChecksums.ValidatingBufferedStream
Expand Down Expand Up @@ -81,6 +82,46 @@ final class S3FlexibleChecksumsTests: S3XCTestCase {
XCTAssertEqual(data, originalData)
}

// Streaming unsigned payload without checksum (chunked encoding)
class DisablePayloadSigning<InputType, OutputType>: HttpInterceptor {
func modifyBeforeRetryLoop(context: some MutableRequest<InputType, RequestType>) async throws {
context.getAttributes().set(key: SmithyHTTPAPIKeys.hasUnsignedPayloadTrait, value: true)
}
func modifyBeforeTransmit(context: some MutableRequest<InputType, RequestType>) async throws {
context.getRequest().withHeader(name: "x-amz-content-sha256", value: "STREAMING-UNSIGNED-PAYLOAD-TRAILER")
}
}

class DisablePayloadSigningProvider: HttpInterceptorProvider {
func create<InputType, OutputType>() -> any HttpInterceptor<InputType, OutputType> {
return DisablePayloadSigning()
}
}

func test_putGetObject_streamining_unsigned_chunked() async throws {
let config = try await S3Client.S3ClientConfiguration(region: region)
config.addInterceptorProvider(DisablePayloadSigningProvider())
let customizedClient = S3Client(config: config)

let bufferedStream = BufferedStream(data: originalData, isClosed: true)
let objectName = "flexible-checksums-s3-test-unsigned-chunked"

let putObjectInput = PutObjectInput(
body: .stream(bufferedStream),
bucket: bucketName,
key: objectName
)

_ = try await customizedClient.putObject(input: putObjectInput)

let getObjectInput = GetObjectInput(bucket: bucketName, key: objectName)
let getObjectOutput = try await client.getObject(input: getObjectInput)
let body = try XCTUnwrap(getObjectOutput.body)
let data = try await body.readData()
XCTAssertEqual(data, originalData)
}


// MARK: - Private methods

private func _testPutGetObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,10 @@ public class AWSSigV4Signer: SmithyHTTPAuthAPI.Signer {
}
}

// streaming + eligible for chunked transfer
if !checksumIsPresent {
return isUnsignedBody ? .unsignedPayload : .streamingSha256Payload
} else {
// checksum is present
return isUnsignedBody ? .streamingUnsignedPayloadTrailer : .streamingSha256PayloadTrailer
}
// STREAMING-UNSIGNED-PAYLOAD-TRAILER
if isUnsignedBody { return .streamingUnsignedPayloadTrailer }

return checksumIsPresent ? .streamingSha256PayloadTrailer : .streamingSha256Payload
}
}

Expand Down

0 comments on commit cafc52c

Please sign in to comment.