-
Notifications
You must be signed in to change notification settings - Fork 1.9k
refactor: consolidate RBAC into dojo/authorization package #14764
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
Draft
Maffooch
wants to merge
8
commits into
DefectDojo:dev
Choose a base branch
from
Maffooch:perm-cleanup
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac1b1bb
refactor: consolidate RBAC into dojo/authorization package
Maffooch b039303
Merge branch 'dev' into perm-cleanup
Maffooch 8162bfd
Merge remote-tracking branch 'upstream/dev' into perm-cleanup
Maffooch 8b334f5
fix: update notifications API import after permissions move
Maffooch 781e32a
fix: sort imports in notifications API views
Maffooch 78b949b
Merge remote-tracking branch 'upstream/dev' into perm-cleanup
Maffooch f82779e
Merge branch 'dev' into perm-cleanup
Maffooch 098b102
Merge branch 'dev' into perm-cleanup
Maffooch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Import query_registrations to trigger RBAC filter registration at startup | ||
| from dojo.authorization import query_registrations # noqa: F401 | ||
| from dojo.authorization.authorization import ( # noqa: F401 | ||
| user_has_configuration_permission, | ||
| user_has_global_permission, | ||
| user_has_global_permission_or_403, | ||
| user_has_permission, | ||
| user_has_permission_or_403, | ||
| user_is_superuser_or_global_owner, | ||
| ) | ||
| from dojo.authorization.roles_permissions import Permissions, Roles # noqa: F401 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from django.core.exceptions import PermissionDenied | ||
| from django.shortcuts import get_object_or_404 | ||
|
|
||
| from dojo.authorization.authorization import ( | ||
| user_has_configuration_permission, | ||
| user_has_global_permission_or_403, | ||
| user_has_permission_or_403, | ||
| ) | ||
| from dojo.authorization.url_permissions import URL_PERMISSIONS | ||
|
|
||
|
|
||
| class AuthorizationMiddleware: | ||
| def __init__(self, get_response): | ||
| self.get_response = get_response | ||
|
|
||
| def __call__(self, request): | ||
| return self.get_response(request) | ||
|
|
||
| def process_view(self, request, view_func, view_args, view_kwargs): | ||
| # Skip API paths -- DRF has its own permission classes | ||
| if request.path.startswith("/api/"): | ||
| return | ||
|
|
||
| resolver_match = request.resolver_match | ||
| if resolver_match is None: | ||
| return | ||
|
|
||
| url_name = resolver_match.url_name | ||
| checks = URL_PERMISSIONS.get(url_name) | ||
| if not checks: | ||
| return | ||
|
|
||
| for check in checks: | ||
| check_type = check[0] | ||
| if check_type == "global": | ||
| _, permission = check | ||
| user_has_global_permission_or_403(request.user, permission) | ||
| elif check_type == "config": | ||
| _, permission = check | ||
| if not user_has_configuration_permission(request.user, permission): | ||
| raise PermissionDenied | ||
| elif check_type == "object": | ||
| _, model, permission, arg_name = check | ||
| lookup_value = view_kwargs.get(arg_name) | ||
| if lookup_value is None: | ||
| continue # kwarg not present, skip this check | ||
| obj = get_object_or_404(model, pk=lookup_value) | ||
| user_has_permission_or_403(request.user, obj, permission) | ||
|
|
||
| return | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from django.db import models | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
|
|
||
| class Role(models.Model): | ||
| name = models.CharField(max_length=255, unique=True) | ||
| is_owner = models.BooleanField(default=False) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
| ordering = ("name",) | ||
|
|
||
| def __str__(self): | ||
| return self.name | ||
|
|
||
|
|
||
| class Dojo_Group_Member(models.Model): | ||
| group = models.ForeignKey("dojo.Dojo_Group", on_delete=models.CASCADE) | ||
| user = models.ForeignKey("dojo.Dojo_User", on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE, help_text=_("This role determines the permissions of the user to manage the group."), verbose_name=_("Group role")) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
|
|
||
|
|
||
| class Global_Role(models.Model): | ||
| user = models.OneToOneField("dojo.Dojo_User", null=True, blank=True, on_delete=models.CASCADE) | ||
| group = models.OneToOneField("dojo.Dojo_Group", null=True, blank=True, on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE, null=True, blank=True, help_text=_("The global role will be applied to all product types and products."), verbose_name=_("Global role")) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
|
|
||
|
|
||
| class Product_Member(models.Model): | ||
| product = models.ForeignKey("dojo.Product", on_delete=models.CASCADE) | ||
| user = models.ForeignKey("dojo.Dojo_User", on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
|
|
||
|
|
||
| class Product_Group(models.Model): | ||
| product = models.ForeignKey("dojo.Product", on_delete=models.CASCADE) | ||
| group = models.ForeignKey("dojo.Dojo_Group", on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
|
|
||
|
|
||
| class Product_Type_Member(models.Model): | ||
| product_type = models.ForeignKey("dojo.Product_Type", on_delete=models.CASCADE) | ||
| user = models.ForeignKey("dojo.Dojo_User", on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" | ||
|
|
||
|
|
||
| class Product_Type_Group(models.Model): | ||
| product_type = models.ForeignKey("dojo.Product_Type", on_delete=models.CASCADE) | ||
| group = models.ForeignKey("dojo.Dojo_Group", on_delete=models.CASCADE) | ||
| role = models.ForeignKey(Role, on_delete=models.CASCADE) | ||
|
|
||
| class Meta: | ||
| app_label = "dojo" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we switch to deny by default, or are there many valid urls/views that have no checks? Deny by default would help a lot in catching future changes where a permission check is forgotten.