Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: 修复 Job 引用全局变量场景下值解析不符合预期的问题 #7110

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_job_instance_url,
get_node_callback_url,
has_biz_set,
is_cipher_structure,
loose_strip,
parse_passwd_value,
plat_ip_reg,
Expand Down Expand Up @@ -124,7 +125,21 @@ def execute(self, data, parent_data):
self.biz_scope_type = JobBizScopeType.BIZ.value

for _value in original_global_var:
val = loose_strip(crypto.decrypt(parse_passwd_value(_value["value"])))

if is_cipher_structure(_value["value"]):
# 只有当变量值符合密码结构,才需要尝试解析密码变量
try:
val = loose_strip(crypto.decrypt(parse_passwd_value(_value["value"])))
except Exception:
self.logger.exception(
"[job_execute_task_base] failed to decrypt value -> {value}, use plaintext".format(
value=_value["value"]
)
)
val = loose_strip(_value["value"])
else:
val = loose_strip(_value["value"])

if _value["type"] == JOBV3_VAR_CATEGORY_IP:

ip_list = self.get_ip_list(val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pipeline_plugins.components.utils import (
get_job_instance_url,
get_node_callback_url,
is_cipher_structure,
loose_strip,
parse_passwd_value,
)
Expand Down Expand Up @@ -156,7 +157,21 @@ def execute(self, data, parent_data):
biz_across = data.get_one_of_inputs("biz_across")

for _value in original_global_var:
val = loose_strip(crypto.decrypt(parse_passwd_value(_value["value"])))

if is_cipher_structure(_value["value"]):
# 只有当变量值符合密码结构,才需要尝试解析密码变量
try:
val = loose_strip(crypto.decrypt(parse_passwd_value(_value["value"])))
except Exception:
self.logger.exception(
"[job_execute_task_base] failed to decrypt value -> {value}, use plaintext".format(
value=_value["value"]
)
)
val = loose_strip(_value["value"])
else:
val = loose_strip(_value["value"])

# category为3,表示变量类型为IP
if _value["category"] == JOBV3_VAR_CATEGORY_IP:
self.logger.info("[job_execute_task_base] start find ip, var={}".format(val))
Expand Down
10 changes: 10 additions & 0 deletions pipeline_plugins/components/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ def convert_num_to_str(export_data: list):
return export_data


def is_cipher_structure(value: typing.Any) -> bool:
"""检测一个变量是否具备密码变量结构"""
if isinstance(value, str):
return True
# 密码变量格式:{"tag": "xxx", "value": "xxx"}
if isinstance(value, dict) and "tag" in value and "value" in value:
return True
return False


def parse_passwd_value(passwd_value: typing.Union[str, typing.Dict[str, str]]) -> str:
if isinstance(passwd_value, str):
return passwd_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,30 @@ def __init__(
"id": 1000032,
"type": JOBV3_VAR_CATEGORY_PASSWORD,
"name": "password",
# 密文变量
"value": {"tag": "variable", "value": crypto.encrypt("123")},
},
{
"id": 1000033,
"type": 1,
"name": "dict",
# 结构化数据
"value": {"value": 1, "test": True},
},
{
"id": 1000034,
"type": 1,
"name": "int from page",
# 页面输入的整型
"value": 0,
},
{
"id": 1000035,
"type": 1,
"name": "int from page",
# 页面输入的整型
"value": 1232314345,
},
{"id": 1000031, "type": 3, "name": "ip", "value": "0:192.168.20.218", "description": ""},
],
}
Expand Down Expand Up @@ -758,6 +780,9 @@ def __init__(
"global_var_list": [
{"id": 1000030, "value": "test"},
{"id": 1000032, "value": "123"},
{"id": 1000033, "value": "{'value': 1, 'test': True}"},
{"id": 1000034, "value": "0"},
{"id": 1000035, "value": "1232314345"},
{"id": 1000031, "server": {"ip_list": [{"ip": "192.168.20.218", "bk_cloud_id": 0}]}},
],
"callback_url": "callback_url",
Expand Down
Loading