-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Nordstrom/feature/4-connector-can-assume-i…
…am-role Functionality to allow connector to assume an AWS IAM role when invok…
- Loading branch information
Showing
16 changed files
with
426 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/com/nordstrom/kafka/connect/auth/AWSAssumeRoleCredentialsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.nordstrom.kafka.connect.auth; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider; | ||
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; | ||
import org.apache.kafka.common.Configurable; | ||
|
||
import java.util.Map; | ||
|
||
//import org.slf4j.Logger; | ||
//import org.slf4j.LoggerFactory; | ||
|
||
public class AWSAssumeRoleCredentialsProvider implements AWSCredentialsProvider, Configurable { | ||
// Uncomment slf4j imports and field declaration to enable logging. | ||
// private static final Logger log = LoggerFactory.getLogger(AWSAssumeRoleCredentialsProvider.class); | ||
|
||
public static final String EXTERNAL_ID_CONFIG = "external.id"; | ||
public static final String ROLE_ARN_CONFIG = "role.arn"; | ||
public static final String SESSION_NAME_CONFIG = "session.name"; | ||
|
||
private String externalId; | ||
private String roleArn; | ||
private String sessionName; | ||
|
||
@Override | ||
public void configure(Map<String, ?> map) { | ||
externalId = getOptionalField(map, EXTERNAL_ID_CONFIG); | ||
roleArn = getRequiredField(map, ROLE_ARN_CONFIG); | ||
sessionName = getRequiredField(map, SESSION_NAME_CONFIG); | ||
} | ||
|
||
@Override | ||
public AWSCredentials getCredentials() { | ||
AWSSecurityTokenServiceClientBuilder clientBuilder = AWSSecurityTokenServiceClientBuilder.standard(); | ||
AWSCredentialsProvider provider = new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, sessionName) | ||
.withStsClient(clientBuilder.defaultClient()) | ||
.withExternalId(externalId) | ||
.build(); | ||
|
||
return provider.getCredentials(); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
//Nothing to do really, since we are assuming a role. | ||
} | ||
|
||
String getOptionalField(final Map<String, ?> map, final String fieldName) { | ||
final Object field = map.get(fieldName); | ||
if (isNotNull(field)) { | ||
return field.toString(); | ||
} | ||
return null; | ||
} | ||
|
||
String getRequiredField(final Map<String, ?> map, final String fieldName) { | ||
final Object field = map.get(fieldName); | ||
verifyNotNull(field, fieldName); | ||
final String fieldValue = field.toString(); | ||
verifyNotNullOrEmpty(fieldValue, fieldName); | ||
|
||
return fieldValue; | ||
} | ||
|
||
private boolean isNotNull(final Object field) { | ||
return null != field; | ||
} | ||
|
||
private boolean isNotNullOrEmpty(final String field) { | ||
return null != field && !field.isEmpty(); | ||
} | ||
|
||
private void verifyNotNull(final Object field, final String fieldName) { | ||
if (!isNotNull(field)) { | ||
throw new IllegalArgumentException(String.format("The field '%1s' should not be null", fieldName)); | ||
} | ||
} | ||
|
||
private void verifyNotNullOrEmpty(final String field, final String fieldName) { | ||
if (!isNotNullOrEmpty(field)) { | ||
throw new IllegalArgumentException(String.format("The field '%1s' should not be null or empty", fieldName)); | ||
} | ||
} | ||
|
||
String getExternalId() { | ||
return this.externalId; | ||
} | ||
|
||
String getRoleArn() { | ||
return this.roleArn; | ||
} | ||
|
||
String getSessionName() { | ||
return this.sessionName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.