Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move permissions, allow blocks, canned queries and more out of metadata.yaml and into datasette.yaml #2191

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ def app_css_hash(self):
return self._app_css_hash

async def get_canned_queries(self, database_name, actor):
queries = self.metadata("queries", database=database_name, fallback=False) or {}
queries = (
((self.config or {}).get("databases") or {}).get(database_name) or {}
).get("queries") or {}
for more_queries in pm.hook.canned_queries(
datasette=self,
database=database_name,
Expand Down Expand Up @@ -1315,7 +1317,7 @@ async def _asset_urls(self, key, template, context, request, view_name):
):
hook = await await_me_maybe(hook)
collected.extend(hook)
collected.extend(self.metadata(key) or [])
collected.extend((self.config or {}).get(key) or [])
output = []
for url_or_dict in collected:
if isinstance(url_or_dict, dict):
Expand Down
37 changes: 22 additions & 15 deletions datasette/default_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ async def inner():
"view-query",
"execute-sql",
):
result = await _resolve_metadata_view_permissions(
result = await _resolve_config_view_permissions(
datasette, actor, action, resource
)
if result is not None:
return result

# Check custom permissions: blocks
result = await _resolve_metadata_permissions_blocks(
result = await _resolve_config_permissions_blocks(
datasette, actor, action, resource
)
if result is not None:
Expand All @@ -164,10 +164,10 @@ async def inner():
return inner


async def _resolve_metadata_permissions_blocks(datasette, actor, action, resource):
async def _resolve_config_permissions_blocks(datasette, actor, action, resource):
# Check custom permissions: blocks
metadata = datasette.metadata()
root_block = (metadata.get("permissions", None) or {}).get(action)
config = datasette.config or {}
root_block = (config.get("permissions", None) or {}).get(action)
if root_block:
root_result = actor_matches_allow(actor, root_block)
if root_result is not None:
Expand All @@ -180,7 +180,7 @@ async def _resolve_metadata_permissions_blocks(datasette, actor, action, resourc
else:
database = resource[0]
database_block = (
(metadata.get("databases", {}).get(database, {}).get("permissions", None)) or {}
(config.get("databases", {}).get(database, {}).get("permissions", None)) or {}
).get(action)
if database_block:
database_result = actor_matches_allow(actor, database_block)
Expand All @@ -192,7 +192,7 @@ async def _resolve_metadata_permissions_blocks(datasette, actor, action, resourc
database, table_or_query = resource
table_block = (
(
metadata.get("databases", {})
config.get("databases", {})
.get(database, {})
.get("tables", {})
.get(table_or_query, {})
Expand All @@ -207,7 +207,7 @@ async def _resolve_metadata_permissions_blocks(datasette, actor, action, resourc
# Finally the canned queries
query_block = (
(
metadata.get("databases", {})
config.get("databases", {})
.get(database, {})
.get("queries", {})
.get(table_or_query, {})
Expand All @@ -222,25 +222,30 @@ async def _resolve_metadata_permissions_blocks(datasette, actor, action, resourc
return None


async def _resolve_metadata_view_permissions(datasette, actor, action, resource):
async def _resolve_config_view_permissions(datasette, actor, action, resource):
config = datasette.config or {}
if action == "view-instance":
allow = datasette.metadata("allow")
allow = config.get("allow")
if allow is not None:
return actor_matches_allow(actor, allow)
elif action == "view-database":
database_allow = datasette.metadata("allow", database=resource)
database_allow = ((config.get("databases") or {}).get(resource) or {}).get(
"allow"
)
if database_allow is None:
return None
return actor_matches_allow(actor, database_allow)
elif action == "view-table":
database, table = resource
tables = datasette.metadata("tables", database=database) or {}
tables = ((config.get("databases") or {}).get(database) or {}).get(
"tables"
) or {}
table_allow = (tables.get(table) or {}).get("allow")
if table_allow is None:
return None
return actor_matches_allow(actor, table_allow)
elif action == "view-query":
# Check if this query has a "allow" block in metadata
# Check if this query has a "allow" block in config
database, query_name = resource
query = await datasette.get_canned_query(database, query_name, actor)
assert query is not None
Expand All @@ -250,9 +255,11 @@ async def _resolve_metadata_view_permissions(datasette, actor, action, resource)
return actor_matches_allow(actor, allow)
elif action == "execute-sql":
# Use allow_sql block from database block, or from top-level
database_allow_sql = datasette.metadata("allow_sql", database=resource)
database_allow_sql = ((config.get("databases") or {}).get(resource) or {}).get(
"allow_sql"
)
if database_allow_sql is None:
database_allow_sql = datasette.metadata("allow_sql")
database_allow_sql = config.get("allow_sql")
if database_allow_sql is None:
return None
return actor_matches_allow(actor, database_allow_sql)
Expand Down
Loading
Loading