Skip to content

Commit 132eea3

Browse files
sichanyooSichan Yoo
andauthored
fix!: Fix MD5 middleware generation criteria in codegen code & MD5 checksum hash calculation critera in runtime code. (#885)
Co-authored-by: Sichan Yoo <[email protected]>
1 parent e209cae commit 132eea3

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Sources/ClientRuntime/Networking/Http/Middlewares/ContentMD5Middleware.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@ public struct ContentMD5Middleware<OperationStackInput, OperationStackOutput> {
1515
public init() {}
1616

1717
private func addHeaders(builder: HTTPRequestBuilder, attributes: Context) async throws {
18-
// Skip MD5 hash if using checksums
19-
if builder.headers.exists(name: "x-amz-sdk-checksum-algorithm") {
18+
// Initialize logger
19+
guard let logger = attributes.getLogger() else {
20+
throw ClientError.unknownError("No logger found!")
21+
}
22+
23+
// Skip MD5 hash if using flexible checksum
24+
if builder.headers.headers.contains(where: {
25+
$0.name.lowercased().starts(with: "x-amz-checksum-")
26+
}) {
27+
logger.debug("Flexible checksum configured. Skipping MD5 checksum calculation.")
28+
return
29+
}
30+
31+
// Skip MD5 hash if it was provided in input by the user
32+
if builder.headers.headers.contains(where: {
33+
$0.name.lowercased() == "content-md5"
34+
}) {
35+
logger.debug("MD5 checksum hash provided in the input. Skipping MD5 checksum calculation.")
2036
return
2137
}
2238

@@ -42,15 +58,9 @@ public struct ContentMD5Middleware<OperationStackInput, OperationStackOutput> {
4258
let hashResult = try md5Hasher.digest().toBase64String()
4359
builder.updateHeader(name: "Content-MD5", value: hashResult)
4460
} catch {
45-
guard let logger = attributes.getLogger() else {
46-
return
47-
}
4861
logger.error("Could not compute Content-MD5 of stream due to error \(error)")
4962
}
5063
default:
51-
guard let logger = attributes.getLogger() else {
52-
return
53-
}
5464
logger.error("Unhandled case for Content-MD5")
5565
}
5666
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentMD5Middleware.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ class ContentMD5Middleware(
3838

3939
/**
4040
* Check if MD5 checksum is required
41-
* The Md5 middleware will only be installed if the operation requires a checksum and the user has not opted-in to flexible checksums.
41+
* The Md5 middleware will only be generated if the operation is marked with @httpChecksumRequired trait or if
42+
* only the requestChecksumRequired property of the httpChecksum is set.
43+
* https://smithy.io/2.0/aws/aws-core.html#behavior-with-httpchecksumrequired
44+
*
45+
* ContentMD5Middleware will skip checksum calculation if flexible checksum was calculated for request already,
46+
* determined by presence of a header with "x-amz-checksum-" prefix.
4247
*/
43-
private fun OperationShape.isMD5ChecksumRequired(): Boolean =
44-
// the checksum requirement can be modeled in either HttpChecksumTrait's `requestChecksumRequired` or the HttpChecksumRequired trait
45-
getTrait<HttpChecksumTrait>()?.isRequestChecksumRequired == true || hasTrait<HttpChecksumRequiredTrait>()
48+
private fun OperationShape.isMD5ChecksumRequired(): Boolean {
49+
val httpChecksumTrait = getTrait<HttpChecksumTrait>()
50+
val onlyRequestChecksumRequiredIsSetInHttpChecksumTrait = httpChecksumTrait?.isRequestChecksumRequired == true &&
51+
httpChecksumTrait.requestAlgorithmMember?.isEmpty == true &&
52+
httpChecksumTrait.requestValidationModeMember?.isEmpty == true &&
53+
httpChecksumTrait.responseAlgorithms?.isEmpty() == true
54+
return onlyRequestChecksumRequiredIsSetInHttpChecksumTrait || hasTrait<HttpChecksumRequiredTrait>()
55+
}

0 commit comments

Comments
 (0)