Copilot commented on code in PR #6969:
URL: https://github.com/apache/hive/pull/6969#discussion_r4614924894


##########
service/src/java/org/apache/hive/service/cli/operation/GetColumnsOperation.java:
##########
@@ -233,10 +233,14 @@ public void runInternal() throws HiveSQLException {
 
   private List<HivePrivilegeObject> getPrivObjs(Map<String, List<String>> 
db2Tabs) {
     List<HivePrivilegeObject> privObjs = new ArrayList<>();
+    String catName = catalogName != null ? catalogName : 
SessionState.get().getCurrentCatalog();

Review Comment:
   When `catalogName` is null after a catalog switch, this authorizes the 
session's current catalog, while `BaseMetaStoreClient` resolves the 
catalog-less retrieval calls in `runInternal()` against its configured default. 
`GET_COLUMNS` can therefore return one catalog's metadata under another 
catalog's permissions (VALID under THREAT_MODEL.md §13: authorization scoping 
in §5 at the client boundary in §4). Resolve one catalog and use it 
consistently for authorization and every metadata fetch.



##########
ql/src/java/org/apache/hadoop/hive/ql/security/authorization/plugin/HivePrivilegeObject.java:
##########
@@ -164,105 +175,148 @@ public enum HivePrivObjectActionType {
   // is applied to the table.
   private String rowFilterExpression;
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
   public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName) {
-    this(type, dbname, objectName, HivePrivObjectActionType.OTHER);
+    this(type, null, dbname, objectName, null, null, null, null, null, null, 
null);
   }
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName
-      , HivePrivObjectActionType actionType) {
-    this(type, dbname, objectName, null, null, actionType, null, null);
+  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
+      HivePrivObjectActionType actionType) {
+    this(type, null, dbname, objectName, null, null, actionType, null, null, 
null, null);
   }
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
   public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
       List<String> partKeys, String column) {
-    this(type, dbname, objectName, partKeys,
-        column == null ? null : Arrays.asList(column),
-        HivePrivObjectActionType.OTHER, null, null);
-    }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String objectName) {
-    this(type, null, null, objectName);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName) {
-    this(type, catName, dbname, objectName, null, null);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
-      List<String> partKeys, List<String> columns) {
-    this(type, catName, dbname, objectName, partKeys, columns, 
HivePrivObjectActionType.OTHER, null);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
-      List<String> partKeys, List<String> columns, HivePrivObjectActionType 
actionType, List<String> commandParams) {
-    this(type, catName, dbname, objectName, partKeys, columns, actionType, 
commandParams, null, null, null);
+    this(type, null, dbname, objectName, partKeys, column == null ? null : 
Arrays.asList(column),
+        null, null, null, null, null);
   }
 
   /**
-   * Create HivePrivilegeObject of type {@link 
HivePrivilegeObjectType#COMMAND_PARAMS}
+   * Create HivePrivilegeObject of type {@link 
HivePrivilegeObjectType#COMMAND_PARAMS}.
    * @param cmdParams
    * @return
    */
   public static HivePrivilegeObject createHivePrivilegeObject(List<String> 
cmdParams) {
-    return new HivePrivilegeObject(HivePrivilegeObjectType.COMMAND_PARAMS, 
null, null,
-        null, null, null,null, cmdParams);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
-    List<String> partKeys, List<String> columns, List<String> commandParams) {
-    this(type, dbname, objectName, partKeys, columns, 
HivePrivObjectActionType.OTHER, commandParams, null);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(String dbname, String objectName, List<String> 
columns) {
-    this(HivePrivilegeObjectType.TABLE_OR_VIEW, null, dbname, objectName, 
null, columns);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(String dbname, String objectName, List<String> 
columns,
-      String ownerName, PrincipalType ownerType) {
-    this(HivePrivilegeObjectType.TABLE_OR_VIEW, dbname, objectName, null, 
columns,
-        HivePrivObjectActionType.OTHER, null, null, ownerName, ownerType);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName, List<String> partKeys,
-      List<String> columns, HivePrivObjectActionType actionType, List<String> 
commandParams, String className) {
-    this(type, dbname, objectName, partKeys, columns, actionType, 
commandParams, className, null, null);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName, List<String> partKeys,
-      List<String> columns, HivePrivObjectActionType actionType, List<String> 
commandParams, String className,
-      String ownerName, PrincipalType ownerType) {
-    this(type, null, dbname, objectName, partKeys, columns, actionType, 
commandParams, className, ownerName,
-        ownerType);
+    return 
builder(HivePrivilegeObjectType.COMMAND_PARAMS).commandParams(cmdParams).build();

Review Comment:
   This removes legacy public overloads as well as the catalog-aware 
constructors, including `HivePrivilegeObject(String, String, List<String>)` and 
the owner/action/class-name overloads. Existing authorizer binaries compiled 
against those signatures will now fail with `NoSuchMethodError`; migrating 
Hive's own callers to the builder does not preserve that compatibility. Please 
retain all pre-HIVE-26148 constructor signatures as deprecated delegating 
wrappers.



##########
ql/src/java/org/apache/hadoop/hive/ql/security/authorization/plugin/HivePrivilegeObject.java:
##########
@@ -110,12 +113,20 @@ private int compare(Collection<String> o1, 
Collection<String> o2) {
   }
 
   /**
-   * Try to get thread local configuration from HMSHandler, otherwise create a 
new one.
-   * @return configuration
+   * @return the given catalog name if not blank, otherwise the current 
catalog of the session in HS2,
+   * or the default catalog from the HMSHandler configuration (or a new 
metastore configuration).
    */
-  public static Configuration getConf() {
-    return HMSHandlerContext.getConfiguration()
-        .orElse(new Configuration());
+  private String getCatalogOrDefault(String catName) {
+    if (StringUtils.isNotBlank(catName)) {
+      return catName;
+    } else if (SessionState.get() != null) {
+      // HS2
+      return SessionState.get().getCurrentCatalog();
+    }
+    // HMS or others
+    Configuration configuration =
+        
HMSHandlerContext.getConfiguration().orElse(MetastoreConf.newMetastoreConf());

Review Comment:
   `orElse` evaluates `MetastoreConf.newMetastoreConf()` even when the HMS 
thread-local configuration is present. Every privilege object constructed 
without a catalog outside HS2 therefore performs configuration discovery, scans 
configuration/system properties, and emits config-discovery logs unnecessarily. 
For example, `DataConnectorFilterContext` does this once per connector. Use 
`orElseGet` so the fallback configuration is created only when needed.



##########
ql/src/java/org/apache/hadoop/hive/ql/security/authorization/plugin/HivePrivilegeObject.java:
##########
@@ -164,105 +175,148 @@ public enum HivePrivObjectActionType {
   // is applied to the table.
   private String rowFilterExpression;
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
   public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName) {
-    this(type, dbname, objectName, HivePrivObjectActionType.OTHER);
+    this(type, null, dbname, objectName, null, null, null, null, null, null, 
null);
   }
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName
-      , HivePrivObjectActionType actionType) {
-    this(type, dbname, objectName, null, null, actionType, null, null);
+  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
+      HivePrivObjectActionType actionType) {
+    this(type, null, dbname, objectName, null, null, actionType, null, null, 
null, null);
   }
 
+  /**
+   * Used by external authorizers (e.g. Ranger, Sentry). Use {@link 
#builder(HivePrivilegeObjectType)} instead.
+   */
   @Deprecated
   public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
       List<String> partKeys, String column) {
-    this(type, dbname, objectName, partKeys,
-        column == null ? null : Arrays.asList(column),
-        HivePrivObjectActionType.OTHER, null, null);
-    }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String objectName) {
-    this(type, null, null, objectName);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName) {
-    this(type, catName, dbname, objectName, null, null);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
-      List<String> partKeys, List<String> columns) {
-    this(type, catName, dbname, objectName, partKeys, columns, 
HivePrivObjectActionType.OTHER, null);
-  }
-
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
-      List<String> partKeys, List<String> columns, HivePrivObjectActionType 
actionType, List<String> commandParams) {
-    this(type, catName, dbname, objectName, partKeys, columns, actionType, 
commandParams, null, null, null);
+    this(type, null, dbname, objectName, partKeys, column == null ? null : 
Arrays.asList(column),
+        null, null, null, null, null);
   }
 
   /**
-   * Create HivePrivilegeObject of type {@link 
HivePrivilegeObjectType#COMMAND_PARAMS}
+   * Create HivePrivilegeObject of type {@link 
HivePrivilegeObjectType#COMMAND_PARAMS}.
    * @param cmdParams
    * @return
    */
   public static HivePrivilegeObject createHivePrivilegeObject(List<String> 
cmdParams) {
-    return new HivePrivilegeObject(HivePrivilegeObjectType.COMMAND_PARAMS, 
null, null,
-        null, null, null,null, cmdParams);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName,
-    List<String> partKeys, List<String> columns, List<String> commandParams) {
-    this(type, dbname, objectName, partKeys, columns, 
HivePrivObjectActionType.OTHER, commandParams, null);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(String dbname, String objectName, List<String> 
columns) {
-    this(HivePrivilegeObjectType.TABLE_OR_VIEW, null, dbname, objectName, 
null, columns);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(String dbname, String objectName, List<String> 
columns,
-      String ownerName, PrincipalType ownerType) {
-    this(HivePrivilegeObjectType.TABLE_OR_VIEW, dbname, objectName, null, 
columns,
-        HivePrivObjectActionType.OTHER, null, null, ownerName, ownerType);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName, List<String> partKeys,
-      List<String> columns, HivePrivObjectActionType actionType, List<String> 
commandParams, String className) {
-    this(type, dbname, objectName, partKeys, columns, actionType, 
commandParams, className, null, null);
-  }
-
-  @Deprecated
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String dbname, 
String objectName, List<String> partKeys,
-      List<String> columns, HivePrivObjectActionType actionType, List<String> 
commandParams, String className,
-      String ownerName, PrincipalType ownerType) {
-    this(type, null, dbname, objectName, partKeys, columns, actionType, 
commandParams, className, ownerName,
-        ownerType);
+    return 
builder(HivePrivilegeObjectType.COMMAND_PARAMS).commandParams(cmdParams).build();
   }
 
-  public HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
+  private HivePrivilegeObject(HivePrivilegeObjectType type, String catName, 
String dbname, String objectName,
       List<String> partKeys, List<String> columns, HivePrivObjectActionType 
actionType, List<String> commandParams,
       String className, String ownerName, PrincipalType ownerType) {
     this.type = type;
-    this.catName = catName == null ? 
MetaStoreUtils.getDefaultCatalog(getConf()) : catName;
+    this.catName = getCatalogOrDefault(catName);
     this.dbname = dbname;
     this.objectName = objectName;
     this.partKeys = partKeys;
     this.columns = columns;
-    this.actionType = actionType;
+    // Older constructors defaulted a missing action type to OTHER. 
Authorizers such as
+    // Ranger switch on it, so a null from any constructor is stored as OTHER.
+    this.actionType = actionType == null ? HivePrivObjectActionType.OTHER : 
actionType;
     this.commandParams = commandParams;
     this.className = className;
     this.ownerName = ownerName;
     this.ownerType = ownerType;
   }
 
   public static HivePrivilegeObject forScheduledQuery(String owner, String 
clusterNamespace, String scheduleName) {
-    return new HivePrivilegeObject(HivePrivilegeObjectType.SCHEDULED_QUERY, 
null,
-        /*dbName*/clusterNamespace, /*objectName*/scheduleName, null, null, 
null, null, null,
-        /*ownerName*/owner, null);
+    return builder(HivePrivilegeObjectType.SCHEDULED_QUERY)
+        .dbName(clusterNamespace)
+        .objectName(scheduleName)
+        .ownerName(owner)
+        .build();
+  }
+
+  public static Builder builder(HivePrivilegeObjectType type) {
+    return new Builder(type);
+  }
+
+  /**
+   * Builds a {@link HivePrivilegeObject}. A catalog name left unset resolves 
to the session's
+   * current catalog in HS2, or the default catalog otherwise. An action type 
left unset is
+   * {@link HivePrivObjectActionType#OTHER}.
+   */
+  public static final class Builder {
+    private final HivePrivilegeObjectType type;
+    private String catName;
+    private String dbName;
+    private String objectName;
+    private List<String> partKeys;
+    private List<String> columns;
+    private HivePrivObjectActionType actionType = 
HivePrivObjectActionType.OTHER;
+    private List<String> commandParams;
+    private String className;
+    private String ownerName;
+    private PrincipalType ownerType;
+
+    private Builder(HivePrivilegeObjectType type) {
+      this.type = type;
+    }
+
+    public Builder catName(String catName) {
+      this.catName = catName;
+      return this;
+    }
+
+    public Builder dbName(String dbName) {
+      this.dbName = dbName;
+      return this;
+    }
+
+    public Builder objectName(String objectName) {
+      this.objectName = objectName;
+      return this;
+    }
+
+    public Builder partKeys(List<String> partKeys) {
+      this.partKeys = partKeys;
+      return this;
+    }
+
+    public Builder columns(List<String> columns) {
+      this.columns = columns;
+      return this;
+    }
+
+    public Builder actionType(HivePrivObjectActionType actionType) {
+      this.actionType = actionType == null ? HivePrivObjectActionType.OTHER : 
actionType;
+      return this;
+    }
+
+    public Builder commandParams(List<String> commandParams) {
+      this.commandParams = commandParams;
+      return this;
+    }
+
+    public Builder className(String className) {
+      this.className = className;
+      return this;
+    }
+
+    public Builder ownerName(String ownerName) {
+      this.ownerName = ownerName;
+      return this;
+    }
+
+    public Builder ownerType(PrincipalType ownerType) {
+      this.ownerType = ownerType;
+      return this;
+    }
+
+    public HivePrivilegeObject build() {
+      return new HivePrivilegeObject(type, catName, dbName, objectName, 
partKeys, columns, actionType,
+          commandParams, className, ownerName, ownerType);

Review Comment:
   The promised DATABASE `objectName` compatibility is still missing on several 
migrated paths: `CommandAuthorizerV2`'s DATABASE branch, `CreateFunctionEvent`, 
`PrivilegeSynchronizer`, and the REST authorizer's database builders set only 
`dbName`. Consequently, authorizers reading `getObjectName()` still receive 
null for those operations, although the filter/event helpers now supply the 
database name. Default an unset DATABASE object name to `dbName` in the builder 
so all these paths receive the compatibility fix.



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