snuyanzin commented on code in PR #28167: URL: https://github.com/apache/flink/pull/28167#discussion_r3254845367
########## flink-table/flink-table-api-java/src/test/java/org/apache/flink/table/operations/DescribeCatalogOperationTest.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.operations; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.GlobalConfiguration; +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.internal.TableResultInternal; +import org.apache.flink.table.catalog.CatalogDescriptor; +import org.apache.flink.table.catalog.CatalogManager; +import org.apache.flink.table.catalog.CatalogStoreHolder; +import org.apache.flink.table.catalog.FunctionCatalog; +import org.apache.flink.table.catalog.GenericInMemoryCatalogStore; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.module.ModuleManager; +import org.apache.flink.table.resource.ResourceManager; +import org.apache.flink.table.utils.CatalogManagerMocks; +import org.apache.flink.table.utils.ExpressionResolverMocks; +import org.apache.flink.table.utils.ParserMock; +import org.apache.flink.util.CloseableIterator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link DescribeCatalogOperation}. */ +class DescribeCatalogOperationTest { + + private static final String CATALOG_NAME = "test_cat"; + + private CatalogManager catalogManager; + + @BeforeEach + void setUp() throws Exception { + // Store the descriptor directly in the catalog store so that + // getCatalogDescriptor() returns it without needing to create the actual catalog. + // This lets us use arbitrary options (including sensitive ones) without + // triggering factory validation on the generic_in_memory catalog type. + GenericInMemoryCatalogStore catalogStore = new GenericInMemoryCatalogStore(); + catalogStore.open(); + Configuration config = new Configuration(); + config.setString("type", "generic_in_memory"); + config.setString("safe-option", "safe-value"); + config.setString("password", "topsecret"); + config.setString("my.token", "tok123"); + catalogStore.storeCatalog(CATALOG_NAME, CatalogDescriptor.of(CATALOG_NAME, config)); + + catalogManager = + CatalogManagerMocks.preparedCatalogManager() + .catalogStoreHolder( + CatalogStoreHolder.newBuilder() + .catalogStore(catalogStore) + .classloader( + DescribeCatalogOperationTest.class.getClassLoader()) + .config(new Configuration()) + .build()) + .build(); + catalogManager.initSchemaResolver( + true, ExpressionResolverMocks.dummyResolver(), new ParserMock()); + } + + @Test + void describeCatalogExtendedRedactsSensitiveOptions() throws Exception { + DescribeCatalogOperation op = new DescribeCatalogOperation(CATALOG_NAME, true); + List<RowData> rows = collectRows(op.execute(createContext())); + + List<String> names = getColumn(rows, 0); + List<String> values = getColumn(rows, 1); + + int passwordIdx = names.indexOf("option:password"); + int tokenIdx = names.indexOf("option:my.token"); + int safeIdx = names.indexOf("option:safe-option"); + + assertThat(passwordIdx).isGreaterThanOrEqualTo(0); + assertThat(values.get(passwordIdx)).isEqualTo(GlobalConfiguration.HIDDEN_CONTENT); + + assertThat(tokenIdx).isGreaterThanOrEqualTo(0); + assertThat(values.get(tokenIdx)).isEqualTo(GlobalConfiguration.HIDDEN_CONTENT); + + assertThat(safeIdx).isGreaterThanOrEqualTo(0); + assertThat(values.get(safeIdx)).isEqualTo("safe-value"); + + assertThat(values).doesNotContain("topsecret"); + assertThat(values).doesNotContain("tok123"); + } + + @Test + void describeCatalogNonExtendedDoesNotExposeOptions() throws Exception { + DescribeCatalogOperation op = new DescribeCatalogOperation(CATALOG_NAME, false); + List<RowData> rows = collectRows(op.execute(createContext())); + + List<String> names = getColumn(rows, 0); + assertThat(names).noneMatch(n -> n.startsWith("option:")); + } + + private static List<RowData> collectRows(TableResultInternal result) throws Exception { + List<RowData> rows = new ArrayList<>(); + try (CloseableIterator<RowData> iter = result.collectInternal()) { + iter.forEachRemaining(rows::add); + } + return rows; + } + + private static List<String> getColumn(List<RowData> rows, int col) { + List<String> values = new ArrayList<>(); + for (RowData row : rows) { + values.add(row.isNullAt(col) ? null : row.getString(col).toString()); + } + return values; + } + + private ExecutableOperation.Context createContext() { Review Comment: I wonder if we need to complicate test in such a way for instance: if we replace it with SQL test, then no need for that, WDYT? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
