-
Notifications
You must be signed in to change notification settings - Fork 53
feat: sqs md5 checksum validation #1544
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
Open
xinsong-cui
wants to merge
13
commits into
main
Choose a base branch
from
feat-sqs-checksum-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5bd6de2
add support for md5 checksum validation
xinsong-cui 44b7d47
lint
xinsong-cui 7f1eaff
reduce duplication
xinsong-cui dc370a6
address pr feedbacks
xinsong-cui 70ed729
address pr feedbacks
xinsong-cui f84e6e4
lint
xinsong-cui 3b8c02d
deduplication
xinsong-cui 4c66977
address feedback
xinsong-cui 027ee3d
add changelog
xinsong-cui a37a4f2
better comment wording
xinsong-cui 30ca40d
pr feedback
xinsong-cui d86b08d
increase test coverage
xinsong-cui 893148c
address pr feedback
xinsong-cui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "cc154f8b-62ba-4ab5-8da5-84d1b5fe0d9f", | ||
"type": "feature", | ||
"description": "Added MD5 checksum validation for SQS message operations", | ||
"issues": [ | ||
"awslabs/aws-sdk-kotlin#222" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...in/kotlin/aws/sdk/kotlin/codegen/customization/sqs/SqsMd5ChecksumValidationIntegration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.codegen.customization.sqs | ||
|
||
import aws.sdk.kotlin.codegen.ServiceClientCompanionObjectWriter | ||
import aws.sdk.kotlin.codegen.sdkId | ||
import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
import software.amazon.smithy.kotlin.codegen.core.CodegenContext | ||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.integration.AppendingSectionWriter | ||
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding | ||
import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes | ||
import software.amazon.smithy.kotlin.codegen.model.buildSymbol | ||
import software.amazon.smithy.kotlin.codegen.model.expectShape | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware | ||
import software.amazon.smithy.kotlin.codegen.rendering.util.ConfigProperty | ||
import software.amazon.smithy.kotlin.codegen.rendering.util.ConfigPropertyType | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.shapes.ServiceShape | ||
|
||
/** | ||
* Register interceptor to handle SQS message MD5 checksum validation. | ||
*/ | ||
class SqsMd5ChecksumValidationIntegration : KotlinIntegration { | ||
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = | ||
model.expectShape<ServiceShape>(settings.service).sdkId.lowercase() == "sqs" | ||
|
||
companion object { | ||
val ValidationEnabledProp: ConfigProperty = ConfigProperty { | ||
name = "checksumValidationEnabled" | ||
symbol = buildSymbol { | ||
xinsong-cui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "ValidationEnabled" | ||
namespace = "aws.sdk.kotlin.services.sqs.internal" | ||
nullable = false | ||
} | ||
propertyType = ConfigPropertyType.Custom( | ||
render = { prop, writer -> | ||
writer.write("public val #1L: #2T = builder.#1L ?: #2T.NEVER", prop.propertyName, prop.symbol) | ||
}, | ||
renderBuilder = { prop, writer -> | ||
prop.documentation?.let(writer::dokka) | ||
writer.write("public var #L: #T? = null", prop.propertyName, prop.symbol) | ||
writer.write("") | ||
}, | ||
) | ||
documentation = """ | ||
Specifies when MD5 checksum validation should be performed for SQS messages. This controls the automatic | ||
calculation and validation of checksums during message operations. | ||
|
||
Valid values: | ||
- `ALWAYS` - Checksums are calculated and validated for both sending and receiving operations | ||
(SendMessage, SendMessageBatch, and ReceiveMessage) | ||
- `WHEN_SENDING` - Checksums are only calculated and validated during send operations | ||
(SendMessage and SendMessageBatch) | ||
- `WHEN_RECEIVING` - Checksums are only calculated and validated during receive operations | ||
(ReceiveMessage) | ||
- `NEVER` (default) - No checksum calculation or validation is performed | ||
""".trimIndent() | ||
// TODO: MD5 checksum validation is temporarily disabled. Change default to ALWAYS in v1.5 | ||
} | ||
|
||
private val validationScope = buildSymbol { | ||
name = "ValidationScope" | ||
namespace = "aws.sdk.kotlin.services.sqs.internal" | ||
nullable = false | ||
} | ||
|
||
val ValidationScopeProp: ConfigProperty = ConfigProperty { | ||
name = "checksumValidationScopes" | ||
symbol = KotlinTypes.Collections.set(validationScope) | ||
propertyType = ConfigPropertyType.Custom( | ||
render = { prop, writer -> | ||
writer.write("public val #1L: #2T = builder.#1L ?: #3T.entries.toSet()", prop.propertyName, prop.symbol, validationScope) | ||
}, | ||
renderBuilder = { prop, writer -> | ||
prop.documentation?.let(writer::dokka) | ||
writer.write("public var #L: #T? = null", prop.propertyName, prop.symbol) | ||
writer.write("") | ||
}, | ||
) | ||
documentation = """ | ||
Specifies which parts of an SQS message should undergo MD5 checksum validation. This configuration | ||
accepts a set of validation scopes that determine which message components to validate. | ||
|
||
Valid values: | ||
- `MESSAGE_ATTRIBUTES` - Validates checksums for message attributes | ||
- `MESSAGE_SYSTEM_ATTRIBUTES` - Validates checksums for message system attributes | ||
(Note: Not available for ReceiveMessage operations as SQS does not calculate checksums for | ||
system attributes during message receipt) | ||
- `MESSAGE_BODY` - Validates checksums for the message body | ||
|
||
Default: All three scopes (`MESSAGE_ATTRIBUTES`, `MESSAGE_SYSTEM_ATTRIBUTES`, `MESSAGE_BODY`) | ||
""".trimIndent() | ||
} | ||
} | ||
|
||
override fun additionalServiceConfigProps(ctx: CodegenContext): List<ConfigProperty> = | ||
listOf( | ||
ValidationEnabledProp, | ||
ValidationScopeProp, | ||
) | ||
|
||
override val sectionWriters: List<SectionWriterBinding> | ||
get() = listOf( | ||
SectionWriterBinding( | ||
ServiceClientCompanionObjectWriter.FinalizeEnvironmentalConfig, | ||
finalizeSqsConfigWriter, | ||
), | ||
) | ||
|
||
// add SQS-specific config finalization | ||
private val finalizeSqsConfigWriter = AppendingSectionWriter { writer -> | ||
val finalizeSqsConfig = buildSymbol { | ||
name = "finalizeSqsConfig" | ||
namespace = "aws.sdk.kotlin.services.sqs.internal" | ||
} | ||
writer.write("#T(builder, sharedConfig)", finalizeSqsConfig) | ||
} | ||
|
||
override fun customizeMiddleware( | ||
ctx: ProtocolGenerator.GenerationContext, | ||
resolved: List<ProtocolMiddleware>, | ||
): List<ProtocolMiddleware> = resolved + listOf(SqsMd5ChecksumValidationMiddleware) | ||
} | ||
|
||
internal object SqsMd5ChecksumValidationMiddleware : ProtocolMiddleware { | ||
override fun isEnabledFor(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Boolean = when (op.id.name) { | ||
"ReceiveMessage", | ||
"SendMessage", | ||
"SendMessageBatch", | ||
-> true | ||
else -> false | ||
} | ||
|
||
override val name: String = "SqsMd5ChecksumValidationInterceptor" | ||
|
||
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { | ||
val symbol = buildSymbol { | ||
name = [email protected] | ||
namespace = "aws.sdk.kotlin.services.sqs" | ||
} | ||
|
||
writer.write("op.interceptors.add(#T(config.checksumValidationEnabled, config.checksumValidationScopes))", symbol) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...otlin/aws/sdk/kotlin/codegen/customization/sqs/SqsMd5ChecksumValidationIntegrationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.codegen.customization.sqs | ||
|
||
import aws.sdk.kotlin.codegen.testutil.model | ||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware | ||
import software.amazon.smithy.kotlin.codegen.test.defaultSettings | ||
import software.amazon.smithy.kotlin.codegen.test.newTestContext | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
import kotlin.test.fail | ||
|
||
class SqsMd5ChecksumValidationIntegrationTest { | ||
object FooMiddleware : ProtocolMiddleware { | ||
override val name: String = "FooMiddleware" | ||
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) = | ||
fail("Unexpected call to `FooMiddleware.render`") | ||
} | ||
|
||
@Test | ||
fun testNotExpectedForNonSqsModel() { | ||
val model = model("NotSqs") | ||
val actual = SqsMd5ChecksumValidationIntegration().enabledForService(model, model.defaultSettings()) | ||
|
||
assertFalse(actual) | ||
} | ||
|
||
@Test | ||
fun testExpectedForSqsModel() { | ||
val model = model("Sqs") | ||
val actual = SqsMd5ChecksumValidationIntegration().enabledForService(model, model.defaultSettings()) | ||
|
||
assertTrue(actual) | ||
} | ||
|
||
@Test | ||
fun testMiddlewareAddition() { | ||
val model = model("Sqs") | ||
val preexistingMiddleware = listOf(FooMiddleware) | ||
val ctx = model.newTestContext("Sqs") | ||
val actual = SqsMd5ChecksumValidationIntegration().customizeMiddleware(ctx.generationCtx, preexistingMiddleware) | ||
|
||
assertEquals(listOf(FooMiddleware, SqsMd5ChecksumValidationMiddleware), actual) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.