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

Support ALL PRIVILEGES ON DB.* in MySQLDatabasePrivilegeChecker #34037

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
1. Sharding: Fixes avg, sum, min, max function return empty data when no query result return - [#33449](https://github.com/apache/shardingsphere/pull/33449)
1. Encrypt: Fixes merge exception without encrypt rule in database - [#33708](https://github.com/apache/shardingsphere/pull/33708)
1. SQL Binder: Fixes the expression segment cannot find the outer table when binding - [#34015](https://github.com/apache/shardingsphere/pull/34015)
1. Proxy: Fixes "ALL PRIVILEGES ON `DB`.*" is not recognized during SELECT privilege verification for MySQL - [#34037](https://github.com/apache/shardingsphere/pull/34037)

### Change Logs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private String[][] getRequiredPrivileges(final Connection connection, final Priv
}

private String[][] getSelectRequiredPrivilege(final Connection connection) throws SQLException {
return new String[][]{{"ALL PRIVILEGES", "ON *.*"}, {"SELECT", "ON *.*"}, {"SELECT", String.format("ON `%s`.*", connection.getCatalog()).toUpperCase()}};
String onCatalog = String.format("ON `%s`.*", connection.getCatalog().toUpperCase());
return new String[][]{{"ALL PRIVILEGES", "ON *.*"}, {"SELECT", "ON *.*"}, {"ALL PRIVILEGES", onCatalog}, {"SELECT", onCatalog}};
}

private boolean matchPrivileges(final String grantedPrivileges, final String[][] requiredPrivileges) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void setUp() throws SQLException {
}

@Test
void assertCheckPrivilegeWithParticularSuccess() throws SQLException {
void assertCheckPipelinePrivilegeWithParticularSuccess() throws SQLException {
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%'@'%'");
Expand All @@ -65,22 +65,22 @@ void assertCheckPrivilegeWithParticularSuccess() throws SQLException {
}

@Test
void assertCheckPrivilegeWithAllSuccess() throws SQLException {
void assertCheckPipelinePrivilegeWithAllSuccess() throws SQLException {
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES CLIENT ON *.* TO '%'@'%'");
when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.* TO '%'@'%'");
new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.PIPELINE);
verify(preparedStatement).executeQuery();
}

@Test
void assertCheckPrivilegeLackPrivileges() throws SQLException {
void assertCheckPipelinePrivilegeWithLackPrivileges() throws SQLException {
when(preparedStatement.executeQuery()).thenReturn(resultSet);
assertThrows(MissingRequiredPrivilegeException.class, () -> new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.PIPELINE));
}

@Test
void assertCheckPrivilegeFailure() throws SQLException {
void assertCheckPipelinePrivilegeFailure() throws SQLException {
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenThrow(new SQLException(""));
assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.PIPELINE));
Expand Down Expand Up @@ -127,4 +127,50 @@ void assertCheckXAPrivilegeFailureInMySQL8() throws SQLException {
when(resultSet.next()).thenThrow(new SQLException(""));
assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.XA));
}

@Test
void assertCheckSelectWithSelectPrivileges() throws SQLException {
when(dataSource.getConnection().getCatalog()).thenReturn("foo_db");
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT SELECT ON *.* TO '%'@'%'");
new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.SELECT);
verify(preparedStatement).executeQuery();
}

@Test
void assertCheckSelectWithSelectOnDatabasePrivileges() throws SQLException {
when(dataSource.getConnection().getCatalog()).thenReturn("foo_db");
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT SELECT ON `FOO_DB`.* TO '%'@'%'");
new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.SELECT);
verify(preparedStatement).executeQuery();
}

@Test
void assertCheckSelectWithAllPrivileges() throws SQLException {
when(dataSource.getConnection().getCatalog()).thenReturn("foo_db");
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.* TO '%'@'%'");
new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.SELECT);
verify(preparedStatement).executeQuery();
}

@Test
void assertCheckSelectWithAllPrivilegesOnDatabase() throws SQLException {
when(dataSource.getConnection().getCatalog()).thenReturn("foo_db");
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON `FOO_DB`.* TO '%'@'%'");
new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.SELECT);
verify(preparedStatement).executeQuery();
}

@Test
void assertCheckSelectWithLackPrivileges() throws SQLException {
when(preparedStatement.executeQuery()).thenReturn(resultSet);
assertThrows(MissingRequiredPrivilegeException.class, () -> new MySQLDatabasePrivilegeChecker().check(dataSource, PrivilegeCheckType.SELECT));
}
}
Loading