-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema-compiler): fix DAP with query_rewrite and python config (#…
- Loading branch information
Showing
7 changed files
with
226 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/cubejs-testing/birdbox-fixtures/rbac-python/cube.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Cube configuration options: https://cube.dev/docs/config | ||
|
||
from cube import config | ||
|
||
|
||
@config('context_to_roles') | ||
def context_to_roles(context): | ||
return context.get("securityContext", {}).get("auth", {}).get("roles", []) | ||
|
||
|
||
def extract_matching_dicts(data): | ||
matching_dicts = [] | ||
keys = ['values', 'member', 'operator'] | ||
|
||
# Recursive function to traverse through the list or dictionary | ||
def traverse(element): | ||
if isinstance(element, dict): | ||
# Check if any of the specified keys are in the dictionary | ||
if any(key in element for key in keys): | ||
matching_dicts.append(element) | ||
# Traverse the dictionary values | ||
for value in element.values(): | ||
traverse(value) | ||
elif isinstance(element, list): | ||
# Traverse the list items | ||
for item in element: | ||
traverse(item) | ||
|
||
traverse(data) | ||
return matching_dicts | ||
|
||
|
||
@config('query_rewrite') | ||
def query_rewrite(query: dict, ctx: dict) -> dict: | ||
filters = extract_matching_dicts(query.get('filters')) | ||
|
||
for value in range(len(query['timeDimensions'])): | ||
filters.append(query['timeDimensions'][value]['dateRange']) | ||
|
||
if not filters or None in filters: | ||
raise Exception("Queries can't be run without a filter") | ||
return query | ||
|
||
|
||
@config('check_sql_auth') | ||
def check_sql_auth(query: dict, username: str, password: str) -> dict: | ||
if username == 'admin': | ||
return { | ||
'username': 'admin', | ||
'password': password, | ||
'securityContext': { | ||
'auth': { | ||
'username': 'admin', | ||
'userAttributes': { | ||
'canHaveAdmin': True, | ||
'city': 'New York' | ||
}, | ||
'roles': ['admin'] | ||
} | ||
} | ||
} | ||
raise Exception("Invalid username or password") |
54 changes: 54 additions & 0 deletions
54
packages/cubejs-testing/birdbox-fixtures/rbac-python/model/cubes/users.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
cubes: | ||
- name: users | ||
sql_table: users | ||
|
||
measures: | ||
- name: count | ||
sql: id | ||
type: count | ||
|
||
dimensions: | ||
- name: city | ||
sql: city | ||
type: string | ||
|
||
- name: id | ||
sql: id | ||
type: number | ||
primary_key: true | ||
|
||
access_policy: | ||
- role: "*" | ||
row_level: | ||
filters: | ||
- member: "{CUBE}.city" | ||
operator: equals | ||
values: ["{ security_context.auth.userAttributes.city }"] | ||
- role: admin | ||
conditions: | ||
# This thing will fail if there's no auth info in the context | ||
# Unfortunately, as of now, there's no way to write more complex expressions | ||
# that would allow us to check for the existence of the auth object | ||
- if: "{ security_context.auth.userAttributes.canHaveAdmin }" | ||
row_level: | ||
filters: | ||
- or: | ||
- and: | ||
- member: "{CUBE}.city" | ||
operator: notStartsWith | ||
values: | ||
- London | ||
- "{ security_context.auth.userAttributes.city }" | ||
# mixing string, dynamic values, integers and bools should not | ||
# cause any compilation issues | ||
- 4 | ||
- true | ||
- member: "city" | ||
operator: notEquals | ||
values: | ||
- 'San Francisco' | ||
- member: "{CUBE}.city" | ||
operator: equals | ||
values: | ||
- "New York" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/cubejs-testing/test/__snapshots__/smoke-rbac.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters