fix(dameng): list tables for restricted schemas
This commit is contained in:
2 files changed
+286
-22
No files matched your search
@@ -23,6 +23,7 @@ import java.io.Reader;
|
|||||||
import java.sql.Blob;
|
import java.sql.Blob;
|
||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -193,31 +194,121 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
if (!constraints.includesTableLikeTypes()) {
|
if (!constraints.includesTableLikeTypes()) {
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
|
RuntimeException permissionError;
|
||||||
try {
|
try {
|
||||||
return executeConstrainedTables(buildConstrainedTablesQuery(schema, constraints), constraints);
|
return executeConstrainedTables(buildConstrainedTablesQuery(schema, constraints), constraints);
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
if (needsMaterializedViewClassification(constraints)) {
|
if (!isDamengMetadataPermissionError(e)) {
|
||||||
try {
|
throw e;
|
||||||
return executeConstrainedTables(
|
|
||||||
buildAccessibleConstrainedTablesQuery(schema, constraints),
|
|
||||||
constraints
|
|
||||||
);
|
|
||||||
} catch (RuntimeException ignored) {
|
|
||||||
// Fall through to owner-local and raw catalog fallbacks.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (needsMaterializedViewClassification(constraints) && schemaMatchesConnectedUser(schema)) {
|
permissionError = e;
|
||||||
try {
|
|
||||||
return executeConstrainedTables(
|
|
||||||
buildConstrainedTablesQuery(schema, constraints, DAMENG_USER_MATERIALIZED_VIEW_JOIN_SQL),
|
|
||||||
constraints
|
|
||||||
);
|
|
||||||
} catch (RuntimeException ignored) {
|
|
||||||
// Fall through to the raw catalog path below.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return executeRawConstrainedTables(schema, constraints);
|
|
||||||
}
|
}
|
||||||
|
if (needsMaterializedViewClassification(constraints)) {
|
||||||
|
try {
|
||||||
|
return executeConstrainedTables(
|
||||||
|
buildAccessibleConstrainedTablesQuery(schema, constraints),
|
||||||
|
constraints
|
||||||
|
);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (!isDamengMetadataPermissionError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
permissionError.addSuppressed(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (needsMaterializedViewClassification(constraints) && schemaMatchesConnectedUser(schema)) {
|
||||||
|
try {
|
||||||
|
return executeConstrainedTables(
|
||||||
|
buildConstrainedTablesQuery(schema, constraints, DAMENG_USER_MATERIALIZED_VIEW_JOIN_SQL),
|
||||||
|
constraints
|
||||||
|
);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (!isDamengMetadataPermissionError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
permissionError.addSuppressed(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return executeRawConstrainedTables(schema, constraints);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (!isDamengMetadataPermissionError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
permissionError.addSuppressed(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return executeJdbcMetadataTables(schema, constraints);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
e.addSuppressed(permissionError);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<TableInfo> executeJdbcMetadataTables(String schema, MetadataListConstraints constraints) {
|
||||||
|
return unchecked(() -> {
|
||||||
|
DatabaseMetaData metadata = requireConnected().getMetaData();
|
||||||
|
String schemaPattern = escapeJdbcMetadataPattern(metadata, schema);
|
||||||
|
List<String> supportedTypes = damengTableObjectTypes(constraints);
|
||||||
|
List<TableInfo> result = new ArrayList<>();
|
||||||
|
try (ResultSet rs = metadata.getTables(null, schemaPattern, "%", null)) {
|
||||||
|
while (rs.next()) {
|
||||||
|
String name = rs.getString("TABLE_NAME");
|
||||||
|
String tableType = normalizeObjectType(rs.getString("TABLE_TYPE"));
|
||||||
|
if (name == null || name.isBlank() || !supportedTypes.contains(tableType)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ("TABLE".equals(tableType) && name.startsWith("MTAB$_")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
result.add(new TableInfo(name, tableType, rs.getString("REMARKS")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.sort((left, right) -> left.getName().compareToIgnoreCase(right.getName()));
|
||||||
|
return constraints.filterTables(result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String escapeJdbcMetadataPattern(DatabaseMetaData metadata, String value) throws SQLException {
|
||||||
|
if (value == null || value.isEmpty()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
String escape = metadata.getSearchStringEscape();
|
||||||
|
if (escape == null || escape.isEmpty()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
.replace(escape, escape + escape)
|
||||||
|
.replace("_", escape + "_")
|
||||||
|
.replace("%", escape + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDamengMetadataPermissionError(Throwable error) {
|
||||||
|
for (Throwable current = error; current != null; current = current.getCause()) {
|
||||||
|
if (!(current instanceof SQLException sqlError)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (SQLException candidate = sqlError; candidate != null; candidate = candidate.getNextException()) {
|
||||||
|
String message = candidate.getMessage();
|
||||||
|
if (message == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String normalized = message.toLowerCase(Locale.ROOT);
|
||||||
|
boolean metadataObject = normalized.contains("all_objects")
|
||||||
|
|| normalized.contains("sysobjects")
|
||||||
|
|| normalized.contains("all_dependencies")
|
||||||
|
|| normalized.contains("all_tab_comments");
|
||||||
|
boolean permissionDenied = normalized.contains("权限")
|
||||||
|
|| normalized.contains("privilege")
|
||||||
|
|| normalized.contains("permission denied")
|
||||||
|
|| normalized.contains("access denied")
|
||||||
|
|| normalized.contains("not authorized");
|
||||||
|
if (metadataObject && permissionDenied) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TableInfo> executeConstrainedTables(MetadataQuery query, MetadataListConstraints constraints) {
|
private List<TableInfo> executeConstrainedTables(MetadataQuery query, MetadataListConstraints constraints) {
|
||||||
|
|||||||
+175
-2
@@ -16,6 +16,7 @@ import java.lang.reflect.Method;
|
|||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -86,6 +87,103 @@ class DamengAgentMetadataTest {
|
|||||||
Assertions.assertEquals("用户示例表", tables.get(0).getComment());
|
Assertions.assertEquals("用户示例表", tables.get(0).getComment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void keepsAdvancedCatalogTableListingWhenAvailable() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sqls = new ArrayList<>();
|
||||||
|
int[] jdbcMetadataCalls = {0};
|
||||||
|
TestSupport.setPrivateConnection(agent, advancedTableConnection(sqls, jdbcMetadataCalls));
|
||||||
|
|
||||||
|
List<TableInfo> tables = agent.listTables("APP");
|
||||||
|
|
||||||
|
Assertions.assertEquals(List.of("USERS"), tables.stream().map(TableInfo::getName).toList());
|
||||||
|
Assertions.assertEquals("用户示例表", tables.get(0).getComment());
|
||||||
|
Assertions.assertEquals(0, jdbcMetadataCalls[0]);
|
||||||
|
Assertions.assertEquals(1, sqls.size(), String.join("\n", sqls));
|
||||||
|
Assertions.assertTrue(sqls.get(0).contains("SYS.SYSOBJECTS materialized_view"), sqls.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fallsBackToJdbcMetadataForRestrictedSchemaTables() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sqls = new ArrayList<>();
|
||||||
|
List<String> jdbcMetadataCalls = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, restrictedTableConnection(
|
||||||
|
sqls,
|
||||||
|
jdbcMetadataCalls,
|
||||||
|
List.of(
|
||||||
|
List.of("VIEW_B", "VIEW", "view comment"),
|
||||||
|
List.of("TABLE_A", "TABLE", "table comment"),
|
||||||
|
List.of("MV_C", "MATERIALIZED VIEW", "mv comment"),
|
||||||
|
List.of("APP_PROC", "PROCEDURE", "procedure comment"),
|
||||||
|
List.of("MTAB$_INTERNAL", "TABLE", "internal table")
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
"没有[SYS.ALL_OBJECTS]对象的查询权限"
|
||||||
|
));
|
||||||
|
setConnectedUsername(agent, "APP_DATA%2026");
|
||||||
|
|
||||||
|
List<TableInfo> tables = agent.listTables("APP_DATA%2026");
|
||||||
|
|
||||||
|
Assertions.assertEquals(List.of("MV_C", "TABLE_A", "VIEW_B"), tables.stream().map(TableInfo::getName).toList());
|
||||||
|
Assertions.assertEquals(List.of("MATERIALIZED_VIEW", "TABLE", "VIEW"), tables.stream().map(TableInfo::getTable_type).toList());
|
||||||
|
Assertions.assertEquals(List.of("mv comment", "table comment", "view comment"), tables.stream().map(TableInfo::getComment).toList());
|
||||||
|
Assertions.assertEquals(4, sqls.size(), String.join("\n", sqls));
|
||||||
|
Assertions.assertTrue(sqls.stream().allMatch(sql -> sql.contains("ALL_OBJECTS")), String.join("\n", sqls));
|
||||||
|
Assertions.assertEquals(List.of("catalog=null,schema=APP\\_DATA\\%2026,table=%,types=null"), jdbcMetadataCalls);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsEmptyWhenRestrictedSchemaJdbcMetadataHasNoTables() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
TestSupport.setPrivateConnection(agent, restrictedTableConnection(
|
||||||
|
new ArrayList<>(),
|
||||||
|
new ArrayList<>(),
|
||||||
|
List.of(),
|
||||||
|
null,
|
||||||
|
"no SYS.ALL_OBJECTS privilege"
|
||||||
|
));
|
||||||
|
|
||||||
|
Assertions.assertTrue(agent.listTables("APP").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotFallbackForNonPermissionTableMetadataErrors() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sqls = new ArrayList<>();
|
||||||
|
List<String> jdbcMetadataCalls = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, restrictedTableConnection(
|
||||||
|
sqls,
|
||||||
|
jdbcMetadataCalls,
|
||||||
|
List.of(),
|
||||||
|
null,
|
||||||
|
"ALL_OBJECTS metadata query timed out"
|
||||||
|
));
|
||||||
|
|
||||||
|
RuntimeException error = Assertions.assertThrows(RuntimeException.class, () -> agent.listTables("APP"));
|
||||||
|
|
||||||
|
Assertions.assertEquals("ALL_OBJECTS metadata query timed out", error.getCause().getMessage());
|
||||||
|
Assertions.assertEquals(1, sqls.size(), String.join("\n", sqls));
|
||||||
|
Assertions.assertTrue(jdbcMetadataCalls.isEmpty(), jdbcMetadataCalls.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void propagatesJdbcMetadataFallbackErrors() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
TestSupport.setPrivateConnection(agent, restrictedTableConnection(
|
||||||
|
new ArrayList<>(),
|
||||||
|
new ArrayList<>(),
|
||||||
|
List.of(),
|
||||||
|
new SQLException("JDBC metadata getTables failed"),
|
||||||
|
"没有[SYS.ALL_OBJECTS]对象的查询权限"
|
||||||
|
));
|
||||||
|
|
||||||
|
RuntimeException error = Assertions.assertThrows(RuntimeException.class, () -> agent.listTables("APP"));
|
||||||
|
|
||||||
|
Assertions.assertEquals("JDBC metadata getTables failed", error.getCause().getMessage());
|
||||||
|
Assertions.assertEquals(1, error.getSuppressed().length);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void mapsTableCommentToObjectMetadata() {
|
void mapsTableCommentToObjectMetadata() {
|
||||||
DamengAgent agent = new DamengAgent();
|
DamengAgent agent = new DamengAgent();
|
||||||
@@ -533,7 +631,7 @@ class DamengAgentMetadataTest {
|
|||||||
if (sql.contains("DBMS_METADATA.GET_DDL")) {
|
if (sql.contains("DBMS_METADATA.GET_DDL")) {
|
||||||
return dbmsMetadataStatement(dbmsMetadataDdl, dbmsMetadataResultOpen);
|
return dbmsMetadataStatement(dbmsMetadataDdl, dbmsMetadataResultOpen);
|
||||||
}
|
}
|
||||||
if (sql.contains("SYS.SYSOBJECTS") && sql.contains("TYPE$ = 'SCH'")) {
|
if (sql.startsWith("SELECT NAME FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH'")) {
|
||||||
return metadataStatement(List.of(List.of("APP"), List.of("EMPTY_SCHEMA"), List.of("SYSDBA")));
|
return metadataStatement(List.of(List.of("APP"), List.of("EMPTY_SCHEMA"), List.of("SYSDBA")));
|
||||||
}
|
}
|
||||||
if (sql.contains("ALL_CONS_COLUMNS")) {
|
if (sql.contains("ALL_CONS_COLUMNS")) {
|
||||||
@@ -722,6 +820,81 @@ class DamengAgentMetadataTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Connection advancedTableConnection(List<String> sqls, int[] jdbcMetadataCalls) {
|
||||||
|
return proxy(Connection.class, (method, args) -> {
|
||||||
|
String name = method.getName();
|
||||||
|
if ("prepareStatement".equals(name)) {
|
||||||
|
String sql = (String) args[0];
|
||||||
|
sqls.add(sql);
|
||||||
|
return metadataStatement(List.of(List.of("USERS", "TABLE", "用户示例表")));
|
||||||
|
}
|
||||||
|
if ("getMetaData".equals(name)) {
|
||||||
|
jdbcMetadataCalls[0] += 1;
|
||||||
|
return jdbcTableMetadata(new ArrayList<>(), List.of(), null);
|
||||||
|
}
|
||||||
|
if ("close".equals(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("isClosed".equals(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Connection restrictedTableConnection(
|
||||||
|
List<String> sqls,
|
||||||
|
List<String> jdbcMetadataCalls,
|
||||||
|
List<List<Object>> rows,
|
||||||
|
SQLException jdbcMetadataError,
|
||||||
|
String catalogError
|
||||||
|
) {
|
||||||
|
return proxy(Connection.class, (method, args) -> {
|
||||||
|
String name = method.getName();
|
||||||
|
if ("prepareStatement".equals(name)) {
|
||||||
|
String sql = (String) args[0];
|
||||||
|
sqls.add(sql);
|
||||||
|
return failingMetadataStatement(catalogError);
|
||||||
|
}
|
||||||
|
if ("getMetaData".equals(name)) {
|
||||||
|
return jdbcTableMetadata(jdbcMetadataCalls, rows, jdbcMetadataError);
|
||||||
|
}
|
||||||
|
if ("close".equals(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("isClosed".equals(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DatabaseMetaData jdbcTableMetadata(
|
||||||
|
List<String> calls,
|
||||||
|
List<List<Object>> rows,
|
||||||
|
SQLException failure
|
||||||
|
) {
|
||||||
|
return proxy(DatabaseMetaData.class, (method, args) -> {
|
||||||
|
String name = method.getName();
|
||||||
|
if ("getSearchStringEscape".equals(name)) {
|
||||||
|
return "\\";
|
||||||
|
}
|
||||||
|
if ("getTables".equals(name)) {
|
||||||
|
calls.add(
|
||||||
|
"catalog=" + args[0]
|
||||||
|
+ ",schema=" + args[1]
|
||||||
|
+ ",table=" + args[2]
|
||||||
|
+ ",types=" + args[3]
|
||||||
|
);
|
||||||
|
if (failure != null) {
|
||||||
|
throw failure;
|
||||||
|
}
|
||||||
|
return metadataResultSet(rows);
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static Connection restrictedSchemaConnection(List<String> sqls) {
|
private static Connection restrictedSchemaConnection(List<String> sqls) {
|
||||||
return proxy(Connection.class, (method, args) -> {
|
return proxy(Connection.class, (method, args) -> {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
@@ -825,7 +998,7 @@ class DamengAgentMetadataTest {
|
|||||||
case "NULLABLE" -> string(rows, index[0], 2);
|
case "NULLABLE" -> string(rows, index[0], 2);
|
||||||
case "DATA_DEFAULT" -> null;
|
case "DATA_DEFAULT" -> null;
|
||||||
case "CHAR_USED" -> string(rows, index[0], 7);
|
case "CHAR_USED" -> string(rows, index[0], 7);
|
||||||
case "COMMENTS", "COMMENT$" -> string(rows, index[0], rows.get(index[0]).size() - 1);
|
case "COMMENTS", "COMMENT$", "REMARKS" -> string(rows, index[0], rows.get(index[0]).size() - 1);
|
||||||
case "COLNAME" -> string(rows, index[0], 0);
|
case "COLNAME" -> string(rows, index[0], 0);
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user