feat: current_user_rls_rules Jinja macro#33614
Conversation
| assert cache.current_user_rls_rules() is None | ||
|
|
||
|
|
||
| def test_user_macros_without_cache_key_inclusion(mocker: MockerFixture): |
There was a problem hiding this comment.
This is now cover in the previous test with parametrize
There was a problem hiding this comment.
I've completed my review and didn't find any issues.
Files scanned
| File Path | Reviewed |
|---|---|
| superset/jinja_context.py | ✅ |
Explore our documentation to understand the languages and file types we support and the files we ignore.
Check out our docs on how you can make Korbit work best for you and your team.
| cache key by adding the following parameter to your Jinja code: | ||
|
|
||
| ```python | ||
| {{ current_user_rls_rules(add_to_cache_keys=False) }} |
There was a problem hiding this comment.
Why would we want to do this? Would it allow users to see cached RLS roles that don't belong to them? — that could be a security issue.
There was a problem hiding this comment.
That's a fair point. To be honest, I just copied the same content from other macros in this doc (like current_user_roles) and updated them to my new macro.
I think in practice this is irrelevant, because even if you set add_to_cache_keys=False, the macro returns RLS rules and cache would be isolated by the final SQL query, so different RLS configs should not overlap.
Anyways, I'll be removing this section -- do you think I should also be removing the parameter from the macro so it always add the result to cache keys?
There was a problem hiding this comment.
the macro returns RLS rules and cache would be isolated by the final SQL query, so different RLS configs should not overlap.
But the cache is based on the pre-rendered query, no? At least that's what I understood from @john-bodley's original PR.
There was a problem hiding this comment.
right, but it should still consider the RLS filters at the end, so you would only get the same query if you effectively have the same RLS config as the previous user.
Anyways, let's err on the side of cautious here, so I'll:
- Remove this from the docs.
- Remove support to the
add_to_cache_keysparam. - Always do
self.cache_key_wrapper(json.dumps(rls_rules))in caserls_ruleshas a value.
(cherry picked from commit fdea4e2)
SUMMARY
This PR adds a new Jinja macro:
{{ current_user_rls_rules() }}. The macro returns a list of all RLS rules applied for this user in the current dataset. It works with both embedded and non-embedded users.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Screen.Recording.2025-05-29.at.1.13.16.AM.mov
TESTING INSTRUCTIONS
Unit tests added. For manual testing:
1.. Create a virtual dataset using below SQL:
{% set rls = current_user_rls_rules() %} select {% if rls %} {% for rule in rls %} 'RLS Clause: {{ rule|replace("'", '`')|tojson }}' {% if not loop.last %} || '</br>' || {% endif %} {% endfor %} {% else %} '' {% endif %} as testadminrole (make sure your test account is added to this role).ADDITIONAL INFORMATION