Skip to content

Commit c95d93b

Browse files
authored
Replace region with regionSupplier in all AWS tests (#126285) (#126323)
Rather than hard-coding a region name we should always auto-generate it randomly during test execution. This commit replaces the remaining fixed `String` arguments with a `Supplier<String>` argument to enable this.
1 parent dabea23 commit c95d93b

File tree

11 files changed

+53
-27
lines changed

11 files changed

+53
-27
lines changed

Diff for: modules/repository-s3/src/javaRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3BasicCredentialsRestIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.rules.RuleChain;
2121
import org.junit.rules.TestRule;
2222

23+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
2324
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2425

2526
@ThreadLeakFilters(filters = { TestContainersThreadFilter.class })
@@ -33,7 +34,7 @@ public class RepositoryS3BasicCredentialsRestIT extends AbstractRepositoryS3Rest
3334
private static final String SECRET_KEY = PREFIX + "secret-key";
3435
private static final String CLIENT = "basic_credentials_client";
3536

36-
private static final S3HttpFixture s3Fixture = new S3HttpFixture(true, BUCKET, BASE_PATH, fixedAccessKey(ACCESS_KEY, "*", "s3"));
37+
private static final S3HttpFixture s3Fixture = new S3HttpFixture(true, BUCKET, BASE_PATH, fixedAccessKey(ACCESS_KEY, ANY_REGION, "s3"));
3738

3839
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
3940
.module("repository-s3")

Diff for: modules/repository-s3/src/javaRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3RestReloadCredentialsIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828

29+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
2930
import static fixture.aws.AwsCredentialsUtils.mutableAccessKey;
3031
import static org.hamcrest.CoreMatchers.containsString;
3132
import static org.hamcrest.Matchers.allOf;
@@ -43,7 +44,7 @@ public class RepositoryS3RestReloadCredentialsIT extends ESRestTestCase {
4344
true,
4445
BUCKET,
4546
BASE_PATH,
46-
mutableAccessKey(() -> repositoryAccessKey, "*", "s3")
47+
mutableAccessKey(() -> repositoryAccessKey, ANY_REGION, "s3")
4748
);
4849

4950
private static final MutableSettingsProvider keystoreSettings = new MutableSettingsProvider();

Diff for: modules/repository-s3/src/javaRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3SessionCredentialsRestIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.rules.RuleChain;
2121
import org.junit.rules.TestRule;
2222

23+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
2324
import static fixture.aws.AwsCredentialsUtils.fixedAccessKeyAndToken;
2425

2526
@ThreadLeakFilters(filters = { TestContainersThreadFilter.class })
@@ -38,7 +39,7 @@ public class RepositoryS3SessionCredentialsRestIT extends AbstractRepositoryS3Re
3839
true,
3940
BUCKET,
4041
BASE_PATH,
41-
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, "*", "s3")
42+
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, ANY_REGION, "s3")
4243
);
4344

4445
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()

Diff for: modules/repository-s3/src/yamlRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3ClientYamlTestSuiteIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.rules.RuleChain;
2424
import org.junit.rules.TestRule;
2525

26+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
2627
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2728

2829
@ThreadLeakFilters(filters = { TestContainersThreadFilter.class })
@@ -36,7 +37,7 @@ public class RepositoryS3ClientYamlTestSuiteIT extends AbstractRepositoryS3Clien
3637
true,
3738
"bucket",
3839
"base_path_integration_tests",
39-
fixedAccessKey(ACCESS_KEY, "*", "s3")
40+
fixedAccessKey(ACCESS_KEY, ANY_REGION, "s3")
4041
);
4142

4243
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()

Diff for: plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2EnvironmentVariableCredentialsIT.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.discovery.ec2;
1111

12+
import fixture.aws.DynamicRegionSupplier;
1213
import fixture.aws.ec2.AwsEc2HttpFixture;
1314

1415
import org.elasticsearch.discovery.DiscoveryModule;
@@ -19,17 +20,18 @@
1920
import org.junit.rules.TestRule;
2021

2122
import java.util.List;
23+
import java.util.function.Supplier;
2224

2325
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2426

2527
public class DiscoveryEc2EnvironmentVariableCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {
2628

2729
private static final String PREFIX = getIdentifierPrefix("DiscoveryEc2EnvironmentVariableCredentialsIT");
28-
private static final String REGION = PREFIX + "-region";
2930
private static final String ACCESS_KEY = PREFIX + "-access-key";
3031

32+
private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
3133
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
32-
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
34+
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
3335
DiscoveryEc2EnvironmentVariableCredentialsIT::getAvailableTransportEndpoints
3436
);
3537

@@ -39,7 +41,7 @@ public class DiscoveryEc2EnvironmentVariableCredentialsIT extends DiscoveryEc2Cl
3941
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
4042
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
4143
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
42-
.environment("AWS_REGION", REGION)
44+
.environment("AWS_REGION", regionSupplier)
4345
.environment("AWS_ACCESS_KEY_ID", ACCESS_KEY)
4446
.environment("AWS_SECRET_ACCESS_KEY", ESTestCase::randomSecretKey)
4547
.build();

Diff for: plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2KeystoreCredentialsIT.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.discovery.ec2;
1111

12+
import fixture.aws.DynamicRegionSupplier;
1213
import fixture.aws.ec2.AwsEc2HttpFixture;
1314

1415
import org.elasticsearch.discovery.DiscoveryModule;
@@ -19,17 +20,18 @@
1920
import org.junit.rules.TestRule;
2021

2122
import java.util.List;
23+
import java.util.function.Supplier;
2224

2325
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2426

2527
public class DiscoveryEc2KeystoreCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {
2628

2729
private static final String PREFIX = getIdentifierPrefix("DiscoveryEc2KeystoreCredentialsIT");
28-
private static final String REGION = PREFIX + "-region";
2930
private static final String ACCESS_KEY = PREFIX + "-access-key";
3031

32+
private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
3133
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
32-
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
34+
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
3335
DiscoveryEc2KeystoreCredentialsIT::getAvailableTransportEndpoints
3436
);
3537

@@ -39,7 +41,7 @@ public class DiscoveryEc2KeystoreCredentialsIT extends DiscoveryEc2ClusterFormat
3941
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
4042
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
4143
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
42-
.environment("AWS_REGION", REGION)
44+
.environment("AWS_REGION", regionSupplier)
4345
.keystore("discovery.ec2.access_key", ACCESS_KEY)
4446
.keystore("discovery.ec2.secret_key", ESTestCase::randomSecretKey)
4547
.build();

Diff for: plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2KeystoreSessionCredentialsIT.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.discovery.ec2;
1111

12+
import fixture.aws.DynamicRegionSupplier;
1213
import fixture.aws.ec2.AwsEc2HttpFixture;
1314

1415
import org.elasticsearch.discovery.DiscoveryModule;
@@ -19,18 +20,19 @@
1920
import org.junit.rules.TestRule;
2021

2122
import java.util.List;
23+
import java.util.function.Supplier;
2224

2325
import static fixture.aws.AwsCredentialsUtils.fixedAccessKeyAndToken;
2426

2527
public class DiscoveryEc2KeystoreSessionCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {
2628

2729
private static final String PREFIX = getIdentifierPrefix("DiscoveryEc2KeystoreSessionCredentialsIT");
28-
private static final String REGION = PREFIX + "-region";
2930
private static final String ACCESS_KEY = PREFIX + "-access-key";
3031
private static final String SESSION_TOKEN = PREFIX + "-session-token";
3132

33+
private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
3234
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
33-
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, REGION, "ec2"),
35+
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, regionSupplier, "ec2"),
3436
DiscoveryEc2KeystoreSessionCredentialsIT::getAvailableTransportEndpoints
3537
);
3638

@@ -40,7 +42,7 @@ public class DiscoveryEc2KeystoreSessionCredentialsIT extends DiscoveryEc2Cluste
4042
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
4143
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
4244
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
43-
.environment("AWS_REGION", REGION)
45+
.environment("AWS_REGION", regionSupplier)
4446
.keystore("discovery.ec2.access_key", ACCESS_KEY)
4547
.keystore("discovery.ec2.secret_key", ESTestCase::randomSecretKey)
4648
.keystore("discovery.ec2.session_token", SESSION_TOKEN)

Diff for: plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2SystemPropertyCredentialsIT.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.discovery.ec2;
1111

12+
import fixture.aws.DynamicRegionSupplier;
1213
import fixture.aws.ec2.AwsEc2HttpFixture;
1314

1415
import org.elasticsearch.discovery.DiscoveryModule;
@@ -19,17 +20,18 @@
1920
import org.junit.rules.TestRule;
2021

2122
import java.util.List;
23+
import java.util.function.Supplier;
2224

2325
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2426

2527
public class DiscoveryEc2SystemPropertyCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {
2628

2729
private static final String PREFIX = getIdentifierPrefix("DiscoveryEc2SystemPropertyCredentialsIT");
28-
private static final String REGION = PREFIX + "-region";
2930
private static final String ACCESS_KEY = PREFIX + "-access-key";
3031

32+
private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
3133
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
32-
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
34+
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
3335
DiscoveryEc2SystemPropertyCredentialsIT::getAvailableTransportEndpoints
3436
);
3537

@@ -39,7 +41,7 @@ public class DiscoveryEc2SystemPropertyCredentialsIT extends DiscoveryEc2Cluster
3941
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
4042
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
4143
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
42-
.environment("AWS_REGION", REGION)
44+
.environment("AWS_REGION", regionSupplier)
4345
.systemProperty("aws.accessKeyId", ACCESS_KEY)
4446
.systemProperty("aws.secretAccessKey", ESTestCase::randomSecretKey)
4547
.build();

