-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathMain.java
More file actions
479 lines (431 loc) · 20.2 KB
/
Main.java
File metadata and controls
479 lines (431 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright 2014-2026 Andrew Gaul <andrew@gaul.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gaul.s3proxy;
import java.io.Console;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
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.TimeUnit;
import java.util.regex.Pattern;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSSessionCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.MoreFiles;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.gaul.modernizer_maven_annotations.SuppressModernizer;
import org.jclouds.Constants;
import org.jclouds.ContextBuilder;
import org.jclouds.JcloudsVersion;
import org.jclouds.aws.domain.SessionCredentials;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.concurrent.DynamicExecutors;
import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.domain.Credentials;
import org.jclouds.location.reference.LocationConstants;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext;
import org.jclouds.s3.domain.ObjectMetadata.StorageClass;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private Main() {
throw new AssertionError("intentionally not implemented");
}
private static final class Options {
@Option(name = "--properties",
usage = "S3Proxy configuration (required, multiple allowed)")
private List<Path> properties = new ArrayList<>();
@Option(name = "--version", usage = "display version")
private boolean version;
}
@SuppressWarnings("EqualsIncompatibleType")
public static void main(String[] args) throws Exception {
Console console = System.console();
if (console == null) {
System.setErr(createLoggerErrorPrintStream());
}
var options = new Options();
var parser = new CmdLineParser(options);
try {
parser.parseArgument(args);
} catch (CmdLineException cle) {
usage(parser);
}
if (options.version) {
System.err.println(
Main.class.getPackage().getImplementationVersion());
System.exit(0);
} else if (options.properties.isEmpty()) {
usage(parser);
}
S3Proxy.Builder s3ProxyBuilder = null;
var factory = new ThreadFactoryBuilder()
.setNameFormat("user thread %d")
.setThreadFactory(Executors.defaultThreadFactory())
.build();
ExecutorService executorService = DynamicExecutors.newScalingThreadPool(
1, 20, 60 * 1000, factory);
var locators = ImmutableMap
.<String, Map.Entry<String, BlobStore>>builder();
var globLocators = ImmutableMap
.<PathMatcher, Map.Entry<String, BlobStore>>builder();
Set<String> locatorGlobs = new HashSet<>();
Set<String> parsedIdentities = new HashSet<>();
for (var path : options.properties) {
var properties = new Properties();
try (var is = Files.newInputStream(path)) {
properties.load(is);
}
properties.putAll(System.getProperties());
BlobStore blobStore = createBlobStore(properties, executorService);
var blobStoreType = blobStore.getContext().unwrap().getProviderMetadata().getId();
if (blobStoreType.equals("aws-s3")) {
System.err.println("WARNING: aws-s3 storage backend deprecated -- please use aws-s3-sdk instead");
} else if (blobStoreType.equals("azureblob")) {
System.err.println("WARNING: azureblob storage backend deprecated -- please use azureblob-sdk instead");
} else if (blobStoreType.equals("filesystem")) {
System.err.println("WARNING: filesystem storage backend deprecated -- please use filesystem-nio2 instead");
} else if (blobStoreType.equals("s3")) {
System.err.println("WARNING: s3 storage backend deprecated -- please use aws-s3-sdk instead");
} else if (blobStoreType.equals("transient")) {
System.err.println("WARNING: transient storage backend deprecated -- please use transient-nio2 instead");
}
blobStore = parseMiddlewareProperties(blobStore, executorService,
properties);
String s3ProxyAuthorizationString = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
String localIdentity = null;
if (AuthenticationType.fromString(s3ProxyAuthorizationString) !=
AuthenticationType.NONE) {
localIdentity = properties.getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
String localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
if (parsedIdentities.add(localIdentity)) {
locators.put(localIdentity,
Map.entry(localCredential, blobStore));
}
}
for (String key : properties.stringPropertyNames()) {
if (key.startsWith(S3ProxyConstants.PROPERTY_BUCKET_LOCATOR)) {
String bucketLocator = properties.getProperty(key);
if (locatorGlobs.add(bucketLocator)) {
globLocators.put(
FileSystems.getDefault().getPathMatcher(
"glob:" + bucketLocator),
Maps.immutableEntry(localIdentity, blobStore));
} else {
System.err.println("Multiple definitions of the " +
"bucket locator: " + bucketLocator);
System.exit(1);
}
}
}
S3Proxy.Builder s3ProxyBuilder2 = S3Proxy.Builder
.fromProperties(properties)
.blobStore(blobStore);
if (s3ProxyBuilder != null &&
!s3ProxyBuilder.equals(s3ProxyBuilder2)) {
System.err.println("Multiple configurations require" +
" identical s3proxy properties");
System.exit(1);
}
s3ProxyBuilder = s3ProxyBuilder2;
}
S3Proxy s3Proxy;
try {
s3Proxy = s3ProxyBuilder.build();
} catch (IllegalArgumentException | IllegalStateException e) {
System.err.println(e.getMessage());
System.exit(1);
throw e;
}
var locator = locators.build();
var globLocator = globLocators.build();
if (!locator.isEmpty() || !globLocator.isEmpty()) {
s3Proxy.setBlobStoreLocator(
new GlobBlobStoreLocator(locator, globLocator));
}
try {
s3Proxy.start();
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
private static BlobStore parseMiddlewareProperties(BlobStore blobStore,
ExecutorService executorService, Properties properties)
throws IOException {
var altProperties = new Properties();
for (var entry : properties.entrySet()) {
String key = (String) entry.getKey();
if (key.startsWith(S3ProxyConstants.PROPERTY_ALT_JCLOUDS_PREFIX)) {
key = key.substring(
S3ProxyConstants.PROPERTY_ALT_JCLOUDS_PREFIX.length());
altProperties.put(key, (String) entry.getValue());
}
}
String eventualConsistency = properties.getProperty(
S3ProxyConstants.PROPERTY_EVENTUAL_CONSISTENCY);
if ("true".equalsIgnoreCase(eventualConsistency)) {
BlobStore altBlobStore = createBlobStore(altProperties,
executorService);
int delay = Integer.parseInt(properties.getProperty(
S3ProxyConstants.PROPERTY_EVENTUAL_CONSISTENCY_DELAY,
"5"));
double probability = Double.parseDouble(properties.getProperty(
S3ProxyConstants.PROPERTY_EVENTUAL_CONSISTENCY_PROBABILITY,
"1.0"));
System.err.println("Emulating eventual consistency with delay " +
delay + " seconds and probability " + (probability * 100) +
"%");
blobStore = EventualBlobStore.newEventualBlobStore(
blobStore, altBlobStore,
Executors.newScheduledThreadPool(1),
delay, TimeUnit.SECONDS, probability);
}
String nullBlobStore = properties.getProperty(
S3ProxyConstants.PROPERTY_NULL_BLOBSTORE);
if ("true".equalsIgnoreCase(nullBlobStore)) {
System.err.println("Using null storage backend");
blobStore = NullBlobStore.newNullBlobStore(blobStore);
}
String readOnlyBlobStore = properties.getProperty(
S3ProxyConstants.PROPERTY_READ_ONLY_BLOBSTORE);
if ("true".equalsIgnoreCase(readOnlyBlobStore)) {
System.err.println("Using read-only storage backend");
blobStore = ReadOnlyBlobStore.newReadOnlyBlobStore(blobStore);
}
ImmutableBiMap<String, String> aliases = AliasBlobStore.parseAliases(
properties);
if (!aliases.isEmpty()) {
System.err.println("Using alias backend");
blobStore = AliasBlobStore.newAliasBlobStore(blobStore, aliases);
}
Map<String, String> prefixMap = PrefixBlobStore.parsePrefixes(properties);
if (!prefixMap.isEmpty()) {
System.err.println("Using prefix backend");
blobStore = PrefixBlobStore.newPrefixBlobStore(blobStore,
prefixMap);
}
List<Map.Entry<Pattern, String>> regexs =
RegexBlobStore.parseRegexs(properties);
if (!regexs.isEmpty()) {
System.err.println("Using regex backend");
blobStore = RegexBlobStore.newRegexBlobStore(blobStore, regexs);
}
Map<String, Integer> shards =
ShardedBlobStore.parseBucketShards(properties);
Map<String, String> prefixes =
ShardedBlobStore.parsePrefixes(properties);
if (!shards.isEmpty()) {
System.err.println("Using sharded buckets backend");
blobStore = ShardedBlobStore.newShardedBlobStore(blobStore,
shards, prefixes);
}
String encryptedBlobStore = properties.getProperty(
S3ProxyConstants.PROPERTY_ENCRYPTED_BLOBSTORE);
if ("true".equalsIgnoreCase(encryptedBlobStore)) {
System.err.println("Using encrypted storage backend");
blobStore = EncryptedBlobStore.newEncryptedBlobStore(blobStore,
properties);
}
var storageClass = properties.getProperty(
S3ProxyConstants.PROPERTY_STORAGE_CLASS_BLOBSTORE);
if (!Strings.isNullOrEmpty(storageClass)) {
System.err.println("Using storage class override backend");
var storageClassBlobStore =
StorageClassBlobStore.newStorageClassBlobStore(
blobStore, storageClass);
blobStore = storageClassBlobStore;
System.err.println("Configuration storage class: " + storageClass);
// TODO: This only makes sense for S3 backends.
System.err.println("Mapping storage storage class to: " +
StorageClass.fromTier(storageClassBlobStore.getTier()));
}
String userMetadataReplacerBlobStore = properties.getProperty(
S3ProxyConstants.PROPERTY_USER_METADATA_REPLACER);
if ("true".equalsIgnoreCase(userMetadataReplacerBlobStore)) {
System.err.println("Using user metadata replacers storage backend");
String fromChars = properties.getProperty(S3ProxyConstants
.PROPERTY_USER_METADATA_REPLACER_FROM_CHARS);
String toChars = properties.getProperty(S3ProxyConstants
.PROPERTY_USER_METADATA_REPLACER_TO_CHARS);
blobStore = UserMetadataReplacerBlobStore
.newUserMetadataReplacerBlobStore(
blobStore, fromChars, toChars);
}
Map<String, Long> latencies = LatencyBlobStore.parseLatencies(properties);
Map<String, Long> speeds = LatencyBlobStore.parseSpeeds(properties);
if (!latencies.isEmpty() || !speeds.isEmpty()) {
System.err.println("Using latency storage backend");
blobStore = LatencyBlobStore.newLatencyBlobStore(blobStore, latencies, speeds);
}
String noCacheBlobStore = properties.getProperty(
S3ProxyConstants.PROPERTY_NO_CACHE_BLOBSTORE);
if ("true".equalsIgnoreCase(noCacheBlobStore)) {
System.err.println("Using no-cache storage backend middleware");
blobStore = NoCacheBlobStore
.newNoCacheBlobStore(blobStore);
}
return blobStore;
}
private static PrintStream createLoggerErrorPrintStream() {
return new PrintStream(System.err) {
private final StringBuilder builder = new StringBuilder();
@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
"SLF4J_SIGN_ONLY_FORMAT")
public void print(final String string) {
logger.error("{}", string);
}
@Override
public void write(byte[] buf, int off, int len) {
for (int i = off; i < len; ++i) {
char ch = (char) buf[i];
if (ch == '\n') {
if (builder.length() != 0) {
print(builder.toString());
builder.setLength(0);
}
} else {
builder.append(ch);
}
}
}
};
}
private static BlobStore createBlobStore(Properties properties,
ExecutorService executorService) throws IOException {
String provider = properties.getProperty(Constants.PROPERTY_PROVIDER);
String identity = properties.getProperty(Constants.PROPERTY_IDENTITY);
String credential = properties.getProperty(
Constants.PROPERTY_CREDENTIAL);
String endpoint = properties.getProperty(Constants.PROPERTY_ENDPOINT);
properties.remove(Constants.PROPERTY_ENDPOINT);
String region = properties.getProperty(
LocationConstants.PROPERTY_REGION);
if (provider == null) {
System.err.println(
"Properties file must contain: " +
Constants.PROPERTY_PROVIDER);
System.exit(1);
}
if (provider.equals("filesystem") ||
provider.equals("filesystem-nio2") ||
provider.equals("transient") ||
provider.equals("transient-nio2")) {
// local blobstores do not require credentials
identity = Strings.nullToEmpty(identity);
credential = Strings.nullToEmpty(credential);
} else if (provider.equals("google-cloud-storage")) {
var path = FileSystems.getDefault().getPath(credential);
if (Files.exists(path)) {
credential = MoreFiles.asCharSource(path,
StandardCharsets.UTF_8).read();
}
properties.remove(Constants.PROPERTY_CREDENTIAL);
// We also need to clear the system property, otherwise the
// credential will be overridden by the system property.
System.clearProperty(Constants.PROPERTY_CREDENTIAL);
}
if (identity == null || credential == null) {
System.err.println(
"Properties file must contain: " +
Constants.PROPERTY_IDENTITY + " and " +
Constants.PROPERTY_CREDENTIAL);
System.exit(1);
}
properties.setProperty(Constants.PROPERTY_USER_AGENT,
String.format("s3proxy/%s jclouds/%s java/%s",
Main.class.getPackage().getImplementationVersion(),
JcloudsVersion.get(),
System.getProperty("java.version")));
ContextBuilder builder = ContextBuilder
.newBuilder(provider)
.modules(List.of(
new SLF4JLoggingModule(),
new ExecutorServiceModule(executorService)))
.overrides(properties);
if (!Strings.isNullOrEmpty(endpoint)) {
builder = builder.endpoint(endpoint);
}
if ((identity.isEmpty() || credential.isEmpty()) && provider.equals("aws-s3")) {
@SuppressModernizer
Supplier<Credentials> credentialsSupplier = new Supplier<Credentials>() {
@Override
public Credentials get() {
AWSCredentialsProvider authChain = DefaultAWSCredentialsProviderChain.getInstance();
AWSCredentials newCreds = authChain.getCredentials();
Credentials jcloudsCred = null;
if (newCreds instanceof AWSSessionCredentials) {
jcloudsCred = SessionCredentials.builder()
.accessKeyId(newCreds.getAWSAccessKeyId())
.secretAccessKey(newCreds.getAWSSecretKey())
.sessionToken(((AWSSessionCredentials) newCreds).getSessionToken())
.build();
} else {
jcloudsCred = new Credentials(
newCreds.getAWSAccessKeyId(), newCreds.getAWSSecretKey()
);
}
return jcloudsCred;
}
};
builder = builder.credentialsSupplier(credentialsSupplier);
} else {
builder = builder.credentials(identity, credential);
}
BlobStoreContext context = builder.build(BlobStoreContext.class);
BlobStore blobStore;
if (context instanceof RegionScopedBlobStoreContext &&
region != null) {
blobStore = ((RegionScopedBlobStoreContext) context)
.getBlobStore(region);
} else {
blobStore = context.getBlobStore();
}
return blobStore;
}
private static void usage(CmdLineParser parser) {
System.err.println("Usage: s3proxy [options...]");
parser.printUsage(System.err);
System.exit(1);
}
}