Skip to main content

Netbox-Branch-Guard

This Netbox middleware plugin can be used to guard against writes to the Main branch in Netbox. It can also be setup to enforce branch ownership checks and control which branches users are able to write into.

It is used in conjunction with the Netbox Branching plugin Netbox Branching.

See the compatibility matrix for supported NetBox versions.

[!NOTE] This module has only been tested in conjuction with Netbox Community Edition. Please ensure that you fully test the behaviour of this module in a test environment before using in production.

Requirements

  • NetBox 4.x
  • Netbox-Branching plugin for branch operations
  • Python 3.10+

Installation

  1. Install the plugin:
pip install netbox-branch-guard 
  1. Enable in NetBox configuration.py:

netbox-branching must come last

PLUGINS = [
    'netbox_branch_guard',
    'netbox_branching',
]

PLUGINS_CONFIG = {
    'netbox_branch_guard': {              # Default setting in (brackets)
        'enabled': True,                  # (True) / False = plugin is enabled.
        'api_bypass': True,               # (True) / False = API can write to Main, else it's blocked.
        'superuser_bypass': True,         # (True) / False = Superuser can write to Main, else it's blocked.
        'enforce_ownership': False,       # (True) / False = Users can only write to branches they own.
        'logging': True,                  # True / (False) = Output detailed logging to the netbox log.

        # Optional - Map user groups to their allowed branches. Wildcards are allowed.
        'group_branch_map': {
            'Group 1': ['Branch 1', 'Branch 2'],
            'Group 2': ['Branch 3'],
            'Group AB *': ['Branch AB *'],
        },

        # Optional - Entire models that are safe to modify directly on main
        'excluded_models': [
            'extras.tag',
            'extras.journalentry',
            'netbox_topology_views.models.IndividualOptions',
        ],

        # Optional - Specific fields allowed to be updated on any object directly on main
        'excluded_fields': [
            'description',
            'status',
            'comments',
        ],

       # Optional - Map views to models, where those views aren't exposed so that they can excluded by model type
       'view_model_map': {
           'netbox_topology_views.views.TopologyIndividualOptionsView':
               'netbox_topology_views.models.IndividualOptions',
       },

    },

}
  1. Migrate:
python manage.py migrate
  1. Restart NetBox.

Using Netbox-Branch-Guard with NetBox Docker

  1. Configure the plugin Create plugins.py to store the plugin's configuration.
  1. Add netbox_branch_guard to PLUGINS and PLUGINS_CONFIG in plugins.py

[!IMPORTANT] netbox-branching must come last

PLUGINS = [
    'netbox_branch_guard',
    'netbox_branching',
]

PLUGINS_CONFIG = {
    'netbox_branch_guard': {              # Default setting in (brackets)
        'enabled': True,                  # (True) / False = plugin is enabled.
        'api_bypass': True,               # (True) / False = API can write to Main, else it's blocked.
        'superuser_bypass': True,         # (True) / False = Superuser can write to Main, else it's blocked.
        'enforce_ownership': False,       # (True) / False = Users can only write to branches they own.
        'logging': True,                  # True / (False) = Output detailed logging to the netbox log.

        # Optional - Map user groups to their allowed branches. Wildcards are allowed.
        'group_branch_map': {
            'Group 1': ['Branch 1', 'Branch 2'],
            'Group 2': ['Branch 3'],
            'Group AB *': ['Branch AB *'],
        },

        # Optional - Entire models that are safe to modify directly on main
        'excluded_models': [
            'extras.tag',
            'extras.journalentry',
            'netbox_topology_views.models.IndividualOptions',
        ],

        # Optional - Specific fields allowed to be updated on any object directly on main
        'excluded_fields': [
            'description',
            'status',
            'comments',
        ],

       # Optional - Map views to models, where those views aren't exposed so that they can excluded by model type
       'view_model_map': {
           'netbox_topology_views.views.TopologyIndividualOptionsView':
               'netbox_topology_views.models.IndividualOptions',
       },

    },

}
  1. Build the NetBox image docker compose build --no-cache

  2. Start NetBox Docker docker compose up -d

Usage

Permissions

  • If you are giving users the ability to create their own branches, then it's suggested to set "enforce_ownership"
  • The users will need to have the permissiones set in Netbox to allow branch add, create, and optionally, delete, for the branch object type

Branch Permissions
Permissions required to allow users to create a branch

  • If you are creating the branch beforehand and assigning the user to the appropriate group, then it's suggested to not set "enforce_ownership"
  • In the example given, you would assign the user to either "Group 1" or "Group 2" and create the branches "Branch 1", "Branch 2" and "Branch 3"

