-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: DATABASE() and USER() system functions #35946
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f30ce8c
Base implementation for User and Database functions
astefan 4d4a76a
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan a47952f
Polishing
astefan 292bf60
Added tests
astefan e5e5a13
More tests
astefan 64cbd71
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan 6e4edc8
More polishing
astefan 1b6a406
* documentation for DATABASE and USER functions
astefan ec94f69
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan e8ea027
Switched from /_xpack/sql to /sql
astefan 706652f
Made Transports final
astefan 953d634
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
...sql/qa/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/UserFunctionIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.qa.security; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.test.NotEqualMessageBuilder; | ||
import org.elasticsearch.test.rest.ESRestTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.sql.JDBCType; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo; | ||
|
||
public class UserFunctionIT extends ESRestTestCase { | ||
astefan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final String SQL = "SELECT USER()"; | ||
// role defined in roles.yml | ||
private static final String MINIMAL_ACCESS_ROLE = "rest_minimal"; | ||
|
||
@Override | ||
protected Settings restClientSettings() { | ||
return RestSqlIT.securitySettings(); | ||
} | ||
|
||
@Override | ||
protected String getProtocol() { | ||
return RestSqlIT.SSL_ENABLED ? "https" : "http"; | ||
} | ||
|
||
public void testSingleRandomUser() throws IOException { | ||
String randomUserName = randomAlphaOfLengthBetween(1, 15); | ||
String mode = randomMode().toString(); | ||
createUser(randomUserName, MINIMAL_ACCESS_ROLE); | ||
|
||
Map<String, Object> expected = new HashMap<>(); | ||
expected.put("columns", Arrays.asList( | ||
columnInfo(mode, "USER", "keyword", JDBCType.VARCHAR, 0))); | ||
expected.put("rows", Arrays.asList(Arrays.asList(randomUserName))); | ||
Map<String, Object> actual = runSql(randomUserName, mode, SQL); | ||
|
||
assertResponse(expected, actual); | ||
} | ||
|
||
public void testMultipleRandomUsersAccess() throws IOException { | ||
// create a random number of users | ||
int usersCount = randomIntBetween(5, 15); | ||
Set<String> users = new HashSet<String>(); | ||
for(int i = 0; i < usersCount; i++) { | ||
String randomUserName = randomAlphaOfLengthBetween(1, 15); | ||
users.add(randomUserName); | ||
createUser(randomUserName, MINIMAL_ACCESS_ROLE); | ||
} | ||
|
||
// run 30 queries and pick randomly each time one of the 5-15 users created previously | ||
for (int i = 0; i < 30; i++) { | ||
String mode = randomMode().toString(); | ||
String randomlyPickedUsername = randomFrom(users); | ||
Map<String, Object> expected = new HashMap<>(); | ||
|
||
expected.put("columns", Arrays.asList( | ||
columnInfo(mode, "USER", "keyword", JDBCType.VARCHAR, 0))); | ||
expected.put("rows", Arrays.asList(Arrays.asList(randomlyPickedUsername))); | ||
Map<String, Object> actual = runSql(randomlyPickedUsername, mode, SQL); | ||
|
||
// expect the user that ran the query to be the same as the one returned by the `USER()` function | ||
assertResponse(expected, actual); | ||
} | ||
} | ||
|
||
public void testSelectUserFromIndex() throws IOException { | ||
index("{\"test\":\"doc1\"}", | ||
"{\"test\":\"doc2\"}", | ||
"{\"test\":\"doc3\"}"); | ||
String randomUserName = randomAlphaOfLengthBetween(1, 15); | ||
String mode = randomMode().toString(); | ||
createUser(randomUserName, MINIMAL_ACCESS_ROLE); | ||
|
||
Map<String, Object> expected = new HashMap<>(); | ||
expected.put("columns", Arrays.asList( | ||
columnInfo(mode, "USER", "keyword", JDBCType.VARCHAR, 0))); | ||
expected.put("rows", Arrays.asList(Arrays.asList(randomUserName), | ||
Arrays.asList(randomUserName), | ||
Arrays.asList(randomUserName))); | ||
Map<String, Object> actual = runSql(randomUserName, mode, "SELECT USER() FROM test"); | ||
|
||
assertResponse(expected, actual); | ||
} | ||
|
||
private void createUser(String name, String role) throws IOException { | ||
Request request = new Request("PUT", "/_xpack/security/user/" + name); | ||
XContentBuilder user = JsonXContent.contentBuilder().prettyPrint(); | ||
user.startObject(); { | ||
user.field("password", "testpass"); | ||
user.field("roles", role); | ||
} | ||
user.endObject(); | ||
request.setJsonEntity(Strings.toString(user)); | ||
client().performRequest(request); | ||
} | ||
|
||
private Map<String, Object> runSql(@Nullable String asUser, String mode, String sql) throws IOException { | ||
astefan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return runSql(asUser, mode, new StringEntity("{\"query\": \"" + sql + "\"}", ContentType.APPLICATION_JSON)); | ||
} | ||
|
||
private Map<String, Object> runSql(@Nullable String asUser, String mode, HttpEntity entity) throws IOException { | ||
astefan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Request request = new Request("POST", "/_xpack/sql"); | ||
if (false == mode.isEmpty()) { | ||
request.addParameter("mode", mode); | ||
} | ||
if (asUser != null) { | ||
RequestOptions.Builder options = request.getOptions().toBuilder(); | ||
options.addHeader("es-security-runas-user", asUser); | ||
request.setOptions(options); | ||
} | ||
request.setEntity(entity); | ||
return toMap(client().performRequest(request)); | ||
} | ||
|
||
private void assertResponse(Map<String, Object> expected, Map<String, Object> actual) { | ||
if (false == expected.equals(actual)) { | ||
NotEqualMessageBuilder message = new NotEqualMessageBuilder(); | ||
message.compareMaps(actual, expected); | ||
fail("Response does not match:\n" + message.toString()); | ||
} | ||
} | ||
|
||
private static Map<String, Object> toMap(Response response) throws IOException { | ||
try (InputStream content = response.getEntity().getContent()) { | ||
return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false); | ||
} | ||
} | ||
|
||
private String randomMode() { | ||
return randomFrom("plain", "jdbc", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the Mode enum? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mode is not visible in the |
||
} | ||
|
||
private void index(String... docs) throws IOException { | ||
Request request = new Request("POST", "/test/test/_bulk"); | ||
request.addParameter("refresh", "true"); | ||
StringBuilder bulk = new StringBuilder(); | ||
for (String doc : docs) { | ||
bulk.append("{\"index\":{}\n"); | ||
bulk.append(doc + "\n"); | ||
} | ||
request.setJsonEntity(bulk.toString()); | ||
client().performRequest(request); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tests! I have some questions/comments though:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests with WHERE.
null
user is handled in https://github.com/elastic/elasticsearch/pull/35946/files#diff-36793f91ca7279a48976a3247c18272fR32. Good point about clashing users, so I've changed the way the usernames are managed.