Skip to content

Commit 2057682

Browse files
committed
Merge branch 'feature/per-repo-endpoint' of https://github.com/brutasse/elasticsearch-cloud-aws into brutasse-feature/per-repo-endpoint
2 parents fca3add + 30d80cc commit 2057682

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ The following settings are supported:
163163

164164
* `bucket`: The name of the bucket to be used for snapshots. (Mandatory)
165165
* `region`: The region where bucket is located. Defaults to US Standard
166+
* `endpoint`: The endpoint to the S3 API. Defaults to AWS's default S3 endpoint. Note that setting a region overrides the endpoint setting.
167+
* `protocol`: The protocol to use (`http` or `https`). Defaults to `https`.
166168
* `base_path`: Specifies the path within bucket to repository data. Defaults to root directory.
167169
* `access_key`: The access key to use for authentication. Defaults to value of `cloud.aws.access_key`.
168170
* `secret_key`: The secret key to use for authentication. Defaults to value of `cloud.aws.secret_key`.
@@ -289,10 +291,16 @@ repositories:
289291
remote-bucket:
290292
bucket: <bucket in other region>
291293
region: <region>
294+
external-bucket:
295+
bucket: <bucket>
296+
access_key: <access key>
297+
secret_key: <secret key>
298+
endpoint: <endpoint>
299+
protocol: <protocol>
292300
293301
```
294302

295-
Replace all occurrences of `access_key`, `secret_key`, `bucket` and `region` with your settings. Please, note that the test will delete all snapshot/restore related files in the specified buckets.
303+
Replace all occurrences of `access_key`, `secret_key`, `endpoint`, `protocol`, `bucket` and `region` with your settings. Please, note that the test will delete all snapshot/restore related files in the specified buckets.
296304

297305
To run test:
298306

src/main/java/org/elasticsearch/cloud/aws/AwsS3Service.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public interface AwsS3Service extends LifecycleComponent<AwsS3Service> {
2929
AmazonS3 client();
3030

31-
AmazonS3 client(String region, String account, String key);
31+
AmazonS3 client(String endpoint, String protocol, String region, String account, String key);
3232

33-
AmazonS3 client(String region, String account, String key, Integer maxRetries);
33+
AmazonS3 client(String endpoint, String protocol, String region, String account, String key, Integer maxRetries);
3434
}

src/main/java/org/elasticsearch/cloud/aws/InternalAwsS3Service.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,43 @@ public synchronized AmazonS3 client() {
6060
String account = componentSettings.get("access_key", settings.get("cloud.account"));
6161
String key = componentSettings.get("secret_key", settings.get("cloud.key"));
6262

63-
return getClient(endpoint, account, key, null);
63+
return getClient(endpoint, "https", account, key, null);
6464
}
6565

6666
@Override
67-
public AmazonS3 client(String region, String account, String key) {
68-
return client(region, account, key, null);
67+
public AmazonS3 client(String endpoint, String protocol, String region, String account, String key) {
68+
return client(endpoint, protocol, region, account, key, null);
6969
}
7070

7171
@Override
72-
public synchronized AmazonS3 client(String region, String account, String key, Integer maxRetries) {
73-
String endpoint;
74-
if (region == null) {
75-
endpoint = getDefaultEndpoint();
76-
} else {
72+
public synchronized AmazonS3 client(String endpoint, String protocol, String region, String account, String key, Integer maxRetries) {
73+
if (region != null && endpoint == null) {
7774
endpoint = getEndpoint(region);
7875
logger.debug("using s3 region [{}], with endpoint [{}]", region, endpoint);
76+
} else if (endpoint == null) {
77+
endpoint = getDefaultEndpoint();
7978
}
8079
if (account == null || key == null) {
8180
account = componentSettings.get("access_key", settings.get("cloud.account"));
8281
key = componentSettings.get("secret_key", settings.get("cloud.key"));
8382
}
8483

85-
return getClient(endpoint, account, key, maxRetries);
84+
return getClient(endpoint, protocol, account, key, maxRetries);
8685
}
8786

8887

89-
private synchronized AmazonS3 getClient(String endpoint, String account, String key, Integer maxRetries) {
88+
private synchronized AmazonS3 getClient(String endpoint, String protocol, String account, String key, Integer maxRetries) {
9089
Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
9190
AmazonS3Client client = clients.get(clientDescriptor);
9291
if (client != null) {
9392
return client;
9493
}
9594

9695
ClientConfiguration clientConfiguration = new ClientConfiguration();
97-
String protocol = componentSettings.get("protocol", "https").toLowerCase();
98-
protocol = componentSettings.get("s3.protocol", protocol).toLowerCase();
96+
if (protocol == null) {
97+
protocol = "https";
98+
}
99+
99100
if ("http".equals(protocol)) {
100101
clientConfiguration.setProtocol(Protocol.HTTP);
101102
} else if ("https".equals(protocol)) {

src/main/java/org/elasticsearch/repositories/s3/S3Repository.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public S3Repository(RepositoryName name, RepositorySettings repositorySettings,
7979
throw new RepositoryException(name.name(), "No bucket defined for s3 gateway");
8080
}
8181

82+
String endpoint = repositorySettings.settings().get("endpoint", componentSettings.get("endpoint"));
83+
String protocol = componentSettings.get("protocol", "https").toLowerCase();
84+
protocol = componentSettings.get("s3.protocol", protocol).toLowerCase();
85+
protocol = repositorySettings.settings().get("protocol", protocol);
86+
8287
String region = repositorySettings.settings().get("region", componentSettings.get("region"));
8388
if (region == null) {
8489
// Bucket setting is not set - use global region setting
@@ -124,10 +129,10 @@ public S3Repository(RepositoryName name, RepositorySettings repositorySettings,
124129
this.chunkSize = repositorySettings.settings().getAsBytesSize("chunk_size", componentSettings.getAsBytesSize("chunk_size", new ByteSizeValue(100, ByteSizeUnit.MB)));
125130
this.compress = repositorySettings.settings().getAsBoolean("compress", componentSettings.getAsBoolean("compress", false));
126131

127-
logger.debug("using bucket [{}], region [{}], chunk_size [{}], server_side_encryption [{}], buffer_size [{}], max_retries [{}]",
128-
bucket, region, chunkSize, serverSideEncryption, bufferSize, maxRetries);
132+
logger.debug("using bucket [{}], region [{}], endpoint [{}], protocol [{}], chunk_size [{}], server_side_encryption [{}], buffer_size [{}], max_retries [{}]",
133+
bucket, region, endpoint, protocol, chunkSize, serverSideEncryption, bufferSize, maxRetries);
129134

130-
blobStore = new S3BlobStore(settings, s3Service.client(region, repositorySettings.settings().get("access_key"), repositorySettings.settings().get("secret_key"), maxRetries), bucket, region, serverSideEncryption, bufferSize, maxRetries);
135+
blobStore = new S3BlobStore(settings, s3Service.client(endpoint, protocol, region, repositorySettings.settings().get("access_key"), repositorySettings.settings().get("secret_key"), maxRetries), bucket, region, serverSideEncryption, bufferSize, maxRetries);
131136
String basePath = repositorySettings.settings().get("base_path", null);
132137
if (Strings.hasLength(basePath)) {
133138
BlobPath path = new BlobPath();

src/test/java/org/elasticsearch/cloud/aws/TestAwsS3Service.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public synchronized AmazonS3 client() {
4545
}
4646

4747
@Override
48-
public synchronized AmazonS3 client(String region, String account, String key) {
49-
return cachedWrapper(super.client(region, account, key));
48+
public synchronized AmazonS3 client(String endpoint, String protocol, String region, String account, String key) {
49+
return cachedWrapper(super.client(endpoint, protocol, region, account, key));
5050
}
5151

5252
@Override
53-
public synchronized AmazonS3 client(String region, String account, String key, Integer maxRetries) {
54-
return cachedWrapper(super.client(region, account, key, maxRetries));
53+
public synchronized AmazonS3 client(String endpoint, String protocol, String region, String account, String key, Integer maxRetries) {
54+
return cachedWrapper(super.client(endpoint, protocol, region, account, key, maxRetries));
5555
}
5656

5757
private AmazonS3 cachedWrapper(AmazonS3 client) {

src/test/java/org/elasticsearch/repositories/s3/AbstractS3SnapshotRestoreTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public void testEncryption() {
192192
Settings settings = internalCluster().getInstance(Settings.class);
193193
Settings bucket = settings.getByPrefix("repositories.s3.");
194194
AmazonS3 s3Client = internalCluster().getInstance(AwsS3Service.class).client(
195+
bucket.get("endpoint", settings.get("repositories.s3.endpoint")),
196+
bucket.get("protocol", settings.get("repositories.s3.protocol")),
195197
bucket.get("region", settings.get("repositories.s3.region")),
196198
bucket.get("access_key", settings.get("cloud.aws.access_key")),
197199
bucket.get("secret_key", settings.get("cloud.aws.secret_key")));
@@ -278,6 +280,22 @@ public void testRepositoryWithCustomCredentials() {
278280
assertRepositoryIsOperational(client, "test-repo");
279281
}
280282

283+
@Test
284+
public void testRepositoryWithCustomEndpointProtocol() {
285+
Client client = client();
286+
Settings bucketSettings = internalCluster().getInstance(Settings.class).getByPrefix("repositories.s3.external-bucket.");
287+
logger.info("--> creating s3 repostoriy with endpoint [{}], bucket[{}] and path [{}]", bucketSettings.get("endpoint"), bucketSettings.get("bucket"), basePath);
288+
PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo")
289+
.setType("s3").setSettings(ImmutableSettings.settingsBuilder()
290+
.put("protocol", bucketSettings.get("protocol"))
291+
.put("endpoint", bucketSettings.get("endpoint"))
292+
.put("access_key", bucketSettings.get("access_key"))
293+
.put("secret_key", bucketSettings.get("secret_key"))
294+
).get();
295+
assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
296+
assertRepositoryIsOperational(client, "test-repo");
297+
}
298+
281299
/**
282300
* This test verifies that the test configuration is set up in a manner that
283301
* does not make the test {@link #testRepositoryInRemoteRegion()} pointless.
@@ -430,6 +448,8 @@ public void cleanRepositoryFiles(String basePath) {
430448
settings.getByPrefix("repositories.s3.remote-bucket.")
431449
};
432450
for (Settings bucket : buckets) {
451+
String endpoint = bucket.get("endpoint", settings.get("repositories.s3.endpoint"));
452+
String protocol = bucket.get("protocol", settings.get("repositories.s3.protocol"));
433453
String region = bucket.get("region", settings.get("repositories.s3.region"));
434454
String accessKey = bucket.get("access_key", settings.get("cloud.aws.access_key"));
435455
String secretKey = bucket.get("secret_key", settings.get("cloud.aws.secret_key"));
@@ -438,7 +458,7 @@ public void cleanRepositoryFiles(String basePath) {
438458
// We check that settings has been set in elasticsearch.yml integration test file
439459
// as described in README
440460
assertThat("Your settings in elasticsearch.yml are incorrects. Check README file.", bucketName, notNullValue());
441-
AmazonS3 client = internalCluster().getInstance(AwsS3Service.class).client(region, accessKey, secretKey);
461+
AmazonS3 client = internalCluster().getInstance(AwsS3Service.class).client(endpoint, protocol, region, accessKey, secretKey);
442462
try {
443463
ObjectListing prevListing = null;
444464
//From http://docs.amazonwebservices.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html

0 commit comments

Comments
 (0)