vikramahuja1001 commented on code in PR #6610:
URL: https://github.com/apache/hive/pull/6610#discussion_r3598294643


##########
ql/src/java/org/apache/hadoop/hive/ql/security/authorization/plugin/AuthorizationMetaStoreFilterHook.java:
##########


Review Comment:
   done



##########
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"),

Review Comment:
   edited



-- 
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]

Reply via email to