Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS session tokens #30414

Merged
merged 32 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2ccaeaa
Add support for AWS session tokens
DaveCTurner May 5, 2018
0fbe039
Add PR number to changelog
DaveCTurner May 6, 2018
0299a75
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner May 22, 2018
c93e568
discovery-ec2 docs shouldn't refer to S3
DaveCTurner May 22, 2018
ee41de6
Reduce scope of expectThrows
DaveCTurner May 22, 2018
ed62728
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner Jun 7, 2018
85737f6
Support injection of session token from the environment
DaveCTurner Jun 7, 2018
2eea829
Extend QA tests to test using both temporary and permanent credentials
DaveCTurner Jun 7, 2018
d431f28
Imports
DaveCTurner Jun 7, 2018
ff9f8a1
Merge commit 'afc91e92fb697a3241b26a5bb7cdc0a3769f4085^' into 2018-05…
DaveCTurner Jun 25, 2018
2602b32
Merge commit 'afc91e92fb697a3241b26a5bb7cdc0a3769f4085' into 2018-05-…
DaveCTurner Jun 25, 2018
9fd5d44
Merge commit '3378240b2954fcf879ac84f440002685c4b30cc9^' into 2018-05…
DaveCTurner Jun 25, 2018
9db4a59
Merge commit '3378240b2954fcf879ac84f440002685c4b30cc9' into 2018-05-…
DaveCTurner Jun 25, 2018
c1e0a97
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner Jun 25, 2018
552e852
Add support for serving two buckets
DaveCTurner Jun 25, 2018
e583a0c
Reinstate verification of credentials against the appropriate bucket
DaveCTurner Jun 25, 2018
87b6c58
Update docs
DaveCTurner Jun 25, 2018
7eabb56
Add discovery-ec2 session token setting
DaveCTurner Jun 25, 2018
6915c47
Use session token in discovery-ec2 if provided
DaveCTurner Jun 25, 2018
1475a26
Add assertions to Ec2DiscoveryPluginTests too
DaveCTurner Jun 25, 2018
439b763
Add support for session token to repository-s3 plugin
DaveCTurner Jun 25, 2018
2a93e94
Extend QA tests to test both permanent and temporary credentials
DaveCTurner Jun 25, 2018
60e7b83
Be pickier about auth verification in AmazonS3Fixture
DaveCTurner Jun 25, 2018
9de75c6
Resolve merge conflicts manually
DaveCTurner Jun 25, 2018
63caae2
Be more certain
DaveCTurner Jun 25, 2018
be7b51c
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner Jun 25, 2018
d6abddc
Less hard-coding
DaveCTurner Jun 25, 2018
aafb0aa
Tidy imports
DaveCTurner Jun 26, 2018
ff29696
Merge #31601
DaveCTurner Jul 2, 2018
86fa97a
Merge #31601 for temp creds too
DaveCTurner Jul 2, 2018
34ffc72
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner Jul 2, 2018
da791b3
Merge branch 'master' into 2018-05-06-aws-session-token
DaveCTurner Jul 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ option. ({pull}30140[#29658])
A new analysis plugin called `analysis_nori` that exposes the Lucene Korean
analysis module. ({pull}30397[#30397])

The `discovery-ec2` and `repository-s3` plugins now have support for AWS
session credentials. ({pull}30414[#30414])

[float]
=== Enhancements

Expand Down
5 changes: 5 additions & 0 deletions docs/plugins/discovery-ec2.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Those that must be stored in the keystore are marked as `Secure`.

An s3 secret key. The `access_key` setting must also be specified. (Secure)

`session_token`::

An s3 session token. The `access_key` and `secret_key` settings must also
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: An -> A

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky but I think "an" is preferred here: S3 is pronounced "ess three" which begins with a vowel. However, on reflection these docs should refer to EC2 and not S3, so I pushed c93e568.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I just correct an English about his English grammar? Yes I did. Was I right? Absolutly not.

Thanks :)

be specified. (Secure)

`endpoint`::

The ec2 service endpoint to connect to. This will be automatically
Expand Down
5 changes: 5 additions & 0 deletions docs/plugins/repository-s3.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ are marked as `Secure`.

An s3 secret key. The `access_key` setting must also be specified. (Secure)

`session_token`::

An s3 session token. The `access_key` and `secret_key` settings must also
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: An -> A

be specified. (Secure)

`endpoint`::

The s3 service endpoint to connect to. This will be automatically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class HostType {
/** The secret key (ie password) for connecting to ec2. */
Setting<SecureString> SECRET_KEY_SETTING = SecureSetting.secureString("discovery.ec2.secret_key", null);

/** The (optional) session token for connecting to ec2. */
Setting<SecureString> SESSION_TOKEN_SETTING = SecureSetting.secureString("discovery.ec2.session_token", null);

/** An override for the ec2 endpoint to connect to. */
Setting<String> ENDPOINT_SETTING = new Setting<>("discovery.ec2.endpoint", "",
s -> s.toLowerCase(Locale.ROOT), Property.NodeScope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,30 @@
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.http.IdleConnectionReaper;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.retry.RetryPolicy;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;

class AwsEc2ServiceImpl extends AbstractComponent implements AwsEc2Service, Closeable {
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(AwsEc2ServiceImpl.class));

public static final String EC2_METADATA_URL = "http://169.254.169.254/latest/meta-data/";

Expand All @@ -67,20 +75,38 @@ public synchronized AmazonEC2 client() {
}

protected static AWSCredentialsProvider buildCredentials(Logger logger, Settings settings) {
AWSCredentialsProvider credentials;

try (SecureString key = ACCESS_KEY_SETTING.get(settings);
SecureString secret = SECRET_KEY_SETTING.get(settings)) {
SecureString secret = SECRET_KEY_SETTING.get(settings);
SecureString sessionToken = SESSION_TOKEN_SETTING.get(settings)) {
if (key.length() == 0 && secret.length() == 0) {
if (sessionToken.length() > 0) {
throw new SettingsException("Setting [{}] is set but [{}] and [{}] are not",
SESSION_TOKEN_SETTING.getKey(), ACCESS_KEY_SETTING.getKey(), SECRET_KEY_SETTING.getKey());
}

logger.debug("Using either environment variables, system properties or instance profile credentials");
credentials = new DefaultAWSCredentialsProviderChain();
return new DefaultAWSCredentialsProviderChain();
} else {
logger.debug("Using basic key/secret credentials");
credentials = new StaticCredentialsProvider(new BasicAWSCredentials(key.toString(), secret.toString()));
if (key.length() == 0) {
DEPRECATION_LOGGER.deprecated("Setting [{}] is set but [{}] is not, which will be unsupported in future",
SECRET_KEY_SETTING.getKey(), ACCESS_KEY_SETTING.getKey());
}
if (secret.length() == 0) {
DEPRECATION_LOGGER.deprecated("Setting [{}] is set but [{}] is not, which will be unsupported in future",
ACCESS_KEY_SETTING.getKey(), SECRET_KEY_SETTING.getKey());
}

final AWSCredentials credentials;
if (sessionToken.length() == 0) {
logger.debug("Using basic key/secret credentials");
credentials = new BasicAWSCredentials(key.toString(), secret.toString());
} else {
logger.debug("Using basic session credentials");
credentials = new BasicSessionCredentials(key.toString(), secret.toString(), sessionToken.toString());
}
return new AWSStaticCredentialsProvider(credentials);
}
}

return credentials;
}

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public List<Setting<?>> getSettings() {
// Register EC2 discovery settings: discovery.ec2
AwsEc2Service.ACCESS_KEY_SETTING,
AwsEc2Service.SECRET_KEY_SETTING,
AwsEc2Service.SESSION_TOKEN_SETTING,
AwsEc2Service.ENDPOINT_SETTING,
AwsEc2Service.PROTOCOL_SETTING,
AwsEc2Service.PROXY_HOST_SETTING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import org.elasticsearch.common.settings.MockSecureSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.ec2.AwsEc2Service;
import org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -44,17 +42,56 @@ public void testAWSCredentialsWithSystemProviders() {
}

public void testAWSCredentialsWithElasticsearchAwsSettings() {
MockSecureSettings secureSettings = new MockSecureSettings();
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.access_key", "aws_key");
secureSettings.setString("discovery.ec2.secret_key", "aws_secret");
final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger,
Settings.builder().setSecureSettings(secureSettings).build()).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is("aws_key"));
assertThat(credentials.getAWSSecretKey(), is("aws_secret"));
}

public void testAWSSessionCredentialsWithElasticsearchAwsSettings() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.access_key", "aws_key");
secureSettings.setString("discovery.ec2.secret_key", "aws_secret");
Settings settings = Settings.builder().setSecureSettings(secureSettings).build();
launchAWSCredentialsWithElasticsearchSettingsTest(settings, "aws_key", "aws_secret");
secureSettings.setString("discovery.ec2.session_token", "aws_session_token");
final BasicSessionCredentials credentials = (BasicSessionCredentials) AwsEc2ServiceImpl.buildCredentials(logger,
Settings.builder().setSecureSettings(secureSettings).build()).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is("aws_key"));
assertThat(credentials.getAWSSecretKey(), is("aws_secret"));
assertThat(credentials.getSessionToken(), is("aws_session_token"));
}

public void testDeprecationOfLoneAccessKey() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.access_key", "aws_key");
final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger,
Settings.builder().setSecureSettings(secureSettings).build()).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is("aws_key"));
assertThat(credentials.getAWSSecretKey(), is(""));
assertSettingDeprecationsAndWarnings(new String[]{},
"Setting [discovery.ec2.access_key] is set but [discovery.ec2.secret_key] is not, which will be unsupported in future");
}

public void testDeprecationOfLoneSecretKey() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.secret_key", "aws_secret");
final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger,
Settings.builder().setSecureSettings(secureSettings).build()).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is(""));
assertThat(credentials.getAWSSecretKey(), is("aws_secret"));
assertSettingDeprecationsAndWarnings(new String[]{},
"Setting [discovery.ec2.secret_key] is set but [discovery.ec2.access_key] is not, which will be unsupported in future");
}

