Skip to content

Commit 3977694

Browse files
committed
Add EC2/S3 Signer API setting
If you are using a compatible EC2 or S3 service, they might be using an older API to sign the requests. You can set your compatible signer API using `cloud.aws.signer` (or `cloud.aws.ec2.signer` and `cloud.aws.s3.signer`) with the right signer to use. Defaults to `AWS4SignerType`. Supported today (time when this commit is done): * `QueryStringSignerType` * `AWS3SignerType` * `AWS4SignerType` * `NoOpSignerType` Closes #155. (cherry picked from commit 33b18b4) (cherry picked from commit 9809af5)
1 parent a723875 commit 3977694

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ The `cloud.aws.region` can be set to a region and will automatically use the rel
101101
* `sa-east` (`sa-east-1`)
102102
* `cn-north` (`cn-north-1`)
103103

104+
105+
### EC2/S3 Signer API
106+
107+
If you are using a compatible EC2 or S3 service, they might be using an older API to sign the requests.
108+
You can set your compatible signer API using `cloud.aws.signer` (or `cloud.aws.ec2.signer` and `cloud.aws.s3.signer`)
109+
with the right signer to use. Defaults to `AWS4SignerType`.
110+
111+
104112
## EC2 Discovery
105113

106114
ec2 discovery allows to use the ec2 APIs to perform automatic discovery (similar to multicast in non hostile multicast environments). Here is a simple sample configuration:

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public synchronized AmazonEC2 client() {
8989
clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
9090
}
9191

92+
// #155: we might have 3rd party users using older EC2 API version
93+
String awsSigner = settings.get("cloud.aws.ec2.signer", settings.get("cloud.aws.signer"));
94+
if (awsSigner != null) {
95+
logger.debug("using AWS API signer [{}]", awsSigner);
96+
try {
97+
AwsSigner.configureSigner(awsSigner, clientConfiguration);
98+
} catch (ElasticsearchIllegalArgumentException e) {
99+
logger.warn("wrong signer set for [cloud.aws.ec2.signer] or [cloud.aws.signer]: [{}]", awsSigner);
100+
}
101+
}
102+
92103
AWSCredentialsProvider credentials;
93104

94105
if (account == null && key == null) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
20+
package org.elasticsearch.cloud.aws;
21+
22+
import com.amazonaws.ClientConfiguration;
23+
import com.amazonaws.auth.SignerFactory;
24+
import org.elasticsearch.ElasticsearchIllegalArgumentException;
25+
26+
public class AwsSigner {
27+
28+
private AwsSigner() {
29+
30+
}
31+
32+
/**
33+
* Add a AWS API Signer.
34+
* @param signer Signer to use
35+
* @param configuration AWS Client configuration
36+
* @throws ElasticsearchIllegalArgumentException if signer does not exist
37+
*/
38+
public static void configureSigner(String signer, ClientConfiguration configuration)
39+
throws ElasticsearchIllegalArgumentException {
40+
41+
if (signer == null) {
42+
throw new ElasticsearchIllegalArgumentException("[null] signer set");
43+
}
44+
45+
try {
46+
// We check this signer actually exists in AWS SDK
47+
// It throws a IllegalArgumentException if not found
48+
SignerFactory.getSignerByTypeAndService(signer, null);
49+
configuration.setSignerOverride(signer);
50+
} catch (IllegalArgumentException e) {
51+
throw new ElasticsearchIllegalArgumentException("wrong signer set [" + signer + "]");
52+
}
53+
}
54+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ private synchronized AmazonS3 getClient(String endpoint, String protocol, String
122122
clientConfiguration.setMaxErrorRetry(maxRetries);
123123
}
124124

125+
// #155: we might have 3rd party users using older S3 API version
126+
String awsSigner = settings.get("cloud.aws.s3.signer", settings.get("cloud.aws.signer"));
127+
if (awsSigner != null) {
128+
logger.debug("using AWS API signer [{}]", awsSigner);
129+
try {
130+
AwsSigner.configureSigner(awsSigner, clientConfiguration);
131+
} catch (ElasticsearchIllegalArgumentException e) {
132+
logger.warn("wrong signer set for [cloud.aws.s3.signer] or [cloud.aws.signer]: [{}]", awsSigner);
133+
}
134+
}
135+
125136
AWSCredentialsProvider credentials;
126137

127138
if (account == null && key == null) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
20+
package org.elasticsearch.cloud.aws;
21+
22+
import com.amazonaws.ClientConfiguration;
23+
import org.elasticsearch.ElasticsearchIllegalArgumentException;
24+
import org.elasticsearch.test.ElasticsearchTestCase;
25+
import org.junit.Test;
26+
27+
import static org.hamcrest.CoreMatchers.is;
28+
29+
public class AWSSignersTest extends ElasticsearchTestCase {
30+
31+
@Test
32+
public void testSigners() {
33+
assertThat(signerTester(null), is(false));
34+
assertThat(signerTester("QueryStringSignerType"), is(true));
35+
assertThat(signerTester("AWS3SignerType"), is(true));
36+
assertThat(signerTester("AWS4SignerType"), is(true));
37+
assertThat(signerTester("NoOpSignerType"), is(true));
38+
assertThat(signerTester("UndefinedSigner"), is(false));
39+
}
40+
41+
/**
42+
* Test a signer configuration
43+
* @param signer signer name
44+
* @return true if successful, false otherwise
45+
*/
46+
private boolean signerTester(String signer) {
47+
try {
48+
AwsSigner.configureSigner(signer, new ClientConfiguration());
49+
return true;
50+
} catch (ElasticsearchIllegalArgumentException e) {
51+
return false;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)