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

Make expanded grants immutable. #278

Merged
merged 2 commits into from
Dec 31, 2024
Merged

Conversation

ggreer
Copy link
Contributor

@ggreer ggreer commented Dec 31, 2024

If we expand a grant and there isn't already a direct grant for the same entitlement and principal, mark the grant as immutable.

Also add tests.

Summary by CodeRabbit

  • New Features

    • Enhanced grant creation with immutability support.
    • Added test coverage for grant immutability scenarios.
  • Tests

    • Introduced new tests to validate grant immutability during group membership expansion, including cyclic group memberships.

@ggreer ggreer requested a review from jirwin as a code owner December 31, 2024 02:13
Copy link

coderabbitai bot commented Dec 31, 2024

Walkthrough

The pull request introduces a modification to the grant creation process in the syncer package. The primary change involves adding an immutability annotation to grants created through the newExpandedGrant method. This ensures that grants generated indirectly (through group expansions) are marked as immutable. New test cases have been added to verify the behavior of grant immutability, specifically testing the immutability of grants created through group membership expansions, including scenarios with cyclic group memberships.

Changes

File Change Summary
pkg/sync/syncer.go Added GrantImmutable annotation to grants created in newExpandedGrant method.
pkg/sync/syncer_test.go Added TestExpandGrantImmutable and TestExpandGrantImmutableCycle test functions to verify grant immutability; minor adjustments to existing tests.

Sequence Diagram

sequenceDiagram
    participant Syncer
    participant Entitlement
    participant Grant
    participant Annotations

    Syncer->>Entitlement: Expand group membership
    Syncer->>Grant: Create new grant
    Syncer->>Annotations: Add GrantImmutable annotation
    Annotations-->>Grant: Attach annotations
    Grant-->>Syncer: Return immutable grant
Loading

Poem

🐰 A Rabbit's Ode to Immutable Grants 🔒

In sync's domain, where permissions dance,
Grants now wear an unbreakable stance.
Immutable, strong, they shall not bend,
A rabbit's wisdom, protection to send.
No changes allowed, their purpose clear,
Guarding access with magical cheer! 🛡️

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -81,6 +81,107 @@ func TestExpandGrants(t *testing.T) {
_ = os.Remove(c1zpath)
}

func TestExpandGrantImmutable(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to add a test for a circular group expansion? i think this current test is a fine validation, but i was wondering about the behavoir of full circles, eg group A has member B which has member group C which has member A -> and a user could be attached at any point, but only have direct in a single group?

Copy link
Contributor Author

@ggreer ggreer Dec 31, 2024

Choose a reason for hiding this comment

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

I added a test but it looks like we'll need more changes to correctly support immutable in circular group membership. We currently fail to mark them immutable. I think that's OK for now.

If we expand a grant and there isn't already a direct grant for the same entitlement and principal, mark the grant as immutable.

Also add tests.
@ggreer ggreer force-pushed the ggreer/immutable-grant-expansion branch from 693a322 to f7bd344 Compare December 31, 2024 22:12
@ggreer ggreer merged commit 3dc26a8 into main Dec 31, 2024
4 checks passed
@ggreer ggreer deleted the ggreer/immutable-grant-expansion branch December 31, 2024 22:15
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/sync/syncer_test.go (2)

84-182: LGTM with minor suggestions for improved documentation.

The test function effectively verifies the grant immutability behavior for both direct and indirect grants. The test setup and assertions are comprehensive.

Consider adding documentation to clarify the test scenario:

+// TestExpandGrantImmutable verifies that:
+// 1. Direct grants (user1 -> group1) remain mutable
+// 2. Indirect grants through group expansion (user2 -> group2 -> group1) are marked as immutable
+// This ensures that only expanded grants without existing direct grants are immutable.
 func TestExpandGrantImmutable(t *testing.T) {

Also, consider adding more descriptive assertion messages:

-require.False(t, hasImmutable) // Direct grant should not be immutable
+require.False(t, hasImmutable, "Direct grant (user1 -> group1) should not be immutable")

-require.True(t, hasImmutable) // Expanded indirect grant should be immutable
+require.True(t, hasImmutable, "Indirect grant (user2 -> group2 -> group1) should be immutable")

184-296: LGTM with action items for cyclic grant expansion.

The test effectively sets up a cyclic group membership scenario and verifies direct grant behavior. However, there's a TODO indicating that indirect grant immutability in cyclic scenarios needs to be implemented.

The TODO comment at line 291 indicates that cyclic grant expansion needs to be fixed. Would you like me to:

  1. Help implement the fix for cyclic grant expansion?
  2. Open a GitHub issue to track this enhancement?

Consider adding documentation to clarify the cyclic test scenario:

+// TestExpandGrantImmutableCycle verifies grant immutability behavior in cyclic group memberships:
+// - Group cycle: group1 -> group2 -> group3 -> group1
+// - Direct grant: user1 -> group1 (should remain mutable)
+// - Indirect grant through cycle: user2 -> group2 -> group1 (should be immutable)
+// TODO: Currently, indirect grants in cyclic scenarios are not properly handled.
 func TestExpandGrantImmutableCycle(t *testing.T) {

Also, consider adding more descriptive assertion messages:

-require.False(t, hasImmutable) // Direct grant should not be immutable
+require.False(t, hasImmutable, "Direct grant (user1 -> group1) should not be immutable even in cyclic scenario")

-// require.True(t, hasImmutable) // Expanded indirect grant should be immutable
-require.False(t, hasImmutable) // TODO: delete this and fix the code so the above line passes
+// TODO: Enable this assertion after fixing cyclic grant expansion
+// require.True(t, hasImmutable, "Indirect grant through cycle (user2 -> group2 -> group1) should be immutable")
+require.False(t, hasImmutable, "Temporary assertion until cyclic grant expansion is fixed")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 693a322 and f7bd344.

📒 Files selected for processing (2)
  • pkg/sync/syncer.go (1 hunks)
  • pkg/sync/syncer_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/sync/syncer.go
🔇 Additional comments (1)
pkg/sync/syncer_test.go (1)

12-14: LGTM!

The new imports are necessary for reading and verifying grant annotations in the test functions.

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