Skip to content

feat(rls): Add Groups association to RLS#32718

Closed
Antonio-RiveroMartnez wants to merge 7 commits intoapache:masterfrom
Antonio-RiveroMartnez:rls_with_groups
Closed

feat(rls): Add Groups association to RLS#32718
Antonio-RiveroMartnez wants to merge 7 commits intoapache:masterfrom
Antonio-RiveroMartnez:rls_with_groups

Conversation

@Antonio-RiveroMartnez
Copy link
Copy Markdown
Member

SUMMARY

We recently introduced the Groups with this PR, these changes are adding support for them on the RLS rules. This way it become even more friendly and usable setting rules for users.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Creating a RLS rule

Regular rule
Screenshot 2025-03-18 at 11 47 14 AM
Base rule
Screenshot 2025-03-18 at 11 47 26 AM

Rules being applied based on groups from direct user <-> group association and rule being linked to the group

Screenshot 2025-03-18 at 11 54 34 AM

Rules being applied based on groups from role association (i.e, no direct user <-> group relation, but the group linked to a role and the user linked to that same role)

RLS rule details
Screenshot 2025-03-18 at 11 58 38 AM
Group details
Screenshot 2025-03-18 at 11 58 53 AM
Rule applied
Screenshot 2025-03-18 at 11 59 52 AM

TESTING INSTRUCTIONS

  1. Create group(s) using the existing functionality
  2. Access the Row Level Security menu and associate the group(s) you created
  3. Verify the clause gets into the generated query when for example creating a chart using a restricted table

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

- Add new table to hold many to many relation between rls and groups
- Support groups as part of rls APIs
- Consider groups when fetching rls filters
- Update the UI to allow group management for rls
…valid

- Move group query to util method and use it from commands
- Adjust schemas for API
- Consider resolving RLS clauses with roles coming from groups and viceversa
- Add integration tests to our RLS APIs
@dosubot dosubot Bot added authentication:row-level-security Related to Row Level Security change:backend Requires changing the backend labels Mar 18, 2025
@github-actions github-actions Bot added risk:db-migration PRs that require a DB migration api Related to the REST API doc Namespace | Anything related to documentation labels Mar 18, 2025
Copy link
Copy Markdown

@korbit-ai korbit-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Security Missing unique constraint on RLS filter group assignments ▹ view
Error Handling Missing FK Constraint Handling in Downgrade ▹ view
Files scanned
File Path Reviewed
superset-frontend/src/features/rls/types.ts
superset/commands/security/create.py
superset/migrations/versions/2025-03-13_16-26_19631c6eb7fc_add_relationship_between_rls_filters_.py
superset/commands/security/update.py
superset/commands/exceptions.py
superset/commands/utils.py
superset/row_level_security/schemas.py
superset/row_level_security/api.py
superset-frontend/src/features/rls/RowLevelSecurityModal.tsx
superset/connectors/sqla/models.py
superset/security/manager.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • On any given comment that Korbit raises on your pull request, you can have a discussion with Korbit by replying to the comment.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Feedback and Support

Comment thread superset/connectors/sqla/models.py
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.45%. Comparing base (76d897e) to head (c8502b0).
Report is 1666 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #32718       +/-   ##
===========================================
+ Coverage   60.48%   83.45%   +22.96%     
===========================================
  Files        1931      549     -1382     
  Lines       76236    39473    -36763     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    32941    -13173     
+ Misses      28017     6532    -21485     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 48.42% <42.55%> (-0.73%) ⬇️
javascript ?
mysql 75.68% <100.00%> (?)
postgres 75.76% <100.00%> (?)
presto 52.92% <55.31%> (-0.89%) ⬇️
python 83.45% <100.00%> (+19.94%) ⬆️
sqlite 75.27% <100.00%> (?)
unit 61.36% <55.31%> (+3.73%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- Fix lint on ui test
),
),
UniqueConstraint("group_id", "rls_filter_id", name="uq_rls_filter_group"),
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for roles we have:

RLSFilterRoles = DBTable(
    "rls_filter_roles",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("role_id", Integer, ForeignKey("ab_role.id"), nullable=False),
    Column("rls_filter_id", Integer, ForeignKey("row_level_security_filters.id")),
)

do we need to remove nullable=False now? because we can have a roles or a group?

Copy link
Copy Markdown
Member Author

