From 013a530b38b9c6cd0e1c1ca1d27be1dffc3f0b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E7=A7=8B=E5=B9=B3?= <446105468@qq.com> Date: Tue, 2 Apr 2024 11:24:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=9D=9E=E7=9B=B4=E8=BF=9E=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=B8=8B=20Agent=20=E5=AE=89=E8=A3=85=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E6=9F=A5=E8=AF=A2=E9=94=99=E8=AF=AF=20(closed=20#1966?= =?UTF-8?q?)=20#=20Reviewed,=20transaction=20id:=205929?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collections/agent_new/query_password.py | 5 +++++ .../agent_new/test_query_password.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/apps/backend/components/collections/agent_new/query_password.py b/apps/backend/components/collections/agent_new/query_password.py index 334787cce..c32bea999 100644 --- a/apps/backend/components/collections/agent_new/query_password.py +++ b/apps/backend/components/collections/agent_new/query_password.py @@ -151,6 +151,8 @@ def _execute(self, data, parent_data, common_data: AgentCommonData): ) no_need_query_inst_ids = [] + direct_connection_only_inst_ids = [] + # 这里暂不支持多 cloud_ip_map = {} oa_ticket = "" @@ -160,6 +162,8 @@ def _execute(self, data, parent_data, common_data: AgentCommonData): if host.identity.auth_type != constants.AuthType.TJJ_PASSWORD: no_need_query_inst_ids.append(sub_inst.id) + elif host.bk_cloud_id != constants.DEFAULT_CLOUD: + direct_connection_only_inst_ids.append(sub_inst.id) else: cloud_ip_map[f"{host.bk_cloud_id}-{host.inner_ip}"] = {"host": host, "sub_inst_id": sub_inst.id} # 兼容 extra 为 None 的情况 @@ -167,6 +171,7 @@ def _execute(self, data, parent_data, common_data: AgentCommonData): oa_ticket = host.identity.extra_data.get("oa_ticket") self.log_info(sub_inst_ids=no_need_query_inst_ids, log_content=_("当前主机验证类型无需查询密码")) + self.move_insts_to_failed(sub_inst_ids=direct_connection_only_inst_ids, log_content=_("密码查询逻辑仅支持直连")) need_query_inst_ids = [item["sub_inst_id"] for item in cloud_ip_map.values()] if not need_query_inst_ids: return True diff --git a/apps/backend/tests/components/collections/agent_new/test_query_password.py b/apps/backend/tests/components/collections/agent_new/test_query_password.py index a87f53d93..69f66ffa5 100644 --- a/apps/backend/tests/components/collections/agent_new/test_query_password.py +++ b/apps/backend/tests/components/collections/agent_new/test_query_password.py @@ -13,6 +13,8 @@ import random import mock +from django.db.models import F +from django.utils.translation import ugettext as _ from apps.backend.components.collections.agent_new import query_password from apps.backend.components.collections.agent_new.components import ( @@ -154,3 +156,20 @@ def assert_in_teardown(self): ).count(), len(self.obj_factory.bk_host_ids), ) + + +class QueryPasswordNotInDefaultCloudIDTestCase(QueryPasswordFailedTestCase): + def _do_case_assert(self, service, method, assertion, no, name, args=None, kwargs=None): + models.Host.objects.all().update(bk_cloud_id=F("bk_cloud_id") + 1) + super()._do_case_assert(service, method, assertion, no, name, args, kwargs) + failed_subscription_instance_id_reason_map = service.failed_subscription_instance_id_reason_map + self.assertEqual(len(failed_subscription_instance_id_reason_map), self.obj_factory.init_host_num) + self.assertEqual(list(failed_subscription_instance_id_reason_map.values()), [_("密码查询逻辑仅支持直连")] * 255) + + +class QueryPasswordInDefaultCloudIDTestCase(QueryPasswordFailedTestCase): + def _do_case_assert(self, service, method, assertion, no, name, args=None, kwargs=None): + super()._do_case_assert(service, method, assertion, no, name, args, kwargs) + failed_subscription_instance_id_reason_map = service.failed_subscription_instance_id_reason_map + self.assertEqual(len(failed_subscription_instance_id_reason_map), self.obj_factory.init_host_num) + self.assertNotEquals(list(failed_subscription_instance_id_reason_map.values()), [_("密码查询逻辑仅支持直连")] * 255)