perf: 优化黑名单命令提示

pull/10721/head
Aaron3S 2023-06-14 19:48:43 +08:00 committed by Jiangjie.Bai
parent 954f86f8a9
commit 9e31a5064b
2 changed files with 8 additions and 2 deletions

View File

@ -7,6 +7,10 @@ from django.conf import settings
from .callback import DefaultCallback from .callback import DefaultCallback
class CommandInBlackListException(Exception):
pass
class AdHocRunner: class AdHocRunner:
cmd_modules_choices = ('shell', 'raw', 'command', 'script', 'win_shell') cmd_modules_choices = ('shell', 'raw', 'command', 'script', 'win_shell')
@ -28,7 +32,7 @@ class AdHocRunner:
if self.module not in self.cmd_modules_choices: if self.module not in self.cmd_modules_choices:
return return
if self.module_args and self.module_args.split()[0] in settings.SECURITY_COMMAND_BLACKLIST: if self.module_args and self.module_args.split()[0] in settings.SECURITY_COMMAND_BLACKLIST:
raise Exception("command not allowed: {}".format(self.module_args[0])) raise CommandInBlackListException("command not allowed:{}".format(self.module_args.split()[0]))
def run(self, verbosity=0, **kwargs): def run(self, verbosity=0, **kwargs):
self.check_module() self.check_module()

View File

@ -19,7 +19,7 @@ from simple_history.models import HistoricalRecords
from accounts.models import Account from accounts.models import Account
from acls.models import CommandFilterACL from acls.models import CommandFilterACL
from assets.models import Asset from assets.models import Asset
from ops.ansible import JMSInventory, AdHocRunner, PlaybookRunner from ops.ansible import JMSInventory, AdHocRunner, PlaybookRunner, CommandInBlackListException
from ops.mixin import PeriodTaskModelMixin from ops.mixin import PeriodTaskModelMixin
from ops.variables import * from ops.variables import *
from ops.const import Types, Modules, RunasPolicies, JobStatus from ops.const import Types, Modules, RunasPolicies, JobStatus
@ -450,6 +450,8 @@ class JobExecution(JMSOrgBaseModel):
cb = runner.run(**kwargs) cb = runner.run(**kwargs)
self.set_result(cb) self.set_result(cb)
return cb return cb
except CommandInBlackListException as e:
print("command is rejected by black list: {}".format(e))
except Exception as e: except Exception as e:
logging.error(e, exc_info=True) logging.error(e, exc_info=True)
self.set_error(e) self.set_error(e)