77package org .elasticsearch .xpack .sql .qa .security ;
88
99import 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+
2111import java .sql .Connection ;
2212import java .sql .DriverManager ;
2313import java .sql .ResultSet ;
2414import java .sql .SQLException ;
2515import java .sql .Statement ;
26- import java .util .ArrayList ;
27- import java .util .List ;
28- import java .util .Map ;
2916import java .util .Properties ;
3017
3118import 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 ) {
0 commit comments