Deactivate agents via security config - #851
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the existing security configuration allow-listing of onboarding protocols to support the new OnboardingProtocol.AGENT (value 9), and uses that configuration to disable agent functionality (UI + API) when the AGENT protocol is not permitted.
Changes:
- Extend
SecurityConfigdefaults to include onboarding protocol value9(Agent). - Filter device onboarding protocol form choices based on
SecurityConfig.permitted_onboarding_protocols. - Gate agent web views and agent API endpoints behind a security-config check (403 for API, redirect for web UI).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| trustpoint/management/models/security.py | Adds AGENT (9) to the default onboarding protocol allow-lists used by security modes. |
| trustpoint/devices/forms.py | Adds SecurityConfig-driven filtering of onboarding protocol choices; updates onboarding protocol widget setup. |
| trustpoint/agents/web_views.py | Wraps multiple agent UI views with AgentSecurityMixin gating. |
| trustpoint/agents/security.py | Introduces shared helper/mixin to enforce “AGENT protocol must be permitted” policy. |
| trustpoint/agents/api_views.py | Returns 403 from agent API endpoints when AGENT protocol is disabled by security config. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| #: All OnboardingProtocol values | ||
| _ALL_ONBOARDING_PROTOCOLS: ClassVar[list[int]] = [0, 1, 2, 3, 4, 5, 6, 7, 8] | ||
| _ALL_ONBOARDING_PROTOCOLS: ClassVar[list[int]] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
| #: All OnboardingProtocol values except MANUAL (0) | ||
| _ONBOARDING_PROTOCOLS_NO_MANUAL: ClassVar[list[int]] = [1, 2, 3, 4, 5, 6, 7, 8] | ||
| _ONBOARDING_PROTOCOLS_NO_MANUAL: ClassVar[list[int]] = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| all_protocol_values = {proto[0] for proto in permitted_protocols} | ||
| disabled_options = [ | ||
| proto for proto in [ | ||
| OnboardingProtocol.MANUAL, | ||
| OnboardingProtocol.AOKI, | ||
| OnboardingProtocol.BRSKI, | ||
| OnboardingProtocol.OPC_GDS_PUSH, | ||
| ] | ||
| if proto.value in all_protocol_values | ||
| ] | ||
| self.fields['onboarding_protocol'].widget = DisableOptionsSelect(disabled_options=disabled_options) |
| if not AgentSecurity.is_agent_protocol_permitted(): | ||
| return Response( | ||
| {'detail': 'Agent functionality is disabled by the current security configuration.'}, | ||
| status=status.HTTP_403_FORBIDDEN, | ||
| ) |
| class AgentProfileDefinitionTableView( | ||
| AgentSecurityMixin, PageContextMixin, LoggerMixin, ListView[AgentProfileDefinition] | ||
| ): |
| try: | ||
| cfg: SecurityConfig = SecurityConfig.objects.get() | ||
| permitted: list[int] = cfg.permitted_onboarding_protocols or [] | ||
| except SecurityConfig.DoesNotExist: | ||
| return ONBOARDING_PROTOCOLS_ALLOWED_FOR_FORMS |
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (5)
trustpoint/management/models/security.py:219
- permitted_onboarding_protocols help_text is now missing the new AGENT=9 value, and the default allow-list constants are hard-coded integer arrays that have to be manually kept in sync with OnboardingProtocol. Deriving these lists from OnboardingProtocol.values avoids future drift when new protocols are added.
#: All OnboardingProtocol values
_ALL_ONBOARDING_PROTOCOLS: ClassVar[list[int]] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#: All OnboardingProtocol values except MANUAL (0)
_ONBOARDING_PROTOCOLS_NO_MANUAL: ClassVar[list[int]] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
trustpoint/agents/web_views.py:35
- These web views are now gated by AgentSecurityMixin. The existing web view tests in trustpoint/agents/tests/test_views.py cover the happy path but don’t cover the disabled-by-security-config redirect/message behavior; adding a negative test would help ensure agents can be reliably deactivated via SecurityConfig.
class AgentProfileDefinitionTableView(
AgentSecurityMixin, PageContextMixin, LoggerMixin, ListView[AgentProfileDefinition]
):
trustpoint/agents/api_views.py:268
- New behavior: when the AGENT protocol is not permitted by SecurityConfig, this endpoint now returns 403. There are already comprehensive tests for AgentJobsView in trustpoint/agents/tests/test_views.py, but none exercise the “agent disabled by security config” path; adding a test would prevent regressions.
if not AgentSecurity.is_agent_protocol_permitted():
return Response(
{'detail': 'Agent functionality is disabled by the current security configuration.'},
status=status.HTTP_403_FORBIDDEN,
)
trustpoint/devices/forms.py:538
- DisableOptionsSelect compares the rendered option value (typically a string) against widget.disabled_options. Here disabled_options is built from OnboardingProtocol enum members, so the comparison can fail and the options won’t be disabled even when intended. Normalize both the permitted protocol values and disabled_options to strings before passing them to the widget.
disabled_options = [
proto for proto in [
OnboardingProtocol.MANUAL,
OnboardingProtocol.AOKI,
OnboardingProtocol.BRSKI,
trustpoint/agents/api_views.py:407
- New behavior: when the AGENT protocol is not permitted by SecurityConfig, this endpoint now returns 403. The existing AgentJobResultView tests do not cover the “agent disabled by security config” path; please add a test to lock in the expected 403 behavior.
if not AgentSecurity.is_agent_protocol_permitted():
return Response(
{'detail': 'Agent functionality is disabled by the current security configuration.'},
status=status.HTTP_403_FORBIDDEN,
)
|
Legal