Compare commits

...

1 Commits

Author SHA1 Message Date
Brian Tiemann
dc6a54ec21 Add filtering of Script objects based on object permissions with custom constraints 2026-01-18 17:23:51 -05:00
3 changed files with 98 additions and 72 deletions

View File

@@ -18,7 +18,17 @@ They can also be used as a mechanism for validating the integrity of data within
Custom scripts are Python code which exists outside the NetBox code base, so they can be updated and changed without interfering with the core NetBox installation. And because they're completely custom, there is no inherent limitation on what a script can accomplish. Custom scripts are Python code which exists outside the NetBox code base, so they can be updated and changed without interfering with the core NetBox installation. And because they're completely custom, there is no inherent limitation on what a script can accomplish.
!!! danger "Only install trusted scripts" !!! danger "Only install trusted scripts"
Custom scripts have unrestricted access to change anything in the databse and are inherently unsafe and should only be installed and run from trusted sources. You should also review and set permissions for who can run scripts if the script can modify any data. Custom scripts have unrestricted access to change anything in the database and are inherently unsafe and should only be installed and run from trusted sources. You should also review and set permissions for who can run scripts if the script can modify any data.
!!! tip "Permissions for Custom Scripts"
A user can be granted permissions on all Custom Scripts via the "Managed File" object-level permission. To further restrict a user to only be able to access certain scripts, create an additional permission on the "Script" object type, with appropriate queryset-style constraints matching fields available on Script. For example:
```json
{
"name__in": [
"MyScript"
]
}
```
## Writing Custom Scripts ## Writing Custom Scripts

View File

@@ -24,9 +24,11 @@ from extras.utils import SharedObjectViewMixin
from netbox.object_actions import * from netbox.object_actions import *
from netbox.views import generic from netbox.views import generic
from netbox.views.generic.mixins import TableMixin from netbox.views.generic.mixins import TableMixin
from users.models import ObjectPermission
from utilities.forms import ConfirmationForm, get_field_value from utilities.forms import ConfirmationForm, get_field_value
from utilities.htmx import htmx_partial, htmx_maybe_redirect_current_page from utilities.htmx import htmx_partial, htmx_maybe_redirect_current_page
from utilities.paginator import EnhancedPaginator, get_paginate_count from utilities.paginator import EnhancedPaginator, get_paginate_count
from utilities.permissions import qs_filter_from_constraints
from utilities.query import count_related from utilities.query import count_related
from utilities.querydict import normalize_querydict from utilities.querydict import normalize_querydict
from utilities.request import copy_safe_request from utilities.request import copy_safe_request
@@ -1441,12 +1443,24 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
return 'extras.view_script' return 'extras.view_script'
def get(self, request): def get(self, request):
# Permissions for the Scripts page are given via the "Managed File" object permission. To further restrict
# users to access only specified scripts, create permissions on the "Script" object with appropriate
# queryset-style constraints matching fields available on Script.
script_modules = ScriptModule.objects.restrict(request.user).prefetch_related( script_modules = ScriptModule.objects.restrict(request.user).prefetch_related(
'data_source', 'data_file', 'jobs' 'data_source', 'data_file', 'jobs'
) )
script_ct = ContentType.objects.get_for_model(Script)
script_permissions = qs_filter_from_constraints(
ObjectPermission.objects.filter(
users=self.request.user, object_types=script_ct
).values_list("constraints", flat=True)
)
available_scripts = Script.objects.filter(script_permissions, module__in=script_modules)
context = { context = {
'model': ScriptModule, 'model': ScriptModule,
'script_modules': script_modules, 'script_modules': script_modules,
'available_scripts': available_scripts,
} }
# Use partial template for dashboard widgets # Use partial template for dashboard widgets

View File