@Antonio-RiveroMartnez Antonio-RiveroMartnez Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the association table so we should instead make the group_id not nullable in the RLSFilterGroups, like in the migration and match the existing RLSFilterRoles

Comment thread superset/security/manager.py Outdated
return []

# pylint: disable=import-outside-toplevel
from flask_appbuilder.security.sqla.models import assoc_group_role
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could move this one to a top level import

)

# Roles and Groups directly linked to the user
user_roles = [role.id for role in self.get_user_roles(g.user)]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_user_roles(g.user) will get all roles that are assigned to a user, directly or indirectly via groups

- Remove duplicate import
- Leverage get_user_roles that already gives you all the roles of a user no matter if direct or indirect (via groups)
@villebro
Copy link
Copy Markdown
Member

@Antonio-RiveroMartnez @dpgaspar as proposed in SIP-126, which was integrated into SIP-152, there was talk of introducing a Subject model that could be either a Role, Group or User. As we start rolling out Groups into the various entity models, it feels introducing that new model + component now would be very beneficial, as otherwise we'll have to duplicate the components (one for roles, one for groups and later one for users). This will become particularly convoluted in the Dashboard and Chart models, if we introduce editors and viewers, in which case there would be 6 dropdowns all together. Doing that work now would avoid making big design overhauls later, and would save development time by not having to migrate to the new model when it's ultimately introduced.

Thoughts?

@michael-s-molina
Copy link
Copy Markdown
Member

@dpgaspar I agree with @villebro's comment that we should not have separate selects for each entity. We should introduce the Subject model and be able to select any subject type for RLS.

@Antonio-RiveroMartnez
Copy link
Copy Markdown
Member Author

Hey @villebro @michael-s-molina, totally agree on bringing the Subject model soon 🙏 but seems to me that it was considered as part of a phase 2 in SIP-152? While the Group entity in RLS would be still considered phase 1 since it's just a complementary element of the initial PR thus it could potentially be added now? What do you guys think? Thanks for looking into this

@michael-s-molina
Copy link
Copy Markdown
Member

michael-s-molina commented Mar 18, 2025

I think it will be problematic to introduce another select for group on all screens and later consolidate all selects into one. Why don't we start with a single select given that this is a new feature? In other words, why introduce technical debt when we can actually make the experience simpler by reducing the number of things users need to handle? As @villebro pointed out, with the concepts of Editor and Viewer, we would be talking about 6 selects to handle access.

It's important to also consider the cost of doing in one way to later change it. We have docs and training courses that need to be updated.

@villebro
Copy link
Copy Markdown
Member

villebro commented Mar 18, 2025

@Antonio-RiveroMartnez I think it was my oversight not to add RLS as part of the Subject model overhaul (I wrote the bullet list of entities that will be changed that was later merged into the final SIP description). I totally assumed RLS to be part of this work, as the current Role reference in RLS really is a subject reference.

The distinction in the phasing here IMO is that Phase 1 is all about introducing Groups, and Phase 2 is all about replacing existing subject-like references with the new Subject model. The fact that we're here essentially simulating the new Subject model via double drop downs is IMO a strong indication that the Subject model + component should be introduced first, as otherwise this change would need to be completely redone in Phase 2.

@Antonio-RiveroMartnez
Copy link
Copy Markdown
Member Author

@villebro @michael-s-molina If Groups are meant to be introduced in Phase 1, shouldn’t they also work within RLS to be fully integrated? otherwise, they are partially usable. Also, as things stand, a refactor will be needed when we introduce the Subject entity regardless of Groups being part of RLS or not, but I see that we would be adding extra work on top of that.

I completely understand the concerns you've raised and I truly appreciate your time to review and having this discussion, I'm just trying to fully grasp the Phase 1 vs Phase 2 approach so I can contribute in the best way possible.

Thanks.

@dpgaspar
Copy link
Copy Markdown
Member

dpgaspar commented Mar 19, 2025

@Antonio-RiveroMartnez I think it was my oversight not to add RLS as part of the Subject model overhaul (I wrote the bullet list of entities that will be changed that was later merged into the final SIP description). I totally assumed RLS to be part of this work, as the current Role reference in RLS really is a subject reference.

The distinction in the phasing here IMO is that Phase 1 is all about introducing Groups, and Phase 2 is all about replacing existing subject-like references with the new Subject model. The fact that we're here essentially simulating the new Subject model via double drop downs is IMO a strong indication that the Subject model + component should be introduced first, as otherwise this change would need to be completely redone in Phase 2.

