5
5
*/
6
6
package org .elasticsearch .xpack .security .authc ;
7
7
8
- import org .elasticsearch .ElasticsearchSecurityException ;
9
- import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
10
- import org .elasticsearch .action .admin .cluster .node .info .NodeInfo ;
11
- import org .elasticsearch .action .admin .cluster .node .info .NodesInfoResponse ;
12
8
import org .elasticsearch .client .Request ;
13
9
import org .elasticsearch .client .RequestOptions ;
14
10
import org .elasticsearch .client .ResponseException ;
15
- import org .elasticsearch .client .transport .TransportClient ;
16
- import org .elasticsearch .common .settings .SecureString ;
17
- import org .elasticsearch .common .settings .Settings ;
18
- import org .elasticsearch .common .transport .TransportAddress ;
19
11
import org .elasticsearch .test .SecurityIntegTestCase ;
20
12
import org .elasticsearch .test .SecuritySettingsSource ;
21
- import org .elasticsearch .test .SecuritySettingsSourceField ;
22
- import org .elasticsearch .xpack .core .TestXPackTransportClient ;
23
13
import org .elasticsearch .xpack .core .security .authc .AuthenticationServiceField ;
24
- import org .elasticsearch .xpack .security .LocalStateSecurity ;
25
- import org .elasticsearch .xpack .core .security .SecurityField ;
26
14
import org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken ;
27
15
import org .junit .BeforeClass ;
28
16
29
- import java .util .Collections ;
30
- import java .util .HashMap ;
31
- import java .util .List ;
32
- import java .util .Map ;
33
-
34
17
import static org .elasticsearch .test .SecuritySettingsSourceField .TEST_PASSWORD_SECURE_STRING ;
35
- import static org .hamcrest .Matchers .containsString ;
36
- import static org .hamcrest .Matchers .greaterThan ;
37
18
import static org .hamcrest .Matchers .is ;
38
19
39
20
public class RunAsIntegTests extends SecurityIntegTestCase {
@@ -86,43 +67,6 @@ protected boolean transportSSLEnabled() {
86
67
return false ;
87
68
}
88
69
89
- public void testUserImpersonation () throws Exception {
90
- try (TransportClient client = getTransportClient (Settings .builder ()
91
- .put (SecurityField .USER_SETTING .getKey (), TRANSPORT_CLIENT_USER + ":" +
92
- SecuritySettingsSourceField .TEST_PASSWORD ).build ())) {
93
- //ensure the client can connect
94
- assertBusy (() -> assertThat (client .connectedNodes ().size (), greaterThan (0 )));
95
-
96
- // make sure the client can't get health
97
- try {
98
- client .admin ().cluster ().prepareHealth ().get ();
99
- fail ("the client user should not have privileges to get the health" );
100
- } catch (ElasticsearchSecurityException e ) {
101
- assertThat (e .getMessage (), containsString ("unauthorized" ));
102
- }
103
-
104
- // let's run as without authorization
105
- try {
106
- Map <String , String > headers = Collections .singletonMap (AuthenticationServiceField .RUN_AS_USER_HEADER ,
107
- SecuritySettingsSource .TEST_USER_NAME );
108
- client .filterWithHeader (headers )
109
- .admin ().cluster ().prepareHealth ().get ();
110
- fail ("run as should be unauthorized for the transport client user" );
111
- } catch (ElasticsearchSecurityException e ) {
112
- assertThat (e .getMessage (), containsString ("unauthorized" ));
113
- assertThat (e .getMessage (), containsString ("run as" ));
114
- }
115
-
116
- Map <String , String > headers = new HashMap <>();
117
- headers .put ("Authorization" , UsernamePasswordToken .basicAuthHeaderValue (RUN_AS_USER ,
118
- new SecureString (SecuritySettingsSourceField .TEST_PASSWORD .toCharArray ())));
119
- headers .put (AuthenticationServiceField .RUN_AS_USER_HEADER , SecuritySettingsSource .TEST_USER_NAME );
120
- // lets set the user
121
- ClusterHealthResponse response = client .filterWithHeader (headers ).admin ().cluster ().prepareHealth ().get ();
122
- assertThat (response .isTimedOut (), is (false ));
123
- }
124
- }
125
-
126
70
public void testUserImpersonationUsingHttp () throws Exception {
127
71
// use the transport client user and try to run as
128
72
try {
@@ -156,29 +100,6 @@ public void testUserImpersonationUsingHttp() throws Exception {
156
100
getRestClient ().performRequest (requestForUserRunAsUser (SecuritySettingsSource .TEST_USER_NAME ));
157
101
}
158
102
159
- public void testEmptyUserImpersonationHeader () throws Exception {
160
- try (TransportClient client = getTransportClient (Settings .builder ()
161
- .put (SecurityField .USER_SETTING .getKey (), TRANSPORT_CLIENT_USER + ":"
162
- + SecuritySettingsSourceField .TEST_PASSWORD ).build ())) {
163
- //ensure the client can connect
164
- awaitBusy (() -> {
165
- return client .connectedNodes ().size () > 0 ;
166
- });
167
-
168
- try {
169
- Map <String , String > headers = new HashMap <>();
170
- headers .put ("Authorization" , UsernamePasswordToken .basicAuthHeaderValue (RUN_AS_USER ,
171
- new SecureString (SecuritySettingsSourceField .TEST_PASSWORD .toCharArray ())));
172
- headers .put (AuthenticationServiceField .RUN_AS_USER_HEADER , "" );
173
-
174
- client .filterWithHeader (headers ).admin ().cluster ().prepareHealth ().get ();
175
- fail ("run as header should not be allowed to be empty" );
176
- } catch (ElasticsearchSecurityException e ) {
177
- assertThat (e .getMessage (), containsString ("unable to authenticate" ));
178
- }
179
- }
180
- }
181
-
182
103
public void testEmptyHeaderUsingHttp () throws Exception {
183
104
try {
184
105
getRestClient ().performRequest (requestForUserRunAsUser ("" ));
@@ -188,29 +109,6 @@ public void testEmptyHeaderUsingHttp() throws Exception {
188
109
}
189
110
}
190
111
191
- public void testNonExistentRunAsUser () throws Exception {
192
- try (TransportClient client = getTransportClient (Settings .builder ()
193
- .put (SecurityField .USER_SETTING .getKey (), TRANSPORT_CLIENT_USER + ":" +
194
- SecuritySettingsSourceField .TEST_PASSWORD ).build ())) {
195
- //ensure the client can connect
196
- awaitBusy (() -> {
197
- return client .connectedNodes ().size () > 0 ;
198
- });
199
-
200
- try {
201
- Map <String , String > headers = new HashMap <>();
202
- headers .put ("Authorization" , UsernamePasswordToken .basicAuthHeaderValue (RUN_AS_USER ,
203
- new SecureString (SecuritySettingsSourceField .TEST_PASSWORD .toCharArray ())));
204
- headers .put (AuthenticationServiceField .RUN_AS_USER_HEADER , "idontexist" );
205
-
206
- client .filterWithHeader (headers ).admin ().cluster ().prepareHealth ().get ();
207
- fail ("run as header should not accept non-existent users" );
208
- } catch (ElasticsearchSecurityException e ) {
209
- assertThat (e .getMessage (), containsString ("unauthorized" ));
210
- }
211
- }
212
- }
213
-
214
112
public void testNonExistentRunAsUserUsingHttp () throws Exception {
215
113
try {
216
114
getRestClient ().performRequest (requestForUserRunAsUser ("idontexist" ));
@@ -228,21 +126,4 @@ private static Request requestForUserRunAsUser(String user) {
228
126
request .setOptions (options );
229
127
return request ;
230
128
}
231
-
232
- // build our own here to better mimic an actual client...
233
- TransportClient getTransportClient (Settings extraSettings ) {
234
- NodesInfoResponse nodeInfos = client ().admin ().cluster ().prepareNodesInfo ().get ();
235
- List <NodeInfo > nodes = nodeInfos .getNodes ();
236
- assertTrue (nodes .isEmpty () == false );
237
- TransportAddress publishAddress = randomFrom (nodes ).getTransport ().address ().publishAddress ();
238
- String clusterName = nodeInfos .getClusterName ().value ();
239
-
240
- Settings settings = Settings .builder ()
241
- .put (extraSettings )
242
- .put ("cluster.name" , clusterName )
243
- .build ();
244
-
245
- return new TestXPackTransportClient (settings , LocalStateSecurity .class )
246
- .addTransportAddress (publishAddress );
247
- }
248
129
}
0 commit comments