Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): Variabilize settings and implement django tests #477

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


## [Unreleased]
### Added
- [SYSTEM] [SETTINGS] Allow to configure project with django-environ
- [TESTS] [SYSTEM] Implement Network test cases
- [TESTS] [WORKFLOW] Implement Workflow test cases
### Removed
- [API_PARSER] [CORTEX_XDR] Remove fields used for backward compatibility
- [TESTS] Remove old 'testing' folder
### Changed
- [API_PARSER] [NETSKOPE] Add the possiblity to retrieve 3 new kinds of logs
- [SYSTEM] [SETTINGS] Use Hostname value from Django settings
### Fixed
- [SYSTEM] [MONGODB] Always refer to settings to connect to mongodb
- [API_PARSER] [NETSKOPE] Remove duplicated logs
- [SYSTEM] [PATHS] Variabilize all system paths and allow to configure them from settings


## [2.19.0] - 2025-01-16
Expand Down
17 changes: 15 additions & 2 deletions home/jails.apache/.zfs-source/home/vlt-os/scripts/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@
pki["organizational_unit"]
)

with open("/var/db/pki/ca.pem", "wb") as cert_file:
cert_file.write(cacert_pem)
with open("/var/db/pki/ca.key", "wb") as key_file:
key_file.write(cakey_pem)

""" Build node certificate (overwrite if it exist) """
hostname = subprocess.check_output(['hostname']).strip().decode('utf-8')
# TODO give ca_cert and ca_key
_, _ = mk_signed_cert_files(
cert_pem, key_pem = mk_signed_cert_files(
hostname,
pki["country"],
pki["state"],
Expand All @@ -81,6 +85,15 @@
cakey_pem
)

node_pem = cert_pem + key_pem

with open("/var/db/pki/node.pem", "wb") as node_file:
node_file.write(node_pem)
with open("/var/db/pki/node.cert", "wb") as cert_file:
cert_file.write(cert_pem)
with open("/var/db/pki/node.key", "wb") as key_file:
key_file.write(key_pem)

""" Generate Diffie hellman configuration """
os.system("openssl dhparam -out /var/db/pki/dh2048.pem 2048")

Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pymongo
jinja2
iptools
django-crontab
django-environ
requests
pyOpenSSL
redis~=4.5
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ django==4.2.11 \
django-crontab==0.7.1 \
--hash=sha256:1201810a212460aaaa48eb6a766738740daf42c1a4f6aafecfb1525036929236
# via -r requirements.in
django-environ==0.11.2 \
--hash=sha256:0ff95ab4344bfeff693836aa978e6840abef2e2f1145adff7735892711590c05 \
--hash=sha256:f32a87aa0899894c27d4e1776fa6b477e8164ed7f6b3e410a62a6d72caaf64be
# via -r requirements.in
django-jsoneditor==0.2.4 \
--hash=sha256:1d3dfca28f047feefa6ebc6f9541179eb815fb459b006faf3fb8d0fb2197d2df \
--hash=sha256:d7a639a7251e376126b5be64ea588c925c7a40d45e0e212f66ef475d2f0f90bb
Expand Down
5 changes: 3 additions & 2 deletions vulture_os/applications/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

# Extern modules imports
from jinja2 import Environment, FileSystemLoader
from os.path import join as path_join

# Required exceptions imports
from jinja2.exceptions import (TemplateAssertionError, TemplateNotFound, TemplatesNotFound, TemplateRuntimeError,
Expand Down Expand Up @@ -118,13 +119,13 @@
)

# Jinja template for backends rendering
JINJA_PATH = "/home/vlt-os/vulture_os/applications/backend/config/"
JINJA_PATH = path_join(settings.BASE_DIR, "applications/backend/config/")
JINJA_TEMPLATE = "haproxy_backend.conf"

BACKEND_OWNER = HAPROXY_OWNER
BACKEND_PERMS = HAPROXY_PERMS

UNIX_SOCKET_PATH = "/var/sockets/rsyslog"
UNIX_SOCKET_PATH = path_join(settings.SOCKETS_PATH, "rsyslog")


class Backend(models.Model):
Expand Down
4 changes: 2 additions & 2 deletions vulture_os/applications/logfwd/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

# Extern modules imports
from jinja2 import Environment, FileSystemLoader
from os.path import join as path_join
import pymongo
import hashlib

Expand Down Expand Up @@ -81,8 +82,7 @@
('stream', "Stream mode, using xadd"),
)

