Переглянути джерело

Merge pull request #30458 from overleaf/mj-git-bridge-local-swap

[git-bridge] Mock S3 locally with minio

GitOrigin-RevId: d56659d601e4450f69332202b86e61a443375101
Mathias Jakobsen 9 місяців тому
батько
коміт
bf662f74f5

+ 2 - 1
services/git-bridge/conf/envsubst_template.json

@@ -18,7 +18,8 @@
     "awsAccessKey": "${GIT_BRIDGE_SWAPSTORE_AWS_ACCESS_KEY}",
     "awsSecret": "${GIT_BRIDGE_SWAPSTORE_AWS_SECRET}",
     "s3BucketName": "${GIT_BRIDGE_SWAPSTORE_S3_BUCKET_NAME}",
-    "awsRegion": "${GIT_BRIDGE_SWAPSTORE_AWS_REGION:-us-east-1}"
+    "awsRegion": "${GIT_BRIDGE_SWAPSTORE_AWS_REGION:-us-east-1}",
+    "awsEndpoint": "${GIT_BRIDGE_SWAPSTORE_AWS_ENDPOINT}"
   },
   "swapJob": {
     "minProjects": ${GIT_BRIDGE_SWAPJOB_MIN_PROJECTS:-50},

+ 20 - 6
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/store/S3SwapStore.java

@@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge.swap.store;
 
 import com.amazonaws.auth.AWSStaticCredentialsProvider;
 import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
 import com.amazonaws.services.s3.AmazonS3;
 import com.amazonaws.services.s3.AmazonS3ClientBuilder;
 import com.amazonaws.services.s3.model.*;
@@ -17,22 +18,35 @@ public class S3SwapStore implements SwapStore {
   private final String bucketName;
 
   public S3SwapStore(SwapStoreConfig cfg) {
-    this(cfg.getAwsAccessKey(), cfg.getAwsSecret(), cfg.getS3BucketName(), cfg.getAwsRegion());
+    this(
+        cfg.getAwsAccessKey(),
+        cfg.getAwsSecret(),
+        cfg.getS3BucketName(),
+        cfg.getAwsRegion(),
+        cfg.getAwsEndpoint());
   }
 
-  S3SwapStore(String accessKey, String secret, String bucketName, String region) {
+  S3SwapStore(String accessKey, String secret, String bucketName, String region, String endpoint) {
     String regionToUse = null;
     if (region == null) {
       regionToUse = "us-east-1";
     } else {
       regionToUse = region;
     }
-    s3 =
+
+    AmazonS3ClientBuilder builder =
         AmazonS3ClientBuilder.standard()
-            .withRegion(regionToUse)
             .withCredentials(
-                new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret)))
-            .build();
+                new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret)));
+
+    if (endpoint != null && !endpoint.isEmpty()) {
+      builder
+          .enablePathStyleAccess()
+          .withEndpointConfiguration(new EndpointConfiguration(endpoint, regionToUse));
+    } else {
+      builder.withRegion(regionToUse);
+    }
+    s3 = builder.build();
     this.bucketName = bucketName;
   }
 

+ 22 - 5
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/swap/store/SwapStoreConfig.java

@@ -5,28 +5,40 @@ package uk.ac.ic.wlgitbridge.bridge.swap.store;
  */
 public class SwapStoreConfig {
 
-  public static final SwapStoreConfig NOOP = new SwapStoreConfig("noop", null, null, null, null);
+  public static final SwapStoreConfig NOOP =
+      new SwapStoreConfig("noop", null, null, null, null, null);
 
   private String type;
   private String awsAccessKey;
   private String awsSecret;
   private String s3BucketName;
   private String awsRegion;
+  private String awsEndpoint;
 
   public SwapStoreConfig() {}
 
   public SwapStoreConfig(
-      String awsAccessKey, String awsSecret, String s3BucketName, String awsRegion) {
-    this("s3", awsAccessKey, awsSecret, s3BucketName, awsRegion);
+      String awsAccessKey,
+      String awsSecret,
+      String s3BucketName,
+      String awsRegion,
+      String awsEndpoint) {
+    this("s3", awsAccessKey, awsSecret, s3BucketName, awsRegion, awsEndpoint);
   }
 
   SwapStoreConfig(
-      String type, String awsAccessKey, String awsSecret, String s3BucketName, String awsRegion) {
+      String type,
+      String awsAccessKey,
+      String awsSecret,
+      String s3BucketName,
+      String awsRegion,
+      String awsEndpoint) {
     this.type = type;
     this.awsAccessKey = awsAccessKey;
     this.awsSecret = awsSecret;
     this.s3BucketName = s3BucketName;
     this.awsRegion = awsRegion;
+    this.awsEndpoint = awsEndpoint;
   }
 
   public String getType() {
@@ -49,13 +61,18 @@ public class SwapStoreConfig {
     return awsRegion;
   }
 
+  public String getAwsEndpoint() {
+    return awsEndpoint;
+  }
+
   public SwapStoreConfig sanitisedCopy() {
     return new SwapStoreConfig(
         type,
         awsAccessKey == null ? null : "<awsAccessKey>",
         awsSecret == null ? null : "<awsSecret>",
         s3BucketName,
-        awsRegion);
+        awsRegion,
+        awsEndpoint);
   }
 
   public static SwapStoreConfig sanitisedCopy(SwapStoreConfig swapStore) {

+ 2 - 1
services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/bridge/swap/store/S3SwapStoreTest.java

@@ -11,6 +11,7 @@ public class S3SwapStoreTest {
   private static final String secret = null;
   private static final String bucketName = "com.overleaf.testbucket";
   private static final String region = "us-east-1";
+  private static final String endpoint = null;
 
   private S3SwapStore s3;
 
@@ -20,7 +21,7 @@ public class S3SwapStoreTest {
       s3 = null;
       return;
     }
-    s3 = new S3SwapStore(accessKey, secret, bucketName, region);
+    s3 = new S3SwapStore(accessKey, secret, bucketName, region, endpoint);
   }
 
   //    @Ignore