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

Fix #1418 Expose AwsCredentialsProvider to AmazonS3InstallationService #1419

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

seratch
Copy link
Member

@seratch seratch commented Jan 14, 2025

This pull request resolves #1418

Category (place an x in each of the [ ])

  • bolt (Bolt for Java)
  • bolt-{sub modules} (Bolt for Java - optional modules)
  • slack-api-client (Slack API Clients)
  • slack-api-model (Slack API Data Models)
  • slack-api-*-kotlin-extension (Kotlin Extensions for Slack API Clients)
  • slack-app-backend (The primitive layer of Bolt for Java)

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you agree to those rules.

@seratch seratch added enhancement M-T: A feature request for new functionality project:bolt labels Jan 14, 2025
@seratch seratch added this to the 1.45.2 milestone Jan 14, 2025
@seratch seratch self-assigned this Jan 14, 2025
Copy link

codecov bot commented Jan 14, 2025

Codecov Report

Attention: Patch coverage is 57.14286% with 12 lines in your changes missing coverage. Please review.

Project coverage is 73.17%. Comparing base (253cffc) to head (b53e018).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...olt/service/builtin/AmazonS3OAuthStateService.java 50.00% 5 Missing and 2 partials ⚠️
...t/service/builtin/AmazonS3InstallationService.java 64.28% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1419      +/-   ##
============================================
- Coverage     73.26%   73.17%   -0.09%     
- Complexity     4370     4372       +2     
============================================
  Files           474      474              
  Lines         14185    14205      +20     
  Branches       1433     1437       +4     
============================================
+ Hits          10393    10395       +2     
- Misses         2947     2961      +14     
- Partials        845      849       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bruceadowns
Copy link

This change would work fine, however I also need to add region and endpointUrl.

I can put this in a PR if you want, but here is a diff showing you my thinking:

diff --git a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java
index 5aaa58b85..abe68acf2 100644
--- a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java
+++ b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java
@@ -7,13 +7,16 @@ import com.slack.api.bolt.model.builtin.DefaultBot;
 import com.slack.api.bolt.model.builtin.DefaultInstaller;
 import com.slack.api.bolt.service.InstallationService;
 import com.slack.api.bolt.util.JsonOps;
+import java.net.URI;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import software.amazon.awssdk.auth.credentials.AwsCredentials;
 import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
 import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
 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.*;
 
@@ -26,18 +29,44 @@ import java.util.Optional;
 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 = bucketName;
+        this.credentialsProvider = DefaultCredentialsProvider.create();
+        this.region = Region.of(System.getenv("AWS_REGION"));
+        this.endpointOverride = null;
+    }
+
+    public AmazonS3InstallationService(String bucketName,
+        AwsCredentialsProvider credentialsProvider) {
+        this.bucketName = bucketName;
+        this.credentialsProvider = credentialsProvider;
+        this.region = Region.of(System.getenv("AWS_REGION"));
+        this.endpointOverride = null;
+    }
+
+    public AmazonS3InstallationService(String bucketName,
+        AwsCredentialsProvider credentialsProvider,
+        String region,
+        String endpointUrl) {
+        this.bucketName = bucketName;
+        this.credentialsProvider = credentialsProvider;
+        this.region = Region.of(region);
+        if (StringUtils.isBlank(endpointUrl)) {
+            this.endpointOverride = null;
+        } else {
+            this.endpointOverride = URI.create(endpointUrl);
+        }
     }
 
     @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);
             if (credentials == null || credentials.accessKeyId() == null) {
                 throw new IllegalStateException("AWS credentials not found");
@@ -304,7 +333,11 @@ public class AmazonS3InstallationService implements InstallationService {
     }
 
     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) {
@@ -330,4 +363,5 @@ public class AmazonS3InstallationService implements InstallationService {
                 + "-"
                 + Optional.ofNullable(teamId).orElse("none");
     }
+
 }

@seratch
Copy link
Member Author

seratch commented Jan 14, 2025

Thanks for the suggestions; I've revised this PR

@bruceadowns
Copy link

Thanks for the suggestions; I've revised this PR

That looks like it'll work. 👍 I'll try it out, once merged.

this.bucketName = bucketName;
this.credentialsProvider = credentialsProvider;
this.region = region != null || System.getenv("AWS_REGION") == null ? region : Region.of(System.getenv("AWS_REGION"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

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;

Choose a reason for hiding this comment

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

Suggested change
this.endpointOverride = (endpointOverride != null && !endpointOverride.isEmpty()) ? URI.create(endpointOverride) : null;
this.endpointOverride = StringUtils.isBlank(endpointOverride) ? null : URI.create(endpointOverride);

@seratch seratch merged commit 6a7fe19 into slackapi:main Jan 15, 2025
6 checks passed
@seratch seratch deleted the issue-1418 branch January 15, 2025 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement M-T: A feature request for new functionality project:bolt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose AwsCredentialsProvider to AmazonS3InstallationService
3 participants