Skip to content

Commit b991e3c

Browse files
authored
Merge pull request kroxylicious#1671 from k-wall/aws-use-metadata-latest
Comply with the recommendation that AWS metadata endpoint is addressed using /latest/
2 parents 36104b7 + 6c24fe0 commit b991e3c

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

kroxylicious-kms-provider-aws-kms/src/main/java/io/kroxylicious/kms/provider/aws/kms/credentials/Ec2MetadataCredentialsProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ public class Ec2MetadataCredentialsProvider implements CredentialsProvider {
8484
private static final String TOKEN_RETRIEVAL_ENDPOINT = "/latest/api/token";
8585

8686
/**
87-
* EC2 Meta-data security credentials endpoint.
87+
* EC2 Meta-data security credentials endpoint. AWS recommend that latest is used. That's the approach taken by their
88+
* own SDK.
8889
*/
89-
private static final String META_DATA_IAM_SECURITY_CREDENTIALS_ENDPOINT = "/2024-04-11/meta-data/iam/security-credentials/";
90+
private static final String META_DATA_IAM_SECURITY_CREDENTIALS_ENDPOINT = "/latest/meta-data/iam/security-credentials/";
9091

9192
private final Clock systemClock;
9293
private final AtomicReference<CompletableFuture<SecurityCredentials>> current = new AtomicReference<>();

kroxylicious-kms-provider-aws-kms/src/test/java/io/kroxylicious/kms/provider/aws/kms/credentials/Ec2MetadataCredentialsProviderTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Ec2MetadataCredentialsProviderTest {
4747
private static final String MY_TOKEN = "mytoken";
4848
private static final String IAM_ROLE = "myrole";
4949
private static final String TOKEN_RETRIEVAL_ENDPOINT = "/latest/api/token";
50-
private static final String META_DATA_IAM_SECURITY_CREDENTIALS_ENDPOINT = "/2024-04-11/meta-data/iam/security-credentials/";
50+
private static final String META_DATA_IAM_SECURITY_CREDENTIALS_ENDPOINT = "/latest/meta-data/iam/security-credentials/";
5151

5252
// From https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-security-credentials.html
5353
private static final String KNOWN_GOOD_SECURITY_CREDENTIAL_RESPONSE = """
@@ -60,6 +60,19 @@ class Ec2MetadataCredentialsProviderTest {
6060
"Token" : "token",
6161
"Expiration" : "2017-05-17T15:09:54Z"
6262
}""";
63+
64+
private static final String SECURITY_CREDENTIAL_RESPONSE_WITH_ADDITIONAL_PROPERTIES = """
65+
{
66+
"Code" : "Success",
67+
"LastUpdated" : "2012-04-26T16:39:16Z",
68+
"Type" : "AWS-HMAC",
69+
"AccessKeyId" : "ASIAIOSFODNN7EXAMPLE",
70+
"SecretAccessKey" : "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
71+
"Token" : "token",
72+
"Expiration" : "2017-05-17T15:09:54Z",
73+
"Foo" : "Bar"
74+
}""";
75+
6376
private static WireMockServer metadataServer;
6477
private Ec2MetadataCredentialsProviderConfig config;
6578

@@ -114,6 +127,27 @@ void credentialFromKnownGood() {
114127
}
115128
}
116129

130+
/**
131+
* In line with AWS advice, the code uses the /latest/ endpoint. The reason for this test is to ensure
132+
* that if AWS adds a new field to the response, that we'll continue to unmarshall the object without failure.
133+
*/
134+
@Test
135+
void allowsCredentialsResponseWithAdditionalProperties() {
136+
metadataServer.stubFor(
137+
get(urlEqualTo(META_DATA_IAM_SECURITY_CREDENTIALS_ENDPOINT + IAM_ROLE))
138+
.willReturn(WireMock.aResponse()
139+
.withBody(SECURITY_CREDENTIAL_RESPONSE_WITH_ADDITIONAL_PROPERTIES)));
140+
141+
try (var provider = new Ec2MetadataCredentialsProvider(config)) {
142+
var credentialsStage = provider.getCredentials();
143+
assertThat(credentialsStage)
144+
.succeedsWithin(Duration.ofSeconds(5))
145+
.returns("ASIAIOSFODNN7EXAMPLE", SecurityCredentials::accessKeyId)
146+
.returns("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", SecurityCredentials::secretAccessKey)
147+
.returns(Instant.parse("2017-05-17T15:09:54Z"), SecurityCredentials::expiration);
148+
}
149+
}
150+
117151
@Test
118152
void subsequentCallReturnsCachedCredential() {
119153
var now = Instant.now();

0 commit comments

Comments
 (0)