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

chore: improve the rbac migrations #28123

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

chore: improve the rbac migrations #28123

wants to merge 4 commits into from

Conversation

zlwaterfield
Copy link
Contributor

@zlwaterfield zlwaterfield commented Jan 30, 2025

Changes

Follow along for RBAC: #24512

This is just improving the feature flag RBAC migration to make sure all cases are covers

  1. Organization wide view access to feature flags
  2. Organization wide edit access to feature flags (when the above is turned on)
  3. (this existing) specific feature flag access

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Does this work well for both Cloud and self-hosted?

It doesn't have an impact.

How did you test this code?

Added more to the existing tests

@zlwaterfield zlwaterfield self-assigned this Jan 30, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Based on the provided files and context, I'll summarize the key changes in this pull request:

This PR improves the feature flag RBAC migration by enhancing access control handling and coverage. Here are the key changes:

  • Replaces PROJECT_BASED_PERMISSIONING with ADVANCED_PERMISSIONS across the codebase for unified access control
  • Adds comprehensive handling for organization-wide feature flag permissions:
    • Default view-only access for all feature flags
    • Role-based edit access when view-only is enabled
    • Specific per-flag role access controls
  • Implements atomic migration within transactions to safely convert existing permissions
  • Adds extensive test coverage for new access control scenarios including organization-wide and role-specific permissions

The changes appear well-tested and maintain backward compatibility while preparing for enhanced RBAC features.

Potential concerns:

  • Migration should be monitored for large organizations with many feature flags
  • Need to ensure proper cleanup of old FeatureFlagRoleAccess records
  • Consider performance impact of additional access control queries

18 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +358 to +359
access_level=21, # view only
)
Copy link
Contributor

Choose a reason for hiding this comment

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

style: magic number 21 used for access level - consider using a constant or enum for better maintainability

Comment on lines +373 to +379
# Should create viewer access for all flags
viewer_access = AccessControl.objects.filter(
resource="feature_flag",
access_level="viewer",
role__isnull=True,
)
self.assertEqual(viewer_access.count(), 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: test verifies viewer access but not the cleanup of old FeatureFlagRoleAccess entries - add assertion for FeatureFlagRoleAccess.objects.count() == 0

Suggested change
# Should create viewer access for all flags
viewer_access = AccessControl.objects.filter(
resource="feature_flag",
access_level="viewer",
role__isnull=True,
)
self.assertEqual(viewer_access.count(), 3)
# Should create viewer access for all flags
viewer_access = AccessControl.objects.filter(
resource="feature_flag",
access_level="viewer",
role__isnull=True,
)
self.assertEqual(viewer_access.count(), 3)
self.assertEqual(FeatureFlagRoleAccess.objects.count(), 0) # Verify old entries are cleaned up

Comment on lines 22 to 26
organization_resource_access = OrganizationResourceAccess.objects.filter(
organization_id=organization_id,
resource="feature_flag",
access_level=21,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Magic number 21 used for access_level. Should be defined as a constant or enum to improve maintainability and prevent errors


# Then we want to look at all roles where the feature flag role is edit level to apply those to all flags, we need
# to do this if the organization resource access is view only
editor_roles = Role.objects.filter(organization_id=organization_id, feature_flags_access_level=37)
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Magic number 37 used for feature_flags_access_level. Should be defined as a constant or enum

Comment on lines +29 to +36
for feature_flag in FeatureFlag.objects.filter(team__organization_id=organization_id):
AccessControl.objects.create(
# Note: no user or role so it's project wide
team=feature_flag.team,
access_level="viewer",
resource="feature_flag",
resource_id=feature_flag.id,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: No error handling for AccessControl.objects.create() - could fail silently if duplicate entries exist

Comment on lines +42 to +49
for feature_flag in FeatureFlag.objects.filter(team__organization_id=organization_id):
AccessControl.objects.create(
team=feature_flag.team,
access_level="editor",
resource="feature_flag",
resource_id=feature_flag.id,
role=role,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Potential N+1 query issue with nested loops over roles and feature flags. Consider using bulk_create

Suggested change
for feature_flag in FeatureFlag.objects.filter(team__organization_id=organization_id):
AccessControl.objects.create(
team=feature_flag.team,
access_level="editor",
resource="feature_flag",
resource_id=feature_flag.id,
role=role,
)
access_controls = [
AccessControl(
team=feature_flag.team,
access_level="editor",
resource="feature_flag",
resource_id=feature_flag.id,
role=role,
)
for feature_flag in FeatureFlag.objects.filter(team__organization_id=organization_id)
]
AccessControl.objects.bulk_create(access_controls)

@zlwaterfield zlwaterfield changed the title chore: improve the feature flag rbac migration chore: improve the rbac migrations Jan 30, 2025
@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

6 snapshot changes in total. 0 added, 6 modified, 0 deleted:

  • chromium: 0 added, 6 modified, 0 deleted (diff for shard 1)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@zlwaterfield zlwaterfield requested review from benjackwhite and a team January 30, 2025 23:53
Copy link
Contributor

github-actions bot commented Jan 31, 2025

Size Change: +5 B (0%)

Total Size: 1.16 MB

ℹ️ View Unchanged
Filename Size Change
frontend/dist/toolbar.js 1.16 MB +5 B (0%)

compressed-size-action

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

6 snapshot changes in total. 0 added, 6 modified, 0 deleted:

  • chromium: 0 added, 6 modified, 0 deleted (diff for shard 1)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants