-
Notifications
You must be signed in to change notification settings - Fork 40
Backport to branch(3.15) : Add JDBC database permission test (Part 2) #2941
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| package com.scalar.db.storage.jdbc; | ||
|
|
||
| import static com.scalar.db.storage.jdbc.JdbcPermissionTestUtils.DDL_WAIT_SECONDS; | ||
|
|
||
| import com.google.common.util.concurrent.Uninterruptibles; | ||
| import com.scalar.db.api.DistributedStoragePermissionIntegrationTestBase; | ||
| import com.scalar.db.config.DatabaseConfig; | ||
| import com.scalar.db.util.AdminTestUtils; | ||
| import com.scalar.db.util.PermissionTestUtils; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class JdbcPermissionIntegrationTest extends DistributedStoragePermissionIntegrationTestBase { | ||
| private RdbEngineStrategy rdbEngine; | ||
|
|
||
| @Override | ||
| protected Properties getProperties(String testName) { | ||
| return JdbcEnv.getProperties(testName); | ||
| Properties properties = JdbcEnv.getProperties(testName); | ||
| rdbEngine = RdbEngineFactory.create(new JdbcConfig(new DatabaseConfig(properties))); | ||
| return properties; | ||
|
Comment on lines
17
to
+20
Contributor
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. The Properties properties = JdbcEnv.getProperties(testName);
if (rdbEngine == null) {
rdbEngine = RdbEngineFactory.create(new JdbcConfig(new DatabaseConfig(properties)));
}
return properties; |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -25,4 +34,12 @@ protected PermissionTestUtils getPermissionTestUtils(String testName) { | |
| protected AdminTestUtils getAdminTestUtils(String testName) { | ||
| return new JdbcAdminTestUtils(getProperties(testName)); | ||
| } | ||
|
|
||
| @Override | ||
| protected void waitForDdlCompletion() { | ||
| if (JdbcTestUtils.isYugabyte(rdbEngine)) { | ||
| // This is needed to avoid schema or catalog version mismatch database errors. | ||
| Uninterruptibles.sleepUninterruptibly(DDL_WAIT_SECONDS, TimeUnit.SECONDS); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import org.apache.commons.dbcp2.BasicDataSource; | ||
|
|
||
| public class JdbcPermissionTestUtils implements PermissionTestUtils { | ||
| public static final int DDL_WAIT_SECONDS = 1; | ||
| private final RdbEngineStrategy rdbEngine; | ||
| private final BasicDataSource dataSource; | ||
|
|
||
|
|
@@ -20,25 +21,29 @@ public JdbcPermissionTestUtils(Properties properties) { | |
|
|
||
| @Override | ||
| public void createNormalUser(String userName, String password) { | ||
| try (Connection connection = dataSource.getConnection()) { | ||
| String createUserSql = getCreateUserSql(userName, password); | ||
| try (Statement statement = connection.createStatement()) { | ||
| statement.execute(createUserSql); | ||
| if (!JdbcTestUtils.isDb2(rdbEngine)) { | ||
|
||
| try (Connection connection = dataSource.getConnection()) { | ||
| String createUserSql = getCreateUserSql(userName, password); | ||
| try (Statement statement = connection.createStatement()) { | ||
| statement.execute(createUserSql); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Failed to create user: " + userName, e); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Failed to create user: " + userName, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void dropNormalUser(String userName) { | ||
| try (Connection connection = dataSource.getConnection()) { | ||
| String dropUserSql = getDropUserSql(userName); | ||
| try (Statement statement = connection.createStatement()) { | ||
| statement.execute(dropUserSql); | ||
| if (!JdbcTestUtils.isDb2(rdbEngine)) { | ||
| try (Connection connection = dataSource.getConnection()) { | ||
| String dropUserSql = getDropUserSql(userName); | ||
| try (Statement statement = connection.createStatement()) { | ||
| statement.execute(dropUserSql); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Failed to drop user: " + userName, e); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Failed to drop user: " + userName, e); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -128,6 +133,11 @@ private String[] getGrantPermissionStatements(String userName) { | |
| String.format("ALTER ROLE [db_datareader] ADD MEMBER %s", userName), | ||
| String.format("ALTER ROLE [db_datawriter] ADD MEMBER %s", userName) | ||
| }; | ||
| } else if (JdbcTestUtils.isDb2(rdbEngine)) { | ||
| return new String[] { | ||
| String.format("GRANT DBADM ON DATABASE TO USER %s", userName), | ||
| String.format("GRANT DATAACCESS ON DATABASE TO USER %s", userName) | ||
| }; | ||
| } else { | ||
| throw new UnsupportedOperationException( | ||
| "Granting permissions is not supported for " + rdbEngine); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ public abstract class DistributedStorageAdminPermissionIntegrationTestBase { | |
| TableMetadata.newBuilder() | ||
| .addColumn(COL_NAME1, DataType.INT) | ||
| .addColumn(COL_NAME2, DataType.TEXT) | ||
| .addColumn(COL_NAME3, DataType.TEXT) | ||
| .addColumn(COL_NAME3, DataType.INT) | ||
|
Contributor
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. |
||
| .addColumn(COL_NAME4, DataType.INT) | ||
| .addPartitionKey(COL_NAME1) | ||
| .addClusteringKey(COL_NAME2, Scan.Ordering.Order.ASC) | ||
|
|
||
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.
The
rdbEnginefield is re-initialized every timegetProperties()is called. This method can be invoked multiple times during the test setup, leading to unnecessary object creation. Consider initializingrdbEngineonly once using a null check.