Diff for: test/fixtures/aws-fixture-utils/src/main/java/fixture/aws/AwsCredentialsUtils.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,35 @@
2323
public enum AwsCredentialsUtils {
2424
;
2525

26+
/**
27+
* Region supplier which matches any region.
28+
*/
29+
public static final Supplier<String> ANY_REGION = () -> "*";
30+
2631
/**
2732
* @return an authorization predicate that ensures the authorization header matches the given access key, region and service name.
2833
* @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
29-
* @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter
34+
* @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region
35+
* parameter
3036
*/
31-
public static BiPredicate<String, String> fixedAccessKey(String accessKey, String region, String serviceName) {
32-
return mutableAccessKey(() -> accessKey, region, serviceName);
37+
public static BiPredicate<String, String> fixedAccessKey(String accessKey, Supplier<String> regionSupplier, String serviceName) {
38+
return mutableAccessKey(() -> accessKey, regionSupplier, serviceName);
3339
}
3440

3541
/**
3642
* @return an authorization predicate that ensures the authorization header matches the access key supplied by the given supplier,
3743
* and also matches the given region and service name.
3844
* @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
39-
* @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter
45+
* @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region
46+
* parameter
4047
*/
41-
public static BiPredicate<String, String> mutableAccessKey(Supplier<String> accessKeySupplier, String region, String serviceName) {
48+
public static BiPredicate<String, String> mutableAccessKey(
49+
Supplier<String> accessKeySupplier,
50+
Supplier<String> regionSupplier,
51+
String serviceName
52+
) {
4253
return (authorizationHeader, sessionTokenHeader) -> authorizationHeader != null
43-
&& isValidAwsV4SignedAuthorizationHeader(accessKeySupplier.get(), region, serviceName, authorizationHeader);
54+
&& isValidAwsV4SignedAuthorizationHeader(accessKeySupplier.get(), regionSupplier.get(), serviceName, authorizationHeader);
4455
}
4556

4657
/**
@@ -72,16 +83,17 @@ public static boolean isValidAwsV4SignedAuthorizationHeader(
7283
/**
7384
* @return an authorization predicate that ensures the access key, session token, region and service name all match the given values.
7485
* @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
75-
* @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter
86+
* @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region
87+
* parameter
7688
*/
7789
public static BiPredicate<String, String> fixedAccessKeyAndToken(
7890
String accessKey,
7991
String sessionToken,
80-
String region,
92+
Supplier<String> regionSupplier,
8193
String serviceName
8294
) {
8395
Objects.requireNonNull(sessionToken);
84-
final var accessKeyPredicate = fixedAccessKey(accessKey, region, serviceName);
96+
final var accessKeyPredicate = fixedAccessKey(accessKey, regionSupplier, serviceName);
8597
return (authorizationHeader, sessionTokenHeader) -> accessKeyPredicate.test(authorizationHeader, sessionTokenHeader)
8698
&& sessionToken.equals(sessionTokenHeader);
8799
}

Diff for: test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixture.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Objects;
2020
import java.util.function.BiPredicate;
2121

22+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
2223
import static fixture.aws.AwsCredentialsUtils.checkAuthorization;
2324
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
2425
import static fixture.aws.AwsFixtureUtils.getLocalFixtureAddress;
@@ -33,7 +34,7 @@ public class S3HttpFixture extends ExternalResource {
3334
private final BiPredicate<String, String> authorizationPredicate;
3435

3536
public S3HttpFixture(boolean enabled) {
36-
this(enabled, "bucket", "base_path_integration_tests", fixedAccessKey("s3_test_access_key", "*", "s3"));
37+
this(enabled, "bucket", "base_path_integration_tests", fixedAccessKey("s3_test_access_key", ANY_REGION, "s3"));
3738
}
3839

3940
public S3HttpFixture(boolean enabled, String bucket, String basePath, BiPredicate<String, String> authorizationPredicate) {

Diff for: x-pack/plugin/searchable-snapshots/qa/s3/src/javaRestTest/java/org/elasticsearch/xpack/searchablesnapshots/s3/S3SearchableSnapshotsCredentialsReloadIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.IOException;
3737
import java.util.function.UnaryOperator;
3838

39+
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
3940
import static fixture.aws.AwsCredentialsUtils.mutableAccessKey;
4041
import static org.hamcrest.CoreMatchers.containsString;
4142
import static org.hamcrest.Matchers.allOf;
@@ -51,7 +52,7 @@ public class S3SearchableSnapshotsCredentialsReloadIT extends ESRestTestCase {
5152
true,
5253
BUCKET,
5354
BASE_PATH,
54-
mutableAccessKey(() -> repositoryAccessKey, "*", "s3")
55+
mutableAccessKey(() -> repositoryAccessKey, ANY_REGION, "s3")
5556
);
5657

5758
private static final MutableSettingsProvider keystoreSettings = new MutableSettingsProvider();

0 commit comments

Comments
 (0)