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
- Install the plugin:
pip install netbox-branch-guard
- 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',
},
},
}
- Migrate:
python manage.py migrate
- Restart NetBox.
Using Netbox-Branch-Guard with NetBox Docker
- Configure the plugin Create plugins.py to store the plugin's configuration.
- 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',
},
},
}
-
Build the NetBox image docker compose build --no-cache
-
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
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
Writes to the Main branch are restricted
You cannot use 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.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| netbox_branch_guard-1.1.1.tar.gz | 89.7 kB | Without provenance | Details for netbox_branch_guard-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| netbox_branch_guard-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for netbox_branch_guard-1.1.1-py3-none-any.whl |
Total release size: 104.6 kB
Release files / netbox_branch_guard-1.1.1.tar.gz
| Download URL | netbox_branch_guard-1.1.1.tar.gz |
|---|---|
| Size | 89.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
594ecc048ab87cb65b705f242eb463a1793b599928baca4258472b3378010afa
|
|
BLAKE2b-256 checksum How to use checksums |
dd1839dee8605d4b64668d30f5b8aa78fb71f9b30727e37defb361f9989b6790
|
| 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.1-py3-none-any.whl
| Download URL | netbox_branch_guard-1.1.1-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0741d570481fc0e77a92ee70c91525a18cf812f421568392a883b44c7269bc59
|
|
BLAKE2b-256 checksum How to use checksums |
2fe660b662a1329cd61f570cc1bf7601d51509f377b87b699a32c83f21af1430
|
| 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.1
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.