Skip to content

Commit 4b793c9

Browse files
authored
refactor!: require a resolved configuration (#351)
1 parent 9f9353c commit 4b793c9

File tree

19 files changed

+340
-244
lines changed

19 files changed

+340
-244
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.runtime.config
7+
8+
import aws.sdk.kotlin.runtime.auth.credentials.CredentialsProvider
9+
import aws.sdk.kotlin.runtime.auth.credentials.DefaultChainCredentialsProvider
10+
import aws.sdk.kotlin.runtime.client.AwsClientConfig
11+
import aws.sdk.kotlin.runtime.region.resolveRegion
12+
import aws.smithy.kotlin.runtime.util.Platform
13+
import aws.smithy.kotlin.runtime.util.PlatformProvider
14+
15+
/**
16+
* Options that control how an [aws.sdk.kotlin.runtime.client.AwsClientConfig] is resolved
17+
*/
18+
public class AwsClientConfigLoadOptions {
19+
// TODO - see Go SDK for an idea of possible knobs
20+
// https://github.com/aws/aws-sdk-go-v2/blob/973e5f5e9477e0690bcb4be3145bb72e956651cc/config/load_options.go#L22
21+
// Most of these will not be needed if you are using a corresponding environment variable or system property but there should be an explicit
22+
// option to control behavior.
23+
24+
/**
25+
* The region to make requests to. When set, region will not attempt to be resolved from other sources
26+
*/
27+
public var region: String? = null
28+
29+
/**
30+
* The [CredentialsProvider] to use when sourcing [aws.sdk.kotlin.runtime.auth.credentials.Credentials].
31+
*/
32+
public var credentialsProvider: CredentialsProvider? = null
33+
34+
// FIXME - expose profile name override and thread through region/cred provider chains
35+
}
36+
37+
/**
38+
* Load the AWS client configuration from the environment.
39+
*/
40+
public suspend fun AwsClientConfig.Companion.fromEnvironment(
41+
block: AwsClientConfigLoadOptions.() -> Unit = {}
42+
): AwsClientConfig = loadAwsClientConfig(Platform, block)
43+
44+
internal suspend fun loadAwsClientConfig(
45+
platformProvider: PlatformProvider,
46+
block: AwsClientConfigLoadOptions.() -> Unit
47+
): AwsClientConfig {
48+
val opts = AwsClientConfigLoadOptions().apply(block)
49+
50+
val region = opts.region ?: resolveRegion(platformProvider)
51+
52+
val credentialsProvider = opts.credentialsProvider ?: DefaultChainCredentialsProvider()
53+
54+
return ResolvedAwsConfig(region, credentialsProvider)
55+
}
56+
57+
private data class ResolvedAwsConfig(
58+
override val region: String,
59+
override val credentialsProvider: CredentialsProvider
60+
) : AwsClientConfig

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/region/DefaultRegionProviderChain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ import aws.smithy.kotlin.runtime.util.PlatformProvider
1616
* 3. Check the AWS config files/profile for region information
1717
* 4. If running on EC2, check the EC2 metadata service for region
1818
*/
19-
public expect class DefaultRegionProviderChain public constructor(
19+
internal expect class DefaultRegionProviderChain constructor(
2020
platformProvider: PlatformProvider = Platform
2121
) : RegionProvider, Closeable

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/region/ImdsRegionProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private const val REGION_PATH: String = "/latest/meta-data/placement/region"
2121
* @param client the IMDS client to use to resolve region information with
2222
* @param platformProvider the [PlatformEnvironProvider] instance
2323
*/
24-
public class ImdsRegionProvider(
24+
internal class ImdsRegionProvider(
2525
private val client: Lazy<ImdsClient> = lazy { ImdsClient() },
2626
private val platformProvider: PlatformEnvironProvider = Platform,
2727
) : RegionProvider, Closeable {

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/region/ResolveRegion.kt

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,14 @@
55

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

8-
import aws.sdk.kotlin.runtime.ClientException
9-
import aws.sdk.kotlin.runtime.InternalSdkApi
10-
import aws.sdk.kotlin.runtime.client.AwsAdvancedClientOption
11-
import aws.sdk.kotlin.runtime.client.AwsClientOption
12-
import aws.smithy.kotlin.runtime.client.ExecutionContext
8+
import aws.sdk.kotlin.runtime.ConfigurationException
9+
import aws.smithy.kotlin.runtime.io.use
10+
import aws.smithy.kotlin.runtime.util.PlatformProvider
1311

1412
/**
1513
* Attempt to resolve the region to make requests to.
16-
*
17-
* Regions are resolved in the following order:
18-
* 1. From the existing [ctx]
19-
* 2. From the region [config]
20-
* 3. Using default region detection (only if-enabled)
2114
*/
22-
@InternalSdkApi
23-
public suspend fun resolveRegionForOperation(ctx: ExecutionContext, config: RegionConfig): String {
24-
// favor the context if it's already set
25-
val regionFromCtx = ctx.getOrNull(AwsClientOption.Region)
26-
if (regionFromCtx != null) return regionFromCtx
27-
28-
// use the default from the service config if configured
29-
if (config.region != null) return config.region!!
30-
31-
// attempt to detect
32-
val allowDefaultRegionDetect = ctx.getOrNull(AwsAdvancedClientOption.EnableDefaultRegionDetection) ?: true
33-
if (!allowDefaultRegionDetect) {
34-
throw ClientException("No region was configured and region detection has been disabled")
15+
internal suspend fun resolveRegion(platformProvider: PlatformProvider): String =
16+
DefaultRegionProviderChain(platformProvider).use { providerChain ->
17+
providerChain.getRegion() ?: throw ConfigurationException("unable to auto detect AWS region, tried: $providerChain")
3518
}
36-
37-
// TODO - propagate any relevant ctx/config to the default chain
38-
val providerChain = DefaultRegionProviderChain()
39-
return providerChain.getRegion() ?: throw ClientException("unable to auto detect AWS region, tried: $providerChain")
40-
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.runtime.config
7+
8+
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
9+
import aws.sdk.kotlin.runtime.testing.TestPlatformProvider
10+
import aws.sdk.kotlin.runtime.testing.runSuspendTest
11+
import kotlin.test.Test
12+
import kotlin.test.assertEquals
13+
14+
class AwsClientConfigLoaderTest {
15+
@Test
16+
fun testExplicit(): Unit = runSuspendTest {
17+
val provider = TestPlatformProvider()
18+
val actual = loadAwsClientConfig(provider) {
19+
region = "us-east-2"
20+
credentialsProvider = StaticCredentialsProvider {
21+
accessKeyId = "AKID"
22+
secretAccessKey = "secret"
23+
}
24+
}
25+
assertEquals("us-east-2", actual.region)
26+
assertEquals("AKID", actual.credentialsProvider.getCredentials().accessKeyId)
27+
}
28+
}

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/region/ResolveRegionTest.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

aws-runtime/aws-config/jvm/src/aws/sdk/kotlin/runtime/region/DefaultRegionProviderChainJVM.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package aws.sdk.kotlin.runtime.region
88
import aws.smithy.kotlin.runtime.io.Closeable
99
import aws.smithy.kotlin.runtime.util.PlatformProvider
1010

11-
public actual class DefaultRegionProviderChain public actual constructor(
11+
internal actual class DefaultRegionProviderChain actual constructor(
1212
platformProvider: PlatformProvider
1313
) : RegionProvider,
1414
Closeable,

aws-runtime/aws-core/common/src/aws/sdk/kotlin/runtime/client/AwsAdvancedClientOption.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.

aws-runtime/aws-types/common/src/aws/sdk/kotlin/runtime/auth/AuthConfig.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.runtime.client
7+
8+
import aws.sdk.kotlin.runtime.auth.credentials.CredentialsProvider
9+
10+
/**
11+
* Shared AWS service client configuration that all AWS service clients implement as part of their configuration state.
12+
*/
13+
public interface AwsClientConfig {
14+
/**
15+
* The AWS region to make requests to
16+
*/
17+
public val region: String
18+
19+
/**
20+
* The [CredentialsProvider] that will be called to resolve credentials before making AWS service calls
21+
*/
22+
public val credentialsProvider: CredentialsProvider
23+
24+
public companion object {}
25+
}

0 commit comments

Comments
 (0)