-
Notifications
You must be signed in to change notification settings - Fork 623
[JDBC-v2] Readonly Profile tests #2883
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
+119
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a090c0
Added readonly profile tests
chernser ec56786
fixed security
chernser c02ea43
updated docs/ai-review.md about creating users in tests and what to v…
chernser c8ce979
fixed created user and profile to cleanup first
chernser 0c27309
fixed typos
chernser 36b8079
fixed password for cloud
chernser 74d2073
skip tests in cloud because of configuration limitations
chernser c170d3d
Fixed setup test for cloud. Sorry.
chernser 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
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
102 changes: 102 additions & 0 deletions
102
jdbc-v2/src/test/java/com/clickhouse/jdbc/ReadonlyProfileTest.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,102 @@ | ||
| package com.clickhouse.jdbc; | ||
|
|
||
| import com.clickhouse.client.api.ClientConfigProperties; | ||
| import com.clickhouse.client.api.internal.ServerSettings; | ||
| import org.testng.Assert; | ||
| import org.testng.SkipException; | ||
| import org.testng.annotations.AfterClass; | ||
| import org.testng.annotations.BeforeClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.Properties; | ||
| import java.util.UUID; | ||
|
|
||
| public class ReadonlyProfileTest extends JdbcIntegrationTest { | ||
|
|
||
| private String password; | ||
|
|
||
| @BeforeClass(groups = { "integration" }) | ||
| public void setup() throws SQLException { | ||
| if (isCloud()) { | ||
| return; | ||
| } | ||
| // Append fixed character classes so the random password satisfies server | ||
| // password-complexity policies (uppercase, lowercase, digit, special). | ||
| password = UUID.randomUUID().toString() + "Aa1!"; | ||
| com.clickhouse.client.ClickHouseServerForTest.beforeSuite(); | ||
| try (Connection conn = getJdbcConnection(); | ||
| Statement stmt = conn.createStatement()) { | ||
| stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS jdbc_test_profile_readonly_1 SETTINGS readonly=1"); | ||
| stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS jdbc_test_profile_readonly_2 SETTINGS readonly=2"); | ||
| stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_1"); | ||
| stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_2"); | ||
| stmt.execute("CREATE USER jdbc_test_user_readonly_1 IDENTIFIED WITH plaintext_password BY '" + password + "' SETTINGS PROFILE jdbc_test_profile_readonly_1"); | ||
| stmt.execute("CREATE USER jdbc_test_user_readonly_2 IDENTIFIED WITH plaintext_password BY '" + password + "' SETTINGS PROFILE jdbc_test_profile_readonly_2"); | ||
| String db = getDatabase(); | ||
| stmt.execute("GRANT SELECT ON " + db + ".* TO jdbc_test_user_readonly_1"); | ||
| stmt.execute("GRANT SELECT ON " + db + ".* TO jdbc_test_user_readonly_2"); | ||
| } | ||
| } | ||
|
|
||
| @AfterClass(groups = { "integration" }) | ||
| public void teardown() throws SQLException { | ||
| if (isCloud()) { | ||
| return; | ||
| } | ||
| try (Connection conn = getJdbcConnection(); | ||
| Statement stmt = conn.createStatement()) { | ||
| stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_1"); | ||
| stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_2"); | ||
| stmt.execute("DROP SETTINGS PROFILE IF EXISTS jdbc_test_profile_readonly_1"); | ||
| stmt.execute("DROP SETTINGS PROFILE IF EXISTS jdbc_test_profile_readonly_2"); | ||
| } | ||
| } | ||
|
|
||
| @Test(groups = { "integration" }) | ||
| public void testReadonly1CannotChangeSettings() throws Exception { | ||
| if (isCloud()) { | ||
| throw new SkipException("Should not be tested in cloud because creates users and profiles"); | ||
| } | ||
| Properties properties = new Properties(); | ||
| properties.setProperty("user", "jdbc_test_user_readonly_1"); | ||
| properties.setProperty("password", password); | ||
| properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1"); | ||
|
|
||
| try (Connection conn = getJdbcConnection(properties)) { | ||
| try (Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery("SELECT 1")) { | ||
| Assert.fail("Should have thrown an exception because readonly=1 prevents changing settings"); | ||
| } | ||
| } catch (SQLException e) { | ||
| Assert.assertTrue(e.getMessage().contains("Cannot modify"), "Exception message should indicate setting cannot be modified: " + e.getMessage()); | ||
|
chernser marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| @Test(groups = { "integration" }) | ||
| public void testReadonly2CanChangeSettings() throws Exception { | ||
| if (isCloud()) { | ||
| throw new SkipException("Should not be tested in cloud because creates users and profiles"); | ||
| } | ||
| if (isVersionMatch("(,24.8]")) { | ||
| throw new SkipException("Old versions do not support JSON"); | ||
| } | ||
|
|
||
| Properties properties = new Properties(); | ||
| properties.setProperty("user", "jdbc_test_user_readonly_2"); | ||
| properties.setProperty("password", password); | ||
| properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1"); | ||
| properties.put(ClientConfigProperties.serverSetting("allow_experimental_json_type"), "1"); | ||
|
|
||
| try (Connection conn = getJdbcConnection(properties)) { | ||
| try (Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery("SELECT '{\"key\":\"value\"}'::JSON")) { | ||
| Assert.assertTrue(rs.next()); | ||
| Assert.assertEquals(rs.getString(1), "{\"key\":\"value\"}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.