Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query anonymization should work in fallback log #2804

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
LOG.info(
"[{}] Request {} is not supported and falling back to old SQL engine",
QueryContext.getRequestId(),
newSqlRequest);
newSqlRequest.toString(QueryDataAnonymizer::anonymizeData));
LOG.info("Request Query: {}", QueryDataAnonymizer.anonymizeData(sqlRequest.getSql()));
QueryAction queryAction = explainRequest(client, sqlRequest, format);
executeSqlRequest(request, queryAction, client, restChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package org.opensearch.sql.legacy.unittest.utils;

import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.opensearch.sql.legacy.utils.QueryDataAnonymizer;
import org.opensearch.sql.sql.domain.SQLQueryRequest;

public class QueryDataAnonymizerTest {

Expand Down Expand Up @@ -91,4 +93,19 @@ public void unionQueriesShouldAnonymizeSensitiveData() {
+ "UNION SELECT identifier, identifier FROM table )";
Assert.assertEquals(expectedQuery, QueryDataAnonymizer.anonymizeData(query));
}

@Test
public void test_to_anonymous_string_of_SQLQueryRequest() {
String query =
"SELECT a.account_number, a.firstname, a.lastname, e.id, e.name "
+ "FROM accounts a JOIN employees e";
SQLQueryRequest request =
new SQLQueryRequest(null, query, "/_plugins/_sql", Map.of("pretty", "true"), null);
String actualQuery = request.toString(QueryDataAnonymizer::anonymizeData);
String expectedQuery =
"SQLQueryRequest(query=( SELECT identifier, identifier, identifier, identifier, identifier"
+ " FROM table a JOIN table e ), path=/_plugins/_sql, format=jdbc,"
+ " params={pretty=true}, sanitize=true, cursor=Optional.empty)";
Assert.assertEquals(expectedQuery, actualQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -148,4 +149,22 @@ private boolean shouldSanitize(Map<String, String> params) {
}
return true;
}

/** A new toString() with anonymizer parameter to anonymize its query statement. */
public String toString(Function<String, String> anonymizer) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder do you see future requirement for different anonymizer? Is toString(boolean anonymized) or copy to a new request clear enough?

return "SQLQueryRequest("
+ "query="
+ anonymizer.apply(getQuery())
+ ", path="
+ path
+ ", format="
+ format
+ ", params="
+ params
+ ", sanitize="
+ sanitize
+ ", cursor="
+ getCursor()
+ ')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ public void should_support_raw_format() {
assertTrue(csvRequest.isSupported());
}

@Test
public void test_to_string_with_anonymizer() {
SQLQueryRequest request = SQLQueryRequestBuilder.request("SELECT 1").build();
String actual = request.toString(s -> "***");
String expected =
"SQLQueryRequest(query=***, path=_plugins/_sql, format=jdbc, params={}, sanitize=true,"
+ " cursor=Optional.empty)";
assertEquals(expected, actual);
}

/** SQL query request build helper to improve test data setup readability. */
private static class SQLQueryRequestBuilder {
private String jsonContent;
Expand Down
Loading