Skip to content

Commit cbc2654

Browse files
Adding safe return on grants (#246)
Adding a safe return for grants to handle corner cases
1 parent 4454491 commit cbc2654

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

src/databricks/labs/ucx/hive_metastore/grants.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections.abc import Iterator
23
from dataclasses import dataclass
34
from functools import partial
@@ -6,6 +7,8 @@
67
from databricks.labs.ucx.framework.parallel import ThreadedExecution
78
from databricks.labs.ucx.hive_metastore.tables import TablesCrawler
89

10+
logger = logging.getLogger(__name__)
11+
912

1013
@dataclass(frozen=True)
1114
class Grant:
@@ -219,20 +222,24 @@ def _grants(
219222
any_file=any_file,
220223
anonymous_function=anonymous_function,
221224
)
222-
object_type_normalization = {"SCHEMA": "DATABASE", "CATALOG$": "CATALOG"}
223-
for row in self._fetch(f"SHOW GRANTS ON {on_type} {key}"):
224-
(principal, action_type, object_type, _) = row
225-
if object_type in object_type_normalization:
226-
object_type = object_type_normalization[object_type]
227-
if on_type != object_type:
228-
continue
229-
yield Grant(
230-
principal=principal,
231-
action_type=action_type,
232-
table=table,
233-
view=view,
234-
database=database,
235-
catalog=catalog,
236-
any_file=any_file,
237-
anonymous_function=anonymous_function,
238-
)
225+
try:
226+
object_type_normalization = {"SCHEMA": "DATABASE", "CATALOG$": "CATALOG"}
227+
for row in self._fetch(f"SHOW GRANTS ON {on_type} {key}"):
228+
(principal, action_type, object_type, _) = row
229+
if object_type in object_type_normalization:
230+
object_type = object_type_normalization[object_type]
231+
if on_type != object_type:
232+
continue
233+
yield Grant(
234+
principal=principal,
235+
action_type=action_type,
236+
table=table,
237+
view=view,
238+
database=database,
239+
catalog=catalog,
240+
any_file=any_file,
241+
anonymous_function=anonymous_function,
242+
)
243+
except RuntimeError as e:
244+
logger.error(f"Couldn't fetch grants for object {on_type} {key}: {e}")
245+
return []

tests/unit/hive_metastore/test_grants.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,31 @@ def test_crawler_snapshot():
165165
crawler = GrantsCrawler(table)
166166
snapshot = crawler.snapshot("hive_metastore", "schema")
167167
assert len(snapshot) == 3
168+
169+
170+
def test_grants_returning_error_when_describing():
171+
errors = {"SHOW GRANTS ON TABLE hive_metastore.test_database.table1": "error"}
172+
rows = {
173+
"SHOW TABLES FROM hive_metastore.test": [("dummy", "table1", False), ("dummy", "table2", False)],
174+
"SHOW GRANTS ON TABLE hive_metastore.test_database.table2": [("principal1", "OWNER", "TABLE", "")],
175+
"DESCRIBE *": [
176+
("Catalog", "catalog", ""),
177+
("Type", "delta", ""),
178+
],
179+
}
180+
181+
tc = TablesCrawler(MockBackend(fails_on_first=errors, rows=rows), "main", "default")
182+
crawler = GrantsCrawler(tc)
183+
184+
results = crawler._crawl(catalog="hive_metastore", database="test_database")
185+
assert results == [
186+
Grant(
187+
principal="principal1",
188+
action_type="OWNER",
189+
catalog="hive_metastore",
190+
database="test_database",
191+
table="table2",
192+
any_file=False,
193+
anonymous_function=False,
194+
)
195+
]

0 commit comments

Comments
 (0)