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 all 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
8 changes: 6 additions & 2 deletions docs/plugins/discovery-ec2.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ Those that must be stored in the keystore are marked as `Secure`.

`access_key`::

An s3 access key. The `secret_key` setting must also be specified. (Secure)
An ec2 access key. The `secret_key` setting must also be specified. (Secure)

`secret_key`::

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

`session_token`::
An ec2 session token. The `access_key` and `secret_key` settings must also
be specified. (Secure)

`endpoint`::

Expand Down
4 changes: 4 additions & 0 deletions docs/plugins/repository-s3.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ 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
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 @@ -19,12 +19,9 @@

package org.elasticsearch.discovery.ec2;

import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.http.IdleConnectionReaper;
import com.amazonaws.internal.StaticCredentialsProvider;
Expand All @@ -39,6 +36,9 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.LazyInitializable;

import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

class AwsEc2ServiceImpl extends AbstractComponent implements AwsEc2Service {

public static final String EC2_METADATA_URL = "http://169.254.169.254/latest/meta-data/";
Expand Down Expand Up @@ -99,7 +99,7 @@ static ClientConfiguration buildConfiguration(Logger logger, Ec2ClientSettings c

// pkg private for tests
static AWSCredentialsProvider buildCredentials(Logger logger, Ec2ClientSettings clientSettings) {
final BasicAWSCredentials credentials = clientSettings.credentials;
final AWSCredentials credentials = clientSettings.credentials;
if (credentials == null) {
logger.debug("Using either environment variables, system properties or instance profile credentials");
return new DefaultAWSCredentialsProviderChain();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@

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.apache.logging.log4j.Logger;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.SecureSetting;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.unit.TimeValue;

import java.util.Locale;

/**
Expand All @@ -42,6 +48,9 @@ final class Ec2ClientSettings {
/** The secret key (ie password) for connecting to ec2. */
static final Setting<SecureString> SECRET_KEY_SETTING = SecureSetting.secureString("discovery.ec2.secret_key", null);

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

/** The host name of a proxy to connect to ec2 through. */
static final Setting<String> PROXY_HOST_SETTING = Setting.simpleString("discovery.ec2.proxy.host", Property.NodeScope);

Expand All @@ -66,8 +75,12 @@ final class Ec2ClientSettings {
static final Setting<TimeValue> READ_TIMEOUT_SETTING = Setting.timeSetting("discovery.ec2.read_timeout",
TimeValue.timeValueMillis(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT), Property.NodeScope);

private static final Logger logger = Loggers.getLogger(Ec2ClientSettings.class);

private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(logger);

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

/**
* The ec2 endpoint the client should talk to, or empty string to use the
Expand Down Expand Up @@ -96,7 +109,7 @@ final class Ec2ClientSettings {
/** The read timeout for the ec2 client. */
final int readTimeoutMillis;

protected Ec2ClientSettings(BasicAWSCredentials credentials, String endpoint, Protocol protocol, String proxyHost, int proxyPort,
protected Ec2ClientSettings(AWSCredentials credentials, String endpoint, Protocol protocol, String proxyHost, int proxyPort,
String proxyUsername, String proxyPassword, int readTimeoutMillis) {
this.credentials = credentials;
this.endpoint = endpoint;
Expand All @@ -108,26 +121,45 @@ protected Ec2ClientSettings(BasicAWSCredentials credentials, String endpoint, Pr
this.readTimeoutMillis = readTimeoutMillis;
}

static BasicAWSCredentials loadCredentials(Settings settings) {
try (SecureString accessKey = ACCESS_KEY_SETTING.get(settings);
SecureString secretKey = SECRET_KEY_SETTING.get(settings);) {
if (accessKey.length() != 0) {
if (secretKey.length() != 0) {
return new BasicAWSCredentials(accessKey.toString(), secretKey.toString());
static AWSCredentials loadCredentials(Settings settings) {
try (SecureString key = ACCESS_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");
return null;
} else {
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 {
throw new IllegalArgumentException("Missing secret key for ec2 client.");
logger.debug("Using basic session credentials");
credentials = new BasicSessionCredentials(key.toString(), secret.toString(), sessionToken.toString());
}
} else if (secretKey.length() != 0) {
throw new IllegalArgumentException("Missing access key for ec2 client.");
return credentials;
}
return null;
}
}

// pkg private for tests
/** Parse settings for a single client. */
static Ec2ClientSettings getClientSettings(Settings settings) {
final BasicAWSCredentials credentials = loadCredentials(settings);
final AWSCredentials credentials = loadCredentials(settings);
try (SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings);
SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings)) {
return new Ec2ClientSettings(
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
Ec2ClientSettings.ACCESS_KEY_SETTING,
Ec2ClientSettings.SECRET_KEY_SETTING,
Ec2ClientSettings.SESSION_TOKEN_SETTING,
Ec2ClientSettings.ENDPOINT_SETTING,
Ec2ClientSettings.PROTOCOL_SETTING,
Ec2ClientSettings.PROXY_HOST_SETTING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
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.Settings;
import org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.instanceOf;
Expand All @@ -44,15 +45,53 @@ public void testAWSCredentialsWithElasticsearchAwsSettings() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.access_key", "aws_key");
secureSettings.setString("discovery.ec2.secret_key", "aws_secret");
final Settings settings = Settings.builder().setSecureSettings(secureSettings).build();
launchAWSCredentialsWithElasticsearchSettingsTest(settings, "aws_key", "aws_secret");
final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger,
Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())).getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is("aws_key"));
assertThat(credentials.getAWSSecretKey(), is("aws_secret"));
}

protected void launchAWSCredentialsWithElasticsearchSettingsTest(Settings settings, String expectedKey, String expectedSecret) {
final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger, Ec2ClientSettings.getClientSettings(settings))
.getCredentials();
assertThat(credentials.getAWSAccessKeyId(), is(expectedKey));
assertThat(credentials.getAWSSecretKey(), is(expectedSecret));
public void testAWSSessionCredentialsWithElasticsearchAwsSettings() {
final MockSecureSettings secureSettings = new MockSecureSettings();
secureSettings.setString("discovery.ec2.access_key", "aws_key");
secureSettings.setString("discovery.ec2.secret_key", "aws_secret");
secureSettings.setString("discovery.ec2.session_token", "aws_session_token");
final BasicSessionCredentials credentials = (BasicSessionCredentials) AwsEc2ServiceImpl.buildCredentials(logger,
Ec2ClientSettings.getClientSettings(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,
Ec2ClientSettings.getClientSettings(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,
Ec2ClientSettings.getClientSettings(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");
}

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,
Ec2ClientSettings.getClientSettings(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
Loading