|
47 | 47 | import com.datastax.oss.driver.api.core.config.DriverOption; |
48 | 48 | import com.datastax.oss.driver.api.core.context.DriverContext; |
49 | 49 | import com.datastax.oss.driver.api.core.metadata.EndPoint; |
| 50 | +import org.apache.commons.lang3.StringUtils; |
50 | 51 | import software.amazon.awssdk.auth.credentials.AwsCredentials; |
51 | 52 | import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
52 | 53 | import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; |
|
55 | 56 | import software.amazon.awssdk.regions.Region; |
56 | 57 | import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; |
57 | 58 | import software.amazon.awssdk.services.sts.StsClient; |
58 | | -import software.amazon.awssdk.services.sts.StsClientBuilder; |
59 | 59 | import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; |
60 | | -import software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider; |
61 | 60 | import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; |
62 | 61 |
|
63 | 62 | import static software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider.create; |
@@ -112,11 +111,7 @@ public String getPath() { |
112 | 111 | } |
113 | 112 | }; |
114 | 113 |
|
115 | | - private final static DriverOption ROLE_OPTION = new DriverOption() { |
116 | | - public String getPath() { |
117 | | - return "advanced.auth-provider.aws-role"; |
118 | | - } |
119 | | - }; |
| 114 | + private final static DriverOption ROLE_OPTION = () -> "advanced.auth-provider.aws-role"; |
120 | 115 |
|
121 | 116 | /** |
122 | 117 | * This constructor is provided so that the driver can create |
@@ -152,8 +147,19 @@ public SigV4AuthProvider(DriverContext driverContext) { |
152 | 147 | * null value indicates to use the AWS_REGION environment |
153 | 148 | * variable, or the "aws.region" system property to configure it. |
154 | 149 | */ |
| 150 | + public SigV4AuthProvider(final String region) { |
| 151 | + this(create(), region); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Create a new Provider, using the specified region and IAM role to assume. |
| 156 | + * @param region the region (e.g. us-east-1) to use for signing. A |
| 157 | + * null value indicates to use the AWS_REGION environment |
| 158 | + * variable, or the "aws.region" system property to configure it. |
| 159 | + * @param roleArn The IAM Role ARN which the connecting client should assume before connecting with Amazon Keyspaces. |
| 160 | + */ |
155 | 161 | public SigV4AuthProvider(final String region,final String roleArn) { |
156 | | - this(Optional.ofNullable(roleArn).map(r->(AwsCredentialsProvider)createSTSRoleCredentialProvider(r,"keyspaces-session",region)).orElse(create()), region); |
| 162 | + this(Optional.ofNullable(roleArn).map(r->(AwsCredentialsProvider)createSTSRoleCredentialProvider(r,region)).orElse(create()), region); |
157 | 163 | } |
158 | 164 |
|
159 | 165 | /** |
@@ -393,10 +399,12 @@ static int indexOf(byte[] target, byte[] pattern) { |
393 | 399 | * @param roleArn The ARN of the role to assume |
394 | 400 | * @param sessionName The name of the session |
395 | 401 | * @param stsRegion The region of the STS endpoint |
396 | | - * @return |
| 402 | + * @return The STS role credential provider |
397 | 403 | */ |
398 | 404 | private static StsAssumeRoleCredentialsProvider createSTSRoleCredentialProvider(String roleArn, |
399 | | - String sessionName, String stsRegion) { |
| 405 | + String stsRegion) { |
| 406 | + final String roleName= StringUtils.substringAfterLast(roleArn,":"); |
| 407 | + final String sessionName="keyspaces-session-"+roleName+System.currentTimeMillis(); |
400 | 408 | StsClient stsClient = StsClient.builder() |
401 | 409 | .region(Region.of(stsRegion)) |
402 | 410 | .build(); |
|
0 commit comments