From 133a2e4714e141268acdc65b25733cedd31e6176 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 13 Nov 2020 20:03:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(assets):=20=E4=BF=AE=E5=A4=8D=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/base.py | 6 ++++-- apps/assets/models/user.py | 5 +++++ apps/assets/tasks/push_system_user.py | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/assets/models/base.py b/apps/assets/models/base.py index 282c9b928..b7239da75 100644 --- a/apps/assets/models/base.py +++ b/apps/assets/models/base.py @@ -158,9 +158,11 @@ class AuthMixin: if update_fields: self.save(update_fields=update_fields) - def has_special_auth(self, asset=None): + def has_special_auth(self, asset=None, username=None): from .authbook import AuthBook - queryset = AuthBook.objects.filter(username=self.username) + if username is None: + username = self.username + queryset = AuthBook.objects.filter(username=username) if asset: queryset = queryset.filter(asset=asset) return queryset.exists() diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 1dfd79ad2..5a1f47284 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -165,6 +165,11 @@ class SystemUser(BaseUser): def is_need_test_asset_connective(self): return self.protocol not in self.application_category_protocols + def has_special_auth(self, asset=None, username=None): + if username is None and self.username_same_with_user: + raise TypeError('System user is dynamic, username should be pass') + return super().has_special_auth(asset=asset, username=username) + @property def can_perm_to_asset(self): return self.protocol not in self.application_category_protocols diff --git a/apps/assets/tasks/push_system_user.py b/apps/assets/tasks/push_system_user.py index 0072a862a..0bb9be407 100644 --- a/apps/assets/tasks/push_system_user.py +++ b/apps/assets/tasks/push_system_user.py @@ -139,6 +139,7 @@ def get_push_windows_system_user_tasks(system_user, username=None): tasks = [] if not password: + logger.error("Error: no password found") return tasks task = { 'name': 'Add user {}'.format(username), @@ -214,14 +215,15 @@ def push_system_user_util(system_user, assets, task_name, username=None): print(_("Start push system user for platform: [{}]").format(platform)) print(_("Hosts count: {}").format(len(_hosts))) - if not system_user.has_special_auth(): + # 如果没有特殊密码设置,就不需要单独推送某台机器了 + if not system_user.has_special_auth(username=username): logger.debug("System user not has special auth") tasks = get_push_system_user_tasks(system_user, platform, username=username) run_task(tasks, _hosts) continue for _host in _hosts: - system_user.load_asset_special_auth(_host) + system_user.load_asset_special_auth(_host, username=username) tasks = get_push_system_user_tasks(system_user, platform, username=username) run_task(tasks, [_host])