From fde85f508e2db8cb0e825dd25bc7593171d535fe Mon Sep 17 00:00:00 2001 From: seolmin Date: Fri, 5 Jan 2024 00:19:08 +0900 Subject: [PATCH 1/2] fix: apply get token type method by using JWTUtil --- .../connector/datasource_plugin_connector.py | 33 +++++++++++++----- .../cost_analysis/manager/plugin_manager.py | 34 +++++++++++++------ .../cost_analysis/manager/secret_manager.py | 30 +++++++++------- .../cost_analysis/service/job_service.py | 4 +-- 4 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/spaceone/cost_analysis/connector/datasource_plugin_connector.py b/src/spaceone/cost_analysis/connector/datasource_plugin_connector.py index 210a188d..e8536ac5 100644 --- a/src/spaceone/cost_analysis/connector/datasource_plugin_connector.py +++ b/src/spaceone/cost_analysis/connector/datasource_plugin_connector.py @@ -1,7 +1,7 @@ import logging -from google.protobuf.json_format import MessageToDict from spaceone.core.connector import BaseConnector +from spaceone.core.auth.jwt.jwt_util import JWTUtil __all__ = ["DataSourcePluginConnector"] @@ -15,6 +15,8 @@ def __init__(self, *args, **kwargs): self.secret_data = None self.options = None self.schema = None + token = self.transaction.get_meta("token") + self.token_type = JWTUtil.get_value_from_token(token, "typ") def initialize(self, endpoint): static_endpoint = self.config.get("endpoint") @@ -29,9 +31,16 @@ def initialize(self, endpoint): self.schema = self.config.get("schema") def init(self, options, domain_id): - return self.client.dispatch( - "DataSource.init", {"options": options, "domain_id": domain_id} - ) + if self.token_type == "SYSTEM_TOKEN": + return self.client.dispatch( + "DataSource.init", + {"options": options, "domain_id": domain_id}, + x_domain_id=domain_id, + ) + else: + return self.client.dispatch( + "DataSource.init", {"options": options, "domain_id": domain_id} + ) def verify(self, options, secret_data, schema, domain_id): params = { @@ -60,8 +69,12 @@ def get_tasks( "last_synchronized_at": last_synchronized_at, "domain_id": domain_id, } - - return self.client.dispatch("Job.get_tasks", params) + if self.token_type == "SYSTEM_TOKEN": + return self.client.dispatch( + "DataSource.get_tasks", params, x_domain_id=domain_id + ) + else: + return self.client.dispatch("Job.get_tasks", params) def get_cost_data(self, options, secret_data, schema, task_options, domain_id): params = { @@ -71,5 +84,9 @@ def get_cost_data(self, options, secret_data, schema, task_options, domain_id): "task_options": task_options, "domain_id": domain_id, } - - return self.client.dispatch("Cost.get_data", params) + if self.token_type == "SYSTEM_TOKEN": + return self.client.dispatch( + "Cost.get_data", params, x_domain_id=domain_id + ) + else: + return self.client.dispatch("Cost.get_data", params) diff --git a/src/spaceone/cost_analysis/manager/plugin_manager.py b/src/spaceone/cost_analysis/manager/plugin_manager.py index 7270fec6..65d232e4 100644 --- a/src/spaceone/cost_analysis/manager/plugin_manager.py +++ b/src/spaceone/cost_analysis/manager/plugin_manager.py @@ -3,6 +3,7 @@ from spaceone.core import config from spaceone.core.manager import BaseManager from spaceone.core.connector.space_connector import SpaceConnector +from spaceone.core.auth.jwt.jwt_util import JWTUtil _LOGGER = logging.getLogger(__name__) @@ -10,19 +11,32 @@ class PluginManager(BaseManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + token = self.transaction.get_meta("token") + self.token_type = JWTUtil.get_value_from_token(token, "typ") self.plugin_connector: SpaceConnector = self.locator.get_connector( SpaceConnector, service="plugin", token=config.get_global("TOKEN") ) def get_plugin_endpoint(self, plugin_info, domain_id): - response = self.plugin_connector.dispatch( - "Plugin.get_plugin_endpoint", - { - "plugin_id": plugin_info["plugin_id"], - "version": plugin_info.get("version"), - "upgrade_mode": plugin_info.get("upgrade_mode", "AUTO"), - "domain_id": domain_id, - }, - ) - + if self.token_type == "SYSTEM_TOKEN": + response = self.plugin_connector.dispatch( + "Plugin.get_plugin_endpoint", + { + "plugin_id": plugin_info["plugin_id"], + "version": plugin_info.get("version"), + "upgrade_mode": plugin_info.get("upgrade_mode", "AUTO"), + "domain_id": domain_id, + }, + x_domain_id=domain_id, + ) + else: + response = self.plugin_connector.dispatch( + "Plugin.get_plugin_endpoint", + { + "plugin_id": plugin_info["plugin_id"], + "version": plugin_info.get("version"), + "upgrade_mode": plugin_info.get("upgrade_mode", "AUTO"), + "domain_id": domain_id, + }, + ) return response["endpoint"], response.get("updated_version") diff --git a/src/spaceone/cost_analysis/manager/secret_manager.py b/src/spaceone/cost_analysis/manager/secret_manager.py index ee796058..cfcfb6b3 100644 --- a/src/spaceone/cost_analysis/manager/secret_manager.py +++ b/src/spaceone/cost_analysis/manager/secret_manager.py @@ -4,7 +4,7 @@ from spaceone.core.manager import BaseManager from spaceone.core.connector.space_connector import SpaceConnector from spaceone.core import utils -from spaceone.cost_analysis.error import * +from spaceone.core.auth.jwt.jwt_util import JWTUtil _LOGGER = logging.getLogger(__name__) @@ -12,15 +12,17 @@ class SecretManager(BaseManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + token = self.transaction.get_meta("token") + self.token_type = JWTUtil.get_value_from_token(token, "typ") self.secret_connector: SpaceConnector = self.locator.get_connector( "SpaceConnector", service="secret" ) def create_secret( - self, - secret_data: dict, - resource_group: str, - schema_id: str, + self, + secret_data: dict, + resource_group: str, + schema_id: str, ): def _rollback(secret_id: str): _LOGGER.info(f"[create_secret._rollback] Delete secret : {secret_id}") @@ -45,14 +47,18 @@ def _rollback(secret_id: str): def delete_secret(self, secret_id: str): self.secret_connector.dispatch("Secret.delete", {"secret_id": secret_id}) - def list_secrets(self, query: dict): - return self.secret_connector.dispatch("Secret.list", {"query": query}) + def list_secrets(self, query: dict, domain_id: str = None) -> dict: + params = {"query": query} - def list_secrets_with_system_token(self, query: dict, domain_id: str): - token = config.get_global("TOKEN") - return self.secret_connector.dispatch( - "Secret.list", {"query": query}, x_domain_id=domain_id, token=token - ) + if domain_id: + params["domain_id"] = domain_id + + if self.token_type == "SYSTEM_TOKEN": + return self.secret_connector.dispatch( + "Secret.list", params, x_domain_id=domain_id + ) + else: + return self.secret_connector.dispatch("Secret.list", params) def get_secret(self, secret_id: str): return self.secret_connector.dispatch("Secret.get", {"secret_id": secret_id}) diff --git a/src/spaceone/cost_analysis/service/job_service.py b/src/spaceone/cost_analysis/service/job_service.py index a26768f9..371ca1ba 100644 --- a/src/spaceone/cost_analysis/service/job_service.py +++ b/src/spaceone/cost_analysis/service/job_service.py @@ -440,7 +440,7 @@ def _list_secret_ids_from_secret_filter( secret_filter, provider, workspace_id, domain_id ) query = {"filter": _filter} if _filter else {} - response = secret_manager.list_secrets_with_system_token(query, domain_id) + response = secret_manager.list_secrets(query, domain_id) return [ secret_info.get("secret_id") for secret_info in response.get("results", []) ] @@ -487,7 +487,7 @@ def _get_service_account_id_and_project_id(self, secret_id, domain_id): if secret_id: _query = {"filter": [{"k": "secret_id", "v": secret_id, "o": "eq"}]} - response = secret_mgr.list_secrets_with_system_token(_query, domain_id) + response = secret_mgr.list_secrets(_query, domain_id) results = response.get("results", []) if results: secret_info = results[0] From 0adf2c995b670cfeca40129ae5765875b946efae Mon Sep 17 00:00:00 2001 From: seolmin Date: Fri, 5 Jan 2024 01:05:51 +0900 Subject: [PATCH 2/2] fix: fix typo --- src/spaceone/cost_analysis/manager/secret_manager.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/spaceone/cost_analysis/manager/secret_manager.py b/src/spaceone/cost_analysis/manager/secret_manager.py index cfcfb6b3..9ef64b21 100644 --- a/src/spaceone/cost_analysis/manager/secret_manager.py +++ b/src/spaceone/cost_analysis/manager/secret_manager.py @@ -50,9 +50,6 @@ def delete_secret(self, secret_id: str): def list_secrets(self, query: dict, domain_id: str = None) -> dict: params = {"query": query} - if domain_id: - params["domain_id"] = domain_id - if self.token_type == "SYSTEM_TOKEN": return self.secret_connector.dispatch( "Secret.list", params, x_domain_id=domain_id