Skip to content

[8.x] Replace region with regionSupplier in all AWS tests (#126285) #126323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -20,6 +20,7 @@
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;

import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;

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

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

public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
.module("repository-s3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;

import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.mutableAccessKey;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.allOf;
Expand All @@ -43,7 +44,7 @@ public class RepositoryS3RestReloadCredentialsIT extends ESRestTestCase {
true,
BUCKET,
BASE_PATH,
mutableAccessKey(() -> repositoryAccessKey, "*", "s3")
mutableAccessKey(() -> repositoryAccessKey, ANY_REGION, "s3")
);

private static final MutableSettingsProvider keystoreSettings = new MutableSettingsProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;

import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.fixedAccessKeyAndToken;

@ThreadLeakFilters(filters = { TestContainersThreadFilter.class })
Expand All @@ -38,7 +39,7 @@ public class RepositoryS3SessionCredentialsRestIT extends AbstractRepositoryS3Re
true,
BUCKET,
BASE_PATH,
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, "*", "s3")
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, ANY_REGION, "s3")
);

public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;

import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;

@ThreadLeakFilters(filters = { TestContainersThreadFilter.class })
Expand All @@ -36,7 +37,7 @@ public class RepositoryS3ClientYamlTestSuiteIT extends AbstractRepositoryS3Clien
true,
"bucket",
"base_path_integration_tests",
fixedAccessKey(ACCESS_KEY, "*", "s3")
fixedAccessKey(ACCESS_KEY, ANY_REGION, "s3")
);

public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.discovery.ec2;

import fixture.aws.DynamicRegionSupplier;
import fixture.aws.ec2.AwsEc2HttpFixture;

import org.elasticsearch.discovery.DiscoveryModule;
Expand All @@ -19,17 +20,18 @@
import org.junit.rules.TestRule;

import java.util.List;
import java.util.function.Supplier;

import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;

public class DiscoveryEc2EnvironmentVariableCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {

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

private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
DiscoveryEc2EnvironmentVariableCredentialsIT::getAvailableTransportEndpoints
);

Expand All @@ -39,7 +41,7 @@ public class DiscoveryEc2EnvironmentVariableCredentialsIT extends DiscoveryEc2Cl
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
.environment("AWS_REGION", REGION)
.environment("AWS_REGION", regionSupplier)
.environment("AWS_ACCESS_KEY_ID", ACCESS_KEY)
.environment("AWS_SECRET_ACCESS_KEY", ESTestCase::randomSecretKey)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.discovery.ec2;

import fixture.aws.DynamicRegionSupplier;
import fixture.aws.ec2.AwsEc2HttpFixture;

import org.elasticsearch.discovery.DiscoveryModule;
Expand All @@ -19,17 +20,18 @@
import org.junit.rules.TestRule;

import java.util.List;
import java.util.function.Supplier;

import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;

public class DiscoveryEc2KeystoreCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {

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

private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
DiscoveryEc2KeystoreCredentialsIT::getAvailableTransportEndpoints
);

Expand All @@ -39,7 +41,7 @@ public class DiscoveryEc2KeystoreCredentialsIT extends DiscoveryEc2ClusterFormat
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
.environment("AWS_REGION", REGION)
.environment("AWS_REGION", regionSupplier)
.keystore("discovery.ec2.access_key", ACCESS_KEY)
.keystore("discovery.ec2.secret_key", ESTestCase::randomSecretKey)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.discovery.ec2;

import fixture.aws.DynamicRegionSupplier;
import fixture.aws.ec2.AwsEc2HttpFixture;

import org.elasticsearch.discovery.DiscoveryModule;
Expand All @@ -19,18 +20,19 @@
import org.junit.rules.TestRule;

import java.util.List;
import java.util.function.Supplier;

import static fixture.aws.AwsCredentialsUtils.fixedAccessKeyAndToken;

public class DiscoveryEc2KeystoreSessionCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {

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

private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, REGION, "ec2"),
fixedAccessKeyAndToken(ACCESS_KEY, SESSION_TOKEN, regionSupplier, "ec2"),
DiscoveryEc2KeystoreSessionCredentialsIT::getAvailableTransportEndpoints
);

