Skip to content
Open
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
6 changes: 3 additions & 3 deletions bkflow/apigw/serializers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from pipeline.exceptions import PipelineException
from rest_framework import serializers

from bkflow.constants import MAX_LEN_OF_TASK_NAME, USER_NAME_MAX_LENGTH
from bkflow.constants import MAX_LEN_OF_TASK_NAME, USER_NAME_MAX_LENGTH, ValidateType
from bkflow.label.models import Label
from bkflow.pipeline_web.parser.validator import validate_web_pipeline_tree
from bkflow.pipeline_validate.handler import ValidatorHandler
from bkflow.template.models import TemplateMockData
from bkflow.utils.strings import standardize_pipeline_node_name

Expand Down Expand Up @@ -199,7 +199,7 @@ class PipelineTreeSerializer(serializers.Serializer):
def validate_pipeline_tree(self, pipeline_tree):
try:
standardize_pipeline_node_name(pipeline_tree)
validate_web_pipeline_tree(pipeline_tree)
ValidatorHandler.validate(pipeline_tree, validate_type=ValidateType.TASK)
except PipelineException as e:
raise serializers.ValidationError(_("流程树校验失败: {}").format(str(e)))

Expand Down
12 changes: 8 additions & 4 deletions bkflow/apigw/serializers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from pipeline.validators import validate_pipeline_tree
from rest_framework import serializers

from bkflow.constants import MAX_LEN_OF_TEMPLATE_NAME, USER_NAME_MAX_LENGTH
from bkflow.constants import (
MAX_LEN_OF_TEMPLATE_NAME,
USER_NAME_MAX_LENGTH,
ValidateType,
)
from bkflow.label.models import Label
from bkflow.pipeline_validate.handler import ValidatorHandler
from bkflow.space.models import Space
from bkflow.template.models import Template

Expand Down Expand Up @@ -105,7 +109,7 @@ def validate(self, attrs):

if pipeline_tree:
try:
validate_pipeline_tree(pipeline_tree, cycle_tolerate=True)
ValidatorHandler.validate(pipeline_tree, validate_type=ValidateType.TEMPLATE)
except Exception as e:
logger.exception(f"CreateTemplateSerializer pipeline validate error, err = {e}")
raise serializers.ValidationError(_(f"参数校验失败,pipeline校验不通过, err={e}"))
Expand Down Expand Up @@ -166,7 +170,7 @@ def validate(self, attrs):

if pipeline_tree:
try:
validate_pipeline_tree(pipeline_tree, cycle_tolerate=True)
ValidatorHandler.validate(pipeline_tree, validate_type=ValidateType.TEMPLATE)
except Exception as e:
logger.exception(f"CreateTemplateSerializer pipeline validate error, err = {e}")
raise serializers.ValidationError(_(f"参数校验失败,pipeline校验不通过, err={e}"))
Expand Down
6 changes: 6 additions & 0 deletions bkflow/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,9 @@ class VariableType(Enum):


VARIABLE_TYPES = [VariableType.SPACE.value, VariableType.SCOPE.value]


class ValidateType(Enum):
TEMPLATE = "template"
TASK = "task"
GENERAL = "general"
18 changes: 18 additions & 0 deletions bkflow/pipeline_validate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
31 changes: 31 additions & 0 deletions bkflow/pipeline_validate/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""

from django.apps import AppConfig


class PipelineValidateConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "bkflow.pipeline_validate"
label = "pipeline_validate"
verbose_name = "流程校验"

def ready(self):
# 导入 validators 包,触发所有校验类的加载与注册
import bkflow.pipeline_validate.validators # noqa
58 changes: 58 additions & 0 deletions bkflow/pipeline_validate/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ 项目规范要求新增 Python 文件顶部包含 # -*- coding: utf-8 -*- 及版权注释。本 PR 新增的文件缺少 # -*- coding: utf-8 -*- 行(虽然 Python 3 默认 UTF-8,但项目有此约定)。

TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from typing import Optional

from pipeline.exceptions import PipelineException

from bkflow.constants import ValidateType


class ValidatorHandler:
"""校验器处理器"""

__hub = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__hub 作为可变类变量在测试场景下可能存在状态污染(上一个测试注册的 validator 会影响下一个)。考虑提供 clear()unregister() 类方法供测试清理使用。


@classmethod
def register(cls, validator_cls) -> None:
"""注册校验器类"""
if validator_cls.name is None:
raise ValueError(f"校验器 {validator_cls.__name__} 的 name 属性不能为 None")
if validator_cls.name in cls.__hub:
existing_cls = cls.__hub[validator_cls.name]
raise ValueError(f"校验器名称 '{validator_cls.name}' 已被 {existing_cls.__name__} 注册,")
cls.__hub[validator_cls.name] = validator_cls

@classmethod
def validate(cls, web_pipeline_tree: dict, validate_type: Optional[ValidateType] = None):
validators_to_run = []
for validator_name, validator_cls in cls.__hub.items():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ ValidatorHandler.validate 中校验器的执行顺序依赖 __hub dict 的插入顺序(即 apps.ready() 中的 import 顺序)。建议在 BasePipelineValidator 中增加 priority 属性,validate 方法按 priority 排序后执行,避免隐式的顺序依赖。

