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

feat: OBS-406 - [Netbox Diode Plugin] Secure NetBox diode plugin API endpoints #46

Merged

Conversation

Julio-Oliveira-Encora
Copy link
Contributor

Added the permission classes IsAuthenticated and IsDiodeViewer] for ObjectStateView.
It checks if the user is authenticated and if the user has permissions according to the "object_type" parameter.
Also, check if the request is GET.

Created permissions.py.
Added permissions for tests.
Copy link

linear bot commented Feb 26, 2024

OBS-406 Secure NetBox diode plugin API endpoints

NetBox Diode plugin API endpoints need to be secured.

We should consider setting dedicated permissions per API endpoint/view, so we can decide if NetBox user can perform specific operations.

Example use of custom permissions per view: https://github.com/netbox-community/netbox/blob/develop/netbox/virtualization/views.py#L651-L652

Copy link
Member

@mfiedorowicz mfiedorowicz left a comment

Choose a reason for hiding this comment

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

Alternative to avoid the need to check for individual objects permissions:

  1. Create netbox_diode_plugin/models.py:
#!/usr/bin/env python
# Copyright 2024 NetBox Labs Inc
"""Diode Netbox Plugin - Models."""

from django.db import models


class ObjectState(models.Model):
    """
    Dummy model used to generate permissions for Diode NetBox Plugin. Does not exist in the database.
    """

    class Meta:
        managed = False

        default_permissions = ()
        
        permissions = (
            ("view_objectstate", "Can view ObjectState"),
        )
  1. In your permissions.py:
#!/usr/bin/env python
# Copyright 2024 NetBox Labs Inc
"""Diode Netbox Plugin - API Permissions."""

from rest_framework.permissions import SAFE_METHODS, BasePermission


class IsDiodeViewer(BasePermission):
    """
    Custom permission to allow users that has permissions to view the object type.

    For example, if the request contains "object_type=dcim.site" and the user has this permission, he can see the object.
    """

    def has_permission(self, request, view):
        """Check if the request is in SAFE_METHODS and user has netbox_diode_plugin.view_objectstate permission."""

        return request.method in SAFE_METHODS and request.user.has_perm(f'netbox_diode_plugin.view_objectstate')

NetBox permission with our ObjectState object based on dummy model:

Screenshot 2024-02-26 at 21 23 37

What do you think?

@Julio-Oliveira-Encora Julio-Oliveira-Encora merged commit 43aec3e into develop Feb 27, 2024
14 checks passed
@Julio-Oliveira-Encora Julio-Oliveira-Encora deleted the obs-406-secure-netbox-diode-plugin-api-endpoints branch February 27, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants