Skip to content

Commit 199dce7

Browse files
committed
[fix](auth) Check external catalog visibility before SHOW DATABASES
1 parent 7a5d64e commit 199dce7

2 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowDatabasesCommand.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.doris.catalog.info.TableNameInfo;
2525
import org.apache.doris.common.AnalysisException;
2626
import org.apache.doris.common.CaseSensibility;
27+
import org.apache.doris.common.ErrorCode;
28+
import org.apache.doris.common.ErrorReport;
2729
import org.apache.doris.common.PatternMatcher;
2830
import org.apache.doris.common.PatternMatcherWrapper;
2931
import org.apache.doris.datasource.CatalogIf;
@@ -121,17 +123,24 @@ public ShowResultSetMetaData getMetaData() {
121123
@Override
122124
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
123125
validate(ctx);
126+
CatalogIf catalogIf = ctx.getCatalog(catalog);
127+
if (catalogIf == null) {
128+
throw new AnalysisException("No catalog found with name " + catalog);
129+
}
130+
// SHOW includes visibility inherited from database/table/column grants. Check it before either
131+
// listing remote databases or planning the WHERE form, which can initialize external metadata too.
132+
if (!catalogIf.isInternalCatalog() && !Env.getCurrentEnv().getAccessManager()
133+
.checkCtlPriv(ctx, catalog, PrivPredicate.SHOW)) {
134+
ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED,
135+
ctx.getQualifiedUser(), catalog);
136+
}
124137
if (whereClause != null) {
125138
Expression rewrited = whereClause.accept(new ReplaceColumnNameVisitor(), null);
126139
String whereCondition = " WHERE `CATALOG_NAME` = '" + catalog
127140
+ "' AND " + rewrited.toSql();
128141
return execute(ctx, executor, whereCondition);
129142
}
130143
List<List<String>> rows = Lists.newArrayList();
131-
CatalogIf catalogIf = ctx.getCatalog(catalog);
132-
if (catalogIf == null) {
133-
throw new AnalysisException("No catalog found with name " + catalog);
134-
}
135144
List<String> dbNames = catalogIf.getDbNames();
136145
PatternMatcher matcher = null;
137146
if (likePattern != null) {
@@ -146,7 +155,7 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc
146155
continue;
147156
}
148157

149-
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), catalog,
158+
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ctx, catalog,
150159
fullName, PrivPredicate.SHOW)) {
151160
continue;
152161
}

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowDatabasesCommandTest.java‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,36 @@
1717

1818
package org.apache.doris.nereids.trees.plans.commands;
1919

20+
import org.apache.doris.analysis.TablePattern;
2021
import org.apache.doris.backup.CatalogMocker;
2122
import org.apache.doris.catalog.Env;
23+
import org.apache.doris.common.AnalysisException;
2224
import org.apache.doris.datasource.CatalogMgr;
25+
import org.apache.doris.datasource.ExternalCatalog;
2326
import org.apache.doris.datasource.InternalCatalog;
2427
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;
2533
import org.apache.doris.nereids.analyzer.UnboundSlot;
2634
import org.apache.doris.nereids.trees.expressions.EqualTo;
2735
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
2836
import org.apache.doris.qe.ConnectContext;
37+
import org.apache.doris.qe.ShowResultSet;
2938

3039
import org.junit.jupiter.api.AfterEach;
3140
import org.junit.jupiter.api.Assertions;
3241
import org.junit.jupiter.api.BeforeEach;
3342
import org.junit.jupiter.api.Test;
43+
import org.mockito.InOrder;
3444
import org.mockito.MockedStatic;
3545
import org.mockito.Mockito;
3646

47+
import java.util.Arrays;
48+
import java.util.Collections;
49+
3750
public class ShowDatabasesCommandTest {
3851
private static final String internalCtl = InternalCatalog.INTERNAL_CATALOG_NAME;
3952

@@ -80,5 +93,84 @@ void testValidate() {
8093
ShowDatabasesCommand command2 = new ShowDatabasesCommand(null, null, equalTo);
8194
Assertions.assertDoesNotThrow(() -> command2.validate(ctx));
8295
}
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+
}
83175
}
84176

0 commit comments

Comments
 (0)