Skip to content

Commit 882550d

Browse files
authored
fix: override auth attributes from resolved endpoint metadata (#89)
1 parent 198bb5b commit 882550d

File tree

9 files changed

+96
-20
lines changed

9 files changed

+96
-20
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ env:
1313
# host owned by CRT team to host aws-crt-builder releases. Contact their on-call with any issues
1414
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
1515
PACKAGE_NAME: aws-sdk-kotlin
16-
LINUX_BASE_IMAGE: ubuntu-16-x64
1716
RUN: ${{ github.run_id }}-${{ github.run_number }}
1817

1918
jobs:
@@ -22,22 +21,21 @@ jobs:
2221
steps:
2322
- name: Checkout sources
2423
uses: actions/checkout@v2
25-
- uses: actions/cache@v2
26-
with:
27-
path: |
28-
~/.gradle/caches
29-
~/.gradle/wrapper
30-
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
31-
restore-keys: |
32-
${{ runner.os }}-gradle-
3324
- name: Build and Test ${{ env.PACKAGE_NAME }}
3425
env:
3526
CI_USER: ${{ secrets.CI_USER}}
3627
CI_ACCESS_TOKEN: ${{ secrets.CI_ACCESS_TOKEN }}
3728
run: |
38-
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder.pyz')"
39-
chmod a+x builder.pyz
40-
GIT_ASKPASS="$(pwd)/.github/scripts/git-ci-askpass.sh" ./builder.pyz build -p ${{ env.PACKAGE_NAME }}
29+
echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u awslabs --password-stdin
30+
export DOCKER_IMAGE=docker.pkg.github.com/awslabs/aws-crt-builder/aws-crt-ubuntu-16-x64:${{ env.BUILDER_VERSION }}
31+
docker pull $DOCKER_IMAGE
32+
docker run --mount type=bind,source=$(pwd),target=/root/${{ env.PACKAGE_NAME }} \
33+
--env GITHUB_REF \
34+
--env GITHUB_HEAD_REF \
35+
--env CI_USER \
36+
--env CI_ACCESS_TOKEN \
37+
--env GIT_ASKPASS=/root/${{ env.PACKAGE_NAME }}/.github/scripts/git-ci-askpass.sh \
38+
$DOCKER_IMAGE build -p ${{ env.PACKAGE_NAME }} --build-dir=/root/${{ env.PACKAGE_NAME }}
4139
4240
macos-compat:
4341
runs-on: macos-latest

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ allprojects {
1010
repositories {
1111
mavenLocal()
1212
mavenCentral()
13-
jcenter()
1413
}
1514
}
1615

client-runtime/auth/common/src/aws/sdk/kotlin/runtime/auth/AwsSigv4Signer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import aws.sdk.kotlin.crt.auth.signing.AwsSigningConfig
1212
import aws.sdk.kotlin.crt.toSignableCrtRequest
1313
import aws.sdk.kotlin.crt.update
1414
import aws.sdk.kotlin.runtime.InternalSdkApi
15+
import aws.sdk.kotlin.runtime.execution.AuthAttributes
1516
import software.aws.clientrt.client.ExecutionContext
1617
import software.aws.clientrt.http.*
1718
import software.aws.clientrt.http.operation.SdkHttpOperation

client-runtime/auth/common/test/aws/sdk/kotlin/runtime/auth/AwsSigv4SignerTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package aws.sdk.kotlin.runtime.auth
77

8+
import aws.sdk.kotlin.runtime.execution.AuthAttributes
89
import aws.sdk.kotlin.runtime.testing.runSuspendTest
910
import software.aws.clientrt.client.ExecutionContext
1011
import software.aws.clientrt.http.*

client-runtime/auth/common/src/aws/sdk/kotlin/runtime/auth/AuthAttributes.kt renamed to client-runtime/aws-client-rt/common/src/aws/sdk/kotlin/runtime/execution/AuthAttributes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
package aws.sdk.kotlin.runtime.auth
6+
package aws.sdk.kotlin.runtime.execution
77

88
import software.aws.clientrt.client.ClientOption
99
import software.aws.clientrt.time.Instant

client-runtime/protocols/http/common/src/aws/sdk/kotlin/runtime/http/middleware/ServiceEndpointResolver.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package aws.sdk.kotlin.runtime.http.middleware
88
import aws.sdk.kotlin.runtime.InternalSdkApi
99
import aws.sdk.kotlin.runtime.client.AwsClientOption
1010
import aws.sdk.kotlin.runtime.endpoint.EndpointResolver
11+
import aws.sdk.kotlin.runtime.execution.AuthAttributes
1112
import software.aws.clientrt.http.*
1213
import software.aws.clientrt.http.operation.HttpOperationContext
1314
import software.aws.clientrt.http.operation.SdkHttpOperation
@@ -56,6 +57,13 @@ public class ServiceEndpointResolver(
5657
req.builder.url.host = hostname
5758
req.builder.headers["Host"] = hostname
5859

60+
endpoint.signingName?.let {
61+
if (it.isNotBlank()) req.context[AuthAttributes.SigningService] = it
62+
}
63+
endpoint.signingRegion?.let {
64+
if (it.isNotBlank()) req.context[AuthAttributes.SigningRegion] = it
65+
}
66+
5967
next.call(req)
6068
}
6169
}

