-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
356 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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
42 changes: 42 additions & 0 deletions
42
...nfig/common/src/aws/sdk/kotlin/runtime/config/checksums/ResolveFlexibleChecksumsConfig.kt
This file contains 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,42 @@ | ||
package aws.sdk.kotlin.runtime.config.checksums | ||
|
||
import aws.sdk.kotlin.runtime.ConfigurationException | ||
import aws.sdk.kotlin.runtime.InternalSdkApi | ||
import aws.sdk.kotlin.runtime.config.AwsSdkSetting | ||
import aws.sdk.kotlin.runtime.config.profile.AwsProfile | ||
import aws.sdk.kotlin.runtime.config.profile.requestChecksumCalculation | ||
import aws.sdk.kotlin.runtime.config.profile.responseChecksumValidation | ||
import aws.smithy.kotlin.runtime.client.config.HttpChecksumConfigOption | ||
import aws.smithy.kotlin.runtime.config.resolve | ||
import aws.smithy.kotlin.runtime.util.LazyAsyncValue | ||
import aws.smithy.kotlin.runtime.util.PlatformProvider | ||
|
||
/** | ||
* Attempts to resolve requestChecksumCalculation from the specified sources. | ||
* @return requestChecksumCalculation setting if found, the default value if not. | ||
*/ | ||
@InternalSdkApi | ||
public suspend fun resolveRequestChecksumCalculation(platform: PlatformProvider = PlatformProvider.System, profile: LazyAsyncValue<AwsProfile>): HttpChecksumConfigOption { | ||
val unparsedString = AwsSdkSetting.AwsRequestChecksumCalculation.resolve(platform) ?: profile.get().requestChecksumCalculation | ||
return parseHttpChecksumConfigOption(unparsedString, "requestChecksumCalculation") | ||
} | ||
|
||
/** | ||
* Attempts to resolve responseChecksumValidation from the specified sources. | ||
* @return responseChecksumValidation setting if found, the default value if not. | ||
*/ | ||
@InternalSdkApi | ||
public suspend fun resolveResponseChecksumValidation(platform: PlatformProvider = PlatformProvider.System, profile: LazyAsyncValue<AwsProfile>): HttpChecksumConfigOption { | ||
val unparsedString = AwsSdkSetting.AwsResponseChecksumValidation.resolve(platform) ?: profile.get().responseChecksumValidation | ||
return parseHttpChecksumConfigOption(unparsedString, "responseChecksumValidation") | ||
} | ||
|
||
private fun parseHttpChecksumConfigOption(unparsedString: String?, configOption: String): HttpChecksumConfigOption = | ||
when (unparsedString?.uppercase()) { | ||
null -> HttpChecksumConfigOption.WHEN_SUPPORTED | ||
"WHEN_SUPPORTED" -> HttpChecksumConfigOption.WHEN_SUPPORTED | ||
"WHEN_REQUIRED" -> HttpChecksumConfigOption.WHEN_REQUIRED | ||
else -> throw ConfigurationException( | ||
"'$unparsedString' is not a valid value for $configOption. Valid values are: ${HttpChecksumConfigOption.entries}", | ||
) | ||
} |
29 changes: 0 additions & 29 deletions
29
...g/common/src/aws/sdk/kotlin/runtime/config/checksums/ResolveRequestChecksumCalculation.kt
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
...g/common/src/aws/sdk/kotlin/runtime/config/checksums/ResolveResponseChecksumValidation.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
47 changes: 47 additions & 0 deletions
47
...tp/common/src/aws/sdk/kotlin/runtime/http/interceptors/S3CompositeChecksumsInterceptor.kt
This file contains 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,47 @@ | ||
package aws.sdk.kotlin.runtime.http.interceptors | ||
|
||
import aws.smithy.kotlin.runtime.client.ProtocolResponseInterceptorContext | ||
import aws.smithy.kotlin.runtime.http.HeadersBuilder | ||
import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
import aws.smithy.kotlin.runtime.http.response.HttpResponse | ||
import aws.smithy.kotlin.runtime.http.response.toBuilder | ||
import aws.smithy.kotlin.runtime.telemetry.logging.Logger | ||
import aws.smithy.kotlin.runtime.telemetry.logging.logger | ||
import kotlin.coroutines.coroutineContext | ||
|
||
private const val CHECKSUM_HEADER_PREFIX = "x-amz-checksum-" | ||
|
||
/** | ||
* Removes any checksum headers that contain composite checksums from an S3 response. | ||
*/ | ||
public class S3CompositeChecksumsInterceptor : HttpInterceptor { | ||
override suspend fun modifyBeforeDeserialization(context: ProtocolResponseInterceptorContext<Any, HttpRequest, HttpResponse>): HttpResponse { | ||
val response = context.protocolResponse.toBuilder() | ||
val logger = coroutineContext.logger<S3CompositeChecksumsInterceptor>() | ||
|
||
response.headers.removeCompositeChecksums(logger) | ||
|
||
return response.build() | ||
} | ||
} | ||
|
||
/** | ||
* Removes any checksum headers that contain composite checksums. | ||
*/ | ||
internal fun HeadersBuilder.removeCompositeChecksums(logger: Logger): Unit = | ||
names().forEach { header -> | ||
if (header.startsWith(CHECKSUM_HEADER_PREFIX, ignoreCase = true) && get(header)!!.isCompositeChecksum()) { | ||
logger.warn { "S3 returned a composite checksum, composite checksums are not supported, removing checksum" } | ||
remove(header) | ||
} | ||
} | ||
|
||
/** | ||
* Verifies if a checksum is composite. | ||
*/ | ||
private fun String.isCompositeChecksum(): Boolean { | ||
// Ends with "-#" where "#" is a number | ||
val regex = Regex("-(\\d)+$") | ||
return regex.containsMatchIn(this) | ||
} |
35 changes: 35 additions & 0 deletions
35
...mmon/test/aws/sdk/kotlin/runtime/http/interceptors/S3CompositeChecksumsInterceptorTest.kt
This file contains 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,35 @@ | ||
package aws.sdk.kotlin.runtime.http.interceptors | ||
|
||
import aws.smithy.kotlin.runtime.http.HeadersBuilder | ||
import aws.smithy.kotlin.runtime.telemetry.logging.logger | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.coroutines.coroutineContext | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class S3CompositeChecksumsInterceptorTest { | ||
@Test | ||
fun compositeChecksumRemoval() = runTest { | ||
val headers = HeadersBuilder() | ||
|
||
headers.append("x-amz-checksum-crc32", "foo-1") | ||
headers.append("x-amz-checksum-sha256", "bar") | ||
|
||
assertTrue( | ||
headers.contains("x-amz-checksum-crc32"), | ||
) | ||
assertTrue( | ||
headers.contains("x-amz-checksum-sha256"), | ||
) | ||
|
||
headers.removeCompositeChecksums(coroutineContext.logger<S3CompositeChecksumsInterceptor>()) | ||
|
||
assertFalse( | ||
headers.contains("x-amz-checksum-crc32"), | ||
) | ||
assertTrue( | ||
headers.contains("x-amz-checksum-sha256"), | ||
) | ||
} | ||
} |
This file contains 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
This file contains 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
44 changes: 44 additions & 0 deletions
44
.../sdk/kotlin/codegen/customization/flexiblechecksums/s3/S3CompositeChecksumsIntegration.kt
This file contains 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,44 @@ | ||
package aws.sdk.kotlin.codegen.customization.flexiblechecksums.s3 | ||
|
||
import aws.sdk.kotlin.codegen.AwsRuntimeTypes.Http.Interceptors.S3CompositeChecksumsInterceptor | ||
import aws.sdk.kotlin.codegen.customization.s3.isS3 | ||
import software.amazon.smithy.aws.traits.HttpChecksumTrait | ||
import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
import software.amazon.smithy.kotlin.codegen.model.expectShape | ||
import software.amazon.smithy.kotlin.codegen.model.hasTrait | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.shapes.ServiceShape | ||
|
||
/** | ||
* Removes composite checksums returned by S3 so that flexible checksums won't validate them. | ||
* Composite checksums are used for multipart uploads and end with "-#" where "#" is a number. | ||
*/ | ||
class S3CompositeChecksumsIntegration : KotlinIntegration { | ||
override val order: Byte | ||
get() = -128 | ||
|
||
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = | ||
model.expectShape<ServiceShape>(settings.service).isS3 | ||
|
||
override fun customizeMiddleware(ctx: ProtocolGenerator.GenerationContext, resolved: List<ProtocolMiddleware>) = | ||
resolved + s3CompositeChecksumsMiddleware | ||
} | ||
|
||
/** | ||
* Adds [S3CompositeChecksumsInterceptor] to interceptors when operation has flexible checksums. | ||
*/ | ||
private val s3CompositeChecksumsMiddleware = object : ProtocolMiddleware { | ||
override val name: String = "S3CompositeChecksumsMiddleware" | ||
|
||
override fun isEnabledFor(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Boolean = | ||
op.hasTrait<HttpChecksumTrait>() | ||
|
||
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { | ||
writer.write("op.interceptors.add(#T())", S3CompositeChecksumsInterceptor) | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.