Messages

Netbox UI Examples

Restricted Main
Writes to the Main branch are restricted

Restricted Branch
You cannot use branch "..."

Own Branch
You can only modify a branch you own

Netbox Log Examples

The logging output will only appear in the Netbox log when logging is enabled and that log_level is set higher than the current Netbox log_level

The current BranchGuard settings are displayed when the plugin is initialized. Note that you may see multiple entries if there are multiple workers configured.

[BranchGuard SETTINGS] enabled: True, api_bypass: True, superuser_bypass: True, enforce_ownership: False, logging: True, group_branch_map: {'Group 1': ['Branch 1', 'Branch 2'], 'Group 2': ['Branch 3']}

Example output showing that an attempt to write to the Main branch was blocked.

[BranchGuard REQUEST] <WSGIRequest: POST '/dcim/sites/813/edit/'>
[BranchGuard USER] User: True, Groups: {'Group 1'}, requst.user.is_authenticated: True, requst.user.is_superuser: False, requst.path: /dcim/sites/813/edit/
[BranchGuard DEBUG] header=None, query=None, session=None, cookies=None, branch_id=None
[BranchGuard BLOCK] user=JohnDoe, POST /dcim/sites/813/edit/ -> No Branch (UI/API)
[BranchGuard BLOCK] Blocking writes to Main

Example output showing that an attempt to write to a branch that the user is not assigned too, was blocked.

[BranchGuard REQUEST] <WSGIRequest: POST '/dcim/sites/813/edit/'>
[BranchGuard USER] User: True, Groups: {'Group 1'}, requst.user.is_authenticated: True, requst.user.is_superuser: False, requst.path: /dcim/sites/813/edit/
[BranchGuard DEBUG] header=None, query=None, session=None, cookies=sfaqlxj5, branch_id=sfaqlxj5
[BranchGuard BLOCK] You cannot use branch "Branch 3"
[BranchGuard BLOCK] Only: "Branch 1", "Branch 2"

Example output showing that an attempt to write ot a branch was blocked as the the user is not the owner of the branch and "enforce_ownership" is set.

[BranchGuard REQUEST] <WSGIRequest: POST '/dcim/sites/813/edit/'>
[BranchGuard USER] User: True, Groups: {'RIS-View', 'RIS-Modify'}, requst.user.is_authenticated: True, requst.user.is_superuser: False, requst.path: /dcim/sites/813/edit/
[BranchGuard DEBUG] header=None, query=None, session=None, cookies=5o9aq85u, branch_id=5o9aq85u
[BranchGuard BLOCK] user=JohnDoe, branch_owner=admin, branch=5o9aq85u -> Not Branch Owner

Example output showing that the user has write permissiones and are trying to save a change in a branch but they are not a memnber of any of the groups in "group_branch_map"

[BranchGuard BLOCK] You are not assigned to a branch group"

Metadata

Release files for netbox-branch-guard 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for netbox-branch-guard 1.1.0
File Size Uploaded Provenance Actions
netbox_branch_guard-1.1.0.tar.gz 89.7 kB Without provenance Details for netbox_branch_guard-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for netbox-branch-guard 1.1.0
File Interpreter ABI Platform Actions
netbox_branch_guard-1.1.0-py3-none-any.whl Python 3 none any Details for netbox_branch_guard-1.1.0-py3-none-any.whl

Total release size: 104.5 kB

Release files / netbox_branch_guard-1.1.0.tar.gz

Download URL netbox_branch_guard-1.1.0.tar.gz
Size 89.7 kB
Tags Source
SHA-256 checksum
How to use checksums
11d7869bf246fcc446d43764d40d848b01f12b861ed408969a37474357ce7fbb
BLAKE2b-256 checksum
How to use checksums
377199249fc0c0b2d1fd03d3436e81503857c1fd85d1a0801b25824efa9c9502
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Oracle Linux Server","version":"9.8","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / netbox_branch_guard-1.1.0-py3-none-any.whl

Download URL netbox_branch_guard-1.1.0-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
149980210e87746aaaddc681e477ed4e5d59adceafe98208f6e1ba1f45a5368c
BLAKE2b-256 checksum
How to use checksums
69cfdc28dc2844eca5fd8fe9a5fb16b6d44c1e4ff004b8dd8624c2c8db93c796
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Oracle Linux Server","version":"9.8","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for netbox-branch-guard 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page