Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
package org.elasticsearch.discovery.ec2;

import fixture.aws.DynamicAwsCredentials;
import fixture.aws.DynamicRegionSupplier;
import fixture.aws.ec2.AwsEc2HttpFixture;
import fixture.aws.imds.Ec2ImdsHttpFixture;
import fixture.aws.imds.Ec2ImdsServiceBuilder;
import fixture.aws.imds.Ec2ImdsVersion;

import org.elasticsearch.common.util.LazyInitializable;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
Expand All @@ -29,7 +28,7 @@
public class DiscoveryEc2InstanceProfileIT extends DiscoveryEc2ClusterFormationTestCase {

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package fixture.aws;

import org.elasticsearch.test.ESTestCase;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

/**
* Lazy supplier for a region name. We cannot use randomness like {@link ESTestCase#randomIdentifier()} when creating the test fixtures in
* the first place because this happens in static context, so instead we create one of these and defer the creation of the region name
* itself until the test actually starts running.
*/
public class DynamicRegionSupplier implements Supplier<String> {
private final AtomicReference<String> generatedRegion = new AtomicReference<>();

@Override
public String get() {
return Objects.requireNonNullElseGet(generatedRegion.get(), this::generateAndGet);
}

private String generateAndGet() {
final var newRegion = "DynamicRegionSupplier-" + ESTestCase.randomIdentifier();
return Objects.requireNonNullElse(generatedRegion.compareAndExchange(null, newRegion), newRegion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void handle(final HttpExchange exchange) throws IOException {
exchange.close();
return;
}
final var accessKey = randomIdentifier();
final var accessKey = "test_key_STS_" + randomIdentifier();
final var sessionToken = randomIdentifier();
newCredentialsConsumer.accept(accessKey, sessionToken);
final byte[] response = String.format(
Expand Down Expand Up @@ -104,7 +105,7 @@ public void handle(final HttpExchange exchange) throws IOException {
ROLE_NAME,
sessionToken,
randomSecretKey(),
ZonedDateTime.now().plusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")),
ZonedDateTime.now(Clock.systemUTC()).plusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

accessKey
).getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "text/xml; charset=UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void handle(final HttpExchange exchange) throws IOException {

if ("GET".equals(requestMethod)) {
if (path.equals(IMDS_SECURITY_CREDENTIALS_PATH) && dynamicProfileNames) {
final var profileName = randomIdentifier();
final var profileName = "imds_profile_" + randomIdentifier();
validCredentialsEndpoints.add(IMDS_SECURITY_CREDENTIALS_PATH + profileName);
sendStringResponse(exchange, profileName);
return;
Expand All @@ -133,7 +133,7 @@ public void handle(final HttpExchange exchange) throws IOException {
sendStringResponse(exchange, Strings.toString(instanceIdentityDocument));
return;
} else if (validCredentialsEndpoints.contains(path)) {
final String accessKey = randomIdentifier();
final String accessKey = "test_key_imds_" + randomIdentifier();
final String sessionToken = randomIdentifier();
newCredentialsConsumer.accept(accessKey, sessionToken);
final byte[] response = Strings.format(
Expand Down