-
Notifications
You must be signed in to change notification settings - Fork 93
/
IntegrationTests.java
91 lines (80 loc) · 4.14 KB
/
IntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.rest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_ENABLED;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_FILEPATH;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_KEYPASSWORD;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_PASSWORD;
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_SSL_HTTP_PEMCERT_FILEPATH;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.common.settings.Settings;
@Disabled("Enable this after integration with security plugin is done")
public class IntegrationTests {
private Request createSampleRequest() {
Request request = new Request("GET", "/_opendistro/_security/authinfo");
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
request.setOptions(builder);
return request;
}
@Test
public void testCreateRestClientWithUser() throws Exception {
RestClient client = new SecureRestClientBuilder("localhost", 9200, true, "admin", "admin").build();
Response response = client.performRequest(createSampleRequest());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseBody.contains("user_name\":\"admin"));
client.close();
}
@Test
public void testCreateRestClientWithCerts() throws Exception {
String dir = new File(getClass().getClassLoader().getResource("sample.pem").getPath()).getParentFile().getAbsolutePath();
Path configPath = Paths.get(dir);
Settings settings = Settings
.builder()
.put("http.port", 9200)
.put(OPENSEARCH_SECURITY_SSL_HTTP_ENABLED, true)
.put(OPENSEARCH_SECURITY_SSL_HTTP_PEMCERT_FILEPATH, "sample.pem")
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_FILEPATH, "test-kirk.jks")
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_PASSWORD, "changeit")
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_KEYPASSWORD, "changeit")
.build();
RestClient client = new SecureRestClientBuilder(settings, configPath).build();
Response response = client.performRequest(createSampleRequest());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseBody.contains("user_name\":\"CN=kirk"));
client.close();
}
@Test
public void testCreateRestClientWithoutPem() throws Exception {
String dir = new File(getClass().getClassLoader().getResource("sample.pem").getPath()).getParentFile().getAbsolutePath();
Path configPath = Paths.get(dir);
Settings settings = Settings
.builder()
.put("http.port", 9200)
.put(OPENSEARCH_SECURITY_SSL_HTTP_ENABLED, true)
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_FILEPATH, "test-kirk.jks")
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_PASSWORD, "changeit")
.put(OPENSEARCH_SECURITY_SSL_HTTP_KEYSTORE_KEYPASSWORD, "changeit")
.build();
RestClient client = new SecureRestClientBuilder(settings, configPath).build();
Response response = client.performRequest(createSampleRequest());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseBody.contains("user_name\":\"CN=kirk"));
client.close();
}
}