Expand All @@ -40,7 +42,7 @@ public class DiscoveryEc2KeystoreSessionCredentialsIT extends DiscoveryEc2Cluste
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
.environment("AWS_REGION", REGION)
.environment("AWS_REGION", regionSupplier)
.keystore("discovery.ec2.access_key", ACCESS_KEY)
.keystore("discovery.ec2.secret_key", ESTestCase::randomSecretKey)
.keystore("discovery.ec2.session_token", SESSION_TOKEN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.discovery.ec2;

import fixture.aws.DynamicRegionSupplier;
import fixture.aws.ec2.AwsEc2HttpFixture;

import org.elasticsearch.discovery.DiscoveryModule;
Expand All @@ -19,17 +20,18 @@
import org.junit.rules.TestRule;

import java.util.List;
import java.util.function.Supplier;

import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;

public class DiscoveryEc2SystemPropertyCredentialsIT extends DiscoveryEc2ClusterFormationTestCase {

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

private static final Supplier<String> regionSupplier = new DynamicRegionSupplier();
private static final AwsEc2HttpFixture ec2ApiFixture = new AwsEc2HttpFixture(
fixedAccessKey(ACCESS_KEY, REGION, "ec2"),
fixedAccessKey(ACCESS_KEY, regionSupplier, "ec2"),
DiscoveryEc2SystemPropertyCredentialsIT::getAvailableTransportEndpoints
);

Expand All @@ -39,7 +41,7 @@ public class DiscoveryEc2SystemPropertyCredentialsIT extends DiscoveryEc2Cluster
.setting(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), Ec2DiscoveryPlugin.EC2_SEED_HOSTS_PROVIDER_NAME)
.setting("logger." + AwsEc2SeedHostsProvider.class.getCanonicalName(), "DEBUG")
.setting(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), ec2ApiFixture::getAddress)
.environment("AWS_REGION", REGION)
.environment("AWS_REGION", regionSupplier)
.systemProperty("aws.accessKeyId", ACCESS_KEY)
.systemProperty("aws.secretAccessKey", ESTestCase::randomSecretKey)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,35 @@
public enum AwsCredentialsUtils {
;

/**
* Region supplier which matches any region.
*/
public static final Supplier<String> ANY_REGION = () -> "*";

/**
* @return an authorization predicate that ensures the authorization header matches the given access key, region and service name.
* @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
* @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter
* @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region
* parameter
*/
public static BiPredicate<String, String> fixedAccessKey(String accessKey, String region, String serviceName) {
return mutableAccessKey(() -> accessKey, region, serviceName);
public static BiPredicate<String, String> fixedAccessKey(String accessKey, Supplier<String> regionSupplier, String serviceName) {
return mutableAccessKey(() -> accessKey, regionSupplier, serviceName);
}

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

/**
Expand Down Expand Up @@ -72,16 +83,17 @@ public static boolean isValidAwsV4SignedAuthorizationHeader(
/**
* @return an authorization predicate that ensures the access key, session token, region and service name all match the given values.
* @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html">AWS v4 Signatures</a>
* @param region the name of the AWS region used to sign the request, or {@code *} to skip validation of the region parameter
* @param regionSupplier supplies the name of the AWS region used to sign the request, or {@code *} to skip validation of the region
* parameter
*/
public static BiPredicate<String, String> fixedAccessKeyAndToken(
String accessKey,
String sessionToken,
String region,
Supplier<String> regionSupplier,
String serviceName
) {
Objects.requireNonNull(sessionToken);
final var accessKeyPredicate = fixedAccessKey(accessKey, region, serviceName);
final var accessKeyPredicate = fixedAccessKey(accessKey, regionSupplier, serviceName);
return (authorizationHeader, sessionTokenHeader) -> accessKeyPredicate.test(authorizationHeader, sessionTokenHeader)
&& sessionToken.equals(sessionTokenHeader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;
import java.util.function.BiPredicate;

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

public S3HttpFixture(boolean enabled) {
this(enabled, "bucket", "base_path_integration_tests", fixedAccessKey("s3_test_access_key", "*", "s3"));
this(enabled, "bucket", "base_path_integration_tests", fixedAccessKey("s3_test_access_key", ANY_REGION, "s3"));
}

public S3HttpFixture(boolean enabled, String bucket, String basePath, BiPredicate<String, String> authorizationPredicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.IOException;
import java.util.function.UnaryOperator;

import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.mutableAccessKey;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.allOf;
Expand All @@ -51,7 +52,7 @@ public class S3SearchableSnapshotsCredentialsReloadIT extends ESRestTestCase {
true,
BUCKET,
BASE_PATH,
mutableAccessKey(() -> repositoryAccessKey, "*", "s3")
mutableAccessKey(() -> repositoryAccessKey, ANY_REGION, "s3")
);

private static final MutableSettingsProvider keystoreSettings = new MutableSettingsProvider();
Expand Down