Skip to content

Commit

Permalink
fix(api/ldap): test_connection use data in db, should use the data fr…
Browse files Browse the repository at this point in the history
…om form (#448)

close #447
  • Loading branch information
wklken authored May 19, 2022
1 parent 78884ff commit a95035f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/api/bkuser_core/categories/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class Syncer:

context: SyncContext = field(default_factory=SyncContext)

# 决定是否初始化client, 默认为True, 即默认会初始化; 某些场景不需要初始化(例如test_connection), 需要设置为False
with_initialize_client: bool = True

def __post_init__(self):
try:
self.category = ProfileCategory.objects.get(pk=self.category_id)
Expand All @@ -228,7 +231,7 @@ def sync(self, *args, **kwargs):
raise NotImplementedError

def get_fetcher(self):
return self.fetcher_cls(self.category_id, self.config_loader)
return self.fetcher_cls(self.category_id, self.config_loader, self.with_initialize_client)

def disable_departments_before_sync(self, exempt_ids: list = None):
"""全同步前禁用该目录下的组织
Expand Down
17 changes: 10 additions & 7 deletions src/api/bkuser_core/categories/plugins/ldap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@
class LDAPClient:
config_provider: "ConfigProvider"

with_initialize_client: bool = True

def __post_init__(self):
self.con = self.initialize(
connection_url=self.config_provider.get("connection_url"),
user=self.config_provider.get("user"),
password=self.config_provider.get("password"),
timeout_setting=self.config_provider.get("timeout_setting", 120),
use_ssl=bool(self.config_provider.get("ssl_encryption") == "SSL"),
)
if self.with_initialize_client:
self.con = self.initialize(
connection_url=self.config_provider.get("connection_url"),
user=self.config_provider.get("user"),
password=self.config_provider.get("password"),
timeout_setting=self.config_provider.get("timeout_setting", 120),
use_ssl=bool(self.config_provider.get("ssl_encryption") == "SSL"),
)

self.start_root = self.config_provider.get("basic_pull_node")

Expand Down
5 changes: 4 additions & 1 deletion src/api/bkuser_core/categories/plugins/ldap/syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ class LDAPFetcher(Fetcher):

DN_REGEX = re.compile("(?P<key>[ \\w-]+)=(?P<value>[ \\w-]+)")

# 决定是否初始化client, 默认为True, 即默认会初始化; 某些场景不需要初始化(例如test_connection), 需要设置为False
with_initialize_client: bool = True

def __post_init__(self):
self.client = LDAPClient(self.config_loader)
self.client = LDAPClient(self.config_loader, self.with_initialize_client)
self.field_mapper = ProfileFieldMapper(config_loader=self.config_loader)
self._data: Tuple[List, List, List] = None

Expand Down
4 changes: 3 additions & 1 deletion src/api/bkuser_core/categories/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def test_connection(self, request, lookup_value):
raise error_codes.LOAD_LDAP_CLIENT_FAILED

try:
syncer_cls(instance.id).fetcher.client.initialize(**serializer.validated_data)
syncer_cls(instance.id, with_initialize_client=False).fetcher.client.initialize(
**serializer.validated_data
)
except Exception:
logger.exception("failed to test initialize category<%s>", instance.id)
raise error_codes.TEST_CONNECTION_FAILED
Expand Down

0 comments on commit a95035f

Please sign in to comment.