@@ -38,81 +38,83 @@
</thead> </thead>
<tbody> <tbody>
{% for script in scripts %} {% for script in scripts %}
{% with last_job=script.get_latest_jobs|first %} {% if script in available_scripts %}
<tr> {% with last_job=script.get_latest_jobs|first %}
<td> <tr>
{% if script.is_executable %} <td>
<a href="{% url 'extras:script' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a> {% if script.is_executable %}
<a href="{% url 'extras:script' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
{% else %}
<a href="{% url 'extras:script_jobs' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a>
<span class="text-danger">
<i class="mdi mdi-alert" title="{% trans "Script is no longer present in the source file" %}"></i>
</span>
{% endif %}
</td>
<td>{{ script.python_class.description|markdown|placeholder }}</td>
{% if last_job %}
<td>
<a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|isodatetime }}</a>
</td>
<td>
{% badge last_job.get_status_display last_job.get_status_color %}
</td>
{% else %} {% else %}
<a href="{% url 'extras:script_jobs' script.pk %}" id="{{ script.module }}.{{ script.class_name }}">{{ script.python_class.name }}</a> <td class="text-muted">{% trans "Never" %}</td>
<span class="text-danger"> <td>{{ ''|placeholder }}</td>
<i class="mdi mdi-alert" title="{% trans "Script is no longer present in the source file" %}"></i>
</span>
{% endif %} {% endif %}
</td>
<td>{{ script.python_class.description|markdown|placeholder }}</td>
{% if last_job %}
<td> <td>
<a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|isodatetime }}</a> {% if request.user|can_run:script and script.is_executable %}
</td> <div class="float-end d-print-none">
<td> <form action="{% url 'extras:script' script.pk %}" method="post">
{% badge last_job.get_status_display last_job.get_status_color %} {% if script.python_class.commit_default %}
</td> <input type="checkbox" name="_commit" hidden checked>
{% else %}
<td class="text-muted">{% trans "Never" %}</td>
<td>{{ ''|placeholder }}</td>
{% endif %}
<td>
{% if request.user|can_run:script and script.is_executable %}
<div class="float-end d-print-none">
<form action="{% url 'extras:script' script.pk %}" method="post">
{% if script.python_class.commit_default %}
<input type="checkbox" name="_commit" hidden checked>
{% endif %}
{% csrf_token %}
<button type="submit" name="_run" class="btn btn-primary{% if embedded %} btn-sm{% endif %}">
{% if last_job %}
<i class="mdi mdi-replay"></i> {% if not embedded %}{% trans "Run Again" %}{% endif %}
{% else %}
<i class="mdi mdi-play"></i> {% if not embedded %}{% trans "Run Script" %}{% endif %}
{% endif %} {% endif %}
</button> {% csrf_token %}
</form> <button type="submit" name="_run" class="btn btn-primary{% if embedded %} btn-sm{% endif %}">
</div> {% if last_job %}
{% endif %} <i class="mdi mdi-replay"></i> {% if not embedded %}{% trans "Run Again" %}{% endif %}
</td> {% else %}
</tr> <i class="mdi mdi-play"></i> {% if not embedded %}{% trans "Run Script" %}{% endif %}
{% if last_job and not embedded %} {% endif %}
{% for test_name, data in last_job.data.tests.items %} </button>
<tr> </form>
<td colspan="4" class="method"> </div>
<span class="ps-3">{{ test_name }}</span> {% endif %}
</td> </td>
<td class="text-end text-nowrap script-stats"> </tr>
<span class="badge text-bg-success">{{ data.success }}</span> {% if last_job and not embedded %}
<span class="badge text-bg-info">{{ data.info }}</span> {% for test_name, data in last_job.data.tests.items %}
<span class="badge text-bg-warning">{{ data.warning }}</span> <tr>
<span class="badge text-bg-danger">{{ data.failure }}</span> <td colspan="4" class="method">
</td> <span class="ps-3">{{ test_name }}</span>
</tr> </td>
{% endfor %} <td class="text-end text-nowrap script-stats">
{% elif last_job and not last_job.data.log and not embedded %} <span class="badge text-bg-success">{{ data.success }}</span>
{# legacy #} <span class="badge text-bg-info">{{ data.info }}</span>
{% for method, stats in last_job.data.items %} <span class="badge text-bg-warning">{{ data.warning }}</span>
<tr> <span class="badge text-bg-danger">{{ data.failure }}</span>
<td colspan="4" class="method"> </td>
<span class="ps-3">{{ method }}</span> </tr>
</td> {% endfor %}
<td class="text-end text-nowrap report-stats"> {% elif last_job and not last_job.data.log and not embedded %}
<span class="badge bg-success">{{ stats.success }}</span> {# legacy #}
<span class="badge bg-info">{{ stats.info }}</span> {% for method, stats in last_job.data.items %}
<span class="badge bg-warning">{{ stats.warning }}</span> <tr>
<span class="badge bg-danger">{{ stats.failure }}</span> <td colspan="4" class="method">
</td> <span class="ps-3">{{ method }}</span>
</tr> </td>
{% endfor %} <td class="text-end text-nowrap report-stats">
{% endif %} <span class="badge bg-success">{{ stats.success }}</span>
{% endwith %} <span class="badge bg-info">{{ stats.info }}</span>
<span class="badge bg-warning">{{ stats.warning }}</span>
<span class="badge bg-danger">{{ stats.failure }}</span>
</td>
</tr>
{% endfor %}
{% endif %}
{% endwith %}
{% endif %}
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>