Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiLang AWS SDK v1 to v2 #1

Merged
merged 16 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions amazon-kinesis-client-multilang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

<artifactId>amazon-kinesis-client-multilang</artifactId>

<properties>
<aws-java-sdk.version>1.12.668</aws-java-sdk.version>
</properties>

<dependencies>
<dependency>
<groupId>software.amazon.kinesis</groupId>
Expand All @@ -43,32 +39,6 @@
<version>${awssdk.version}</version>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>${aws-java-sdk.version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sts</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package software.amazon.kinesis.multilang;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.regions.Regions;
import com.google.common.base.CaseFormat;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.regions.Region;

/**
* Key-Value pairs which may be nested in, and extracted from, a property value
Expand Down Expand Up @@ -73,8 +74,13 @@ void visit(final NestedPropertyProcessor processor, final String endpoint) {
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-regions">Available Regions</a>
*/
ENDPOINT_REGION {
void visit(final NestedPropertyProcessor processor, final String region) {
processor.acceptEndpointRegion(Regions.fromName(region));
ethkatnic marked this conversation as resolved.
Show resolved Hide resolved
void visit(final NestedPropertyProcessor processor, final String regionName) {
List<Region> validRegions = Region.regions();
Region region = Region.of(regionName);
if (!validRegions.contains(region)) {
throw new IllegalArgumentException("Invalid region name: " + regionName);
}
processor.acceptEndpointRegion(region);
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package software.amazon.kinesis.multilang;

import com.amazonaws.regions.Regions;
import software.amazon.awssdk.regions.Region;

/**
* Defines methods to process {@link NestedPropertyKey}s.
Expand All @@ -28,7 +28,7 @@ public interface NestedPropertyProcessor {
* (e.g., https://sns.us-west-1.amazonaws.com, sns.us-west-1.amazonaws.com)
* @param signingRegion the region to use for SigV4 signing of requests (e.g. us-west-1)
*
* @see #acceptEndpointRegion(Regions)
* @see #acceptEndpointRegion(Region)
* @see <a href="https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/client/builder/AwsClientBuilder.EndpointConfiguration.html">
* AwsClientBuilder.EndpointConfiguration</a>
*/
Expand All @@ -42,7 +42,7 @@ public interface NestedPropertyProcessor {
*
* @see #acceptEndpoint(String, String)
*/
void acceptEndpointRegion(Regions region);
void acceptEndpointRegion(Region region);

/**
* Set the external id, an optional field to designate who can assume an IAM role.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package software.amazon.kinesis.multilang.auth;

import java.util.Arrays;

import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
import software.amazon.kinesis.multilang.NestedPropertyKey;
import software.amazon.kinesis.multilang.NestedPropertyProcessor;

public class KclStsAssumeRoleCredentialsProvider implements AwsCredentialsProvider, NestedPropertyProcessor {
ethkatnic marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker for this PR - This class is an extension of the StsProvider. I feel like the users should not have to pick between the normal stsProvider and KCLstsProvider if they want to add extra fields like externalId etc... We can direct all construction of stsProvider to this class internally so users can use all of its feature by declaring the generic stsProvider in the property file.

I think this is especially important for sdk v2 because credential providers don't provide constructors anymore and this stsProvider doesn't have a create() method either meaning that we will need to rely on this class to build stsProvider

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. The old docs described creating an STSAssumeRoleSessionCredentialsProvider from the config, which relied on a deprecated constructor for the STSAssumeRoleSessionCredentialsProvider.

As you said, this is no longer possible because the v2 StsAssumeRoleCredentialsProvider requires a builder, so we can't use a simple constructor. In that way, we are forcing users to use the KclStsAssumeRoleCredentialsProvider as I mentioned in the documentation changes.

private final String roleArn;
private final String roleSessionName;
private Region region;
private String serviceEndpoint;
private String externalId;

public KclStsAssumeRoleCredentialsProvider(String[] params) {
this(params[0], params[1], Arrays.copyOfRange(params, 2, params.length));
}

public KclStsAssumeRoleCredentialsProvider(String roleArn, String roleSessionName, String... params) {
this.roleArn = roleArn;
this.roleSessionName = roleSessionName;
NestedPropertyKey.parse(this, params);
}

@Override
public AwsCredentials resolveCredentials() {
StsAssumeRoleCredentialsProviderConfig config = new StsAssumeRoleCredentialsProviderConfig(
roleArn, roleSessionName, region, serviceEndpoint, externalId);
StsAssumeRoleCredentialsProvider stsAssumeRoleCredentialsProvider =
StsAssumeRoleCredentialsProviderFactory.createProvider(config);
return stsAssumeRoleCredentialsProvider.resolveCredentials();
}

@Override
public void acceptEndpoint(String serviceEndpoint, String signingRegion) {
this.serviceEndpoint = serviceEndpoint;
this.region = Region.of(signingRegion);
ethkatnic marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void acceptEndpointRegion(Region region) {
this.region = region;
}

@Override
public void acceptExternalId(String externalId) {
this.externalId = externalId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package software.amazon.kinesis.multilang.auth;

import lombok.Getter;
import lombok.Setter;
import software.amazon.awssdk.regions.Region;

@Getter
@Setter
public class StsAssumeRoleCredentialsProviderConfig {
private final String roleArn;
private final String roleSessionName;
private final Region region;
private final String serviceEndpoint;
private final String externalId;

public StsAssumeRoleCredentialsProviderConfig(
String roleArn, String roleSessionName, Region region, String serviceEndpoint, String externalId) {
this.roleArn = roleArn;
this.roleSessionName = roleSessionName;
this.region = region;
this.serviceEndpoint = serviceEndpoint;
this.externalId = externalId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package software.amazon.kinesis.multilang.auth;

import java.net.URI;
import java.net.URISyntaxException;

import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.StsClientBuilder;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;

public class StsAssumeRoleCredentialsProviderFactory {

public static StsAssumeRoleCredentialsProvider createProvider(StsAssumeRoleCredentialsProviderConfig config) {
StsClientBuilder stsClientBuilder = StsClient.builder();

if (config.getRegion() != null) {
ethkatnic marked this conversation as resolved.
Show resolved Hide resolved
stsClientBuilder.region(config.getRegion());
}

if (config.getServiceEndpoint() != null) {
try {
stsClientBuilder.endpointOverride(new URI(config.getServiceEndpoint()));
ethkatnic marked this conversation as resolved.
Show resolved Hide resolved
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid service endpoint: " + config.getServiceEndpoint(), e);
}
}

StsClient stsClient = stsClientBuilder.build();

AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder()
.roleArn(config.getRoleArn())
.roleSessionName(config.getRoleSessionName())
.build();

return StsAssumeRoleCredentialsProvider.builder()
.refreshRequest(assumeRoleRequest)
.stsClient(stsClient)
.build();
}
}
Loading