|
18 | 18 | */
|
19 | 19 | package com.asquera.elasticsearch.plugins.http.auth.integration;
|
20 | 20 |
|
21 |
| -import org.apache.http.client.methods.CloseableHttpResponse; |
22 |
| -import org.apache.http.client.methods.HttpUriRequest; |
23 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
24 |
| -import org.apache.http.impl.client.HttpClients; |
25 |
| -import org.elasticsearch.common.settings.ImmutableSettings; |
26 | 21 | import org.elasticsearch.common.settings.Settings;
|
27 | 22 | import org.elasticsearch.common.Base64;
|
28 | 23 | import org.elasticsearch.rest.RestStatus;
|
29 |
| -import org.elasticsearch.test.ElasticsearchIntegrationTest; |
30 | 24 | import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
|
31 |
| -import org.elasticsearch.test.rest.client.http.HttpGetWithEntity; |
32 | 25 | import org.elasticsearch.test.rest.client.http.HttpRequestBuilder;
|
33 | 26 | import org.elasticsearch.test.rest.client.http.HttpResponse;
|
34 | 27 | import org.junit.Test;
|
35 | 28 |
|
36 |
| -import com.asquera.elasticsearch.plugins.http.HttpBasicServerPlugin; |
37 |
| - |
38 |
| -import java.net.URI; |
39 |
| -import java.net.URISyntaxException; |
40 |
| - |
41 | 29 | import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
|
42 | 30 | import static org.hamcrest.Matchers.equalTo;
|
43 | 31 |
|
44 | 32 | /**
|
45 | 33 | * Test a rest action that sets special response headers
|
46 | 34 | */
|
47 | 35 | @ClusterScope(transportClientRatio = 0.0, scope = Scope.SUITE, numDataNodes = 1)
|
48 |
| -public class EmptyWhitelistIntegrationTest extends ElasticsearchIntegrationTest { |
| 36 | +public class EmptyWhitelistIntegrationTest extends HttpBasicServerPluginIntegrationTest { |
49 | 37 |
|
50 | 38 | @Override
|
51 | 39 | protected Settings nodeSettings(int nodeOrdinal) {
|
52 |
| - return ImmutableSettings.settingsBuilder().putArray("http.basic.ipwhitelist", "unkown") |
53 |
| - .put("plugin.types", HttpBasicServerPlugin.class.getName()) |
| 40 | + return builderWithPlugin(). |
| 41 | + putArray("http.basic.ipwhitelist", "unkown") |
54 | 42 | .build();
|
55 | 43 | }
|
56 | 44 |
|
| 45 | +// TODO put the set credentials ussing Setter |
57 | 46 | @Test
|
58 |
| - public void testHealthCheck() throws Exception { |
59 |
| - HttpResponse response = httpClient().path("/").execute(); |
60 |
| - assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
61 |
| - } |
62 |
| - |
63 |
| - @Test |
64 |
| - public void localhostClientIsNotIpAuthenticated() throws Exception { |
| 47 | + public void clientIpAuthenticationFails() throws Exception { |
65 | 48 | HttpResponse response = httpClient().path("/_status").execute();
|
66 | 49 | assertThat(response.getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus()));
|
67 | 50 | }
|
68 |
| - |
69 |
| - @Test |
70 |
| - public void localhostClientIsBasicAuthenticated() throws Exception { |
71 |
| - HttpUriRequest request = httpRequest(); |
72 |
| - String credentials = "admin:admin_pw"; |
73 |
| - request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); |
74 |
| - CloseableHttpResponse response = closeableHttpClient().execute(request); |
75 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
76 |
| - } |
77 |
| - |
78 | 51 |
|
79 | 52 | @Test
|
80 |
| - public void localhostClientIsBasicAuthenticatedPassingXForward() throws Exception { |
81 |
| - HttpUriRequest request = httpRequest(); |
82 |
| - String credentials = "admin:admin_pw"; |
83 |
| - request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); |
84 |
| - request.setHeader("X-Forwarded-For", "1.1.1.1" ); |
85 |
| - CloseableHttpResponse response = closeableHttpClient().execute(request); |
86 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
87 |
| - } |
88 |
| - @Test |
89 |
| - public void localhostClientNotBasicAuthenticated() throws Exception { |
90 |
| - HttpUriRequest request = httpRequest(); |
91 |
| - String credentials = "admin:wrong"; |
92 |
| - request.setHeader("Authorization", "Basic " + Base64.encodeBytes(credentials.getBytes())); |
93 |
| - CloseableHttpResponse response = closeableHttpClient().execute(request); |
94 |
| - assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.UNAUTHORIZED.getStatus())); |
95 |
| - } |
96 |
| - |
97 |
| - public static HttpRequestBuilder httpClient() { |
98 |
| - return new HttpRequestBuilder(HttpClients.createDefault()).host("localhost").port(9200); |
99 |
| - } |
100 |
| - |
101 |
| - public static HttpUriRequest httpRequest() { |
102 |
| - HttpUriRequest httpUriRequest = null; |
103 |
| - try { |
104 |
| - httpUriRequest = new HttpGetWithEntity(new URI("http", null, "localhost", 9200, "/_status", null, null)); |
105 |
| - } catch (URISyntaxException e) { |
106 |
| - throw new IllegalArgumentException(e); |
107 |
| - } |
108 |
| - return httpUriRequest; |
| 53 | + public void clientGoodCredentialsBasicAuthenticationSuceeds() throws Exception { |
| 54 | + HttpResponse response = requestWithCredentials("admin:admin_pw") |
| 55 | + .addHeader("X-Forwarded-For", "1.1.1.1" ).execute(); |
| 56 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
109 | 57 | }
|
110 | 58 |
|
111 |
| - public static CloseableHttpClient closeableHttpClient() { |
112 |
| - return HttpClients.createDefault(); |
| 59 | + @Test |
| 60 | + public void clientBadCredentialsBasicAuthenticationFails() throws Exception { |
| 61 | + HttpResponse response = requestWithCredentials("admin:wrong").execute(); |
| 62 | + assertThat(response.getStatusCode() |
| 63 | + , equalTo(RestStatus.UNAUTHORIZED.getStatus())); |
113 | 64 | }
|
114 |
| - |
115 | 65 | }
|
0 commit comments