CONF_PATH = "/usr/local/etc/rsyslog.d/10-applications.conf"
JINJA_PATH = "/home/vlt-os/vulture_os/applications/logfwd/config/"
JINJA_PATH = path_join(settings.BASE_DIR, "applications/logfwd/config/")


class LogOM (models.Model):
Expand Down Expand Up @@ -256,7 +256,7 @@
subclass_obj = self.logomrelp
elif hasattr(self, 'logomkafka'):
subclass_obj = self.logomkafka
elif hasattr(self, "logom_ptr") and type(self.logom_ptr) != LogOM:

Check failure on line 259 in vulture_os/applications/logfwd/models.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E721)

vulture_os/applications/logfwd/models.py:259:45: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
subclass_obj = self.logom_ptr
else:
raise Exception(f"Cannot find type of LogOM named '{self.name}' !")
Expand Down Expand Up @@ -823,7 +823,7 @@
""" returns the attributes of the class """
template = super().to_template(**kwargs)
tpl = Template(self.key)
key = tpl.render(Context({'ruleset': kwargs.get('ruleset')}))

Check failure on line 826 in vulture_os/applications/logfwd/models.py

View workflow job for this annotation

GitHub Actions / check

Ruff (F841)

vulture_os/applications/logfwd/models.py:826:9: F841 Local variable `key` is assigned to but never used
template.update({
'broker': self.broker,
'topic': self.topic,
Expand Down
3 changes: 2 additions & 1 deletion vulture_os/applications/reputation_ctx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

# Extern modules imports
from gzip import decompress as gzip_decompress
from os.path import join as path_join
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from re import compile as re_compile
Expand Down Expand Up @@ -75,7 +76,7 @@
'digest': HTTPDigestAuth
}

DATABASES_PATH = "/var/db/darwin"
DATABASES_PATH = path_join(settings.DBS_PATH, "darwin")
DATABASES_OWNER = "vlt-os:vlt-conf"
DATABASES_PERMS = "644"

Expand Down
3 changes: 2 additions & 1 deletion vulture_os/authentication/openid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# Django project imports
from authentication.base_repository import BaseRepository
from authentication.user_scope.models import UserScope
from os.path import join as path_join
from system.exceptions import VultureSystemConfigError
from system.pki.models import CERT_OWNER, CERT_PERMS
from toolkit.auth.authy_client import AuthyClient
Expand Down Expand Up @@ -253,7 +254,7 @@ def to_html_template(self):
}

def get_jwt_key_filename(self):
return f"/var/db/pki/openid-{self.pk}.pub"
return path_join(settings.DBS_PATH, "pki/openid-{self.pk}.pub")

@staticmethod
def jwt_validate_with_certificate(jwt_signature_type):
Expand Down
3 changes: 2 additions & 1 deletion vulture_os/authentication/portal_template/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from system.config.models import write_conf

# Extern modules imports
from os.path import join as path_join

# Required exceptions imports

Expand Down Expand Up @@ -308,7 +309,7 @@
continue

