Skip to content

Commit 55ee48a

Browse files
authored
[b/429112319] Support AWS session credentials (#921)
Add support for the temporary AWS session credentials. Additional flag iam-sessiontoken allows user to set the AWS IAM session token that is required if temporary IAM session is used. This should unblock users who use SSO or IAM role for authentication.
1 parent 81b8443 commit 55ee48a

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/ConnectorArguments.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public class ConnectorArguments extends DefaultArguments {
127127
// redshift.
128128
public static final String OPT_IAM_ACCESSKEYID = "iam-accesskeyid";
129129
public static final String OPT_IAM_SECRETACCESSKEY = "iam-secretaccesskey";
130+
public static final String OPT_IAM_SESSIONTOKEN = "iam-sessiontoken";
130131
public static final String OPT_IAM_PROFILE = "iam-profile";
131132

132133
// Port 8020 is used by HDFS to communicate with the NameNode.
@@ -408,6 +409,8 @@ public class ConnectorArguments extends DefaultArguments {
408409
parser.accepts(OPT_IAM_ACCESSKEYID).withRequiredArg();
409410
private final OptionSpec<String> optionRedshiftIAMSecretAccessKey =
410411
parser.accepts(OPT_IAM_SECRETACCESSKEY).withRequiredArg();
412+
private final OptionSpec<String> optionRedshiftIAMSessionToken =
413+
parser.accepts(OPT_IAM_SESSIONTOKEN).withRequiredArg();
411414
private final OptionSpec<String> optionRedshiftIAMProfile =
412415
parser.accepts(OPT_IAM_PROFILE).withRequiredArg();
413416

@@ -1068,6 +1071,11 @@ public String getIAMSecretAccessKey() {
10681071
return getOptions().valueOf(optionRedshiftIAMSecretAccessKey);
10691072
}
10701073

1074+
@CheckForNull
1075+
public String getIamSessionToken() {
1076+
return getOptions().valueOf(optionRedshiftIAMSessionToken);
1077+
}
1078+
10711079
@CheckForNull
10721080
public String getIAMProfile() {
10731081
return getOptions().valueOf(optionRedshiftIAMProfile);

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/redshift/AbstractAwsApiTask.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.amazonaws.auth.AWSCredentialsProvider;
2020
import com.amazonaws.auth.AWSStaticCredentialsProvider;
2121
import com.amazonaws.auth.BasicAWSCredentials;
22+
import com.amazonaws.auth.BasicSessionCredentials;
2223
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
2324
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
2425
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
@@ -115,11 +116,17 @@ private static AWSCredentialsProvider doCreateProvider(ConnectorArguments argume
115116
}
116117
String accessKeyId = arguments.getIAMAccessKeyID();
117118
String secretAccessKey = arguments.getIAMSecretAccessKey();
118-
if (accessKeyId != null && secretAccessKey != null) {
119-
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
120-
return new AWSStaticCredentialsProvider(credentials);
121-
} else {
119+
String sessionToken = arguments.getIamSessionToken();
120+
if (accessKeyId == null || secretAccessKey == null) {
122121
return null;
123122
}
123+
124+
if (sessionToken != null) {
125+
BasicSessionCredentials credentials =
126+
new BasicSessionCredentials(accessKeyId, secretAccessKey, sessionToken);
127+
return new AWSStaticCredentialsProvider(credentials);
128+
}
129+
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
130+
return new AWSStaticCredentialsProvider(credentials);
124131
}
125132
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/redshift/AbstractRedshiftConnector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
arg = ConnectorArguments.OPT_IAM_SECRETACCESSKEY,
6565
description = "The IAM Secret Access Key to use for authentication.",
6666
required = "If present, performs explicit IAM authentication")
67+
@RespectsInput(
68+
order = 457,
69+
arg = ConnectorArguments.OPT_IAM_SESSIONTOKEN,
70+
description = "The IAM Session Token to use for authentication.",
71+
required = "Required if temporary IAM session is created")
6772
@RespectsArgumentJDBCUri
6873
public abstract class AbstractRedshiftConnector extends AbstractJdbcConnector {
6974

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/redshift/RedshiftUrlUtil.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static String makeJdbcUrlRedshiftSimple(ConnectorArguments arguments)
6363
.toJdbcPart();
6464
}
6565

66-
// TODO: [cluster-id]:[region] syntax.
66+
// TODO: [cluster-id]:[region] syntax.
6767
// either profile, or key+ secret
6868
@Nonnull
6969
static String makeJdbcUrlRedshiftIAM(ConnectorArguments arguments)
@@ -89,10 +89,14 @@ private static String makeIamProperties(ConnectorArguments arguments)
8989
}
9090
String keyId = arguments.getIAMAccessKeyID();
9191
String secretKey = arguments.getIAMSecretAccessKey();
92+
String sessionToken = arguments.getIamSessionToken();
9293
if (keyId != null && secretKey != null) {
93-
return new JdbcPropBuilder("?=&")
94-
.prop("AccessKeyID", keyId)
95-
.prop("SecretAccessKey", secretKey)
94+
JdbcPropBuilder builder =
95+
new JdbcPropBuilder("?=&").prop("AccessKeyID", keyId).prop("SecretAccessKey", secretKey);
96+
if (sessionToken != null) {
97+
builder.prop("SessionToken", sessionToken);
98+
}
99+
return builder
96100
.propOrError("DbUser", arguments.getUser(), "--user must be specified")
97101
.toJdbcPart();
98102
} else {

0 commit comments

Comments
 (0)