Skip to content

Commit 11c6571

Browse files
Refactor tests
1 parent 11a99b1 commit 11c6571

File tree

3 files changed

+107
-150
lines changed

3 files changed

+107
-150
lines changed

x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/CliApiKeyIT.java

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,19 @@
77
package org.elasticsearch.xpack.sql.qa.security;
88

99
import org.elasticsearch.client.Request;
10-
import org.elasticsearch.client.Response;
11-
import org.elasticsearch.common.settings.Settings;
12-
import org.elasticsearch.common.xcontent.XContentHelper;
13-
import org.elasticsearch.test.cluster.ElasticsearchCluster;
14-
import org.elasticsearch.test.rest.ESRestTestCase;
15-
import org.elasticsearch.xcontent.json.JsonXContent;
1610
import org.elasticsearch.xpack.sql.qa.cli.EmbeddedCli;
1711
import org.elasticsearch.xpack.sql.qa.cli.EmbeddedCli.ApiKeySecurityConfig;
18-
import org.junit.After;
19-
import org.junit.ClassRule;
20-
21-
import java.io.IOException;
22-
import java.io.InputStream;
23-
import java.util.ArrayList;
24-
import java.util.List;
25-
import java.util.Map;
2612

2713
import static org.elasticsearch.xpack.sql.qa.security.RestSqlIT.SSL_ENABLED;
2814
import static org.hamcrest.Matchers.containsString;
2915

