2121
2222import com .amazonaws .ClientConfiguration ;
2323import com .amazonaws .Protocol ;
24+ import com .amazonaws .auth .AWSCredentials ;
2425import com .amazonaws .auth .BasicAWSCredentials ;
25-
26+ import com .amazonaws .auth .BasicSessionCredentials ;
27+ import org .apache .logging .log4j .Logger ;
28+ import org .elasticsearch .common .logging .DeprecationLogger ;
29+ import org .elasticsearch .common .logging .Loggers ;
2630import org .elasticsearch .common .settings .SecureSetting ;
2731import org .elasticsearch .common .settings .SecureString ;
2832import org .elasticsearch .common .settings .Setting ;
29- import org .elasticsearch .common .settings .Settings ;
3033import org .elasticsearch .common .settings .Setting .Property ;
34+ import org .elasticsearch .common .settings .Settings ;
35+ import org .elasticsearch .common .settings .SettingsException ;
3136import org .elasticsearch .common .unit .TimeValue ;
37+
3238import java .util .Locale ;
3339
3440/**
@@ -42,6 +48,9 @@ final class Ec2ClientSettings {
4248 /** The secret key (ie password) for connecting to ec2. */
4349 static final Setting <SecureString > SECRET_KEY_SETTING = SecureSetting .secureString ("discovery.ec2.secret_key" , null );
4450
51+ /** The session token for connecting to ec2. */
52+ static final Setting <SecureString > SESSION_TOKEN_SETTING = SecureSetting .secureString ("discovery.ec2.session_token" , null );
53+
4554 /** The host name of a proxy to connect to ec2 through. */
4655 static final Setting <String > PROXY_HOST_SETTING = Setting .simpleString ("discovery.ec2.proxy.host" , Property .NodeScope );
4756
@@ -66,8 +75,12 @@ final class Ec2ClientSettings {
6675 static final Setting <TimeValue > READ_TIMEOUT_SETTING = Setting .timeSetting ("discovery.ec2.read_timeout" ,
6776 TimeValue .timeValueMillis (ClientConfiguration .DEFAULT_SOCKET_TIMEOUT ), Property .NodeScope );
6877
78+ private static final Logger logger = Loggers .getLogger (Ec2ClientSettings .class );
79+
80+ private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (logger );
81+
6982 /** Credentials to authenticate with ec2. */
70- final BasicAWSCredentials credentials ;
83+ final AWSCredentials credentials ;
7184
7285 /**
7386 * The ec2 endpoint the client should talk to, or empty string to use the
@@ -96,7 +109,7 @@ final class Ec2ClientSettings {
96109 /** The read timeout for the ec2 client. */
97110 final int readTimeoutMillis ;
98111
99- protected Ec2ClientSettings (BasicAWSCredentials credentials , String endpoint , Protocol protocol , String proxyHost , int proxyPort ,
112+ protected Ec2ClientSettings (AWSCredentials credentials , String endpoint , Protocol protocol , String proxyHost , int proxyPort ,
100113 String proxyUsername , String proxyPassword , int readTimeoutMillis ) {
101114 this .credentials = credentials ;
102115 this .endpoint = endpoint ;
@@ -108,26 +121,45 @@ protected Ec2ClientSettings(BasicAWSCredentials credentials, String endpoint, Pr
108121 this .readTimeoutMillis = readTimeoutMillis ;
109122 }
110123
111- static BasicAWSCredentials loadCredentials (Settings settings ) {
112- try (SecureString accessKey = ACCESS_KEY_SETTING .get (settings );
113- SecureString secretKey = SECRET_KEY_SETTING .get (settings );) {
114- if (accessKey .length () != 0 ) {
115- if (secretKey .length () != 0 ) {
116- return new BasicAWSCredentials (accessKey .toString (), secretKey .toString ());
124+ static AWSCredentials loadCredentials (Settings settings ) {
125+ try (SecureString key = ACCESS_KEY_SETTING .get (settings );
126+ SecureString secret = SECRET_KEY_SETTING .get (settings );
127+ SecureString sessionToken = SESSION_TOKEN_SETTING .get (settings )) {
128+ if (key .length () == 0 && secret .length () == 0 ) {
129+ if (sessionToken .length () > 0 ) {
130+ throw new SettingsException ("Setting [{}] is set but [{}] and [{}] are not" ,
131+ SESSION_TOKEN_SETTING .getKey (), ACCESS_KEY_SETTING .getKey (), SECRET_KEY_SETTING .getKey ());
132+ }
133+
134+ logger .debug ("Using either environment variables, system properties or instance profile credentials" );
135+ return null ;
136+ } else {
137+ if (key .length () == 0 ) {
138+ DEPRECATION_LOGGER .deprecated ("Setting [{}] is set but [{}] is not, which will be unsupported in future" ,
139+ SECRET_KEY_SETTING .getKey (), ACCESS_KEY_SETTING .getKey ());
140+ }
141+ if (secret .length () == 0 ) {
142+ DEPRECATION_LOGGER .deprecated ("Setting [{}] is set but [{}] is not, which will be unsupported in future" ,
143+ ACCESS_KEY_SETTING .getKey (), SECRET_KEY_SETTING .getKey ());
144+ }
145+
146+ final AWSCredentials credentials ;
147+ if (sessionToken .length () == 0 ) {
148+ logger .debug ("Using basic key/secret credentials" );
149+ credentials = new BasicAWSCredentials (key .toString (), secret .toString ());
117150 } else {
118- throw new IllegalArgumentException ("Missing secret key for ec2 client." );
151+ logger .debug ("Using basic session credentials" );
152+ credentials = new BasicSessionCredentials (key .toString (), secret .toString (), sessionToken .toString ());
119153 }
120- } else if (secretKey .length () != 0 ) {
121- throw new IllegalArgumentException ("Missing access key for ec2 client." );
154+ return credentials ;
122155 }
123- return null ;
124156 }
125157 }
126158
127159 // pkg private for tests
128160 /** Parse settings for a single client. */
129161 static Ec2ClientSettings getClientSettings (Settings settings ) {
130- final BasicAWSCredentials credentials = loadCredentials (settings );
162+ final AWSCredentials credentials = loadCredentials (settings );
131163 try (SecureString proxyUsername = PROXY_USERNAME_SETTING .get (settings );
132164 SecureString proxyPassword = PROXY_PASSWORD_SETTING .get (settings )) {
133165 return new Ec2ClientSettings (
0 commit comments