@villebro @michael-s-molina I understand the concerns around aligning RLS with the Subject model. However, as @Antonio-RiveroMartnez pointed out, I also firmly believe that RLS should be introduced in Phase 1, without being tied to the Subject model yet. The core goal of Phase 1 is to introduce Groups in a backward-compatible way, and leaving them out of RLS at this stage would result in an incomplete and inconsistent implementation.

Per SIP-152, Phase 1 must leave Superset in a fully functional state with respect to API and datasource authorization, ensuring a smooth transition while we prepare for the breaking changes in Phase 2. Some refactoring will be necessary in Phase 1, but that would be the case regardless of RLS.

We will not include ownership or DASHBOARD_RBAC on phase 1, so there will be no dropdown sprawling. Just one dropdown this one ;)

I don’t think adding a new dropdown to RLS is a strong enough reason to either prematurely introduce the Subject model or leave Phase 1 incomplete. This is the last change needed to complete Phase 1. Is it a perfect solution? No. But major changes rarely follow a straight, clean path.

@villebro
Copy link
Copy Markdown
Member

villebro commented Mar 19, 2025

Per SIP-152, Phase 1 must leave Superset in a fully functional state with respect to API and datasource authorization, ensuring a smooth transition while we prepare for the breaking changes in Phase 2. Some refactoring will be necessary in Phase 1, but that would be the case regardless of RLS.

@dpgaspar I'm probably missing something, as I assumed RLS should work as before without this change. Is there something in API or datasource authorization that has changed with the introduction of groups, causing issues for RLS? If this is indeed the case, then I can understand this intermediate change. But if not, I still feel this change is best suited for Phase 2 to avoid introducing this temporary workaround.

@eschutho
Copy link
Copy Markdown
Member

@michael-s-molina @villebro what do you think about us creating a subject dropdown that allows people to select either a group or a role? It will be an intermediary step closer to subjects, and then we wouldn't need to have two dropdowns.

@github-actions
Copy link
Copy Markdown
Contributor

@eschutho Processing your ephemeral environment request here. Action: up. More information on how to use or configure ephemeral environments

@github-actions
Copy link
Copy Markdown
Contributor

@eschutho Ephemeral environment spinning up at http://35.161.55.247:8080. Credentials are 'admin'/'admin'. Please allow several minutes for bootstrapping and startup.

@villebro
Copy link
Copy Markdown
Member

@michael-s-molina @villebro what do you think about us creating a subject dropdown that allows people to select either a group or a role? It will be an intermediary step closer to subjects, and then we wouldn't need to have two dropdowns.

That would make it impossible to mix and match different subject types. I also assume it would require an extra db migration, by first adding a group reference to RLS, and later replacing that with the final subject reference (likely a new table).

@dpgaspar
Copy link
Copy Markdown
Member

dpgaspar commented Mar 21, 2025

@michael-s-molina @villebro what do you think about us creating a subject dropdown that allows people to select either a group or a role? It will be an intermediary step closer to subjects, and then we wouldn't need to have two dropdowns.

That would make it impossible to mix and match different subject types. I also assume it would require an extra db migration, by first adding a group reference to RLS, and later replacing that with the final subject reference (likely a new table).

Yes, implementing this would likely require two migrations to reach our final goal, though the specifics depend on the implementation. However, on the positive side, this approach would fully introduce Groups into Superset, a step toward the broader Subjects concept. Given that a full Subjects implementation isn't imminent for several reasons, making Groups a first-class entity now, makes sense (and was approved).

@eschutho's proposal seems like a reasonable compromise, as it avoids introducing a new field to the RLS form—directly addressing @michael-s-molina's concern here. Another alternative could be feature-flagging the dropdown, though that wouldn't be my first choice.

Our goal is to get Phase 1 to be a stable and complete point regarding data authorization and RLS is an important feature on data authorization. Superset users can't fully adopt Groups without RLS.

I agree that we should avoid unnecessary tech debt, but I believe this change is small contained and justified. Especially considering that the next step has already been planned and approved in SIP-152, which builds upon SIP-126.

I think this is a reasonable path forward.

@Antonio-RiveroMartnez
Copy link
Copy Markdown
Member Author

Closing this PR in favor of the Subjects entity coming in Phase 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Related to the REST API authentication:row-level-security Related to Row Level Security change:backend Requires changing the backend doc Namespace | Anything related to documentation risk:db-migration PRs that require a DB migration size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants