|
13 | 13 | */ |
14 | 14 | package io.trino.server; |
15 | 15 |
|
| 16 | +import com.google.common.collect.ImmutableSet; |
16 | 17 | import com.google.inject.Key; |
17 | 18 | import io.airlift.http.client.HttpClient; |
18 | 19 | import io.airlift.http.client.HttpUriBuilder; |
|
29 | 30 | import io.trino.client.QueryDataClientJacksonModule; |
30 | 31 | import io.trino.client.QueryResults; |
31 | 32 | import io.trino.client.ResultRowsDecoder; |
| 33 | +import io.trino.connector.MockConnectorFactory; |
| 34 | +import io.trino.connector.MockConnectorPlugin; |
32 | 35 | import io.trino.execution.QueryInfo; |
33 | 36 | import io.trino.plugin.tpch.TpchPlugin; |
34 | 37 | import io.trino.server.testing.TestingTrinoServer; |
|
39 | 42 | import org.junit.jupiter.api.TestInstance; |
40 | 43 |
|
41 | 44 | import java.net.URI; |
| 45 | +import java.net.URLEncoder; |
42 | 46 | import java.util.List; |
43 | 47 | import java.util.Map; |
| 48 | +import java.util.Optional; |
44 | 49 | import java.util.Set; |
45 | 50 |
|
46 | 51 | import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; |
|
65 | 70 | import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.KILL_QUERY; |
66 | 71 | import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.VIEW_QUERY; |
67 | 72 | import static io.trino.testing.TestingAccessControlManager.privilege; |
| 73 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
68 | 74 | import static java.nio.charset.StandardCharsets.UTF_8; |
69 | 75 | import static org.assertj.core.api.Assertions.assertThat; |
70 | 76 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
@@ -94,6 +100,9 @@ public void setup() |
94 | 100 | { |
95 | 101 | client = new JettyHttpClient(); |
96 | 102 | server = TestingTrinoServer.create(); |
| 103 | + server.installPlugin(new MockConnectorPlugin(MockConnectorFactory.builder() |
| 104 | + .withSecuritySensitivePropertyNames(ImmutableSet.of("password")) |
| 105 | + .build())); |
97 | 106 | server.installPlugin(new TpchPlugin()); |
98 | 107 | server.createCatalog("tpch", "tpch"); |
99 | 108 | } |
@@ -226,6 +235,114 @@ public void testGetQueryInfoExecutionFailure() |
226 | 235 | assertThat(info.getFailureInfo().getErrorCode()).isEqualTo(DIVISION_BY_ZERO.toErrorCode()); |
227 | 236 | } |
228 | 237 |
|
| 238 | + @Test |
| 239 | + public void testGetQueryInfosWithRedactedSecrets() |
| 240 | + { |
| 241 | + String catalog = "catalog_" + randomNameSuffix(); |
| 242 | + runToCompletion( |
| 243 | + """ |
| 244 | + CREATE CATALOG %s USING mock |
| 245 | + WITH ( |
| 246 | + "user" = 'bob', |
| 247 | + "password" = '1234' |
| 248 | + ) |
| 249 | + """.formatted(catalog)); |
| 250 | + |
| 251 | + List<BasicQueryInfo> infos = getQueryInfos("/v1/query"); |
| 252 | + assertThat(infos.size()).isEqualTo(1); |
| 253 | + assertThat(infos.getFirst().getQuery()).isEqualTo( |
| 254 | + """ |
| 255 | + CREATE CATALOG %s USING mock |
| 256 | + WITH ( |
| 257 | + "user" = 'bob', |
| 258 | + "password" = '***' |
| 259 | + )\ |
| 260 | + """.formatted(catalog)); |
| 261 | + } |
| 262 | + |
| 263 | + @Test |
| 264 | + public void testGetQueryInfosWithRedactedSecretsInPreparedStatement() |
| 265 | + { |
| 266 | + String catalog = "catalog_" + randomNameSuffix(); |
| 267 | + runToCompletion( |
| 268 | + "EXECUTE create_catalog", |
| 269 | + Optional.of("create_catalog"), |
| 270 | + Optional.of( |
| 271 | + """ |
| 272 | + CREATE CATALOG %s USING mock |
| 273 | + WITH ( |
| 274 | + "user" = 'bob', |
| 275 | + "password" = '1234' |
| 276 | + ) |
| 277 | + """.formatted(catalog))); |
| 278 | + |
| 279 | + List<BasicQueryInfo> infos = getQueryInfos("/v1/query"); |
| 280 | + assertThat(infos.size()).isEqualTo(1); |
| 281 | + assertThat(infos.getFirst().getQuery()).isEqualTo("EXECUTE create_catalog"); |
| 282 | + assertThat(infos.getFirst().getPreparedQuery()).isEqualTo( |
| 283 | + Optional.of( |
| 284 | + """ |
| 285 | + CREATE CATALOG %s USING mock |
| 286 | + WITH ( |
| 287 | + "user" = 'bob', |
| 288 | + "password" = '***' |
| 289 | + )\ |
| 290 | + """.formatted(catalog))); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + public void testGetQueryInfoWithRedactedSecrets() |
| 295 | + { |
| 296 | + String catalog = "catalog_" + randomNameSuffix(); |
| 297 | + String queryId = runToCompletion( |
| 298 | + """ |
| 299 | + CREATE CATALOG %s USING mock |
| 300 | + WITH ( |
| 301 | + "user" = 'bob', |
| 302 | + "password" = '1234' |
| 303 | + ) |
| 304 | + """.formatted(catalog)); |
| 305 | + |
| 306 | + QueryInfo queryInfo = getQueryInfo(queryId); |
| 307 | + assertThat(queryInfo.getQuery()).isEqualTo( |
| 308 | + """ |
| 309 | + CREATE CATALOG %s USING mock |
| 310 | + WITH ( |
| 311 | + "user" = 'bob', |
| 312 | + "password" = '***' |
| 313 | + )\ |
| 314 | + """.formatted(catalog)); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + public void testGetQueryInfoWithRedactedSecretsInPreparedStatement() |
| 319 | + { |
| 320 | + String catalog = "catalog_" + randomNameSuffix(); |
| 321 | + String queryId = runToCompletion( |
| 322 | + "EXECUTE create_catalog", |
| 323 | + Optional.of("create_catalog"), |
| 324 | + Optional.of( |
| 325 | + """ |
| 326 | + CREATE CATALOG %s USING mock |
| 327 | + WITH ( |
| 328 | + "user" = 'bob', |
| 329 | + "password" = '1234' |
| 330 | + ) |
| 331 | + """.formatted(catalog))); |
| 332 | + |
| 333 | + QueryInfo queryInfo = getQueryInfo(queryId); |
| 334 | + assertThat(queryInfo.getQuery()).isEqualTo("EXECUTE create_catalog"); |
| 335 | + assertThat(queryInfo.getPreparedQuery()).isEqualTo( |
| 336 | + Optional.of( |
| 337 | + """ |
| 338 | + CREATE CATALOG %s USING mock |
| 339 | + WITH ( |
| 340 | + "user" = 'bob', |
| 341 | + "password" = '***' |
| 342 | + )\ |
| 343 | + """.formatted(catalog))); |
| 344 | + } |
| 345 | + |
229 | 346 | @Test |
230 | 347 | public void testCancel() |
231 | 348 | { |
@@ -299,13 +416,23 @@ private void testKilled(String killType) |
299 | 416 | } |
300 | 417 |
|
301 | 418 | private String runToCompletion(String sql) |
| 419 | + { |
| 420 | + return runToCompletion(sql, Optional.empty(), Optional.empty()); |
| 421 | + } |
| 422 | + |
| 423 | + private String runToCompletion(String sql, Optional<String> preparedStatementName, Optional<String> preparedStatement) |
302 | 424 | { |
303 | 425 | URI uri = uriBuilderFrom(server.getBaseUrl().resolve("/v1/statement")).build(); |
304 | | - Request request = preparePost() |
| 426 | + Request.Builder requestBuilder = preparePost() |
305 | 427 | .setHeader(TRINO_HEADERS.requestUser(), "user") |
306 | 428 | .setUri(uri) |
307 | | - .setBodyGenerator(createStaticBodyGenerator(sql, UTF_8)) |
308 | | - .build(); |
| 429 | + .setBodyGenerator(createStaticBodyGenerator(sql, UTF_8)); |
| 430 | + |
| 431 | + if (preparedStatementName.isPresent() && preparedStatement.isPresent()) { |
| 432 | + requestBuilder.setHeader(TRINO_HEADERS.requestPreparedStatement(), preparedStatementName.get() + "=" + URLEncoder.encode(preparedStatement.get(), UTF_8)); |
| 433 | + } |
| 434 | + |
| 435 | + Request request = requestBuilder.build(); |
309 | 436 | QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_JSON_CODEC)); |
310 | 437 | while (queryResults.getNextUri() != null) { |
311 | 438 | request = prepareGet() |
|
0 commit comments