client-runtime/protocols/http/common/test/aws/sdk/kotlin/runtime/http/middleware/ServiceEndpointResolverTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ package aws.sdk.kotlin.runtime.http.middleware
88
import aws.sdk.kotlin.runtime.client.AwsClientOption
99
import aws.sdk.kotlin.runtime.endpoint.Endpoint
1010
import aws.sdk.kotlin.runtime.endpoint.EndpointResolver
11+
import aws.sdk.kotlin.runtime.execution.AuthAttributes
1112
import aws.sdk.kotlin.runtime.testing.runSuspendTest
1213
import software.aws.clientrt.http.*
1314
import software.aws.clientrt.http.engine.HttpClientEngine
1415
import software.aws.clientrt.http.operation.*
1516
import software.aws.clientrt.http.request.HttpRequestBuilder
1617
import software.aws.clientrt.http.response.HttpResponse
18+
import software.aws.clientrt.util.get
1719
import kotlin.test.Test
1820
import kotlin.test.assertEquals
1921
import kotlin.test.assertNotNull
@@ -92,4 +94,45 @@ class ServiceEndpointResolverTest {
9294
val response = op.roundTrip(client, Unit)
9395
assertNotNull(response)
9496
}
97+
98+
@Test
99+
fun `it overrides credential scopes`(): Unit = runSuspendTest {
100+
// if an endpoint specifies credential scopes we should override the context
101+
val expectedHost = "test.com"
102+
val mockEngine = object : HttpClientEngine {
103+
override suspend fun roundTrip(requestBuilder: HttpRequestBuilder): HttpResponse {
104+
assertEquals(expectedHost, requestBuilder.url.host)
105+
assertEquals(expectedHost, requestBuilder.headers["Host"])
106+
assertEquals("https", requestBuilder.url.scheme.protocolName)
107+
return HttpResponse(HttpStatusCode.fromValue(200), Headers {}, HttpBody.Empty, requestBuilder.build())
108+
}
109+
}
110+
111+
val client = sdkHttpClient(mockEngine)
112+
113+
val op = SdkHttpOperation.build<Unit, HttpResponse> {
114+
serializer = UnitSerializer
115+
deserializer = IdentityDeserializer
116+
context {
117+
service = "TestService"
118+
operationName = "testOperation"
119+
}
120+
}
121+
122+
op.install(ServiceEndpointResolver) {
123+
serviceId = "TestService"
124+
resolver = object : EndpointResolver {
125+
override suspend fun resolve(service: String, region: String): Endpoint {
126+
return Endpoint("test.com", "https", signingName = "foo", signingRegion = "us-west-2")
127+
}
128+
}
129+
}
130+
131+
op.context[AwsClientOption.Region] = "us-east-1"
132+
op.context[AuthAttributes.SigningRegion] = "us-east-1"
133+
op.context[AuthAttributes.SigningService] = "quux"
134+
op.roundTrip(client, Unit)
135+
assertEquals("foo", op.context[AuthAttributes.SigningService])
136+
assertEquals("us-west-2", op.context[AuthAttributes.SigningRegion])
137+
}
95138
}

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsHttpProtocolClientGenerator.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,11 @@ class AwsHttpProtocolClientGenerator(
7171
* render a utility function to populate an operation's ExecutionContext with defaults from service config, environment, etc
7272
*/
7373
private fun renderMergeServiceDefaults(writer: KotlinWriter) {
74-
writer.addImport("ExecutionContext", KotlinDependency.CLIENT_RT_CORE, "${KotlinDependency.CLIENT_RT_CORE.namespace}.client")
74+
writer.addImport(RuntimeTypes.Core.ExecutionContext)
7575
writer.addImport("SdkClientOption", KotlinDependency.CLIENT_RT_CORE, "${KotlinDependency.CLIENT_RT_CORE.namespace}.client")
7676
writer.addImport("resolveRegionForOperation", AwsKotlinDependency.AWS_CLIENT_RT_REGIONS)
77-
writer.addImport("AuthAttributes", AwsKotlinDependency.AWS_CLIENT_RT_AUTH)
78-
writer.addImport(
79-
"AwsClientOption",
80-
AwsKotlinDependency.AWS_CLIENT_RT_CORE, "${AwsKotlinDependency.AWS_CLIENT_RT_CORE.namespace}.client"
81-
)
77+
writer.addImport(AwsRuntimeTypes.Core.AuthAttributes)
78+
writer.addImport(AwsRuntimeTypes.Core.AwsClientOption)
8279
writer.addImport("putIfAbsent", KotlinDependency.CLIENT_RT_UTILS)
8380

8481
writer.dokka("merge the defaults configured for the service into the execution context before firing off a request")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
package aws.sdk.kotlin.codegen
7+
8+
import software.amazon.smithy.kotlin.codegen.buildSymbol
9+
import software.amazon.smithy.kotlin.codegen.namespace
10+
11+
/**
12+
* Commonly used AWS runtime types. Provides a single definition of a runtime symbol such that codegen isn't littered
13+
* with inline symbol creation which makes refactoring of the runtime more difficult and error prone.
14+
*
15+
* NOTE: Not all symbols need be added here but it doesn't hurt to define runtime symbols once.
16+
*/
17+
object AwsRuntimeTypes {
18+
object Core {
19+
val AwsClientOption = buildSymbol {
20+
name = "AwsClientOption"
21+
namespace(AwsKotlinDependency.AWS_CLIENT_RT_CORE, subpackage = "client")
22+
}
23+
24+
val AuthAttributes = buildSymbol {
25+
name = "AuthAttributes"
26+
namespace(AwsKotlinDependency.AWS_CLIENT_RT_CORE, subpackage = "execution")
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)