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

fix: customization for x-amz-content-sha256 header bindings #1221

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 8 additions & 0 deletions .changes/42b15168-de5f-46aa-867a-02a28b4a7b31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "42b15168-de5f-46aa-867a-02a28b4a7b31",
"type": "bugfix",
"description": "Properly handle members with HTTP header bindings to x-amz-content-sha256",
"issues": [
"https://github.com/awslabs/aws-sdk-kotlin/issues/1217"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.codegen.customization

import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes
import software.amazon.smithy.kotlin.codegen.core.defaultName
import software.amazon.smithy.kotlin.codegen.core.withBlock
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
import software.amazon.smithy.kotlin.codegen.model.getTrait
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.traits.HttpHeaderTrait

/**
* Registers a middleware which overrides the execution context HashSpecification for members with an HTTP header binding
* to `x-amz-content-sha256`.
* https://github.com/awslabs/aws-sdk-kotlin/issues/1217
*/
class UpdateExecutionContextWithXAmzContentSha256HeaderBinding : KotlinIntegration {
override fun customizeMiddleware(
ctx: ProtocolGenerator.GenerationContext,
resolved: List<ProtocolMiddleware>,
): List<ProtocolMiddleware> = resolved + UpdateExecutionContextWithSha256HeaderBindingMiddleware()
}

private class UpdateExecutionContextWithSha256HeaderBindingMiddleware : ProtocolMiddleware {
override val name: String = "UpdateExecutionContextWithXAmzContentSha256HeaderBinding"

override fun isEnabledFor(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Boolean = getXAmzContentSha256HeaderMember(ctx, op) != null

override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) {
val member = getXAmzContentSha256HeaderMember(ctx, op)!!
writer.withBlock("input.${member.defaultName()}?.let {", "}") {
writer.write("op.context[#1T.#2T] = #2T.Precalculated(it)", RuntimeTypes.Auth.Signing.AwsSigningCommon.AwsSigningAttributes, RuntimeTypes.Auth.Signing.AwsSigningCommon.HashSpecification)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: The HashSpecification class is not the same as the HashSpecification property of AwsSigningAttributes so reusing the same symbol is incorrect. For properties, we should just use the literal name in the codegen text:

writer.write(
    "op.context[#T.HashSpecification] = #T.Precalculated(it)",
    RuntimeTypes.Auth.Signing.AwsSigningCommon.AwsSigningAttributes,
    RuntimeTypes.Auth.Signing.AwsSigningCommon.HashSpecification,
)

}
}

private fun getXAmzContentSha256HeaderMember(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): MemberShape? {
val input = ctx.model.expectShape(op.inputShape)
return input.members().singleOrNull { it.getTrait<HttpHeaderTrait>()?.value?.equals("x-amz-content-sha256") ?: false }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ aws.sdk.kotlin.codegen.customization.RemoveDefaults
aws.sdk.kotlin.codegen.customization.s3.UnsupportedSigningAlgorithmIntegration
aws.sdk.kotlin.codegen.customization.SigV4AsymmetricTraitCustomization
aws.sdk.kotlin.codegen.customization.cloudfrontkeyvaluestore.BackfillSigV4ACustomization
aws.sdk.kotlin.codegen.customization.UpdateExecutionContextWithXAmzContentSha256HeaderBinding
Loading