21
21
22
22
import com .amazonaws .ClientConfiguration ;
23
23
import com .amazonaws .Protocol ;
24
+ import com .amazonaws .auth .AWSCredentials ;
24
25
import 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 ;
26
30
import org .elasticsearch .common .settings .SecureSetting ;
27
31
import org .elasticsearch .common .settings .SecureString ;
28
32
import org .elasticsearch .common .settings .Setting ;
29
- import org .elasticsearch .common .settings .Settings ;
30
33
import org .elasticsearch .common .settings .Setting .Property ;
34
+ import org .elasticsearch .common .settings .Settings ;
35
+ import org .elasticsearch .common .settings .SettingsException ;
31
36
import org .elasticsearch .common .unit .TimeValue ;
37
+
32
38
import java .util .Locale ;
33
39
34
40
/**
@@ -42,6 +48,9 @@ final class Ec2ClientSettings {
42
48
/** The secret key (ie password) for connecting to ec2. */
43
49
static final Setting <SecureString > SECRET_KEY_SETTING = SecureSetting .secureString ("discovery.ec2.secret_key" , null );
44
50
51
+ /** The session token for connecting to ec2. */
52
+ static final Setting <SecureString > SESSION_TOKEN_SETTING = SecureSetting .secureString ("discovery.ec2.session_token" , null );
53
+
45
54
/** The host name of a proxy to connect to ec2 through. */
46
55
static final Setting <String > PROXY_HOST_SETTING = Setting .simpleString ("discovery.ec2.proxy.host" , Property .NodeScope );
47
56
@@ -66,8 +75,12 @@ final class Ec2ClientSettings {
66
75
static final Setting <TimeValue > READ_TIMEOUT_SETTING = Setting .timeSetting ("discovery.ec2.read_timeout" ,
67
76
TimeValue .timeValueMillis (ClientConfiguration .DEFAULT_SOCKET_TIMEOUT ), Property .NodeScope );
68
77
78
+ private static final Logger logger = Loggers .getLogger (Ec2ClientSettings .class );
79
+
80
+ private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (logger );
81
+
69
82
/** Credentials to authenticate with ec2. */
70
- final BasicAWSCredentials credentials ;
83
+ final AWSCredentials credentials ;
71
84
72
85
/**
73
86
* The ec2 endpoint the client should talk to, or empty string to use the
@@ -96,7 +109,7 @@ final class Ec2ClientSettings {
96
109
/** The read timeout for the ec2 client. */
97
110
final int readTimeoutMillis ;
98
111
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 ,
100
113
String proxyUsername , String proxyPassword , int readTimeoutMillis ) {
101
114
this .credentials = credentials ;
102
115
this .endpoint = endpoint ;
@@ -108,26 +121,45 @@ protected Ec2ClientSettings(BasicAWSCredentials credentials, String endpoint, Pr
108
121
this .readTimeoutMillis = readTimeoutMillis ;
109
122
}
110
123
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 ());
117
150
} 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 ());
119
153
}
120
- } else if (secretKey .length () != 0 ) {
121
- throw new IllegalArgumentException ("Missing access key for ec2 client." );
154
+ return credentials ;
122
155
}
123
- return null ;
124
156
}
125
157
}
126
158
127
159
// pkg private for tests
128
160
/** Parse settings for a single client. */
129
161
static Ec2ClientSettings getClientSettings (Settings settings ) {
130
- final BasicAWSCredentials credentials = loadCredentials (settings );
162
+ final AWSCredentials credentials = loadCredentials (settings );
131
163
try (SecureString proxyUsername = PROXY_USERNAME_SETTING .get (settings );
132
164
SecureString proxyPassword = PROXY_PASSWORD_SETTING .get (settings )) {
133
165
return new Ec2ClientSettings (
0 commit comments