-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(schema-compiler): fix DAP with query_rewrite and python config #9033
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.