# 获取校验器的类型
validator_validate_type = getattr(validator_cls, "validate_type", None)

if validate_type is None:
# 默认行为:执行所有校验器
validators_to_run.append((validator_name, validator_cls))
elif validator_validate_type in [validate_type.value, ValidateType.GENERAL.value]:
# 指定类型:执行匹配类型和通用类型的校验器
validators_to_run.append((validator_name, validator_cls))

for validator_name, validator_cls in validators_to_run:
result = validator_cls.validate(web_pipeline_tree)
if not result.is_valid:
raise PipelineException(result.error)
32 changes: 32 additions & 0 deletions bkflow/pipeline_validate/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""

from bkflow.pipeline_validate.validators.general import ( # noqa
ConstantsValidator,
MakoKeywordValidator,
PipelineTreeValidator,
)
from bkflow.pipeline_validate.validators.task import ContextHydrateValidator # noqa
from bkflow.pipeline_validate.validators.template import ( # noqa
ConstantsKeyPatternValidator,
ConstantsSourceInfoValidator,
MutualExclusionValidator,
OutputsKeyPatternValidator,
SchemaValidator,
)
58 changes: 58 additions & 0 deletions bkflow/pipeline_validate/validators/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from typing import Dict

from bkflow.pipeline_validate.handler import ValidatorHandler


class ValidatorResult:
def __init__(self, is_valid: bool, error: str = None):
self.is_valid = is_valid
self.error = error


class BasePipelineValidator:
name = None
validate_type = None

def __init_subclass__(cls, *args, **kwargs):
super().__init_subclass__(*args, **kwargs)

# 检查继承的类中是否有 validate 方法
if not hasattr(cls, "validate"):
raise ValueError(f"[{cls.__name__}] Missing required method: validate")

necessary_attrs = ["name", "validate_type"]
for attr in necessary_attrs:
if not hasattr(cls, attr) or getattr(cls, attr) is None:
raise ValueError(f"[{cls.__name__}] Missing required attribute: {attr}")

ValidatorHandler.register(cls)

@classmethod
def validate(cls, web_pipeline_tree: Dict) -> ValidatorResult:
raise NotImplementedError("子类必须实现 validate 方法")


def _get_constant_display_name(const: dict, key: str) -> str:
"""获取变量的显示名称,优先使用 name 字段"""
name = const.get("name", "")
if name:
return f"「{name}」({key})"
return f"「{key}」"
85 changes: 85 additions & 0 deletions bkflow/pipeline_validate/validators/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from mako.codegen import RESERVED_NAMES
from pipeline.exceptions import PipelineException
from pipeline.validators import validate_pipeline_tree

from bkflow.constants import ValidateType
from bkflow.pipeline_validate.validators.base import (
BasePipelineValidator,
ValidatorResult,
)
from bkflow.pipeline_web.parser.schemas import KEY_PATTERN_RE
from bkflow.utils.pipeline import validate_pipeline_tree_constants


class PipelineTreeValidator(BasePipelineValidator):
name = "pipeline_tree_validator"
validate_type = ValidateType.GENERAL.value

@classmethod
def validate(cls, web_pipeline_tree: dict) -> ValidatorResult:
try:
validate_pipeline_tree(web_pipeline_tree, cycle_tolerate=True)
return ValidatorResult(is_valid=True)
except Exception as e:
error_message = f"流程树校验失败: {str(e)}"
return ValidatorResult(is_valid=False, error=error_message)


class ConstantsValidator(BasePipelineValidator):
"""变量引用校验器,校验 pipeline tree 中 constants 的引用是否合法"""

name = "constants_validator"
validate_type = ValidateType.GENERAL.value

@classmethod
def validate(cls, web_pipeline_tree: dict) -> ValidatorResult:
try:
validate_pipeline_tree_constants(web_pipeline_tree["constants"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ ValidatorHandler.validate 遍历 __hub dict,校验器执行顺序取决于注册顺序。ConstantsValidator (GENERAL) 直接访问 web_pipeline_tree["constants"],如果在 SchemaValidator 之前执行且输入缺少 constants 字段,会抛出 KeyError 而非友好的校验错误。建议用 .get("constants", {}) 防御或确保执行顺序。

return ValidatorResult(is_valid=True)
except PipelineException as e:
error_message = f"变量引用错误: {str(e)}"
return ValidatorResult(is_valid=False, error=error_message)


class MakoKeywordValidator(BasePipelineValidator):
name = "mako_keyword_validator"
validate_type = ValidateType.TASK.value

@classmethod
def validate(cls, web_pipeline_tree: dict) -> ValidatorResult:
validation_errors = []

# 遍历所有常量变量
for key, const in web_pipeline_tree["constants"].items():
# key 格式为 ${variable_name},需提取内部变量名再与 Mako 保留关键字比对
match = KEY_PATTERN_RE.match(key)
if not match:
continue
# 提取 ${ 和 } 之间的变量名
var_name = key[2:-1]
if var_name in RESERVED_NAMES:
validation_errors.append(key)

if validation_errors:
error_message = "变量命名校验失败: 变量 {} 使用了Mako模板引擎的保留关键字".format("; ".join(validation_errors))
return ValidatorResult(is_valid=False, error=error_message)

return ValidatorResult(is_valid=True)
Loading
Loading