-
Notifications
You must be signed in to change notification settings - Fork 98
Add a special case for how S3 requests should be signed #1605
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
365cb3e
Add a special case for how S3 requests should be signed
Baccata b1a6489
Changelog
Baccata f5317a9
Re-enable tests for non-S3 signatures
Baccata b2da154
Avoid un-necessary flatten
Baccata 33eb2d4
Merge branch 'series/0.18' into s3-signing
Baccata 9a3ec23
Regenerated
Baccata 28daab0
jvm17 ordering
Baccata 5287c64
Regen copyright headers
Baccata 67d8928
Merge remote-tracking branch 'origin/series/0.18' into s3-signing
Baccata 908ddd1
Fix changelog
Baccata 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
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 |
|---|---|---|
|
|
@@ -37,6 +37,9 @@ import java.nio.charset.StandardCharsets | |
| */ | ||
| private[aws] object AwsSigning { | ||
|
|
||
| // see https://raw.githubusercontent.com/awslabs/aws-sdk-kotlin/main/codegen/sdk/aws-models/s3.json | ||
| val S3 = "AmazonS3" | ||
|
|
||
| def middleware[F[_]: Concurrent]( | ||
| awsEnvironment: AwsEnvironment[F] | ||
| ): Endpoint.Middleware[Client[F]] = new Endpoint.Middleware[Client[F]] { | ||
|
|
@@ -88,6 +91,12 @@ private[aws] object AwsSigning { | |
| credentials: F[AwsCredentials], | ||
| region: F[AwsRegion] | ||
| ): Request[F] => F[Request[F]] = { | ||
|
|
||
| // S3 has special rules, in that it expects the X-Amz-Content-SHA256 to be set. | ||
| val preSign: PreSigner[F] = | ||
| if (serviceName == S3) new PreSigner.S3InMemorySigned[F] | ||
| else new PreSigner.Standard[F] | ||
|
|
||
| val contentType = org.http4s.headers.`Content-Type`.headerInstance | ||
| val `Content-Type` = contentType.name | ||
|
|
||
|
|
@@ -106,79 +115,78 @@ private[aws] object AwsSigning { | |
| } | ||
|
|
||
| // scalafmt: { align.preset = most, danglingParentheses.preset = false, maxColumn = 240, align.tokens = [{code = ":"}]} | ||
| (request: Request[F]) => { | ||
|
|
||
| val bodyF = request.body.chunks.compile.to(Chunk).map(_.flatten) | ||
| val awsHeadersF = (bodyF, timestamp, credentials, region).mapN { case (body, timestamp, credentials, region) => | ||
| val credentialsScope = s"${timestamp.conciseDate}/$region/$endpointPrefix/aws4_request" | ||
| val queryParams: Vector[(String, String)] = | ||
| request.uri.query.toVector.sorted.map { case (k, v) => k -> v.getOrElse("") } | ||
| val canonicalQueryString = | ||
| if (queryParams.isEmpty) "" | ||
| else | ||
| queryParams | ||
| .map { case (k, v) => | ||
| URLEncoder.encode(k, StandardCharsets.UTF_8.name()) + "=" + URLEncoder.encode(v, StandardCharsets.UTF_8.name()) | ||
| } | ||
| .mkString("&") | ||
|
|
||
| // // !\ Important: these must remain in the same order | ||
| val baseHeadersList = List( | ||
| `Content-Type` -> request.contentType.map(contentType.value(_)).orNull, | ||
| `Host` -> request.uri.host.map(_.renderString).orNull, | ||
| `X-Amz-Date` -> timestamp.conciseDateTime, | ||
| `X-Amz-Security-Token` -> credentials.sessionToken.orNull, | ||
| `X-Amz-Target` -> (serviceName + "." + operationName) | ||
| ).filterNot(_._2 == null) | ||
|
|
||
| val canonicalHeadersString = baseHeadersList | ||
| .map { case (key, value) => | ||
| key.toString.toLowerCase + ":" + value.trim | ||
| } | ||
| .mkString(newline) | ||
| lazy val signedHeadersString = baseHeadersList.map(_._1).map(_.toString.toLowerCase()).mkString(";") | ||
|
|
||
| val payloadHash = sha256HexDigest(body.toArray) | ||
| val pathString = request.uri.path.toAbsolute.renderString | ||
| val canonicalRequest = new StringBuilder() | ||
| .append(request.method.name.toUpperCase()) | ||
| .append(newline) | ||
| .append(pathString) | ||
| .append(newline) | ||
| .append(canonicalQueryString) | ||
| .append(newline) | ||
| .append(canonicalHeadersString) | ||
| .append(newline) | ||
| .append(newline) | ||
| .append(signedHeadersString) | ||
| .append(newline) | ||
| .append(payloadHash) | ||
| .result() | ||
|
|
||
| val canonicalRequestHash = sha256HexDigest(canonicalRequest) | ||
| val signatureKey = getSignatureKey( | ||
| credentials.secretAccessKey, | ||
| timestamp.conciseDate, | ||
| region.value, | ||
| endpointPrefix | ||
| ) | ||
| val stringToSign = List[String]( | ||
| algorithm, | ||
| timestamp.conciseDateTime, | ||
| credentialsScope, | ||
| canonicalRequestHash | ||
| ).mkString(newline) | ||
| val signature = toHexString(hmacSha256(stringToSign, signatureKey)) | ||
| val authHeaderValue = s"${algorithm} Credential=${credentials.accessKeyId}/$credentialsScope, SignedHeaders=$signedHeadersString, Signature=$signature" | ||
| val authHeader = Headers("Authorization" -> authHeaderValue) | ||
| val baseHeaders = Headers(baseHeadersList.map { case (k, v) => Header.Raw(k, v) }) | ||
| authHeader ++ baseHeaders | ||
| } | ||
| (request: Request[F]) => | ||
| preSign(request).flatMap { case (payloadHash, preparedRequest) => | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is best compared side-by-side (split) in github, with whitespace hidden. This is one of the important changes |
||
| val awsHeadersF = (timestamp, credentials, region).mapN { case (timestamp, credentials, region) => | ||
| val credentialsScope = s"${timestamp.conciseDate}/$region/$endpointPrefix/aws4_request" | ||
| val queryParams: Vector[(String, String)] = | ||
| request.uri.query.toVector.sorted.map { case (k, v) => k -> v.getOrElse("") } | ||
| val canonicalQueryString = | ||
| if (queryParams.isEmpty) "" | ||
| else | ||
| queryParams | ||
| .map { case (k, v) => | ||
| URLEncoder.encode(k, StandardCharsets.UTF_8.name()) + "=" + URLEncoder.encode(v, StandardCharsets.UTF_8.name()) | ||
| } | ||
| .mkString("&") | ||
|
|
||
| // // !\ Important: these must remain in the same order | ||
| val baseHeadersList = List( | ||
|
kubukoz marked this conversation as resolved.
|
||
| `Content-Type` -> preparedRequest.contentType.map(contentType.value(_)).orNull, | ||
| `Host` -> preparedRequest.uri.host.map(_.renderString).orNull, | ||
| `X-Amz-Content-SHA256` -> preparedRequest.headers.get(`X-Amz-Content-SHA256`).map(_.head.value).orNull, | ||
| `X-Amz-Date` -> timestamp.conciseDateTime, | ||
| `X-Amz-Security-Token` -> credentials.sessionToken.orNull, | ||
| `X-Amz-Target` -> (serviceName + "." + operationName) | ||
| ).filterNot(_._2 == null) | ||
|
|
||
| val canonicalHeadersString = baseHeadersList | ||
| .map { case (key, value) => | ||
| key.toString.toLowerCase + ":" + value.trim | ||
| } | ||
| .mkString(newline) | ||
| lazy val signedHeadersString = baseHeadersList.map(_._1).map(_.toString.toLowerCase()).mkString(";") | ||
|
|
||
| val pathString = preparedRequest.uri.path.toAbsolute.renderString | ||
| val canonicalRequest = new StringBuilder() | ||
| .append(request.method.name.toUpperCase()) | ||
| .append(newline) | ||
| .append(pathString) | ||
| .append(newline) | ||
| .append(canonicalQueryString) | ||
| .append(newline) | ||
| .append(canonicalHeadersString) | ||
| .append(newline) | ||
| .append(newline) | ||
| .append(signedHeadersString) | ||
| .append(newline) | ||
| .append(payloadHash) | ||
| .result() | ||
|
|
||
| val canonicalRequestHash = sha256HexDigest(canonicalRequest) | ||
| val signatureKey = getSignatureKey( | ||
| credentials.secretAccessKey, | ||
| timestamp.conciseDate, | ||
| region.value, | ||
| endpointPrefix | ||
| ) | ||
| val stringToSign = List[String]( | ||
| algorithm, | ||
| timestamp.conciseDateTime, | ||
| credentialsScope, | ||
| canonicalRequestHash | ||
| ).mkString(newline) | ||
| val signature = toHexString(hmacSha256(stringToSign, signatureKey)) | ||
| val authHeaderValue = s"${algorithm} Credential=${credentials.accessKeyId}/$credentialsScope, SignedHeaders=$signedHeadersString, Signature=$signature" | ||
| val authHeader = Headers("Authorization" -> authHeaderValue) | ||
| val baseHeaders = Headers(baseHeadersList.map { case (k, v) => Header.Raw(k, v) }) | ||
| authHeader ++ baseHeaders | ||
| } | ||
|
|
||
| awsHeadersF.map { headers => | ||
| request.transformHeaders(_ ++ headers) | ||
| awsHeadersF.map { headers => | ||
| preparedRequest.transformHeaders(_ ++ headers) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val newline = System.lineSeparator() | ||
|
|
@@ -187,5 +195,31 @@ private[aws] object AwsSigning { | |
| private val `X-Amz-Security-Token` = CIString("X-Amz-Security-Token") | ||
| private val `X-Amz-Target` = CIString("X-Amz-Target") | ||
| private val algorithm = "AWS4-HMAC-SHA256" | ||
| private val `X-Amz-Content-SHA256` = CIString("X-Amz-Content-SHA256") | ||
|
|
||
| private sealed trait PreSigner[F[_]] { | ||
| def apply(request: Request[F]): F[(String, Request[F])] | ||
| } | ||
| private object PreSigner { | ||
| class Standard[F[_]](implicit F: Concurrent[F]) extends PreSigner[F] { | ||
| def apply(request: Request[F]): F[(String, Request[F])] = { | ||
| request.body.compile.to(Chunk).map { inMemoryBody => | ||
| val payloadHash = sha256HexDigest(inMemoryBody.toArray) | ||
| val newRequest = request.withBodyStream(fs2.Stream.chunk(inMemoryBody)) | ||
| (payloadHash, newRequest) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class S3InMemorySigned[F[_]](implicit F: Concurrent[F]) extends PreSigner[F] { | ||
| def apply(request: Request[F]): F[(String, Request[F])] = { | ||
| request.body.compile.to(Chunk).map { inMemoryBody => | ||
| val payloadHash = sha256HexDigest(inMemoryBody.toArray) | ||
| val newRequest = request.withBodyStream(fs2.Stream.chunk(inMemoryBody)).transformHeaders(_.put(Header.Raw(`X-Amz-Content-SHA256`, payloadHash))) | ||
| (payloadHash, newRequest) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
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
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.