Skip to content

Commit b56dcc6

Browse files
committed
Added documentation and added another constructor for both region and IAM role.
1 parent c191dde commit b56dcc6

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This package implements an authentication plugin for the open-source Datastax Ja
88

99
The plugin depends on the AWS SDK for Java. It uses `AWSCredentialsProvider` to obtain credentials. Because the IAuthenticator interface operates at the level of `InetSocketAddress`, you must specify the service endpoint to use for the connection.
1010
You can provide the Region in the constructor programmatically, via the `AWS_REGION` environment variable, or via the `aws.region` system property.
11+
You can also provide the IAM role, with which you want to connect with KeySpaces, in the constructor programmatically, via advanced.auth-provider.aws-role property in the conf file.
1112

1213
The full documentation for the plugin is available at
1314
https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.credentials.html#programmatic.credentials.SigV4_KEYSPACES.
@@ -35,29 +36,44 @@ You can specify the Region using one of the following four methods:
3536
* Constructor
3637
* Configuration
3738

38-
## Environment Variable
39+
### Environment Variable
3940

4041
You can use the `AWS_REGION` environment variable to match the endpoint that you are communicating with by setting it as part of your application start-up, as follows.
4142

4243
``` shell
4344
$ export AWS_Region=us-east-1
4445
```
45-
## System Property
46+
### System Property
4647

4748
You can use the `aws.region` Java system property by specifying it on the command line, as follows.
4849

4950
``` shell
5051
$ java -Daws.region=us=east-1 ...
5152
```
5253

53-
## Constructor
54+
### Constructor
5455

5556
One of the constructors for `software.aws.mcs.auth.SigV4AuthProvider` takes a `String` representing the Region that will be used for that instance.
5657

57-
## Configuration
58+
### Configuration
5859

5960
Set the Region explicitly in your `advanced.auth-provider.class` configuration (see example below), by specifying the `advanced.auth-provider.aws-region` property.
6061

62+
## Assume IAM Role Configuration
63+
64+
You must configure the AWS Region that the plugin will use when authenticating using mechanism mentioned above.
65+
You can also specify IAM Role (including cross-account IAM role) configuraiton using one of the 2 methods:
66+
* Constructor
67+
* Configuration
68+
69+
### Constructor
70+
71+
One of the constructors for `software.aws.mcs.auth.SigV4AuthProvider` takes two String , first represening the region and second one representing IAM role ARN.
72+
73+
### Configuration
74+
75+
Set the IAM Role explicitly in your `advanced.auth-provider.class` configuration (see example below), by specifying the `advanced.auth-provider.aws-role` property.
76+
6177
## Add the Authentication Plugin to the Application
6278

6379
The authentication plugin supports version 4.x of the DataStax Java Driver for Cassandra.

src/main/java/software/aws/mcs/auth/SigV4AuthProvider.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.datastax.oss.driver.api.core.config.DriverOption;
4848
import com.datastax.oss.driver.api.core.context.DriverContext;
4949
import com.datastax.oss.driver.api.core.metadata.EndPoint;
50+
import org.apache.commons.lang3.StringUtils;
5051
import software.amazon.awssdk.auth.credentials.AwsCredentials;
5152
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
5253
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
@@ -55,9 +56,7 @@
5556
import software.amazon.awssdk.regions.Region;
5657
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
5758
import software.amazon.awssdk.services.sts.StsClient;
58-
import software.amazon.awssdk.services.sts.StsClientBuilder;
5959
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
60-
import software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider;
6160
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
6261

6362
import static software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider.create;
@@ -112,11 +111,7 @@ public String getPath() {
112111
}
113112
};
114113

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";
120115

121116
/**
122117
* This constructor is provided so that the driver can create
@@ -152,8 +147,19 @@ public SigV4AuthProvider(DriverContext driverContext) {
152147
* null value indicates to use the AWS_REGION environment
153148
* variable, or the "aws.region" system property to configure it.
154149
*/
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+
*/
155161
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);
157163
}
158164

159165
/**
@@ -393,10 +399,12 @@ static int indexOf(byte[] target, byte[] pattern) {
393399
* @param roleArn The ARN of the role to assume
394400
* @param sessionName The name of the session
395401
* @param stsRegion The region of the STS endpoint
396-
* @return
402+
* @return The STS role credential provider
397403
*/
398404
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();
400408
StsClient stsClient = StsClient.builder()
401409
.region(Region.of(stsRegion))
402410
.build();

0 commit comments

Comments
 (0)