diff --git a/apps/terminal/migrations/0004_endpoint_is_active.py b/apps/terminal/migrations/0004_endpoint_is_active.py new file mode 100644 index 000000000..fc7cc780f --- /dev/null +++ b/apps/terminal/migrations/0004_endpoint_is_active.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.13 on 2024-09-25 07:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0003_auto_20171230_0308'), + ] + + operations = [ + migrations.AddField( + model_name='endpoint', + name='is_active', + field=models.BooleanField(default=True, verbose_name='Active'), + ), + ] diff --git a/apps/terminal/models/component/endpoint.py b/apps/terminal/models/component/endpoint.py index b347dd12e..0df5f7519 100644 --- a/apps/terminal/models/component/endpoint.py +++ b/apps/terminal/models/component/endpoint.py @@ -1,5 +1,6 @@ from django.core.validators import MinValueValidator, MaxValueValidator from django.db import models +from django.db.models import Prefetch from django.utils.translation import gettext_lazy as _ from assets.models import Asset @@ -23,6 +24,7 @@ class Endpoint(JMSBaseModel): sqlserver_port = PortField(default=14330, verbose_name=_('SQLServer port')) comment = models.TextField(default='', blank=True, verbose_name=_('Comment')) + is_active = models.BooleanField(default=True, verbose_name=_('Active')) default_id = '00000000-0000-0000-0000-000000000001' @@ -98,7 +100,7 @@ class Endpoint(JMSBaseModel): values = instance.labels.filter(label__name='endpoint').values_list('label__value', flat=True) if not values: return None - endpoints = cls.objects.filter(name__in=list(values)).order_by('-date_updated') + endpoints = cls.objects.filter(is_active=True, name__in=list(values)).order_by('-date_updated') for endpoint in endpoints: if endpoint.is_valid_for(instance, protocol): endpoint = cls.handle_endpoint_host(endpoint, request) @@ -128,7 +130,8 @@ class EndpointRule(JMSBaseModel): @classmethod def match(cls, target_instance, target_ip, protocol): - for endpoint_rule in cls.objects.prefetch_related('endpoint').filter(is_active=True): + active_endpoints = Prefetch('endpoint', queryset=Endpoint.objects.filter(is_active=True)) + for endpoint_rule in cls.objects.prefetch_related(active_endpoints).filter(is_active=True): if not contains_ip(target_ip, endpoint_rule.ip_group): continue if not endpoint_rule.endpoint: diff --git a/apps/terminal/serializers/endpoint.py b/apps/terminal/serializers/endpoint.py index 2b734f71e..dbaf482e8 100644 --- a/apps/terminal/serializers/endpoint.py +++ b/apps/terminal/serializers/endpoint.py @@ -28,7 +28,7 @@ class EndpointSerializer(BulkModelSerializer): fields_small = [ 'host', 'https_port', 'http_port', 'ssh_port', 'rdp_port', 'mysql_port', 'mariadb_port', 'postgresql_port', 'redis_port', - 'oracle_port_range', 'oracle_port', 'sqlserver_port', + 'oracle_port_range', 'oracle_port', 'sqlserver_port', 'is_active' ] fields = fields_mini + fields_small + [ 'comment', 'date_created', 'date_updated', 'created_by'