Skip to content

Commit

Permalink
Merge pull request #129 from stat-kwon/master
Browse files Browse the repository at this point in the history
fix: apply get token type method by using JWTUtil
  • Loading branch information
stat-kwon authored Jan 4, 2024
2 parents b167d4c + fde85f5 commit f432f79
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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"]

Expand All @@ -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")
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)
34 changes: 24 additions & 10 deletions src/spaceone/cost_analysis/manager/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,40 @@
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__)


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")
30 changes: 18 additions & 12 deletions src/spaceone/cost_analysis/manager/secret_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
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__)


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}")
Expand All @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions src/spaceone/cost_analysis/service/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit f432f79

Please sign in to comment.