Skip to content

Commit

Permalink
Merge pull request #47 from UKB-IT-Sec/38-hide-armis-import-if-not-co…
Browse files Browse the repository at this point in the history
…nfigured

Armis import is now hidden if not configured
  • Loading branch information
weidenba authored Dec 13, 2024
2 parents 8382d33 + c41b008 commit 2503d0a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/NAC_Service_Portal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'nac.context_processors.armis_context',
],
},
},
Expand Down
11 changes: 11 additions & 0 deletions src/nac/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from helper.armis import get_tenant_url
from django.core.cache import cache


# is armis configured? We need this to render to nav-bar correctly
def armis_context(request):
armis_is_configured = cache.get("armis_is_configured")
if armis_is_configured is None:
armis_is_configured = get_tenant_url() != "https://"
cache.set("armis_is_configured", armis_is_configured)
return {"armis_is_configured": armis_is_configured}
4 changes: 3 additions & 1 deletion src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
Add Device
</a>
</li>
{% if armis_is_configured %}
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'armis_import' %}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cloud-arrow-down" viewBox="0 0 16 16">
Expand All @@ -74,7 +75,8 @@
</svg>
Armis Import
</a>
</li>
</li>
{% endif %}
<a class="nav-link active" aria-current="page" href="{% url 'account-settings' %}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-gear" viewBox="0 0 16 16">
<path d="M11 5a3 3 0 1 1-6 0 3 3 0 0 1 6 0M8 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4m.256 7a4.5 4.5 0 0 1-.229-1.004H3c.001-.246.154-.986.832-1.664C4.484 10.68 5.711 10 8 10q.39 0 .74.025c.226-.341.496-.65.804-.918Q8.844 9.002 8 9c-5 0-6 3-6 4s1 1 1 1zm3.63-4.54c.18-.613 1.048-.613 1.229 0l.043.148a.64.64 0 0 0 .921.382l.136-.074c.561-.306 1.175.308.87.869l-.075.136a.64.64 0 0 0 .382.92l.149.045c.612.18.612 1.048 0 1.229l-.15.043a.64.64 0 0 0-.38.921l.074.136c.305.561-.309 1.175-.87.87l-.136-.075a.64.64 0 0 0-.92.382l-.045.149c-.18.612-1.048.612-1.229 0l-.043-.15a.64.64 0 0 0-.921-.38l-.136.074c-.561.305-1.175-.309-.87-.87l.075-.136a.64.64 0 0 0-.382-.92l-.148-.045c-.613-.18-.613-1.048 0-1.229l.148-.043a.64.64 0 0 0 .382-.921l-.074-.136c-.306-.561.308-1.175.869-.87l.136.075a.64.64 0 0 0 .92-.382zM14 12.5a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0"/>
Expand Down
26 changes: 25 additions & 1 deletion src/tests/test_helper/test_armis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from django.test import RequestFactory
from unittest.mock import patch, MagicMock
from helper.armis import (
armiscloud,
Expand All @@ -7,6 +8,8 @@
_remove_existing_devices,
get_devices, get_tenant_url,
)
from nac.context_processors import armis_context
from django.core.cache import cache


@pytest.fixture
Expand Down Expand Up @@ -99,6 +102,27 @@ def test_get_devices(mock_remove_existing_devices, mock_config):
mock_remove_existing_devices.assert_called_once_with(mock_devices)


def test_get_tenent_url(mock_config):
def test_get_tenant_url(mock_config):
with patch('helper.armis.armis_config', mock_config):
assert get_tenant_url() == "https://test_host"


@pytest.mark.parametrize("tenant_hostname, validity", [
("", False),
("test_host", True),
])
def test_armis_context(tenant_hostname, validity):

mock_config = {
'armis-server': {
'api_secret_key': 'test_key',
'tenant_hostname': tenant_hostname,
'sites_pattern': 'Site\\d+', # 'Site' + one or more numeric id's
'vlan_blacklist': '100,200'
}
}
with patch('helper.armis.armis_config', mock_config):
cache.clear()
factory = RequestFactory()
request = factory.get("/")
assert armis_context(request)["armis_is_configured"] == validity

0 comments on commit 2503d0a

Please sign in to comment.