vikramahuja1001 commented on code in PR #6610: URL: https://github.com/apache/hive/pull/6610#discussion_r3598302539
########## ql/src/test/org/apache/hadoop/hive/ql/security/authorization/plugin/TestAuthorizationMetaStoreFilterHook.java: ########## @@ -0,0 +1,333 @@ +/* + * 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.hadoop.hive.ql.security.authorization.plugin; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.api.PrincipalType; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link AuthorizationMetaStoreFilterHook#filterTables}. + * + * Validates correctness of the O(n) HashMap-based fix replacing the original + * O(n^2) nested-loop implementation, including catName null-handling logic. + */ +@RunWith(MockitoJUnitRunner.class) +public class TestAuthorizationMetaStoreFilterHook { + + private AuthorizationMetaStoreFilterHook hook; + private MockedStatic<SessionState> mockedSessionState; + + @Mock private SessionState sessionState; + @Mock private HiveAuthorizer authorizer; + + @Before + public void setUp() { + hook = new AuthorizationMetaStoreFilterHook(new Configuration()); + when(sessionState.getUserIpAddress()).thenReturn("127.0.0.1"); + when(sessionState.getForwardedAddresses()).thenReturn(Collections.emptyList()); + when(sessionState.getAuthorizerV2()).thenReturn(authorizer); + mockedSessionState = mockStatic(SessionState.class); + mockedSessionState.when(SessionState::get).thenReturn(sessionState); + } + + @After + public void tearDown() { + mockedSessionState.close(); + } + + // ── helpers ──────────────────────────────────────────────────────────────── + + /** Create a Table with catName. */ + private Table createTable(String catName, String dbName, String tableName) { + Table table = new Table(); + table.setCatName(catName); + table.setDbName(dbName); + table.setTableName(tableName); + table.setOwner("testowner"); + table.setOwnerType(PrincipalType.USER); + return table; + } + + /** Create a Table without catName (catName = null). */ + private Table createTable(String dbName, String tableName) { + return createTable(null, dbName, tableName); + } + + /** + * Stub authorizer to return HivePrivilegeObjects with the given catName + * for tables matching dbName + any of the allowedTableNames. + */ + private void allowTablesWithCatName(String catName, String dbName, String... allowedTableNames) + throws HiveAuthzPluginException, HiveAccessControlException { + when(authorizer.filterListCmdObjects(any(), any())).thenAnswer(invocation -> { + List<HivePrivilegeObject> input = invocation.getArgument(0); + List<HivePrivilegeObject> result = new ArrayList<>(); + Set<String> seen = new HashSet<>(); + for (HivePrivilegeObject obj : input) { + for (String allowed : allowedTableNames) { + if (dbName.equals(obj.getDbname()) && allowed.equals(obj.getObjectName())) { + String key = (catName != null ? catName : "") + "\0" + dbName + "\0" + allowed; + if (seen.add(key)) { + result.add(new HivePrivilegeObject( + HivePrivilegeObject.HivePrivilegeObjectType.TABLE_OR_VIEW, catName, dbName, allowed)); + } + break; + } + } + } + return result; + }); + } + + /** Stub authorizer to return all objects unchanged (allow all, catName stays null). */ + private void allowAll() throws HiveAuthzPluginException, HiveAccessControlException { + when(authorizer.filterListCmdObjects(any(), any())).thenAnswer(invocation -> invocation.getArgument(0)); + } + + /** Stub authorizer to deny everything. */ + private void denyAll() throws HiveAuthzPluginException, HiveAccessControlException { + when(authorizer.filterListCmdObjects(any(), any())).thenReturn(Collections.emptyList()); + } + + // ── existing tests (no catName) ──────────────────────────────────────────── + + @Test + public void testFilterTables_emptyInput() throws Exception { + denyAll(); + List<Table> result = hook.filterTables(Collections.emptyList()); + assertTrue(result.isEmpty()); + } + + @Test + public void testFilterTables_allAllowed() throws Exception { + List<Table> input = List.of( + createTable("db1", "tbl1"), + createTable("db1", "tbl2"), + createTable("db1", "tbl3")); + allowAll(); + List<Table> result = hook.filterTables(input); + assertEquals(3, result.size()); + } + + @Test + public void testFilterTables_allFiltered() throws Exception { + List<Table> input = List.of( + createTable("db1", "tbl1"), + createTable("db1", "tbl2")); + denyAll(); + List<Table> result = hook.filterTables(input); + assertTrue(result.isEmpty()); + } + + @Test + public void testFilterTables_someFiltered() throws Exception { + List<Table> input = List.of( + createTable("db1", "tbl1"), + createTable("db1", "tbl2"), + createTable("db1", "tbl3")); + // authorize returns tbl1 and tbl3 with null catName + when(authorizer.filterListCmdObjects(any(), any())).thenAnswer(invocation -> { + List<HivePrivilegeObject> objs = invocation.getArgument(0); + return objs.stream() + .filter(o -> "tbl1".equals(o.getObjectName()) || "tbl3".equals(o.getObjectName())) + .collect(Collectors.toList()); + }); + List<Table> result = hook.filterTables(input); + assertEquals(2, result.size()); + List<String> names = result.stream().map(Table::getTableName).collect(Collectors.toList()); + assertTrue(names.contains("tbl1")); + assertTrue(names.contains("tbl3")); + } + + // ── catName-specific tests ───────────────────────────────────────────────── + + /** + * Case 1: obj.catName == null → must match a table that also has null catName. + * Note: HivePrivilegeObject normalizes null catName to the default catalog ("hive"), + * so the null-catName-obj path in production code is unreachable in practice. + * The live equivalent is tested via testFilterTables_tableCatNameNull_matchesObjWithNonNullCatName. + */ + @Test + public void testFilterTables_objCatNameNull_matchesTableWithNullCatName() throws Exception { + List<Table> input = List.of(createTable(null, "db1", "tbl1")); + allowTablesWithCatName(null, "db1", "tbl1"); + List<Table> result = hook.filterTables(input); + assertEquals("obj with null catName should match table with null catName", 1, result.size()); + } + + /** + * Case 2: obj.catName != null but table.catName == null → still matches. + * Original logic: condition `table.getCatName() != null` is false → skip the check. + */ + @Test + public void testFilterTables_tableCatNameNull_matchesObjWithNonNullCatName() throws Exception { + List<Table> input = List.of(createTable(null, "db1", "tbl1")); // table has null catName + // authorizer returns obj with catName = "cat1" + allowTablesWithCatName("cat1", "db1", "tbl1"); + List<Table> result = hook.filterTables(input); + assertEquals("table with null catName should match obj with any catName", 1, result.size()); + } + + /** + * Case 3: obj.catName != null, table.catName != null, and they match → matches. + */ + @Test + public void testFilterTables_sameCatName_matches() throws Exception { + List<Table> input = List.of(createTable("cat1", "db1", "tbl1")); + allowTablesWithCatName("cat1", "db1", "tbl1"); + List<Table> result = hook.filterTables(input); + assertEquals("same catName on obj and table should match", 1, result.size()); + assertEquals("cat1", result.get(0).getCatName()); + } + + /** + * Both obj.catName and table.catName are non-null but DIFFERENT → no match. + * Original logic: condition is true → continue (skip). + */ + @Test + public void testFilterTables_differentCatName_noMatch() throws Exception { + List<Table> input = List.of(createTable("cat2", "db1", "tbl1")); // table has cat2 + // authorizer says tbl1 is allowed but with catName=cat1 + allowTablesWithCatName("cat1", "db1", "tbl1"); + List<Table> result = hook.filterTables(input); + assertTrue("obj.catName=cat1 vs table.catName=cat2 should not match", result.isEmpty()); + } + + /** + * Multiple catalogs, same db+table name. + * obj.catName=cat1 should return only the cat1 table, not cat2. + */ + @Test + public void testFilterTables_multipleCatalogs_sameDbTable_exactCatNameMatch() throws Exception { + Table cat1Table = createTable("cat1", "db1", "tbl1"); + Table cat2Table = createTable("cat2", "db1", "tbl1"); + List<Table> input = List.of(cat1Table, cat2Table); + + allowTablesWithCatName("cat1", "db1", "tbl1"); + + List<Table> result = hook.filterTables(input); + + assertEquals("Only cat1 table should be returned", 1, result.size()); + assertEquals("cat1", result.get(0).getCatName()); + } + + /** + * obj.catName != null, no exact catName match exists, but a table with + * null catName exists → that null-catName table should be returned. + */ + @Test + public void testFilterTables_noExactCatMatch_fallsBackToNullCatTable() throws Exception { + Table nullCatTable = createTable(null, "db1", "tbl1"); // catName = null + Table cat2Table = createTable("cat2", "db1", "tbl1"); // catName = cat2 + List<Table> input = List.of(nullCatTable, cat2Table); + + // obj requests catName=cat1 but no cat1 table exists; null-catName table should match + allowTablesWithCatName("cat1", "db1", "tbl1"); + + List<Table> result = hook.filterTables(input); + + assertEquals("null-catName table should match obj with non-null catName", 1, result.size()); + assertEquals(null, result.get(0).getCatName()); + } + + /** + * Mixed: some tables have catName, some don't; some objs have catName, some don't. + */ + @Test + public void testFilterTables_mixed_catNameCombinations() throws Exception { + List<Table> input = List.of( + createTable("cat1", "db1", "tbl1"), // exact catName match + createTable(null, "db1", "tbl2"), // null catName — matches any obj catName + createTable("cat2", "db1", "tbl3")); // catName=cat2, obj will request cat1 → no match + + when(authorizer.filterListCmdObjects(any(), any())).thenAnswer(invocation -> { + // return 3 objs: tbl1(cat1), tbl2(cat1), tbl3(cat1) + List<HivePrivilegeObject> result = new ArrayList<>(); + for (String tbl : List.of("tbl1", "tbl2", "tbl3")) { + HivePrivilegeObject obj = new HivePrivilegeObject( + HivePrivilegeObject.HivePrivilegeObjectType.TABLE_OR_VIEW, "cat1", "db1", tbl); + result.add(obj); + } + return result; + }); + + List<Table> result = hook.filterTables(input); + + List<String> names = result.stream().map(Table::getTableName).collect(Collectors.toList()); + assertEquals("Should return tbl1 (exact) and tbl2 (null catName fallback)", 2, result.size()); + assertTrue(names.contains("tbl1")); // cat1 == cat1 → match + assertTrue(names.contains("tbl2")); // null catName → match + assertTrue(!names.contains("tbl3")); // cat2 != cat1 → no match + } + + // ── performance test ─────────────────────────────────────────────────────── + + /** + * 1 lakh tables must complete within 5 seconds. + * The original O(n^2) implementation would take 200-300 seconds. + * Note: uses inline time assertion instead of @Test(timeout) to avoid JUnit 4's + * separate-thread execution, which breaks Mockito's ThreadLocal-based MockedStatic. + */ + @Test + public void testFilterTables_performance_100kTables() throws Exception { Review Comment: Will handle it as a part of a follow up JIRA if that's okay since it is a reasonable scope for a separate JIRA itself? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
