Skip to content

Commit

Permalink
Set region for the STS client via privileged calls in AWS SDK
Browse files Browse the repository at this point in the history
Unfortunately, `AWSSecurityTokenServiceClientBuilder#setRegion` is not just a setter on
the builder. It looks up the region by its name which laziliy initializes some regional
configuration. As a result, the call with an `access denied` error, because
the caller doesn't have permission to call `accessDeclaredMembers` in some Jackson
internals.

We fix that in two ways:
* Make sure `withRegion` call is priviliged
* Eagarly lookup region metadata in `S3RepositoryPlugin`

Fixes elastic#102173
  • Loading branch information
arteam committed Nov 15, 2023
1 parent 3a09c64 commit 8a7c9b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,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);
}
Expand Down
Expand Up @@ -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");
}
Expand Down

0 comments on commit 8a7c9b0

Please sign in to comment.