2022-12-01 11:41:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import re
|
|
|
|
|
|
|
|
from django.db import models
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
from common.utils import lazyproperty, get_logger
|
2022-12-02 03:12:14 +00:00
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
2022-12-05 05:27:51 +00:00
|
|
|
from .base import UserAssetAccountBaseACL
|
2022-12-01 11:41:18 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
class TypeChoices(models.TextChoices):
|
|
|
|
command = 'command', _('Command')
|
|
|
|
regex = 'regex', _('Regex')
|
|
|
|
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
class CommandGroup(JMSOrgBaseModel):
|
2022-12-01 11:41:18 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_("Name"))
|
2022-12-03 16:04:39 +00:00
|
|
|
type = models.CharField(
|
|
|
|
max_length=16, default=TypeChoices.command, choices=TypeChoices.choices,
|
|
|
|
verbose_name=_("Type")
|
|
|
|
)
|
2024-05-17 10:24:13 +00:00
|
|
|
content = models.TextField(verbose_name=_("Content"), help_text=_("One command per line"))
|
2022-12-01 11:41:18 +00:00
|
|
|
ignore_case = models.BooleanField(default=True, verbose_name=_('Ignore case'))
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
TypeChoices = TypeChoices
|
|
|
|
|
2022-12-01 11:41:18 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = [('org_id', 'name')]
|
2022-12-03 16:04:39 +00:00
|
|
|
verbose_name = _("Command group")
|
2022-12-01 11:41:18 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def pattern(self):
|
|
|
|
if self.type == 'command':
|
2022-12-03 16:04:39 +00:00
|
|
|
s = self.construct_command_regex(self.content)
|
2022-12-01 11:41:18 +00:00
|
|
|
else:
|
|
|
|
s = r'{0}'.format(self.content)
|
|
|
|
return s
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def construct_command_regex(cls, content):
|
|
|
|
regex = []
|
|
|
|
content = content.replace('\r\n', '\n')
|
|
|
|
for _cmd in content.split('\n'):
|
|
|
|
cmd = re.sub(r'\s+', ' ', _cmd)
|
|
|
|
cmd = re.escape(cmd)
|
|
|
|
cmd = cmd.replace('\\ ', '\s+')
|
|
|
|
|
|
|
|
# 有空格就不能 铆钉单词了
|
|
|
|
if ' ' in _cmd:
|
|
|
|
regex.append(cmd)
|
|
|
|
continue
|
|
|
|
if not cmd:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# 如果是单个字符
|
|
|
|
if cmd[-1].isalpha():
|
|
|
|
regex.append(r'\b{0}\b'.format(cmd))
|
|
|
|
else:
|
|
|
|
regex.append(r'\b{0}'.format(cmd))
|
|
|
|
s = r'{}'.format('|'.join(regex))
|
|
|
|
return s
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
def match(self, data):
|
|
|
|
succeed, error, pattern = self.compile_regex(self.pattern, self.ignore_case)
|
|
|
|
if not succeed:
|
|
|
|
return False, ''
|
|
|
|
|
|
|
|
found = pattern.search(data)
|
|
|
|
if not found:
|
|
|
|
return False, ''
|
|
|
|
else:
|
|
|
|
return True, found.group()
|
|
|
|
|
2022-12-01 11:41:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def compile_regex(regex, ignore_case):
|
|
|
|
args = []
|
|
|
|
if ignore_case:
|
|
|
|
args.append(re.IGNORECASE)
|
|
|
|
try:
|
|
|
|
pattern = re.compile(regex, *args)
|
|
|
|
except Exception as e:
|
|
|
|
error = _('The generated regular expression is incorrect: {}').format(str(e))
|
|
|
|
logger.error(error)
|
|
|
|
return False, error, None
|
|
|
|
return True, '', pattern
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-12-04 14:46:47 +00:00
|
|
|
return '{} % {}'.format(self.name, self.type)
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2022-12-02 03:12:14 +00:00
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
class CommandFilterACL(UserAssetAccountBaseACL):
|
2023-10-18 10:46:49 +00:00
|
|
|
command_groups = models.ManyToManyField(
|
|
|
|
CommandGroup, verbose_name=_('Command group'),
|
|
|
|
related_name='command_filters'
|
|
|
|
)
|
2022-12-03 16:04:39 +00:00
|
|
|
|
2022-12-05 05:27:51 +00:00
|
|
|
class Meta(UserAssetAccountBaseACL.Meta):
|
|
|
|
abstract = False
|
2022-12-02 03:12:14 +00:00
|
|
|
verbose_name = _('Command acl')
|
|
|
|
|
2022-12-04 14:46:47 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2022-12-06 10:26:07 +00:00
|
|
|
def create_command_review_ticket(self, run_command, session, cmd_filter_acl, org_id):
|
2022-12-01 11:41:18 +00:00
|
|
|
from tickets.const import TicketType
|
|
|
|
from tickets.models import ApplyCommandTicket
|
|
|
|
data = {
|
|
|
|
'title': _('Command confirm') + ' ({})'.format(session.user),
|
|
|
|
'type': TicketType.command_confirm,
|
|
|
|
'applicant': session.user_obj,
|
|
|
|
'apply_run_user_id': session.user_id,
|
|
|
|
'apply_run_asset': str(session.asset),
|
|
|
|
'apply_run_account': str(session.account),
|
|
|
|
'apply_run_command': run_command[:4090],
|
|
|
|
'apply_from_session_id': str(session.id),
|
2022-12-06 10:26:07 +00:00
|
|
|
'apply_from_cmd_filter_acl_id': str(cmd_filter_acl.id),
|
2022-12-01 11:41:18 +00:00
|
|
|
'org_id': org_id,
|
|
|
|
}
|
|
|
|
ticket = ApplyCommandTicket.objects.create(**data)
|
|
|
|
assignees = self.reviewers.all()
|
|
|
|
ticket.open_by_system(assignees)
|
|
|
|
return ticket
|