Skip to content

Commit

Permalink
Allow differentiating blobstores by buckets
Browse files Browse the repository at this point in the history
Allows differentiating the backend blobstores by configured buckets.
The commit allows using the same s3proxy credentials across the
different backends and relies on the bucket names to disambiguate them.

The "default" blobstore is the one that occurs first.

Fixes: gaul#371
  • Loading branch information
timuralp committed Sep 17, 2021
1 parent 35fea54 commit 3fce2d3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
62 changes: 47 additions & 15 deletions src/main/java/org/gaul/s3proxy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -101,8 +103,10 @@ public static void main(String[] args) throws Exception {
.build();
ExecutorService executorService = DynamicExecutors.newScalingThreadPool(
1, 20, 60 * 1000, factory);
ImmutableMap.Builder<String, Map.Entry<String, BlobStore>> locators =
ImmutableMap.Builder<Map.Entry<String, String>,
Map.Entry<String, BlobStore>> locators =
ImmutableMap.builder();
Set<Map.Entry<String, String>> parsedLocations = new HashSet<>();
for (File propertiesFile : options.propertiesFiles) {
Properties properties = new Properties();
try (InputStream is = new FileInputStream(propertiesFile)) {
Expand All @@ -117,14 +121,43 @@ public static void main(String[] args) throws Exception {

String s3ProxyAuthorizationString = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
ImmutableList.Builder<String> locatorBuckets =
new ImmutableList.Builder<>();
for (String key : properties.stringPropertyNames()) {
if (key.startsWith(S3ProxyConstants.PROPERTY_BUCKET_LOCATOR)) {
locatorBuckets.add(properties.getProperty(key));
}
}
ImmutableList<String> buckets = locatorBuckets.build();
String localIdentity = null;
String localCredential = null;
if (AuthenticationType.fromString(s3ProxyAuthorizationString) !=
AuthenticationType.NONE) {
String localIdentity = properties.getProperty(
localIdentity = properties.getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
String localCredential = properties.getProperty(
localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
locators.put(localIdentity, Maps.immutableEntry(
localCredential, blobStore));
Map.Entry<String, String> key = Maps.immutableEntry(
localIdentity, null);
if (!parsedLocations.contains(key)) {
locators.put(key, Maps.immutableEntry(
localCredential, blobStore));
parsedLocations.add(key);
}
}
if (!buckets.isEmpty()) {
for (String bucket : buckets) {
Map.Entry<String, String> key = Maps.immutableEntry(
localIdentity, bucket);
if (!parsedLocations.add(key)) {
System.err.printf("The same bucket locator cannot be" +
" used in two properties files: %s\n",
bucket);
System.exit(1);
}
locators.put(key,
Maps.immutableEntry(localCredential, blobStore));
}
}

S3Proxy.Builder s3ProxyBuilder2 = S3Proxy.Builder
Expand All @@ -149,23 +182,22 @@ public static void main(String[] args) throws Exception {
throw e;
}

final Map<String, Map.Entry<String, BlobStore>> locator =
locators.build();
final Map<Map.Entry<String, String>, Map.Entry<String, BlobStore>>
locator = locators.build();
if (!locator.isEmpty()) {
s3Proxy.setBlobStoreLocator(new BlobStoreLocator() {
@Override
public Map.Entry<String, BlobStore> locateBlobStore(
String identity, String container, String blob) {
Map.Entry<String, BlobStore> entry = locator.get(
Maps.immutableEntry(identity, container));
if (entry != null) {
return entry;
}
if (identity == null) {
if (locator.size() == 1) {
return locator.entrySet().iterator().next()
.getValue();
}
throw new IllegalArgumentException(
"cannot use anonymous access with multiple" +
" backends");
return locator.entrySet().iterator().next().getValue();
}
return locator.get(identity);
return locator.get(Maps.immutableEntry(identity, null));
}
});
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/gaul/s3proxy/S3ProxyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public final class S3ProxyConstants {
"s3proxy.max-single-part-object-size";
public static final String PROPERTY_V4_MAX_NON_CHUNKED_REQUEST_SIZE =
"s3proxy.v4-max-non-chunked-request-size";
/** Used to locate blobstores by specified bucket names. Each property
* file should contain a list of buckets associated with it, e.g.
* s3proxy.bucket-locator.1 = data
* s3proxy.bucket-locator.2 = metadata
* s3proxy.bucket-locator.3 = other
* When a request is made for the specified bucket, the backend defined
* in that properties file is used. This allows using the same
* credentials in multiple properties file and select the backend based
* on the bucket names.
*/
public static final String PROPERTY_BUCKET_LOCATOR =
"s3proxy.bucket-locator";
/** When true, model eventual consistency using two storage backends. */
public static final String PROPERTY_EVENTUAL_CONSISTENCY =
"s3proxy.eventual-consistency";
Expand Down

0 comments on commit 3fce2d3

Please sign in to comment.