2022-12-05 08:07:14 +00:00
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
2022-04-12 09:45:10 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-10-22 03:17:02 +00:00
|
|
|
|
2022-12-05 08:07:14 +00:00
|
|
|
from assets.models import Asset
|
2022-05-07 08:20:12 +00:00
|
|
|
from common.db.fields import PortField
|
2022-12-05 08:07:14 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2022-04-12 09:45:10 +00:00
|
|
|
from common.utils.ip import contains_ip
|
2022-09-22 10:47:16 +00:00
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
2022-07-17 06:28:55 +00:00
|
|
|
class Endpoint(JMSBaseModel):
|
2022-04-12 09:45:10 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'), unique=True)
|
|
|
|
host = models.CharField(max_length=256, blank=True, verbose_name=_('Host'))
|
2022-09-22 07:52:47 +00:00
|
|
|
# value=0 表示 disabled
|
2023-01-16 11:02:09 +00:00
|
|
|
https_port = PortField(default=443, verbose_name=_('HTTPS port'))
|
|
|
|
http_port = PortField(default=80, verbose_name=_('HTTP port'))
|
|
|
|
ssh_port = PortField(default=2222, verbose_name=_('SSH port'))
|
|
|
|
rdp_port = PortField(default=3389, verbose_name=_('RDP port'))
|
|
|
|
mysql_port = PortField(default=33061, verbose_name=_('MySQL port'))
|
|
|
|
mariadb_port = PortField(default=33062, verbose_name=_('MariaDB port'))
|
|
|
|
postgresql_port = PortField(default=54320, verbose_name=_('PostgreSQL port'))
|
|
|
|
redis_port = PortField(default=63790, verbose_name=_('Redis port'))
|
2022-09-21 12:56:40 +00:00
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
|
|
|
|
|
|
|
default_id = '00000000-0000-0000-0000-000000000001'
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Endpoint')
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2022-09-22 07:52:47 +00:00
|
|
|
def get_port(self, target_instance, protocol):
|
2022-12-05 08:07:14 +00:00
|
|
|
from terminal.utils import db_port_manager
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.const import DatabaseTypes
|
|
|
|
if isinstance(target_instance, Asset) and \
|
|
|
|
target_instance.is_type(DatabaseTypes.ORACLE):
|
2022-09-22 07:52:47 +00:00
|
|
|
port = db_port_manager.get_port_by_db(target_instance)
|
|
|
|
else:
|
2023-01-16 11:02:09 +00:00
|
|
|
port = getattr(self, f'{protocol}_port', 0)
|
2022-09-22 07:52:47 +00:00
|
|
|
return port
|
2022-07-13 08:29:05 +00:00
|
|
|
|
2022-04-19 11:44:07 +00:00
|
|
|
def is_default(self):
|
2022-06-28 12:12:55 +00:00
|
|
|
return str(self.id) == self.default_id
|
2022-04-19 11:44:07 +00:00
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
2022-04-19 11:44:07 +00:00
|
|
|
if self.is_default():
|
2022-04-12 09:45:10 +00:00
|
|
|
return
|
|
|
|
return super().delete(using, keep_parents)
|
|
|
|
|
2022-09-22 07:52:47 +00:00
|
|
|
def is_valid_for(self, target_instance, protocol):
|
2022-06-28 12:12:55 +00:00
|
|
|
if self.is_default():
|
|
|
|
return True
|
2022-09-22 07:52:47 +00:00
|
|
|
if self.host and self.get_port(target_instance, protocol) != 0:
|
2022-06-28 12:12:55 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
@classmethod
|
|
|
|
def get_or_create_default(cls, request=None):
|
|
|
|
data = {
|
|
|
|
'id': cls.default_id,
|
|
|
|
'name': 'Default',
|
|
|
|
'host': '',
|
|
|
|
'https_port': 0,
|
|
|
|
'http_port': 0,
|
|
|
|
}
|
|
|
|
endpoint, created = cls.objects.get_or_create(id=cls.default_id, defaults=data)
|
|
|
|
return endpoint
|
|
|
|
|
2022-06-28 12:12:55 +00:00
|
|
|
@classmethod
|
|
|
|
def match_by_instance_label(cls, instance, protocol):
|
|
|
|
from assets.models import Asset
|
|
|
|
from terminal.models import Session
|
|
|
|
if isinstance(instance, Session):
|
2023-02-08 06:12:52 +00:00
|
|
|
instance = instance.get_asset()
|
2022-06-28 12:12:55 +00:00
|
|
|
if not isinstance(instance, Asset):
|
|
|
|
return None
|
|
|
|
values = instance.labels.filter(name='endpoint').values_list('value', flat=True)
|
|
|
|
if not values:
|
|
|
|
return None
|
|
|
|
endpoints = cls.objects.filter(name__in=values).order_by('-date_updated')
|
|
|
|
for endpoint in endpoints:
|
2023-05-24 09:30:30 +00:00
|
|
|
if endpoint.is_valid_for(instance, protocol):
|
2022-06-28 12:12:55 +00:00
|
|
|
return endpoint
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
2022-07-17 06:28:55 +00:00
|
|
|
class EndpointRule(JMSBaseModel):
|
2022-04-12 09:45:10 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'), unique=True)
|
|
|
|
ip_group = models.JSONField(default=list, verbose_name=_('IP group'))
|
|
|
|
priority = models.IntegerField(
|
|
|
|
verbose_name=_("Priority"), validators=[MinValueValidator(1), MaxValueValidator(100)],
|
|
|
|
unique=True, help_text=_("1-100, the lower the value will be match first"),
|
|
|
|
)
|
|
|
|
endpoint = models.ForeignKey(
|
|
|
|
'terminal.Endpoint', null=True, blank=True, related_name='rules',
|
|
|
|
on_delete=models.SET_NULL, verbose_name=_("Endpoint"),
|
|
|
|
)
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2023-06-21 10:33:58 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Endpoint rule')
|
2023-06-21 10:33:58 +00:00
|
|
|
ordering = ('priority', 'is_active', 'name')
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'{self.name}({self.priority})'
|
|
|
|
|
|
|
|
@classmethod
|
2022-09-22 07:52:47 +00:00
|
|
|
def match(cls, target_instance, target_ip, protocol):
|
2023-06-21 10:33:58 +00:00
|
|
|
for endpoint_rule in cls.objects.prefetch_related('endpoint').filter(is_active=True):
|
2022-04-12 09:45:10 +00:00
|
|
|
if not contains_ip(target_ip, endpoint_rule.ip_group):
|
|
|
|
continue
|
|
|
|
if not endpoint_rule.endpoint:
|
|
|
|
continue
|
2022-09-22 07:52:47 +00:00
|
|
|
if not endpoint_rule.endpoint.is_valid_for(target_instance, protocol):
|
2022-04-12 09:45:10 +00:00
|
|
|
continue
|
|
|
|
return endpoint_rule
|
|
|
|
|
|
|
|
@classmethod
|
2022-09-22 07:52:47 +00:00
|
|
|
def match_endpoint(cls, target_instance, target_ip, protocol, request=None):
|
|
|
|
endpoint_rule = cls.match(target_instance, target_ip, protocol)
|
2022-04-12 09:45:10 +00:00
|
|
|
if endpoint_rule:
|
|
|
|
endpoint = endpoint_rule.endpoint
|
|
|
|
else:
|
|
|
|
endpoint = Endpoint.get_or_create_default(request)
|
2022-07-19 10:59:25 +00:00
|
|
|
if not endpoint.host and request:
|
|
|
|
# 动态添加 current request host
|
|
|
|
endpoint.host = request.get_host().split(':')[0]
|
2022-04-12 09:45:10 +00:00
|
|
|
return endpoint
|