Skip to content

Commit

Permalink
add region option to AwsAssumeRoleCredentialsProvider
Browse files Browse the repository at this point in the history
This adds a `region` configuration item for the
AwsAssumeRoleCredentialsProvider. It is used when building a
AWSSecurityTokenServiceClient.

It not specified, uses the default region selector.

Fixes confluentinc#366
  • Loading branch information
munkyboy committed Oct 29, 2023
1 parent d7cb7d7 commit c3c11a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class AwsAssumeRoleCredentialsProvider implements AWSCredentialsProvider,
public static final String ROLE_EXTERNAL_ID_CONFIG = "sts.role.external.id";
public static final String ROLE_ARN_CONFIG = "sts.role.arn";
public static final String ROLE_SESSION_NAME_CONFIG = "sts.role.session.name";
public static final String STS_REGION_CONFIG = "sts.region";

private static final ConfigDef STS_CONFIG_DEF = new ConfigDef()
.define(
Expand All @@ -59,11 +60,18 @@ public class AwsAssumeRoleCredentialsProvider implements AWSCredentialsProvider,
ConfigDef.Type.STRING,
ConfigDef.Importance.HIGH,
"Role session name to use when starting a session"
).define(
STS_REGION_CONFIG,
ConfigDef.Type.STRING,
"",
ConfigDef.Importance.MEDIUM,
"Region of STS service. If not specified, uses a default region selector."
);

private String roleArn;
private String roleExternalId;
private String roleSessionName;
private String region;

private BasicAWSCredentials basicCredentials;

Expand All @@ -77,28 +85,29 @@ public void configure(Map<String, ?> configs) {
roleArn = config.getString(ROLE_ARN_CONFIG);
roleExternalId = config.getString(ROLE_EXTERNAL_ID_CONFIG);
roleSessionName = config.getString(ROLE_SESSION_NAME_CONFIG);
region = config.getString(STS_REGION_CONFIG);
final String accessKeyId = (String) configs.get(AWS_ACCESS_KEY_ID_CONFIG);
final String secretKey = (String) configs.get(AWS_SECRET_ACCESS_KEY_CONFIG);

// default sts client will internally use default credentials chain provider
AWSSecurityTokenServiceClientBuilder stsClientBuilder = AWSSecurityTokenServiceClientBuilder
.standard();
if (StringUtils.isNotBlank(region)) {
stsClientBuilder = stsClientBuilder.withRegion(region);
}

// Use explicit access key and secret if set
if (StringUtils.isNotBlank(accessKeyId) && StringUtils.isNotBlank(secretKey)) {
basicCredentials = new BasicAWSCredentials(accessKeyId, secretKey);
stsCredentialProvider = new STSAssumeRoleSessionCredentialsProvider
.Builder(roleArn, roleSessionName)
.withStsClient(AWSSecurityTokenServiceClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(basicCredentials)).build()
)
.withExternalId(roleExternalId)
.build();
} else {
basicCredentials = null;
stsCredentialProvider = new STSAssumeRoleSessionCredentialsProvider
.Builder(roleArn, roleSessionName)
// default sts client will internally use default credentials chain provider
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default
.withStsClient(AWSSecurityTokenServiceClientBuilder.defaultClient())
.withExternalId(roleExternalId)
.build();
stsClientBuilder = stsClientBuilder
.withCredentials(new AWSStaticCredentialsProvider(basicCredentials));
}

stsCredentialProvider = new STSAssumeRoleSessionCredentialsProvider
.Builder(roleArn, roleSessionName)
.withStsClient(stsClientBuilder.build())
.withExternalId(roleExternalId)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ public void testConfigurableAwsAssumeRoleCredentialsProvider() {
configPrefix.concat(AwsAssumeRoleCredentialsProvider.ROLE_EXTERNAL_ID_CONFIG),
"my-external-id"
);
properties.put(
configPrefix.concat(AwsAssumeRoleCredentialsProvider.STS_REGION_CONFIG),
"us-west-2"
);
connectorConfig = new S3SinkConnectorConfig(properties);

AwsAssumeRoleCredentialsProvider credentialsProvider =
Expand Down

0 comments on commit c3c11a0

Please sign in to comment.