Skip to content

Commit 15c8515

Browse files
authored
fix: add trailing slash to endpoint and disable proxying of requests to IMDS (#1320)
1 parent 953e69e commit 15c8515

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "16007c74-7831-42d2-8a3b-01e4868da600",
3+
"type": "bugfix",
4+
"description": "Disable proxying of requests made to EC2 IMDS",
5+
"issues": [
6+
"https://github.com/awslabs/aws-sdk-kotlin/issues/1315"
7+
]
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "8248143a-7a11-46c0-a44d-7bbeabe34c82",
3+
"type": "misc",
4+
"description": "Add trailing slash to base IMDS endpoint",
5+
"issues": [
6+
"https://github.com/awslabs/aws-sdk-kotlin/issues/1303"
7+
]
8+
}

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/ImdsCredentialsProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import kotlinx.coroutines.sync.withLock
3030
import kotlin.coroutines.coroutineContext
3131
import kotlin.time.Duration.Companion.seconds
3232

33-
private const val CREDENTIALS_BASE_PATH: String = "/latest/meta-data/iam/security-credentials"
33+
private const val CREDENTIALS_BASE_PATH: String = "/latest/meta-data/iam/security-credentials/"
3434
private const val CODE_ASSUME_ROLE_UNAUTHORIZED_ACCESS: String = "AssumeRoleUnauthorizedAccess"
3535
private const val PROVIDER_NAME = "IMDSv2"
3636

@@ -43,7 +43,7 @@ private const val PROVIDER_NAME = "IMDSv2"
4343
* information.
4444
*
4545
* @param profileOverride override the instance profile name. When retrieving credentials, a call must first be made to
46-
* `<IMDS_BASE_URL>/latest/meta-data/iam/security-credentials`. This returns the instance profile used. If
46+
* `<IMDS_BASE_URL>/latest/meta-data/iam/security-credentials/`. This returns the instance profile used. If
4747
* [profileOverride] is set, the initial call to retrieve the profile is skipped and the provided value is used instead.
4848
* @param client the IMDS client to use to resolve credentials information with. This provider takes ownership over
4949
* the lifetime of the given [ImdsClient] and will close it when the provider is closed.

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/imds/ImdsClient.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import aws.smithy.kotlin.runtime.http.*
1616
import aws.smithy.kotlin.runtime.http.HttpCall
1717
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
1818
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
19+
import aws.smithy.kotlin.runtime.http.engine.ProxySelector
1920
import aws.smithy.kotlin.runtime.http.operation.*
2021
import aws.smithy.kotlin.runtime.io.Closeable
2122
import aws.smithy.kotlin.runtime.io.closeIfCloseable
@@ -72,6 +73,9 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
7273
engine = builder.engine ?: DefaultHttpEngine {
7374
connectTimeout = 1.seconds
7475
socketReadTimeout = 1.seconds
76+
77+
// don't proxy IMDS requests. https://github.com/awslabs/aws-sdk-kotlin/issues/1315
78+
proxySelector = ProxySelector.NoProxy
7579
}
7680

7781
httpClient = SdkHttpClient(engine)
@@ -114,7 +118,7 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
114118
val payload = response.body.readAll() ?: throw EC2MetadataError(response.status.value, "no metadata payload")
115119
return payload.decodeToString()
116120
} else {
117-
throw EC2MetadataError(response.status.value, "error retrieving instance metadata")
121+
throw EC2MetadataError(response.status.value, "error retrieving instance metadata: ${response.status.description}")
118122
}
119123
}
120124
}

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/auth/credentials/ImdsCredentialsProviderTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ImdsCredentialsProviderTest {
5959
tokenResponse(DEFAULT_TOKEN_TTL_SECONDS, "TOKEN_A"),
6060
)
6161
expect(
62-
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials", "TOKEN_A"),
62+
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials/", "TOKEN_A"),
6363
imdsResponse("imds-test-role"),
6464
)
6565
expect(
@@ -84,7 +84,7 @@ class ImdsCredentialsProviderTest {
8484

8585
// verify that profile is re-retrieved after credentials expiration
8686
expect(
87-
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials", "TOKEN_A"),
87+
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials/", "TOKEN_A"),
8888
imdsResponse("imds-test-role-2"),
8989
)
9090
expect(
@@ -235,7 +235,7 @@ class ImdsCredentialsProviderTest {
235235
tokenResponse(DEFAULT_TOKEN_TTL_SECONDS, "TOKEN_A"),
236236
)
237237
expect(
238-
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials", "TOKEN_A"),
238+
imdsRequest("http://169.254.169.254/latest/meta-data/iam/security-credentials/", "TOKEN_A"),
239239
HttpResponse(
240240
HttpStatusCode.NotFound,
241241
Headers.Empty,

0 commit comments

Comments
 (0)