Skip to content

Commit

Permalink
Fix #1418 Expose AwsCredentialsProvider to AmazonS3InstallationService (
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Jan 15, 2025
1 parent 253cffc commit 6a7fe19
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
Expand All @@ -26,19 +28,38 @@
public class AmazonS3InstallationService implements InstallationService {

private final String bucketName;
private AwsCredentialsProvider credentialsProvider;

private final AwsCredentialsProvider credentialsProvider;
private final Region region;
private final URI endpointOverride;

private boolean historicalDataEnabled;

public AmazonS3InstallationService(String bucketName) {
this(bucketName, DefaultCredentialsProvider.create(), null, null);
}

public AmazonS3InstallationService(String bucketName, AwsCredentialsProvider credentialsProvider) {
this(bucketName, credentialsProvider, null, null);
}

public AmazonS3InstallationService(
String bucketName,
AwsCredentialsProvider credentialsProvider,
Region region,
String endpointOverride
) {
this.bucketName = bucketName;
this.credentialsProvider = credentialsProvider;
this.region = (region != null || System.getenv("AWS_REGION") == null) ? region : Region.of(System.getenv("AWS_REGION"));
this.endpointOverride = (endpointOverride != null && !endpointOverride.isEmpty()) ? URI.create(endpointOverride) : null;
}

@Override
public Initializer initializer() {
return (app) -> {
// The first access to S3 tends to be slow on AWS Lambda.
this.credentialsProvider = DefaultCredentialsProvider.create();
AwsCredentials credentials = createCredentials(credentialsProvider);
AwsCredentials credentials = createCredentials(this.credentialsProvider);
if (credentials == null || credentials.accessKeyId() == null) {
throw new IllegalStateException("AWS credentials not found");
}
Expand Down Expand Up @@ -304,7 +325,11 @@ protected AwsCredentials createCredentials(AwsCredentialsProvider provider) {
}

protected S3Client createS3Client() {
return S3Client.builder().credentialsProvider(this.credentialsProvider).build();
return S3Client.builder()
.credentialsProvider(this.credentialsProvider)
.region(this.region)
.endpointOverride(this.endpointOverride)
.build();
}

private String getInstallerKey(Installer i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
Expand All @@ -23,18 +25,36 @@
public class AmazonS3OAuthStateService implements OAuthStateService {

private final String bucketName;
private AwsCredentialsProvider credentialsProvider;

private final AwsCredentialsProvider credentialsProvider;
private final Region region;
private final URI endpointOverride;

public AmazonS3OAuthStateService(String bucketName) {
this(bucketName, DefaultCredentialsProvider.create());
}

public AmazonS3OAuthStateService(String bucketName, AwsCredentialsProvider credentialsProvider) {
this(bucketName, credentialsProvider, null, null);
}

public AmazonS3OAuthStateService(
String bucketName,
AwsCredentialsProvider credentialsProvider,
Region region,
String endpointOverride
) {
this.bucketName = bucketName;
this.credentialsProvider = credentialsProvider;
this.region = (region != null || System.getenv("AWS_REGION") == null) ? region : Region.of(System.getenv("AWS_REGION"));
this.endpointOverride = (endpointOverride != null && !endpointOverride.isEmpty()) ? URI.create(endpointOverride) : null;
}

@Override
public Initializer initializer() {
return (app) -> {
// The first access to S3 tends to be slow on AWS Lambda.
this.credentialsProvider = DefaultCredentialsProvider.create();
AwsCredentials credentials = createCredentials(credentialsProvider);
AwsCredentials credentials = createCredentials(this.credentialsProvider);
if (credentials == null || credentials.accessKeyId() == null) {
throw new IllegalStateException("AWS credentials not found");
}
Expand Down Expand Up @@ -103,7 +123,11 @@ protected AwsCredentials createCredentials(AwsCredentialsProvider provider) {
}

protected S3Client createS3Client() {
return S3Client.builder().credentialsProvider(this.credentialsProvider).build();
return S3Client.builder()
.credentialsProvider(this.credentialsProvider)
.region(this.region)
.endpointOverride(this.endpointOverride)
.build();
}

private String getKey(String state) {
Expand Down

0 comments on commit 6a7fe19

Please sign in to comment.