diff --git a/docs/changelog/102230.yaml b/docs/changelog/102230.yaml new file mode 100644 index 0000000000000..20e8d8d1f10a6 --- /dev/null +++ b/docs/changelog/102230.yaml @@ -0,0 +1,6 @@ +pr: 102230 +summary: Set region for the STS client via privileged calls in AWS SDK +area: Snapshot/Restore +type: bug +issues: + - 102173 diff --git a/modules/repository-s3/build.gradle b/modules/repository-s3/build.gradle index 87dda19368d5a..3fb236e1d867f 100644 --- a/modules/repository-s3/build.gradle +++ b/modules/repository-s3/build.gradle @@ -322,6 +322,41 @@ if (useFixture) { } } +// Sanity test for STS Regional Endpoints +if (useFixture) { + tasks.register("yamlRestTestRegionalSTS", RestIntegTestTask.class) { + description = "Runs tests with the Regional STS Endpoint" + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); + SourceSet yamlRestTestSourceSet = sourceSets.getByName(LegacyYamlRestTestPlugin.SOURCE_SET_NAME) + setTestClassesDirs(yamlRestTestSourceSet.getOutput().getClassesDirs()) + setClasspath(yamlRestTestSourceSet.getRuntimeClasspath()) + // Run just the basic sanity test to make sure ES starts up and loads the S3 repository with + // a regional endpoint without an error. It would be great to make actual requests against + // a test fixture, but setting the region means using a production endpoint + systemProperty 'tests.rest.blacklist', [ + 'repository_s3/20_repository_permanent_credentials/*', + 'repository_s3/30_repository_temporary_credentials/*', + 'repository_s3/40_repository_ec2_credentials/*', + 'repository_s3/50_repository_ecs_credentials/*', + 'repository_s3/60_repository_sts_credentials/*' + ].join(",") + } + tasks.named("check").configure { dependsOn("yamlRestTestRegionalSTS") } + + testClusters.matching { it.name == "yamlRestTestRegionalSTS" }.configureEach { + module tasks.named("explodedBundlePlugin") + + File awsWebIdentityTokenExternalLocation = file('src/test/resources/aws-web-identity-token-file') + extraConfigFile 'repository-s3/aws-web-identity-token-file', awsWebIdentityTokenExternalLocation + environment 'AWS_WEB_IDENTITY_TOKEN_FILE', "$awsWebIdentityTokenExternalLocation" + environment 'AWS_ROLE_ARN', 'arn:aws:iam::123456789012:role/FederatedWebIdentityRole' + environment 'AWS_ROLE_SESSION_NAME', 'sts-fixture-test' + // Force the repository to set a regional production endpoint + environment 'AWS_STS_REGIONAL_ENDPOINTS', 'regional' + environment 'AWS_REGION', 'ap-southeast-2' + } +} + // 3rd Party Tests TaskProvider s3ThirdPartyTest = tasks.register("s3ThirdPartyTest", Test) { SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); diff --git a/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java b/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java index ff8261f65e664..16de2284511ca 100644 --- a/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java +++ b/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java @@ -8,6 +8,7 @@ package org.elasticsearch.repositories.s3; +import com.amazonaws.regions.RegionUtils; import com.amazonaws.util.json.Jackson; import org.apache.lucene.util.SetOnce; @@ -60,6 +61,8 @@ public class S3RepositoryPlugin extends Plugin implements RepositoryPlugin, Relo // ClientConfiguration clinit has some classloader problems // TODO: fix that Class.forName("com.amazonaws.ClientConfiguration"); + // Pre-load region metadata to avoid looking them up dynamically without privileges enabled + RegionUtils.initialize(); } catch (final ClassNotFoundException e) { throw new RuntimeException(e); } diff --git a/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java b/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java index 25bba12db6952..e33bfbff141b2 100644 --- a/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java +++ b/modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java @@ -370,7 +370,7 @@ static class CustomWebIdentityTokenCredentialsProvider implements AWSCredentials // https://github.com/aws/amazon-eks-pod-identity-webhook/pull/41 stsRegion = systemEnvironment.getEnv(SDKGlobalConfiguration.AWS_REGION_ENV_VAR); if (stsRegion != null) { - stsClientBuilder.withRegion(stsRegion); + SocketAccess.doPrivilegedVoid(() -> stsClientBuilder.withRegion(stsRegion)); } else { LOGGER.warn("Unable to use regional STS endpoints because the AWS_REGION environment variable is not set"); }