|
17 | 17 |
|
18 | 18 | package org.apache.doris.nereids.trees.plans.commands; |
19 | 19 |
|
| 20 | +import org.apache.doris.analysis.TablePattern; |
20 | 21 | import org.apache.doris.backup.CatalogMocker; |
21 | 22 | import org.apache.doris.catalog.Env; |
| 23 | +import org.apache.doris.common.AnalysisException; |
22 | 24 | import org.apache.doris.datasource.CatalogMgr; |
| 25 | +import org.apache.doris.datasource.ExternalCatalog; |
23 | 26 | import org.apache.doris.datasource.InternalCatalog; |
24 | 27 | import org.apache.doris.mysql.privilege.AccessControllerManager; |
| 28 | +import org.apache.doris.mysql.privilege.ColPrivilegeKey; |
| 29 | +import org.apache.doris.mysql.privilege.PrivBitSet; |
| 30 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 31 | +import org.apache.doris.mysql.privilege.Privilege; |
| 32 | +import org.apache.doris.mysql.privilege.Role; |
25 | 33 | import org.apache.doris.nereids.analyzer.UnboundSlot; |
26 | 34 | import org.apache.doris.nereids.trees.expressions.EqualTo; |
27 | 35 | import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; |
28 | 36 | import org.apache.doris.qe.ConnectContext; |
| 37 | +import org.apache.doris.qe.ShowResultSet; |
29 | 38 |
|
30 | 39 | import org.junit.jupiter.api.AfterEach; |
31 | 40 | import org.junit.jupiter.api.Assertions; |
32 | 41 | import org.junit.jupiter.api.BeforeEach; |
33 | 42 | import org.junit.jupiter.api.Test; |
| 43 | +import org.mockito.InOrder; |
34 | 44 | import org.mockito.MockedStatic; |
35 | 45 | import org.mockito.Mockito; |
36 | 46 |
|
| 47 | +import java.util.Arrays; |
| 48 | +import java.util.Collections; |
| 49 | + |
37 | 50 | public class ShowDatabasesCommandTest { |
38 | 51 | private static final String internalCtl = InternalCatalog.INTERNAL_CATALOG_NAME; |
39 | 52 |
|
@@ -80,5 +93,84 @@ void testValidate() { |
80 | 93 | ShowDatabasesCommand command2 = new ShowDatabasesCommand(null, null, equalTo); |
81 | 94 | Assertions.assertDoesNotThrow(() -> command2.validate(ctx)); |
82 | 95 | } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testDeniedExternalCatalogNeverLoadsMetadata() throws Exception { |
| 99 | + ExternalCatalog external = Mockito.mock(ExternalCatalog.class); |
| 100 | + Mockito.when(ctx.getCatalog("lake")).thenReturn(external); |
| 101 | + Mockito.when(ctx.getDefaultCatalog()).thenReturn("lake"); |
| 102 | + Mockito.when(external.getDbNames()).thenThrow(new IllegalStateException("remote connection details")); |
| 103 | + EqualTo where = new EqualTo(new UnboundSlot("Database"), new StringLiteral("visible")); |
| 104 | + for (ShowDatabasesCommand command : Arrays.asList( |
| 105 | + new ShowDatabasesCommand("lake", null, null), |
| 106 | + new ShowDatabasesCommand(null, null, null), |
| 107 | + new ShowDatabasesCommand("lake", "vis%", null), |
| 108 | + new ShowDatabasesCommand("lake", null, where))) { |
| 109 | + AnalysisException error = Assertions.assertThrows(AnalysisException.class, |
| 110 | + () -> command.doRun(ctx, null)); |
| 111 | + Assertions.assertFalse(error.getMessage().contains("remote connection details")); |
| 112 | + } |
| 113 | + Mockito.verify(external, Mockito.never()).getDbNames(); |
| 114 | + Mockito.verify(accessManager, Mockito.times(4)).checkCtlPriv(ctx, "lake", PrivPredicate.SHOW); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testVisibleExternalCatalogStillFiltersDatabases() throws Exception { |
| 119 | + ExternalCatalog external = Mockito.mock(ExternalCatalog.class); |
| 120 | + Mockito.when(ctx.getCatalog("lake")).thenReturn(external); |
| 121 | + Mockito.when(accessManager.checkCtlPriv(ctx, "lake", PrivPredicate.SHOW)).thenReturn(true); |
| 122 | + Mockito.when(external.getDbNames()).thenReturn(Arrays.asList("visible", "hidden", "other")); |
| 123 | + Mockito.when(accessManager.checkDbPriv(Mockito.nullable(ConnectContext.class), |
| 124 | + Mockito.eq("lake"), Mockito.eq("visible"), Mockito.eq(PrivPredicate.SHOW))).thenReturn(true); |
| 125 | + ShowResultSet result = new ShowDatabasesCommand("lake", null, null).doRun(ctx, null); |
| 126 | + Assertions.assertEquals(Collections.singletonList(Collections.singletonList("visible")), |
| 127 | + result.getResultRows()); |
| 128 | + InOrder order = Mockito.inOrder(accessManager, external); |
| 129 | + order.verify(accessManager).checkCtlPriv(ctx, "lake", PrivPredicate.SHOW); |
| 130 | + order.verify(external).getDbNames(); |
| 131 | + Assertions.assertEquals(result.getResultRows(), |
| 132 | + new ShowDatabasesCommand("lake", "vis%", null).doRun(ctx, null).getResultRows()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testInternalCatalogDoesNotRequireCatalogPrivilege() throws Exception { |
| 137 | + Mockito.when(catalog.isInternalCatalog()).thenReturn(true); |
| 138 | + Mockito.when(ctx.getCatalog(internalCtl)).thenReturn(catalog); |
| 139 | + Mockito.when(catalog.getDbNames()).thenReturn(Collections.emptyList()); |
| 140 | + Assertions.assertTrue(new ShowDatabasesCommand(internalCtl, null, null) |
| 141 | + .doRun(ctx, null).getResultRows().isEmpty()); |
| 142 | + Mockito.verify(accessManager, Mockito.never()).checkCtlPriv(Mockito.any(ConnectContext.class), |
| 143 | + Mockito.anyString(), Mockito.any(PrivPredicate.class)); |
| 144 | + Mockito.verify(catalog).getDbNames(); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void testMissingCatalogKeepsExplicitError() { |
| 149 | + AnalysisException error = Assertions.assertThrows(AnalysisException.class, |
| 150 | + () -> new ShowDatabasesCommand("missing", null, null).doRun(ctx, null)); |
| 151 | + Assertions.assertTrue(error.getMessage().contains("No catalog found")); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testDatabaseTableAndColumnGrantsKeepCatalogVisible() throws Exception { |
| 156 | + ExternalCatalog external = Mockito.mock(ExternalCatalog.class); |
| 157 | + Mockito.when(ctx.getCatalog("lake")).thenReturn(external); |
| 158 | + Mockito.when(external.getDbNames()).thenReturn(Arrays.asList("visible", "hidden")); |
| 159 | + for (String level : Arrays.asList("database", "table", "column")) { |
| 160 | + TablePattern pattern = new TablePattern("lake", "visible", level.equals("database") ? "*" : "t"); |
| 161 | + pattern.analyze(); |
| 162 | + Role role = level.equals("column") |
| 163 | + ? new Role("reader", pattern, PrivBitSet.of(), Collections.singletonMap( |
| 164 | + new ColPrivilegeKey(Privilege.SELECT_PRIV, "lake", "visible", "t"), |
| 165 | + Collections.singleton("c"))) |
| 166 | + : new Role("reader", pattern, PrivBitSet.of(Privilege.SELECT_PRIV)); |
| 167 | + Mockito.when(accessManager.checkCtlPriv(ctx, "lake", PrivPredicate.SHOW)) |
| 168 | + .thenAnswer(invocation -> role.checkCtlPriv("lake", PrivPredicate.SHOW, PrivBitSet.of())); |
| 169 | + Mockito.when(accessManager.checkDbPriv(ctx, "lake", "visible", PrivPredicate.SHOW)) |
| 170 | + .thenAnswer(invocation -> role.checkDbPriv("lake", "visible", PrivPredicate.SHOW, PrivBitSet.of())); |
| 171 | + Assertions.assertEquals(Collections.singletonList(Collections.singletonList("visible")), |
| 172 | + new ShowDatabasesCommand("lake", null, null).doRun(ctx, null).getResultRows(), level); |
| 173 | + } |
| 174 | + } |
83 | 175 | } |
84 | 176 |
|
0 commit comments