@noorbasha534 - I created a helper function/script to do this in my environment that queries the Unity Catalog system tables to generate a unique list of impacted principals/users. It takes in a list of fully qualified object names and will display a unique list of principals that have access to one or more of the specified objects. It just looks at Tables and Views for now, but could be expanded.
def getImpactedPrincipals(objectList: str):
# Split and clean the object names
fullObjectNames = [obj.strip() for obj in objectList.split(",") if obj.strip()]
if not fullObjectNames:
raise ValueError("No valid object names provided.")
allGrants = None
for fullObjectName in fullObjectNames:
try:
catalog, schema, objectName = fullObjectName.split(".")
except ValueError:
raise ValueError(f"Invalid object name format: '{fullObjectName}'. Use 'catalog.schema.object_name'.")
table_info_df = spark.sql(f"""SELECT table_type FROM system.information_schema.tables
WHERE table_catalog = '{catalog}'
AND table_schema = '{schema}'
AND table_name = '{objectName}'""")
table_info = table_info_df.first()
if not table_info:
print(f"Warning: Object `{fullObjectName}` not found โ skipping.")
continue
objectType = table_info['table_type'].upper()
if objectType == 'MANAGED' or objectType == 'VIEW':
grantObject = "TABLE" if objectType == "MANAGED" else objectType
try:
grants = spark.sql(f"""SHOW GRANTS ON {grantObject} `{catalog}`.`{schema}`.`{objectName}`""")
except Exception as e:
print(f"Error querying grants for {fullObjectName}: {e}")
continue
# Union results
if allGrants is None:
allGrants = grants
else:
allGrants = allGrants.unionByName(grants)
if allGrants is None:
raise Exception("No valid grants found for any objects.")
return allGrants.select("principal").distinct()
objectList = "catalogname.schemaname.objectname1,catalogname.schemaname.objectname2"
principals = getImpactedPrincipals(objectList)
display(principals)