|
| 1 | +package com.clickhouse.jdbc; |
| 2 | + |
| 3 | +import com.clickhouse.client.api.ClientConfigProperties; |
| 4 | +import com.clickhouse.client.api.internal.ServerSettings; |
| 5 | +import org.testng.Assert; |
| 6 | +import org.testng.SkipException; |
| 7 | +import org.testng.annotations.AfterClass; |
| 8 | +import org.testng.annotations.BeforeClass; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.ResultSet; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.sql.Statement; |
| 15 | +import java.util.Properties; |
| 16 | +import java.util.UUID; |
| 17 | + |
| 18 | +public class ReadonlyProfileTest extends JdbcIntegrationTest { |
| 19 | + |
| 20 | + private String password; |
| 21 | + |
| 22 | + @BeforeClass(groups = { "integration" }) |
| 23 | + public void setup() throws SQLException { |
| 24 | + if (isCloud()) { |
| 25 | + return; |
| 26 | + } |
| 27 | + // Append fixed character classes so the random password satisfies server |
| 28 | + // password-complexity policies (uppercase, lowercase, digit, special). |
| 29 | + password = UUID.randomUUID().toString() + "Aa1!"; |
| 30 | + com.clickhouse.client.ClickHouseServerForTest.beforeSuite(); |
| 31 | + try (Connection conn = getJdbcConnection(); |
| 32 | + Statement stmt = conn.createStatement()) { |
| 33 | + stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS jdbc_test_profile_readonly_1 SETTINGS readonly=1"); |
| 34 | + stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS jdbc_test_profile_readonly_2 SETTINGS readonly=2"); |
| 35 | + stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_1"); |
| 36 | + stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_2"); |
| 37 | + stmt.execute("CREATE USER jdbc_test_user_readonly_1 IDENTIFIED WITH plaintext_password BY '" + password + "' SETTINGS PROFILE jdbc_test_profile_readonly_1"); |
| 38 | + stmt.execute("CREATE USER jdbc_test_user_readonly_2 IDENTIFIED WITH plaintext_password BY '" + password + "' SETTINGS PROFILE jdbc_test_profile_readonly_2"); |
| 39 | + String db = getDatabase(); |
| 40 | + stmt.execute("GRANT SELECT ON " + db + ".* TO jdbc_test_user_readonly_1"); |
| 41 | + stmt.execute("GRANT SELECT ON " + db + ".* TO jdbc_test_user_readonly_2"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @AfterClass(groups = { "integration" }) |
| 46 | + public void teardown() throws SQLException { |
| 47 | + if (isCloud()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + try (Connection conn = getJdbcConnection(); |
| 51 | + Statement stmt = conn.createStatement()) { |
| 52 | + stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_1"); |
| 53 | + stmt.execute("DROP USER IF EXISTS jdbc_test_user_readonly_2"); |
| 54 | + stmt.execute("DROP SETTINGS PROFILE IF EXISTS jdbc_test_profile_readonly_1"); |
| 55 | + stmt.execute("DROP SETTINGS PROFILE IF EXISTS jdbc_test_profile_readonly_2"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test(groups = { "integration" }) |
| 60 | + public void testReadonly1CannotChangeSettings() throws Exception { |
| 61 | + if (isCloud()) { |
| 62 | + throw new SkipException("Should not be tested in cloud because creates users and profiles"); |
| 63 | + } |
| 64 | + Properties properties = new Properties(); |
| 65 | + properties.setProperty("user", "jdbc_test_user_readonly_1"); |
| 66 | + properties.setProperty("password", password); |
| 67 | + properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1"); |
| 68 | + |
| 69 | + try (Connection conn = getJdbcConnection(properties)) { |
| 70 | + try (Statement stmt = conn.createStatement(); |
| 71 | + ResultSet rs = stmt.executeQuery("SELECT 1")) { |
| 72 | + Assert.fail("Should have thrown an exception because readonly=1 prevents changing settings"); |
| 73 | + } |
| 74 | + } catch (SQLException e) { |
| 75 | + Assert.assertTrue(e.getMessage().contains("Cannot modify"), "Exception message should indicate setting cannot be modified: " + e.getMessage()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test(groups = { "integration" }) |
| 80 | + public void testReadonly2CanChangeSettings() throws Exception { |
| 81 | + if (isCloud()) { |
| 82 | + throw new SkipException("Should not be tested in cloud because creates users and profiles"); |
| 83 | + } |
| 84 | + if (isVersionMatch("(,24.8]")) { |
| 85 | + throw new SkipException("Old versions do not support JSON"); |
| 86 | + } |
| 87 | + |
| 88 | + Properties properties = new Properties(); |
| 89 | + properties.setProperty("user", "jdbc_test_user_readonly_2"); |
| 90 | + properties.setProperty("password", password); |
| 91 | + properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1"); |
| 92 | + properties.put(ClientConfigProperties.serverSetting("allow_experimental_json_type"), "1"); |
| 93 | + |
| 94 | + try (Connection conn = getJdbcConnection(properties)) { |
| 95 | + try (Statement stmt = conn.createStatement(); |
| 96 | + ResultSet rs = stmt.executeQuery("SELECT '{\"key\":\"value\"}'::JSON")) { |
| 97 | + Assert.assertTrue(rs.next()); |
| 98 | + Assert.assertEquals(rs.getString(1), "{\"key\":\"value\"}"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments