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

fix: risk acceptance permission overrides #1417

Conversation

nas-tabchiche
Copy link
Contributor

@nas-tabchiche nas-tabchiche commented Jan 24, 2025

  • Raise if user is not approver when trying to approve/reject/revoke acceptance
  • Add reject and revoke acceptance exceptions
  • refactor: move permission overrides to the view

Summary by CodeRabbit

  • Security Enhancements

    • Improved permission checks for risk acceptance actions.
    • Added more granular control over accepting, rejecting, and revoking risk acceptances.
    • Enhanced error handling and logging for unauthorized access attempts.
  • Bug Fixes

    • Strengthened security by preventing unauthorized state changes for risk acceptances.

Copy link

coderabbitai bot commented Jan 24, 2025

Walkthrough

The pull request introduces enhanced permission handling in the risk acceptance workflow. The changes modify the RiskAcceptanceViewSet and RBACPermissions classes to implement more granular action-specific permission checks. A new permission_overrides mechanism is added, allowing dynamic permission verification based on the specific action being performed, while simplifying the previous logic for special cases.

Changes

File Change Summary
backend/core/permissions.py Updated permission check mechanism to support dynamic action-based permission overrides. Removed special case handling for "risk-acceptances-accept".
backend/core/views.py Added permission_overrides to RiskAcceptanceViewSet with specific permissions for accept, reject, and revoke actions. Enhanced permission checks in methods to raise PermissionDenied when unauthorized.

Sequence Diagram

sequenceDiagram
    participant User
    participant RiskAcceptanceViewSet
    participant RBACPermissions
    
    User->>RiskAcceptanceViewSet: Attempt action (accept/reject/revoke)
    RiskAcceptanceViewSet->>RBACPermissions: Check permissions
    RBACPermissions-->>RiskAcceptanceViewSet: Verify action-specific permission
    alt Permission Granted
        RiskAcceptanceViewSet->>User: Allow action
    else Permission Denied
        RiskAcceptanceViewSet->>User: Raise PermissionDenied
    end
Loading

Poem

🐰 In the realm of risk and might,
Permissions dance with rabbit's delight!
Actions checked with careful grace,
Security finds its rightful place.
A code ballet of access true 🔒
Ensures that only the wise pursue!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c2f212a and 7f5c9ee.

📒 Files selected for processing (1)
  • backend/core/views.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: Analyze (python)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (4)
backend/core/views.py (4)

1679-1683: LGTM! Clean implementation of permission overrides.

The permission mapping is consistent and well-structured, requiring the 'approve_riskacceptance' permission for accept, reject, and revoke actions.


1720-1729: LGTM! Proper permission check and error handling.

The implementation correctly validates that only the approver can accept the risk acceptance, with appropriate error logging and user feedback.


1734-1743: LGTM! Consistent implementation with correct terminology.

The implementation follows the same secure pattern as the accept action, and correctly uses "risk acceptance" in the error message.


1748-1757: LGTM! Consistent implementation with correct terminology.

The implementation follows the same secure pattern as the other actions, and correctly uses "risk acceptance" in the error message.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 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.

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)
backend/core/views.py (2)

1720-1729: Consider a unified permissions strategy.

The check for request.user != self.get_object().approver ensures that only the designated approver can accept the risk acceptance. However, this duplicates the permission checking logic in RBAC. It might be more cohesive to rely solely on RBAC for verifying user eligibility and use the object’s designated approver field only as supporting data if the user passes the RBAC checks.


1734-1743: Retain consistency in naming log messages.

The error message logs “risk rejectance” which differs from “risk acceptance” used in other logs. Consider a uniform naming convention such as “reject risk acceptance” for consistency and clarity in logs.

-                "Only the approver can reject the risk rejectance",
+                "Only the approver can reject the risk acceptance",
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 362f633 and c2f212a.

📒 Files selected for processing (2)
  • backend/core/permissions.py (1 hunks)
  • backend/core/views.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: ruff (3.12)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
backend/core/views.py (2)

1679-1684: Enforce consistent permission checks for risk acceptance actions.

This new permission_overrides dictionary correctly aligns with the PR objective by ensuring that the approve_riskacceptance codename is required for the accept, reject, and revoke actions. This design makes permissions more explicit and more straightforward to maintain.


1748-1757: Verify intention for revoke action checks.

This block follows the same pattern of verifying approver identity before revoking. Ensure that workflow states and transitions allow for revocation by the same user who is the designated approver and confirm alignment with the updated approve_riskacceptance requirement. If additional states or concurrency checks are planned, consider adding those here.

backend/core/permissions.py (1)

40-47: Excellent approach to dynamic permission overrides.

This logic correctly retrieves custom permission codes from view.permission_overrides if the view’s action matches an overridden permission. This design keeps your permission checks flexible and aligns with standard DRF patterns. Good job!

Mohamed-Hacene

This comment was marked as duplicate.

backend/core/views.py Outdated Show resolved Hide resolved
backend/core/views.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@eric-intuitem eric-intuitem left a comment

Choose a reason for hiding this comment

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

LGTM

@nas-tabchiche nas-tabchiche merged commit b8713c9 into main Jan 26, 2025
18 checks passed
@nas-tabchiche nas-tabchiche deleted the CA-824-extern-approver-can-not-reject-revoke-risk-acceptances branch January 26, 2025 20:49
@github-actions github-actions bot locked and limited conversation to collaborators Jan 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants