Skip to content

Commit f1a3b63

Browse files
committed
Improve randomIdentifier usage in AWS tests (elastic#125775)
Adds prefixes to various randomly-generated values to make it easier to pin down where they're coming from in debugging sessions. Also forces the STS expiry time to be rendered in UTC.
1 parent 9a0ddc0 commit f1a3b63

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2InstanceProfileIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
package org.elasticsearch.discovery.ec2;
1111

1212
import fixture.aws.DynamicAwsCredentials;
13+
import fixture.aws.DynamicRegionSupplier;
1314
import fixture.aws.ec2.AwsEc2HttpFixture;
1415
import fixture.aws.imds.Ec2ImdsHttpFixture;
1516
import fixture.aws.imds.Ec2ImdsServiceBuilder;
1617
import fixture.aws.imds.Ec2ImdsVersion;
1718

18-
import org.elasticsearch.common.util.LazyInitializable;
1919
import org.elasticsearch.discovery.DiscoveryModule;
20-
import org.elasticsearch.test.ESTestCase;
2120
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2221
import org.junit.ClassRule;
2322
import org.junit.rules.RuleChain;
@@ -29,7 +28,7 @@
2928
public class DiscoveryEc2InstanceProfileIT extends DiscoveryEc2ClusterFormationTestCase {
3029

3130
// Lazy-initialized so we can generate it randomly, which is not possible in static context.
32-
private static final Supplier<String> regionSupplier = new LazyInitializable<>(ESTestCase::randomIdentifier)::getOrCompute;
31+
private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
3332

3433
private static final DynamicAwsCredentials dynamicCredentials = new DynamicAwsCredentials(regionSupplier, "ec2");
3534

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package fixture.aws;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
14+
import java.util.Objects;
15+
import java.util.concurrent.atomic.AtomicReference;
16+
import java.util.function.Supplier;
17+
18+
/**
19+
* Lazy supplier for a region name. We cannot use randomness like {@link ESTestCase#randomIdentifier()} when creating the test fixtures in
20+
* the first place because this happens in static context, so instead we create one of these and defer the creation of the region name
21+
* itself until the test actually starts running.
22+
*/
23+
public class DynamicRegionSupplier implements Supplier<String> {
24+
private final AtomicReference<String> generatedRegion = new AtomicReference<>();
25+
26+
@Override
27+
public String get() {
28+
return Objects.requireNonNullElseGet(generatedRegion.get(), this::generateAndGet);
29+
}
30+
31+
private String generateAndGet() {
32+
final var newRegion = "DynamicRegionSupplier-" + ESTestCase.randomIdentifier();
33+
return Objects.requireNonNullElse(generatedRegion.compareAndExchange(null, newRegion), newRegion);
34+
}
35+
}

test/fixtures/aws-sts-fixture/src/main/java/fixture/aws/sts/AwsStsHttpHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.net.URLDecoder;
2020
import java.nio.charset.StandardCharsets;
21+
import java.time.Clock;
2122
import java.time.ZonedDateTime;
2223
import java.time.format.DateTimeFormatter;
2324
import java.util.Arrays;
@@ -73,7 +74,7 @@ public void handle(final HttpExchange exchange) throws IOException {
7374
exchange.close();
7475
return;
7576
}
76-
final var accessKey = randomIdentifier();
77+
final var accessKey = "test_key_STS_" + randomIdentifier();
7778
final var sessionToken = randomIdentifier();
7879
newCredentialsConsumer.accept(accessKey, sessionToken);
7980
final byte[] response = String.format(
@@ -104,7 +105,7 @@ public void handle(final HttpExchange exchange) throws IOException {
104105
ROLE_NAME,
105106
sessionToken,
106107
randomSecretKey(),
107-
ZonedDateTime.now().plusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")),
108+
ZonedDateTime.now(Clock.systemUTC()).plusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")),
108109
accessKey
109110
).getBytes(StandardCharsets.UTF_8);
110111
exchange.getResponseHeaders().add("Content-Type", "text/xml; charset=UTF-8");

test/fixtures/ec2-imds-fixture/src/main/java/fixture/aws/imds/Ec2ImdsHttpHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void handle(final HttpExchange exchange) throws IOException {
121121

122122
if ("GET".equals(requestMethod)) {
123123
if (path.equals(IMDS_SECURITY_CREDENTIALS_PATH) && dynamicProfileNames) {
124-
final var profileName = randomIdentifier();
124+
final var profileName = "imds_profile_" + randomIdentifier();
125125
validCredentialsEndpoints.add(IMDS_SECURITY_CREDENTIALS_PATH + profileName);
126126
sendStringResponse(exchange, profileName);
127127
return;
@@ -133,7 +133,7 @@ public void handle(final HttpExchange exchange) throws IOException {
133133
sendStringResponse(exchange, Strings.toString(instanceIdentityDocument));
134134
return;
135135
} else if (validCredentialsEndpoints.contains(path)) {
136-
final String accessKey = randomIdentifier();
136+
final String accessKey = "test_key_imds_" + randomIdentifier();
137137
final String sessionToken = randomIdentifier();
138138
newCredentialsConsumer.accept(accessKey, sessionToken);
139139
final byte[] response = Strings.format(

0 commit comments

Comments
 (0)