This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
AWS SDK v2 support #64
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
367a32b
Added OAuth support for membership fetching endpoint
46758ca
Updated docs for oauth config
4af592a
Fixed unit test for memberships fetch
f2c97e4
Merge branch 'master' of https://github.com/FINRAOS/Fidelius into mem…
c11e030
Additional security headers
7f1afb9
Made Content-Security-Policy header configurable
f25ae7a
Added Content-Security-Policy to UI
73d5bd5
Merge branch 'master' of https://github.com/FINRAOS/Fidelius into sec…
92ebff0
Updated Fidelius backend and SDK to use AWS SDK v2
7efeb33
Merge branch 'master' of https://github.com/FINRAOS/Fidelius into aws…
450d632
Small fix for source names fetched for non-RDS/Aurora source types
0d28667
Removed unused list
b4e2ac7
Updated Maven compiler plugin
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,114 +17,128 @@ | |
|
|
||
| package org.finra.fidelius; | ||
|
|
||
| import java.util.Collections; | ||
| import java.net.URI; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.amazonaws.ClientConfiguration; | ||
| import com.amazonaws.auth.AWSCredentialsProvider; | ||
| import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | ||
| import com.amazonaws.regions.*; | ||
| import com.amazonaws.retry.PredefinedRetryPolicies; | ||
| import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | ||
| import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; | ||
| import com.amazonaws.services.ec2.AmazonEC2; | ||
| import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; | ||
| import com.amazonaws.services.ec2.model.*; | ||
| import com.amazonaws.services.lambda.AWSLambda; | ||
| import com.amazonaws.services.lambda.AWSLambdaClientBuilder; | ||
| import com.amazonaws.services.lambda.model.*; | ||
| import com.amazonaws.services.kms.AWSKMS; | ||
| import com.amazonaws.services.kms.AWSKMSClientBuilder; | ||
| import com.amazonaws.services.securitytoken.AWSSecurityTokenService; | ||
| import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; | ||
| import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest; | ||
| import com.amazonaws.util.EC2MetadataUtils; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.apache.commons.lang3.exception.ExceptionUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.awscore.retry.AwsRetryPolicy; | ||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkClientConfiguration; | ||
| import software.amazon.awssdk.core.internal.http.loader.DefaultSdkHttpClientBuilder; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.http.apache.ApacheHttpClient; | ||
| import software.amazon.awssdk.http.apache.ProxyConfiguration; | ||
| import software.amazon.awssdk.http.apache.internal.impl.ApacheSdkHttpClient; | ||
| import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils; | ||
| import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
| import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; | ||
| import software.amazon.awssdk.services.ec2.Ec2Client; | ||
| import software.amazon.awssdk.services.ec2.Ec2ClientBuilder; | ||
| import software.amazon.awssdk.services.ec2.model.*; | ||
| import software.amazon.awssdk.services.kms.KmsClient; | ||
| import software.amazon.awssdk.services.kms.KmsClientBuilder; | ||
| import software.amazon.awssdk.services.lambda.LambdaClient; | ||
| import software.amazon.awssdk.services.lambda.LambdaClientBuilder; | ||
| import software.amazon.awssdk.services.sts.StsClient; | ||
| import software.amazon.awssdk.services.sts.StsClientBuilder; | ||
| import software.amazon.awssdk.services.sts.model.GetCallerIdentityRequest; | ||
|
|
||
| import javax.swing.plaf.synth.Region; | ||
|
|
||
|
|
||
| public class FideliusClient { | ||
| private static final Logger logger = LoggerFactory.getLogger(FideliusClient.class); | ||
| private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
|
||
| protected EnvConfig envConfig; | ||
| protected ProxyConfiguration proxyConfig; | ||
| protected JCredStash jCredStash; | ||
| protected AWSSecurityTokenService awsSecurityTokenService; | ||
| protected StsClient stsClient; | ||
|
|
||
| private final AmazonEC2 client; | ||
| private final AWSLambda lambda; | ||
| private final Ec2Client ec2Client; | ||
| private final LambdaClient lambdaClient; | ||
|
|
||
| public FideliusClient() { | ||
| this(null, new DefaultAWSCredentialsProviderChain()); | ||
| this(null, AwsCredentialsProviderChain.builder().addCredentialsProvider(DefaultCredentialsProvider.create()).build()); | ||
| } | ||
|
|
||
| public FideliusClient(String region) { | ||
| this(null, new DefaultAWSCredentialsProviderChain(), region); | ||
| this(null, AwsCredentialsProviderChain.builder().addCredentialsProvider(DefaultCredentialsProvider.create()).build(), region); | ||
| } | ||
|
|
||
| public FideliusClient(ClientConfiguration clientConf, AWSCredentialsProvider provider) { | ||
| public FideliusClient(ClientOverrideConfiguration clientConf, AwsCredentialsProvider provider) { | ||
| this(clientConf, provider, null); | ||
| } | ||
|
|
||
| public FideliusClient(ClientConfiguration clientConf, AWSCredentialsProvider provider, String region) { | ||
| public FideliusClient(ClientOverrideConfiguration clientConf, AwsCredentialsProvider provider, String region) { | ||
|
|
||
| envConfig = new EnvConfig(); | ||
| ClientConfiguration kmsEc2ClientConfiguration = clientConf; | ||
| ClientOverrideConfiguration kmsEc2ClientConfiguration = clientConf; | ||
|
|
||
| if(clientConf==null){ | ||
| clientConf = defaultClientConfiguration(envConfig); | ||
| clientConf.setRetryPolicy(PredefinedRetryPolicies.DYNAMODB_DEFAULT); | ||
| kmsEc2ClientConfiguration = defaultClientConfiguration(envConfig); | ||
| kmsEc2ClientConfiguration.setRetryPolicy(PredefinedRetryPolicies.getDefaultRetryPolicyWithCustomMaxRetries(5)); | ||
| } | ||
|
|
||
| AmazonDynamoDBClientBuilder ddbBuilder = AmazonDynamoDBClientBuilder.standard() | ||
| .withCredentials(provider) | ||
| .withClientConfiguration(clientConf); | ||
| DynamoDbClientBuilder dynamoDbBuilder = DynamoDbClient.builder() | ||
| .credentialsProvider(provider) | ||
| .overrideConfiguration(clientConf); | ||
|
|
||
| AWSKMSClientBuilder kmsBuilder = AWSKMSClientBuilder.standard() | ||
| .withCredentials(provider) | ||
| .withClientConfiguration(kmsEc2ClientConfiguration); | ||
| KmsClientBuilder kmsBuilder = KmsClient.builder() | ||
| .credentialsProvider(provider) | ||
| .overrideConfiguration(kmsEc2ClientConfiguration); | ||
|
|
||
| AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard() | ||
| .withClientConfiguration(clientConf) | ||
| .withCredentials(provider); | ||
| StsClientBuilder stsBuilder = StsClient.builder() | ||
| .overrideConfiguration(clientConf) | ||
| .credentialsProvider(provider); | ||
|
|
||
| AmazonEC2ClientBuilder clientBuilder = AmazonEC2ClientBuilder.standard() | ||
| .withCredentials(provider) | ||
| .withClientConfiguration(kmsEc2ClientConfiguration); | ||
| Ec2ClientBuilder clientBuilder = Ec2Client.builder() | ||
| .credentialsProvider(provider) | ||
| .overrideConfiguration(kmsEc2ClientConfiguration); | ||
|
|
||
| AWSLambdaClientBuilder lambdaClientBuilder = AWSLambdaClientBuilder.standard() | ||
| .withClientConfiguration(clientConf) | ||
| .withCredentials(provider); | ||
| LambdaClientBuilder lambdaClientBuilder = LambdaClient.builder() | ||
| .credentialsProvider(provider) | ||
| .overrideConfiguration(clientConf); | ||
|
|
||
| if(region != null){ | ||
| Regions regionEnum = Regions.fromName(region); | ||
| ddbBuilder.withRegion(regionEnum); | ||
| kmsBuilder.withRegion(regionEnum); | ||
| stsBuilder.withRegion(regionEnum); | ||
| clientBuilder.withRegion(regionEnum); | ||
| lambdaClientBuilder.withRegion(regionEnum); | ||
| software.amazon.awssdk.regions.Region awsRegion = software.amazon.awssdk.regions.Region.of(region); | ||
| dynamoDbBuilder = dynamoDbBuilder.region(awsRegion); | ||
| kmsBuilder = kmsBuilder.region(awsRegion); | ||
| stsBuilder = stsBuilder.region(awsRegion); | ||
| clientBuilder = clientBuilder.region(awsRegion); | ||
| lambdaClientBuilder = lambdaClientBuilder.region(awsRegion); | ||
| } | ||
| lambda = lambdaClientBuilder.build(); | ||
| client = clientBuilder.build(); | ||
| awsSecurityTokenService = stsBuilder.build(); | ||
| jCredStash = new JCredStash(ddbBuilder.build(), kmsBuilder.build(), awsSecurityTokenService); | ||
| if(envConfig.hasProxyEnv()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I understand this if condition, don't we have to initialize all the clients if proxy env is provided and if not provided, like if.....else....
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're still building the Clients on lines 128-131. For AWS SDK v2, client builders like the DynamoDbClientBuilder need an additional HTTP Client built-in for proxy configuration to work. The if-block there takes care of that if proxy configuration is provided but skips if it's not. |
||
| SdkHttpClient sdkHttpClient = ApacheHttpClient.builder() | ||
| .proxyConfiguration(proxyConfig).build(); | ||
| dynamoDbBuilder = dynamoDbBuilder.httpClient(sdkHttpClient); | ||
| kmsBuilder = kmsBuilder.httpClient(sdkHttpClient); | ||
| stsBuilder = stsBuilder.httpClient(sdkHttpClient); | ||
| clientBuilder = clientBuilder.httpClient(sdkHttpClient); | ||
| lambdaClientBuilder = lambdaClientBuilder.httpClient(sdkHttpClient); | ||
| } | ||
| lambdaClient = lambdaClientBuilder.build(); | ||
| ec2Client = clientBuilder.build(); | ||
| stsClient = stsBuilder.build(); | ||
| jCredStash = new JCredStash(dynamoDbBuilder.build(), kmsBuilder.build(), stsClient); | ||
| } | ||
|
|
||
| protected void setFideliusClient(AmazonDynamoDB ddb, AWSKMS kms) { | ||
| jCredStash = new JCredStash(ddb, kms, awsSecurityTokenService); | ||
| protected void setFideliusClient(DynamoDbClient ddb, KmsClient kms) { | ||
| jCredStash = new JCredStash(ddb, kms, stsClient); | ||
| } | ||
|
|
||
| protected ClientConfiguration defaultClientConfiguration(EnvConfig envConfig){ | ||
| ClientConfiguration clientConfiguration = new ClientConfiguration(); | ||
| protected ClientOverrideConfiguration defaultClientConfiguration(EnvConfig envConfig){ | ||
| ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder().retryPolicy(AwsRetryPolicy.defaultRetryPolicy()).build(); | ||
| if(envConfig.hasProxyEnv()) { | ||
| clientConfiguration.setProxyHost(envConfig.getProxy()); | ||
| clientConfiguration.setProxyPort(Integer.parseInt(envConfig.getPort())); | ||
| proxyConfig = ProxyConfiguration.builder().endpoint(URI.create(envConfig.getProxy() + ":" + envConfig.getPort())).build(); | ||
| } | ||
| return clientConfiguration; | ||
| } | ||
|
|
@@ -161,23 +175,23 @@ protected HashMap<String, String> getEC2Tags() { | |
|
|
||
| String instanceID = EC2MetadataUtils.getInstanceId(); | ||
|
|
||
| DescribeInstancesRequest instancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceID); | ||
| DescribeInstancesResult instancesResult = client.describeInstances(instancesRequest); | ||
| DescribeInstancesRequest instancesRequest = DescribeInstancesRequest.builder().instanceIds(instanceID).build(); | ||
| DescribeInstancesResponse instancesResult = ec2Client.describeInstances(instancesRequest); | ||
|
|
||
| // There should only be one Instance with identical instanceID | ||
| List<Reservation> reservations = instancesResult.getReservations(); | ||
| List<Reservation> reservations = instancesResult.reservations(); | ||
| if (reservations.size() > 1) { | ||
| return null; | ||
| } | ||
|
|
||
| Reservation reservation = reservations.get(0); | ||
| Instance instance = reservation.getInstances().get(0); | ||
| List<Tag> tagList = instance.getTags(); | ||
| Instance instance = reservation.instances().get(0); | ||
| List<Tag> tagList = instance.tags(); | ||
|
|
||
| HashMap<String, String> tagMap = new HashMap<String, String>(); | ||
| for (Tag t : tagList) { | ||
| if (t.getKey().equals(Constants.FID_CONTEXT_APPLICATION) || t.getKey().equals(Constants.FID_CONTEXT_SDLC) || t.getKey().equals(Constants.FID_CONTEXT_COMPONENT)) | ||
| tagMap.put(t.getKey(), t.getValue()); | ||
| if (t.key().equals(Constants.FID_CONTEXT_APPLICATION) || t.key().equals(Constants.FID_CONTEXT_SDLC) || t.key().equals(Constants.FID_CONTEXT_COMPONENT)) | ||
| tagMap.put(t.key(), t.value()); | ||
| } | ||
| return tagMap; | ||
| } | ||
|
|
@@ -245,7 +259,7 @@ protected String getUser() throws Exception { | |
| } | ||
|
|
||
| protected String getUserIdentity() throws Exception { | ||
| return awsSecurityTokenService.getCallerIdentity(new GetCallerIdentityRequest()).getArn(); | ||
| return stsClient.getCallerIdentity(GetCallerIdentityRequest.builder().build()).arn(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -507,6 +521,9 @@ protected void deleteCredential(String name, String application, String sdlc, | |
| logger.info("User "+ user + " deleted credential " + prefixedName); | ||
| } catch (RuntimeException e) { // Credential not found | ||
| logger.info("Credential " + prefixedName + " not found. [" + e.toString() + "] "); | ||
| for(StackTraceElement ste : e.getStackTrace()) { | ||
| logger.error(ste.toString()); | ||
| } | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update the maven compiler plugin to line up with this PR? #65