3016
/**
3117
* Integration tests for CLI connections using API key authentication.
3218
*/
33-
public class CliApiKeyIT extends ESRestTestCase {
34-
35-
@ClassRule
36-
public static ElasticsearchCluster cluster = SqlSecurityTestCluster.getCluster();
37-
38-
private final List<String> createdApiKeyIds = new ArrayList<>();
39-
40-
@After
41-
public void cleanupApiKeys() throws IOException {
42-
for (String apiKeyId : createdApiKeyIds) {
43-
try {
44-
Request deleteApiKey = new Request("DELETE", "/_security/api_key");
45-
deleteApiKey.setJsonEntity("{\"ids\": [\"" + apiKeyId + "\"]}");
46-
client().performRequest(deleteApiKey);
47-
} catch (Exception e) {
48-
logger.warn("Failed to delete API key [{}]: {}", apiKeyId, e.getMessage());
49-
}
50-
}
51-
createdApiKeyIds.clear();
52-
}
53-
54-
@Override
55-
protected String getTestRestCluster() {
56-
return cluster.getHttpAddresses();
57-
}
58-
59-
@Override
60-
protected Settings restClientSettings() {
61-
return RestSqlIT.securitySettings();
62-
}
63-
64-
@Override
65-
protected String getProtocol() {
66-
return SSL_ENABLED ? "https" : "http";
67-
}
19+
public class CliApiKeyIT extends SqlApiKeyTestCase {
6820

6921
public void testCliConnectionWithApiKey() throws Exception {
70-
String encodedApiKey = createApiKey("cli_test_key", """
22+
String encodedApiKey = createApiKey("""
7123
{
7224
"name": "cli_test_key",
7325
"role_descriptors": {
@@ -161,7 +113,7 @@ public void testCliConnectionWithLimitedApiKey() throws Exception {
161113
""");
162114
client().performRequest(indexRestrictedDoc);
163115

164-
String encodedApiKey = createApiKey("cli_limited_key", """
116+
String encodedApiKey = createApiKey("""
165117
{
166118
"name": "cli_limited_key",
167119
"role_descriptors": {
@@ -187,24 +139,6 @@ public void testCliConnectionWithLimitedApiKey() throws Exception {
187139
}
188140
}
189141

190-
private String elasticsearchAddress() {
191-
String cluster = getTestRestCluster();
192-
return cluster.split(",")[0];
193-
}
194-
195-
private String createApiKey(String name, String body) throws IOException {
196-
Request createApiKey = new Request("POST", "/_security/api_key");
197-
createApiKey.setJsonEntity(body);
198-
Response response = client().performRequest(createApiKey);
199-
200-
try (InputStream content = response.getEntity().getContent()) {
201-
Map<String, Object> responseMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
202-
String apiKeyId = (String) responseMap.get("id");
203-
createdApiKeyIds.add(apiKeyId);
204-
return (String) responseMap.get("encoded");
205-
}
206-
}
207-
208142
private ApiKeySecurityConfig createApiKeySecurityConfig(String apiKey) {
209143
return new ApiKeySecurityConfig(
210144
SSL_ENABLED,

x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/JdbcApiKeyIT.java

Lines changed: 22 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,24 @@
77
package org.elasticsearch.xpack.sql.qa.security;
88

99
import org.elasticsearch.client.Request;
10-
import org.elasticsearch.client.Response;
11-
import org.elasticsearch.common.settings.Settings;
12-
import org.elasticsearch.common.xcontent.XContentHelper;
13-
import org.elasticsearch.test.cluster.ElasticsearchCluster;
14-
import org.elasticsearch.test.rest.ESRestTestCase;
15-
import org.elasticsearch.xcontent.json.JsonXContent;
16-
import org.junit.After;
17-
import org.junit.ClassRule;
18-
19-
import java.io.IOException;
20-
import java.io.InputStream;
10+
2111
import java.sql.Connection;
2212
import java.sql.DriverManager;
2313
import java.sql.ResultSet;
2414
import java.sql.SQLException;
2515
import java.sql.Statement;
26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import java.util.Map;
2916
import java.util.Properties;
3017

3118
import static org.elasticsearch.xpack.sql.qa.security.RestSqlIT.SSL_ENABLED;
19+
import static org.hamcrest.Matchers.containsString;
3220

3321
/**
3422
* Integration tests for JDBC connections using API key authentication.
3523
*/
36-
public class JdbcApiKeyIT extends ESRestTestCase {
37-
38-
@ClassRule
39-
public static ElasticsearchCluster cluster = SqlSecurityTestCluster.getCluster();
40-
41-
private final List<String> createdApiKeyIds = new ArrayList<>();
42-
43-
@After
44-
public void cleanupApiKeys() throws IOException {
45-
for (String apiKeyId : createdApiKeyIds) {
46-
try {
47-
Request deleteApiKey = new Request("DELETE", "/_security/api_key");
48-
deleteApiKey.setJsonEntity("{\"ids\": [\"" + apiKeyId + "\"]}");
49-
client().performRequest(deleteApiKey);
50-
} catch (Exception e) {
51-
logger.warn("Failed to delete API key [{}]: {}", apiKeyId, e.getMessage());
52-
}
53-
}
54-
createdApiKeyIds.clear();
55-
}
56-
57-
@Override
58-
protected String getTestRestCluster() {
59-
return cluster.getHttpAddresses();
60-
}
61-
62-
@Override
63-
protected Settings restClientSettings() {
64-
return RestSqlIT.securitySettings();
65-
}
66-
67-
@Override
68-
protected String getProtocol() {
69-
return SSL_ENABLED ? "https" : "http";
70-
}
24+
public class JdbcApiKeyIT extends SqlApiKeyTestCase {
7125

7226
public void testJdbcConnectionWithApiKey() throws Exception {
73-
String encodedApiKey = createApiKey("jdbc_test_key", """
27+
String encodedApiKey = createApiKey("""
7428
{
7529
"name": "jdbc_test_key",
7630
"role_descriptors": {
@@ -108,12 +62,8 @@ public void testJdbcConnectionWithApiKey() throws Exception {
10862
""");
10963
client().performRequest(indexDoc);
11064

111-
Properties props = new Properties();
112-
props.setProperty("apiKey", encodedApiKey);
113-
props.setProperty("timezone", "UTC");
114-
addSslPropertiesIfNeeded(props);
115-
116-
String jdbcUrl = "jdbc:es://" + getProtocol() + "://" + getTestRestCluster().split(",")[0];
65+
Properties props = createJdbcPropertiesWithApiKey(encodedApiKey);
66+
String jdbcUrl = jdbcUrl();
11767

11868
try (Connection connection = DriverManager.getConnection(jdbcUrl, props)) {
11969
try (Statement statement = connection.createStatement()) {
@@ -127,19 +77,15 @@ public void testJdbcConnectionWithApiKey() throws Exception {
12777
}
12878

12979
public void testJdbcConnectionWithInvalidApiKey() throws Exception {
130-
Properties props = new Properties();
131-
props.setProperty("apiKey", "invalid_api_key_value");
132-
props.setProperty("timezone", "UTC");
133-
addSslPropertiesIfNeeded(props);
134-
135-
String jdbcUrl = "jdbc:es://" + getProtocol() + "://" + getTestRestCluster().split(",")[0];
80+
Properties props = createJdbcPropertiesWithApiKey("invalid_api_key_value");
81+
String jdbcUrl = jdbcUrl();
13682

13783
SQLException e = expectThrows(SQLException.class, () -> {
13884
try (Connection connection = DriverManager.getConnection(jdbcUrl, props)) {
13985
connection.createStatement().executeQuery("SELECT 1");
14086
}
14187
});
142-
assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("security_exception"));
88+
assertThat(e.getMessage(), containsString("security_exception"));
14389
}
14490

14591
public void testJdbcConnectionWithLimitedApiKey() throws Exception {
@@ -164,7 +110,7 @@ public void testJdbcConnectionWithLimitedApiKey() throws Exception {
164110
""");
165111
client().performRequest(indexRestrictedDoc);
166112

167-
String encodedApiKey = createApiKey("limited_key", """
113+
String encodedApiKey = createApiKey("""
168114
{
169115
"name": "limited_key",
170116
"role_descriptors": {
@@ -181,32 +127,27 @@ public void testJdbcConnectionWithLimitedApiKey() throws Exception {
181127
}
182128
""");
183129

184-
Properties props = new Properties();
185-
props.setProperty("apiKey", encodedApiKey);
186-
props.setProperty("timezone", "UTC");
187-
addSslPropertiesIfNeeded(props);
188-
189-
String jdbcUrl = "jdbc:es://" + getProtocol() + "://" + getTestRestCluster().split(",")[0];
130+
Properties props = createJdbcPropertiesWithApiKey(encodedApiKey);
131+
String jdbcUrl = jdbcUrl();
190132

191133
try (Connection connection = DriverManager.getConnection(jdbcUrl, props)) {
192134
try (Statement statement = connection.createStatement()) {
193135
SQLException e = expectThrows(SQLException.class, () -> statement.executeQuery("SELECT * FROM restricted_index"));
194-
assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("Unknown index [restricted_index]"));
136+
assertThat(e.getMessage(), containsString("Unknown index [restricted_index]"));
195137
}
196138
}
197139
}
198140

199-
private String createApiKey(String name, String body) throws IOException {
200-
Request createApiKey = new Request("POST", "/_security/api_key");
201-
createApiKey.setJsonEntity(body);
202-
Response response = client().performRequest(createApiKey);
141+
private String jdbcUrl() {
142+
return "jdbc:es://" + getProtocol() + "://" + elasticsearchAddress();
143+
}
203144

204-
try (InputStream content = response.getEntity().getContent()) {
205-
Map<String, Object> responseMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
206-
String apiKeyId = (String) responseMap.get("id");
207-
createdApiKeyIds.add(apiKeyId);
208-
return (String) responseMap.get("encoded");
209-
}
145+
private Properties createJdbcPropertiesWithApiKey(String apiKey) {
146+
Properties props = new Properties();
147+
props.setProperty("apiKey", apiKey);
148+
props.setProperty("timezone", "UTC");
149+
addSslPropertiesIfNeeded(props);
150+
return props;
210151
}
211152

212153
private void addSslPropertiesIfNeeded(Properties properties) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
package org.elasticsearch.xpack.sql.qa.security;
8+
9+
import org.elasticsearch.client.Request;
10+
import org.elasticsearch.client.Response;
11+
import org.elasticsearch.common.settings.Settings;
12+
import org.elasticsearch.common.xcontent.XContentHelper;
13+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
14+
import org.elasticsearch.test.rest.ESRestTestCase;
15+
import org.elasticsearch.xcontent.json.JsonXContent;
16+
import org.junit.After;
17+
import org.junit.ClassRule;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import static org.elasticsearch.xpack.sql.qa.security.RestSqlIT.SSL_ENABLED;
26+
27+
/**
28+
* Base class for integration tests that use API key authentication.
29+
*/
30+
public abstract class SqlApiKeyTestCase extends ESRestTestCase {
31+
32+
@ClassRule
33+
public static ElasticsearchCluster cluster = SqlSecurityTestCluster.getCluster();
34+
35+
private final List<String> createdApiKeyIds = new ArrayList<>();
36+
37+
@After
38+
public void cleanupApiKeys() throws IOException {
39+
for (String apiKeyId : createdApiKeyIds) {
40+
try {
41+
Request deleteApiKey = new Request("DELETE", "/_security/api_key");
42+
deleteApiKey.setJsonEntity("{\"ids\": [\"" + apiKeyId + "\"]}");
43+
client().performRequest(deleteApiKey);
44+
} catch (Exception e) {
45+
logger.warn("Failed to delete API key [{}]: {}", apiKeyId, e.getMessage());
46+
}
47+
}
48+
createdApiKeyIds.clear();
49+
}
50+
51+
@Override
52+
protected String getTestRestCluster() {
53+
return cluster.getHttpAddresses();
54+
}
55+
56+
@Override
57+
protected Settings restClientSettings() {
58+
return RestSqlIT.securitySettings();
59+
}
60+
61+
@Override
62+
protected String getProtocol() {
63+
return SSL_ENABLED ? "https" : "http";
64+
}
65+
66+
protected String elasticsearchAddress() {
67+
return getTestRestCluster().split(",")[0];
68+
}
69+
70+
protected String createApiKey(String body) throws IOException {
71+
Request createApiKey = new Request("POST", "/_security/api_key");
72+
createApiKey.setJsonEntity(body);
73+
Response response = client().performRequest(createApiKey);
74+
75+
try (InputStream content = response.getEntity().getContent()) {
76+
Map<String, Object> responseMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
77+
String apiKeyId = (String) responseMap.get("id");
78+
createdApiKeyIds.add(apiKeyId);
79+
return (String) responseMap.get("encoded");
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)