protected void launchAWSCredentialsWithElasticsearchSettingsTest(Settings settings, String expectedKey, String expectedSecret) {
AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger, settings).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is(expectedKey));
assertThat(credentials.getAWSSecretKey(), is(expectedSecret));
public void testRejectionOfLoneSessionToken() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.session_token", "aws_session_token");
SettingsException e = expectThrows(SettingsException.class, () -> AwsEc2ServiceImpl.buildCredentials(logger,
Settings.builder().setSecureSettings(secureSettings).build()));
assertThat(e.getMessage(), is(
"Setting [discovery.ec2.session_token] is set but [discovery.ec2.access_key] and [discovery.ec2.secret_key] are not"));
}

public void testAWSDefaultConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ protected Settings nodeSettings(int nodeOrdinal) {
MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString(AwsEc2Service.ACCESS_KEY_SETTING.getKey(), "some_access");
secureSettings.setString(AwsEc2Service.SECRET_KEY_SETTING.getKey(), "some_secret");
if (randomBoolean()) {
secureSettings.setString(AwsEc2Service.SESSION_TOKEN_SETTING.getKey(), "some_token");
}
return Settings.builder().put(super.nodeSettings(nodeOrdinal))
.put(DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING.getKey(), "ec2")
.put("path.logs", resolve)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ static ClientConfiguration buildConfiguration(S3ClientSettings clientSettings) {
// pkg private for tests
static AWSCredentialsProvider buildCredentials(Logger logger, DeprecationLogger deprecationLogger,
S3ClientSettings clientSettings, Settings repositorySettings) {


BasicAWSCredentials credentials = clientSettings.credentials;
AWSCredentials credentials = clientSettings.credentials;
if (S3Repository.ACCESS_KEY_SETTING.exists(repositorySettings)) {
if (S3Repository.SECRET_KEY_SETTING.exists(repositorySettings) == false) {
throw new IllegalArgumentException("Repository setting [" + S3Repository.ACCESS_KEY_SETTING.getKey() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import org.elasticsearch.common.settings.SecureSetting;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Setting;
Expand All @@ -51,6 +53,10 @@ class S3ClientSettings {
static final Setting.AffixSetting<SecureString> SECRET_KEY_SETTING = Setting.affixKeySetting(PREFIX, "secret_key",
key -> SecureSetting.secureString(key, null));

/** The (optional) session token for connecting to s3. */
static final Setting.AffixSetting<SecureString> SESSION_TOKEN_SETTING = Setting.affixKeySetting(PREFIX, "session_token",
key -> SecureSetting.secureString(key, null));

/** An override for the s3 endpoint to connect to. */
static final Setting.AffixSetting<String> ENDPOINT_SETTING = Setting.affixKeySetting(PREFIX, "endpoint",
key -> new Setting<>(key, "", s -> s.toLowerCase(Locale.ROOT), Property.NodeScope));
Expand Down Expand Up @@ -88,7 +94,7 @@ class S3ClientSettings {
key -> Setting.boolSetting(key, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, Property.NodeScope));

/** Credentials to authenticate with s3. */
final BasicAWSCredentials credentials;
final AWSCredentials credentials;

/** The s3 endpoint the client should talk to, or empty string to use the default. */
final String endpoint;
Expand Down Expand Up @@ -119,7 +125,7 @@ class S3ClientSettings {
/** Whether the s3 client should use an exponential backoff retry policy. */
final boolean throttleRetries;

private S3ClientSettings(BasicAWSCredentials credentials, String endpoint, Protocol protocol,
private S3ClientSettings(AWSCredentials credentials, String endpoint, Protocol protocol,
String proxyHost, int proxyPort, String proxyUsername, String proxyPassword,
int readTimeoutMillis, int maxRetries, boolean throttleRetries) {
this.credentials = credentials;
Expand Down Expand Up @@ -158,17 +164,28 @@ static Map<String, S3ClientSettings> load(Settings settings) {
static S3ClientSettings getClientSettings(Settings settings, String clientName) {
try (SecureString accessKey = getConfigValue(settings, clientName, ACCESS_KEY_SETTING);
SecureString secretKey = getConfigValue(settings, clientName, SECRET_KEY_SETTING);
SecureString sessionToken = getConfigValue(settings, clientName, SESSION_TOKEN_SETTING);
SecureString proxyUsername = getConfigValue(settings, clientName, PROXY_USERNAME_SETTING);
SecureString proxyPassword = getConfigValue(settings, clientName, PROXY_PASSWORD_SETTING)) {
BasicAWSCredentials credentials = null;
final AWSCredentials credentials;
if (accessKey.length() != 0) {
if (secretKey.length() != 0) {
credentials = new BasicAWSCredentials(accessKey.toString(), secretKey.toString());
if (sessionToken.length() != 0) {
credentials = new BasicSessionCredentials(accessKey.toString(), secretKey.toString(), sessionToken.toString());
} else {
credentials = new BasicAWSCredentials(accessKey.toString(), secretKey.toString());
}
} else {
throw new IllegalArgumentException("Missing secret key for s3 client [" + clientName + "]");
}
} else if (secretKey.length() != 0) {
throw new IllegalArgumentException("Missing access key for s3 client [" + clientName + "]");
} else {
if (secretKey.length() != 0) {
throw new IllegalArgumentException("Missing access key for s3 client [" + clientName + "]");
}
if (sessionToken.length() != 0) {
throw new IllegalArgumentException("Missing access key and secret key for s3 client [" + clientName + "]");
}
credentials = null;
}
return new S3ClientSettings(
credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public List<Setting<?>> getSettings() {
// named s3 client configuration settings
S3ClientSettings.ACCESS_KEY_SETTING,
S3ClientSettings.SECRET_KEY_SETTING,
S3ClientSettings.SESSION_TOKEN_SETTING,
S3ClientSettings.ENDPOINT_SETTING,
S3ClientSettings.PROTOCOL_SETTING,
S3ClientSettings.PROXY_HOST_SETTING,
Expand Down
Loading