Skip to content

Commit 87f2c38

Browse files
Streamline S3 Repository- and Client-Settings (#37393) (#38010)
* Make repository settings override static settings * Cache clients according to settings * Introduce custom implementations for the AWS credentials here to be able to use them as part of a hash key
1 parent 0c7ba07 commit 87f2c38

File tree

12 files changed

+444
-136
lines changed

12 files changed

+444
-136
lines changed

docs/plugins/repository-s3.asciidoc

+26
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ The following settings are supported:
221221
currently supported by the plugin. For more information about the
222222
different classes, see http://docs.aws.amazon.com/AmazonS3/latest/dev/storage-class-intro.html[AWS Storage Classes Guide]
223223

224+
NOTE: The option of defining client settings in the repository settings as documented below is considered deprecated:
225+
226+
In addition to the above settings, you may also specify all non-secure client settings in the repository settings.
227+
In this case, the client settings found in the repository settings will be merged with those of the named client used by the repository.
228+
Conflicts between client and repository settings are resolved by the repository settings taking precedence over client settings.
229+
230+
For example:
231+
232+
[source,js]
233+
----
234+
PUT _snapshot/my_s3_repository
235+
{
236+
"type": "s3",
237+
"settings": {
238+
"client": "my_client_name",
239+
"bucket": "my_bucket_name",
240+
"endpoint": "my.s3.endpoint"
241+
}
242+
}
243+
----
244+
// CONSOLE
245+
// TEST[skip:we don't have s3 set up while testing this]
246+
247+
This sets up a repository that uses all client settings from the client `my_client_named` except for the `endpoint` that is overridden
248+
to `my.s3.endpoint` by the repository settings.
249+
224250
[[repository-s3-permissions]]
225251
===== Recommended S3 Permissions
226252

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.repositories.s3;
20+
21+
import com.amazonaws.auth.AWSCredentials;
22+
23+
import java.util.Objects;
24+
25+
class S3BasicCredentials implements AWSCredentials {
26+
27+
private final String accessKey;
28+
29+
private final String secretKey;
30+
31+
S3BasicCredentials(String accessKey, String secretKey) {
32+
this.accessKey = accessKey;
33+
this.secretKey = secretKey;
34+
}
35+
36+
@Override
37+
public final String getAWSAccessKeyId() {
38+
return accessKey;
39+
}
40+
41+
@Override
42+
public final String getAWSSecretKey() {
43+
return secretKey;
44+
}
45+
46+
@Override
47+
public boolean equals(final Object o) {
48+
if (this == o) {
49+
return true;
50+
}
51+
if (o == null || getClass() != o.getClass()) {
52+
return false;
53+
}
54+
final S3BasicCredentials that = (S3BasicCredentials) o;
55+
return accessKey.equals(that.accessKey) && secretKey.equals(that.secretKey);
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hash(accessKey, secretKey);
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.repositories.s3;
20+
21+
import com.amazonaws.auth.AWSSessionCredentials;
22+
23+
import java.util.Objects;
24+
25+
final class S3BasicSessionCredentials extends S3BasicCredentials implements AWSSessionCredentials {
26+
27+
private final String sessionToken;
28+
29+
S3BasicSessionCredentials(String accessKey, String secretKey, String sessionToken) {
30+
super(accessKey, secretKey);
31+
this.sessionToken = sessionToken;
32+
}
33+
34+
@Override
35+
public String getSessionToken() {
36+
return sessionToken;
37+
}
38+
39+
@Override
40+
public boolean equals(final Object o) {
41+
if (this == o) {
42+
return true;
43+
}
44+
if (o == null || getClass() != o.getClass()) {
45+
return false;
46+
}
47+
final S3BasicSessionCredentials that = (S3BasicSessionCredentials) o;
48+
return sessionToken.equals(that.sessionToken) &&
49+
getAWSAccessKeyId().equals(that.getAWSAccessKeyId()) &&
50+
getAWSSecretKey().equals(that.getAWSSecretKey());
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(sessionToken, getAWSAccessKeyId(), getAWSSecretKey());
56+
}
57+
}

plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.amazonaws.services.s3.model.ObjectListing;
2626
import com.amazonaws.services.s3.model.S3ObjectSummary;
2727
import com.amazonaws.services.s3.model.StorageClass;
28+
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
2829
import org.elasticsearch.common.blobstore.BlobContainer;
2930
import org.elasticsearch.common.blobstore.BlobPath;
3031
import org.elasticsearch.common.blobstore.BlobStore;
@@ -40,8 +41,6 @@ class S3BlobStore extends AbstractComponent implements BlobStore {
4041

4142
private final S3Service service;
4243

43-
private final String clientName;
44-
4544
private final String bucket;
4645

4746
private final ByteSizeValue bufferSize;
@@ -52,15 +51,18 @@ class S3BlobStore extends AbstractComponent implements BlobStore {
5251

5352
private final StorageClass storageClass;
5453

55-
S3BlobStore(S3Service service, String clientName, String bucket, boolean serverSideEncryption,
56-
ByteSizeValue bufferSize, String cannedACL, String storageClass) {
54+
private final RepositoryMetaData repositoryMetaData;
55+
56+
S3BlobStore(S3Service service, String bucket, boolean serverSideEncryption,
57+
ByteSizeValue bufferSize, String cannedACL, String storageClass,
58+
RepositoryMetaData repositoryMetaData) {
5759
this.service = service;
58-
this.clientName = clientName;
5960
this.bucket = bucket;
6061
this.serverSideEncryption = serverSideEncryption;
6162
this.bufferSize = bufferSize;
6263
this.cannedACL = initCannedACL(cannedACL);
6364
this.storageClass = initStorageClass(storageClass);
65+
this.repositoryMetaData = repositoryMetaData;
6466
}
6567

6668
@Override
@@ -69,7 +71,7 @@ public String toString() {
6971
}
7072

7173
public AmazonS3Reference clientReference() {
72-
return service.client(clientName);
74+
return service.client(repositoryMetaData);
7375
}
7476

7577
public String bucket() {

0 commit comments

Comments
 (0)