From c110a569048b2d392ada114fe2d365ce6b5b0b36 Mon Sep 17 00:00:00 2001 From: Weii Wang Date: Wed, 22 Jan 2025 01:39:12 +0800 Subject: [PATCH] Use the new client --- src/charm.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/charm.py b/src/charm.py index 84eebd0..0997633 100755 --- a/src/charm.py +++ b/src/charm.py @@ -65,6 +65,7 @@ class PlatformNotReady(Exception): _PEER_SECRET_HEALTH_ACCESS_KEY_SECRET_FIELD = "health-access-key" # nosec _CHARM_CALLBACK_SCRIPT_PATH = pathlib.Path("/opt/opencti/charm-callback.sh") _OPENSEARCH_CERT_PATH = pathlib.Path("/opt/opencti/config/opensearch.pem") +_OPENCTI_CONNECTOR_USER_PREFIX = "charm-connector-" # caused by charm libraries @@ -691,10 +692,8 @@ def _reconcile_connector(self) -> None: user = self._setup_connector_integration_and_user(client, integration) if user: current_using_users.add(user) - for opencti_user in client.list_users(): - if opencti_user.name not in current_using_users and opencti_user.name.startswith( - "charm-connector-" - ): + for opencti_user in client.list_users(name_starts_with=_OPENCTI_CONNECTOR_USER_PREFIX): + if opencti_user.name not in current_using_users: client.set_account_status(opencti_user.id, "Inactive") def _setup_connector_integration_and_user( @@ -719,7 +718,9 @@ def _setup_connector_integration_and_user( opencti_url = f"http://{self.app.name}-endpoints.{self.model.name}.svc:8080" integration.data[self.app]["opencti_url"] = opencti_url connector_user = f"charm-connector-{connector_charm_name.replace('_', '-').lower()}" - users = {u.name: u for u in client.list_users()} + users = { + u.name: u for u in client.list_users(name_starts_with=_OPENCTI_CONNECTOR_USER_PREFIX) + } groups = {g.name: g for g in client.list_groups()} if connector_user not in users: group_id = ( @@ -728,7 +729,10 @@ def _setup_connector_integration_and_user( else groups["Connectors"] ).id client.create_user(name=connector_user, groups=[group_id]) - users = {u.name: u for u in client.list_users()} + users = { + u.name: u + for u in client.list_users(name_starts_with=_OPENCTI_CONNECTOR_USER_PREFIX) + } else: if users[connector_user].account_status == "Inactive": client.set_account_status(users[connector_user].id, "Active")