try:
with open("/usr/local/etc/haproxy.d/templates/portal_%s_%s.conf" % (str(self.id), tpl),
with open(path_join(settings.LOCALETC_PATH, "haproxy.d/templates/portal_%s_%s.conf" % (str(self.id), tpl)),
'w') as f:
html = getattr(self, tpl)
if tpl not in ["email_subject", "email_body", "email_register_subject", "email_register_body"]:
Expand Down Expand Up @@ -417,7 +418,7 @@
"""
try:
url = 'portal_statics/{}'.format(self.uid)
except:

Check failure on line 421 in vulture_os/authentication/portal_template/models.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E722)

vulture_os/authentication/portal_template/models.py:421:9: E722 Do not use bare `except`
return None

return url
Expand Down
3 changes: 2 additions & 1 deletion vulture_os/authentication/user_portal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

# Extern modules imports
from jinja2 import Environment, FileSystemLoader
from os.path import join as path_join

# Required exceptions imports
from jinja2.exceptions import (TemplateAssertionError, TemplateNotFound, TemplatesNotFound, TemplateRuntimeError,
Expand All @@ -61,7 +62,7 @@
logging.config.dictConfig(settings.LOG_SETTINGS)
logger = logging.getLogger('gui')

JINJA_PATH = "/home/vlt-os/vulture_os/authentication/user_portal/config/"
JINJA_PATH = path_join(settings.BASE_DIR, "authentication/user_portal/config/")
JINJA_TEMPLATE = "haproxy_portal.conf"


Expand Down
3 changes: 2 additions & 1 deletion vulture_os/darwin/access_control/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from bson import ObjectId
from hashlib import sha1
import logging
from os.path import join as path_join
import json

logging.config.dictConfig(settings.LOG_SETTINGS)
Expand Down Expand Up @@ -114,7 +115,7 @@
]

# Jinja template for backends rendering
JINJA_PATH = "/home/vlt-os/vulture_os/darwin/access_control/config"
JINJA_PATH = path_join(settings.BASE_DIR, "darwin/access_control/config")
JINJA_TEST_TEMPLATE = "haproxy_test.conf"


Expand Down
20 changes: 10 additions & 10 deletions vulture_os/darwin/policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@
logging.config.dictConfig(settings.LOG_SETTINGS)
logger = logging.getLogger('gui')

JINJA_PATH = "/home/vlt-os/vulture_os/darwin/log_viewer/config/"
JINJA_PATH = os_path.join(settings.BASE_DIR, "darwin/log_viewer/config/")


SOCKETS_PATH = "/var/sockets/darwin"
FILTERS_PATH = "/home/darwin/filters"
CONF_PATH = "/home/darwin/conf"
DATA_PATH = "/var/db/darwin"
SOCKETS_PATH = os_path.join(settings.SOCKETS_PATH, "darwin")
FILTERS_PATH = os_path.join(settings.HOMES_PATH, "darwin/filters")
CONF_PATH = os_path.join(settings.HOMES_PATH, "darwin/conf")
DATA_PATH = os_path.join(settings.DBS_PATH, "darwin")
TEMPLATE_OWNER = "darwin:vlt-web"
TEMPLATE_PERMS = "644"

REDIS_SOCKET_PATH = "/var/sockets/redis/redis.sock"
REDIS_SOCKET_PATH = os_path.join(settings.SOCKETS_PATH, "redis/redis.sock")
ALERTS_REDIS_LIST_NAME = "darwin_alerts"
ALERTS_REDIS_CHANNEL_NAME = "darwin.alerts"
ALERTS_LOG_FILEPATH = "/var/log/darwin/alerts.log"
ALERTS_LOG_FILEPATH = os_path.join(settings.LOGS_PATH,"darwin/alerts.log")

DGA_MODELS_PATH = CONF_PATH + '/fdgad/'
VAST_MODELS_PATH = CONF_PATH + '/fvast/'
VAML_MODELS_PATH = CONF_PATH + '/fvaml/'
DGA_MODELS_PATH = os_path.join(CONF_PATH, 'fdgad/')
VAST_MODELS_PATH = os_path.join(CONF_PATH, 'fvast/')
VAML_MODELS_PATH = os_path.join(CONF_PATH, 'fvaml/')

DARWIN_LOGLEVEL_CHOICES = (
('CRITICAL', 'Critical'),
Expand Down Expand Up @@ -777,6 +777,6 @@
def str_attrs():
return ['conf_path', 'nb_thread', 'log_level', 'config']

def __str__(self):

Check failure on line 780 in vulture_os/darwin/policy/models.py

View workflow job for this annotation

GitHub Actions / check

Ruff (F811)

vulture_os/darwin/policy/models.py:780:9: F811 Redefinition of unused `__str__` from line 594
return "[{}] {}".format(self.policy, self.name)

3 changes: 1 addition & 2 deletions vulture_os/gui/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from services.service import Service
from system.cluster.models import Cluster
from system.system import System
from toolkit.network.network import get_hostname
from workflow.workflow import Workflows

import logging
Expand Down Expand Up @@ -65,7 +64,7 @@ def admin_media(request):
'VERSION': settings.VERSION,
'CURRENT_NODE': node_name,
'DEV_MODE': settings.DEV_MODE,
'TITLE': get_hostname(),
'TITLE': settings.HOSTNAME,
'COLLAPSE': request.session.get('collapse')
}

Expand Down
6 changes: 3 additions & 3 deletions vulture_os/gui/crontab/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
import django
from django.conf import settings
django.setup()
from django.utils.crypto import get_random_string

Check failure on line 35 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:35:1: E402 Module level import not at top of file

from django.utils.timezone import now as timezone_now

Check failure on line 37 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:37:1: E402 Module level import not at top of file
from gui.models.rss import RSS

Check failure on line 38 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:38:1: E402 Module level import not at top of file
from toolkit.network.network import get_hostname, get_proxy
from toolkit.network.network import get_proxy

Check failure on line 39 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:39:1: E402 Module level import not at top of file
from applications.reputation_ctx.models import ReputationContext

Check failure on line 40 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:40:1: E402 Module level import not at top of file
from services.rsyslogd.rsyslog import restart_service as restart_rsyslog_service

Check failure on line 41 in vulture_os/gui/crontab/feed.py

View workflow job for this annotation

GitHub Actions / check

Ruff (E402)

vulture_os/gui/crontab/feed.py:41:1: E402 Module level import not at top of file
from system.exceptions import VultureSystemError

import subprocess
Expand Down Expand Up @@ -109,13 +109,13 @@
logger.info("Crontab::security_update: No vulnerability found.")
elif "is vulnerable" in res:
logger.info("Crontab::security_update: Security problem found : {}".format(res))
security_alert("Security problem found on node {}".format(get_hostname()), "danger", res)
security_alert("Security problem found on node {}".format(settings.HOSTNAME), "danger", res)
except subprocess.CalledProcessError as e:
if e.stdout.decode("utf-8").startswith("0 problem"):
logger.info("Crontab::security_update: No vulnerability found.")
elif "is vulnerable" in e.stdout.decode("utf-8"):
logger.info("Crontab::security_update: Security problem found : {}".format(e.stdout.decode('utf-8')))
security_alert("Security problem found on node {}".format(get_hostname()), "danger",
security_alert("Security problem found on node {}".format(settings.HOSTNAME), "danger",
e.stdout.decode("utf-8"))
else:
logger.error("Crontab::security_update: Failed to retrieve vulnerabilities : "
Expand Down
9 changes: 6 additions & 3 deletions vulture_os/gui/crontab/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import subprocess
import os.path

from django.conf import settings

CONF_PATH = os.path.join(settings.DBS_PATH, "acme")

def update_crl():
"""
Expand All @@ -44,7 +47,7 @@ def acme_update():
"""
:return: Run acme.sh to automatically renew Let's encrypt certificates
"""
subprocess.check_output(["/usr/local/sbin/acme.sh", "--cron", "--home", "/var/db/acme/.acme.sh"])
subprocess.check_output(["/usr/local/sbin/acme.sh", "--cron", "--home", os.path.join(CONF_PATH, ".acme.sh")])

""" Now update certificate database"""
need_restart = False
Expand All @@ -53,8 +56,8 @@ def acme_update():
subject = crypto_cert.subject
common_name_obj = subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0]
cn = common_name_obj.value
if os.path.isfile("/var/db/acme/.acme.sh/{}/{}.cer".format(cn, cn)):
with open("/var/db/acme/.acme.sh/{}/{}.cer".format(cn, cn)) as file_cert:
if os.path.isfile(os.path.join(CONF_PATH, ".acme.sh/{}/{}.cer".format(cn, cn))):
with open(os.path.join(CONF_PATH, ".acme.sh/{}/{}.cer".format(cn, cn))) as file_cert:
pem_cert = file_cert.read()
cert.cert = pem_cert
cert.save()
Expand Down
Loading
Loading