Skip to content

Commit

Permalink
pref:完善研发商店组件配置文件参数校验 TencentBlueKing#11269
Browse files Browse the repository at this point in the history
  • Loading branch information
Kzhou991 committed Dec 12, 2024
1 parent 2c9f6db commit b8b3362
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object StoreMessageCode {
const val USER_PULL_FILE_FAIL = "2120041"
// 插件包文件[{0}]不存在,请检查文件所在路径是否正确
const val ATOM_PACKAGE_FILE_NOT_FOUND = "2120042"
const val USER_REPOSITORY_TASK_JSON_FIELD_IS_NOT_SUPPORT = "2120043" // 研发商店:插件配置文件[task.json]{0}字段暂时只支持{1},{2},{3},{4}
const val USER_REPOSITORY_TASK_JSON_FIELD_IS_NOT_SUPPORT = "2120043" // 研发商店:插件配置文件[task.json]{0}字段里面的{1}字段暂时只支持{2}

const val USER_TEMPLATE_VERSION_IS_NOT_FINISH = "2120201" // 研发商店:模板{0}的{1}版本发布未结束,请稍后再试
const val USER_TEMPLATE_RELEASE_STEPS_ERROR = "2120202" // 研发商店:模板发布流程状态变更顺序不正确
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ const val KEY_TARGET = "target"
const val KEY_LANGUAGE = "language"
const val KEY_MINIMUM_VERSION = "minimumVersion"
const val KEY_DEMANDS = "demands"
const val KEY_TYPE = "type"
const val IS_EXPANDED="isExpanded" // 是否默认展开分组
const val KEY_PACKAGE_PATH = "packagePath"
const val KEY_DEFAULT = "default"
const val KEY_ATOM_CODE = "atomCode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.devops.store.atom.service.impl

import com.tencent.devops.common.api.constant.ARTIFACT
import com.tencent.devops.common.api.constant.COMPONENT
import com.tencent.devops.common.api.constant.CommonMessageCode
import com.tencent.devops.common.api.constant.GOLANG
Expand All @@ -35,9 +36,13 @@ import com.tencent.devops.common.api.constant.JAVA
import com.tencent.devops.common.api.constant.KEY_OS
import com.tencent.devops.common.api.constant.KEY_OS_ARCH
import com.tencent.devops.common.api.constant.KEY_OS_NAME
import com.tencent.devops.common.api.constant.LABEL
import com.tencent.devops.common.api.constant.NAME
import com.tencent.devops.common.api.constant.NODEJS
import com.tencent.devops.common.api.constant.PYTHON
import com.tencent.devops.common.api.constant.REPORT
import com.tencent.devops.common.api.constant.REQUIRED
import com.tencent.devops.common.api.constant.STRING
import com.tencent.devops.common.api.constant.TYPE
import com.tencent.devops.common.api.enums.FrontendTypeEnum
import com.tencent.devops.common.api.exception.ErrorCodeException
Expand Down Expand Up @@ -81,6 +86,7 @@ import com.tencent.devops.store.pojo.common.ATOM_POST_ENTRY_PARAM
import com.tencent.devops.store.pojo.common.ATOM_POST_FLAG
import com.tencent.devops.store.pojo.common.ATOM_POST_NORMAL_PROJECT_FLAG_KEY_PREFIX
import com.tencent.devops.store.pojo.common.ATOM_POST_VERSION_TEST_FLAG_KEY_PREFIX
import com.tencent.devops.store.pojo.common.IS_EXPANDED
import com.tencent.devops.store.pojo.common.KEY_ATOM_CODE
import com.tencent.devops.store.pojo.common.KEY_CONFIG
import com.tencent.devops.store.pojo.common.KEY_DEFAULT
Expand All @@ -89,12 +95,14 @@ import com.tencent.devops.store.pojo.common.KEY_DEMANDS
import com.tencent.devops.store.pojo.common.KEY_EXECUTION
import com.tencent.devops.store.pojo.common.KEY_FINISH_KILL_FLAG
import com.tencent.devops.store.pojo.common.KEY_INPUT
import com.tencent.devops.store.pojo.common.KEY_INPUT_GROUPS
import com.tencent.devops.store.pojo.common.KEY_LANGUAGE
import com.tencent.devops.store.pojo.common.KEY_MINIMUM_VERSION
import com.tencent.devops.store.pojo.common.KEY_OUTPUT
import com.tencent.devops.store.pojo.common.KEY_PACKAGE_PATH
import com.tencent.devops.store.pojo.common.KEY_RUNTIME_VERSION
import com.tencent.devops.store.pojo.common.KEY_TARGET
import com.tencent.devops.store.pojo.common.KEY_TYPE
import com.tencent.devops.store.pojo.common.TASK_JSON_NAME
import com.tencent.devops.store.pojo.common.enums.ReleaseTypeEnum
import com.tencent.devops.store.pojo.common.enums.StoreTypeEnum
Expand Down Expand Up @@ -383,6 +391,21 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
)
}
val executionInfoMap = taskDataMap[KEY_EXECUTION] as? Map<String, Any>
val keyInputGroupMapList = taskDataMap[KEY_INPUT_GROUPS] as? List<Map<String, Any>>
if (!keyInputGroupMapList.isNullOrEmpty()) {
fun checkInputGroupFieldNotNull(inputGroupMap: Map<String, Any>, fieldName: String) {
inputGroupMap[fieldName] as? String
?: throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL,
params = arrayOf(fieldName)
)
}
keyInputGroupMapList.forEach { inputGroupMap ->
checkInputGroupFieldNotNull(inputGroupMap, NAME)
checkInputGroupFieldNotNull(inputGroupMap, LABEL)
checkInputGroupFieldNotNull(inputGroupMap, IS_EXPANDED)
}
}
var atomPostInfo: AtomPostInfo? = null
if (executionInfoMap == null) {
// 抛出错误提示
Expand All @@ -393,22 +416,13 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
}
// pref:完善研发商店组件配置文件参数校验 #11269
val supportedLanguages = setOf(JAVA, PYTHON, GOLANG, NODEJS)
val language = executionInfoMap[KEY_LANGUAGE]?.let { language ->
when (language) {
is String -> {
if (language in supportedLanguages) {
language
} else {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NOT_SUPPORT,
params = arrayOf(KEY_LANGUAGE, supportedLanguages.joinToString(separator = ","))
)
}
}

else -> throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_INVALID,
params = arrayOf(KEY_LANGUAGE)
val language = executionInfoMap[KEY_LANGUAGE]?.toString()?.let { language ->
if (language in supportedLanguages) {
language
} else {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NOT_SUPPORT,
params = arrayOf(KEY_EXECUTION,KEY_LANGUAGE, supportedLanguages.joinToString(separator = ","))
)
}
} ?: throw ErrorCodeException(
Expand Down Expand Up @@ -453,7 +467,7 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
} else {
runtimeVersion
}
if (null != osList) {
if (!osList.isNullOrEmpty()) {
val osDefaultEnvNumMap = mutableMapOf<String, Int>()
osList.forEach { osExecutionInfoMap ->
val osName = osExecutionInfoMap[KEY_OS_NAME] as? String
Expand Down Expand Up @@ -526,22 +540,49 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
atomEnvRequests.add(atomEnvRequest)
}

// 校验参数输入参数和输出参数是否超过最大值
val inputDataMap = taskDataMap[KEY_INPUT] as? Map<String, Any>
if (inputDataMap != null && inputDataMap.size > maxInputNum) {
inputDataMap?.let { validateInputOutputData(it, maxInputNum) }

val supportedTypes = setOf(STRING, ARTIFACT, REPORT)
val outputDataMap = taskDataMap[KEY_OUTPUT] as? Map<String, Any>
outputDataMap?.let { validateInputOutputData(it, maxInputNum, supportedTypes, isInput = false) }
return GetAtomConfigResult("0", arrayOf(""), taskDataMap, atomEnvRequests)
}


private fun validateInputOutputData(
dataMap: Map<String, Any>,
maxInputNum: Int,
supportedTypes: Set<String>? = null,
isInput: Boolean = true
) {
if (dataMap.size > maxInputNum) {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_ATOM_INPUT_NUM_IS_TOO_MANY,
params = arrayOf(maxInputNum.toString())
)
}
val outputDataMap = taskDataMap[KEY_OUTPUT] as? Map<String, Any>
if (outputDataMap != null && outputDataMap.size > maxOutputNum) {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_ATOM_OUTPUT_NUM_IS_TOO_MANY,
params = arrayOf(maxOutputNum.toString())
)
}
return GetAtomConfigResult("0", arrayOf(""), taskDataMap, atomEnvRequests)

dataMap.values
.mapNotNull { it as? Map<*, *> }
.forEach { map ->
val type = map[KEY_TYPE]?.toString()
when {
type.isNullOrEmpty() -> {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL,
params = arrayOf(KEY_TYPE)
)
}

!isInput && (supportedTypes != null && type !in supportedTypes) -> {
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NOT_SUPPORT,
params = arrayOf(KEY_OUTPUT, KEY_TYPE,supportedTypes.joinToString(separator = ","))
)
}
}
}
}

private fun validateConfigMap(configMap: Map<String, Any>) {
Expand Down
2 changes: 1 addition & 1 deletion support-files/i18n/store/message_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
2120935=When the queryProjectComponentFlag, installed, or updateFlag parameters are not empty, the projectCode parameter must be non-empty
2120936=store: Unreleased versions are not allowed to be removed from the shelves

2120043=store: The [task.json] file's {0} field currently only supports {1}
2120043=store: In the plugin configuration file [task.json], the field {0} contains the subfield {1}, which currently only supports {2}
ATOM.classify.common=Others
ATOM.classify.compileBuild=Compile
ATOM.classify.deploy=Deploy
Expand Down
2 changes: 1 addition & 1 deletion support-files/i18n/store/message_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
2120040=研发商店:插件配置文件[task.json]config配置格式不正确,{0}
2120041=研发商店: 拉取文件[{0}]失败,失败原因:{1}
2120042=插件包文件[{0}]不存在,请检查文件所在路径是否正确
2120043=研发商店:插件配置文件[task.json]{0}字段暂时只支持{1}
2120043=研发商店:插件配置文件[task.json]{0}字段里面的{1}字段暂时只支持{2}
2120201=研发商店: 模板{0}的{1}版本发布未结束,请稍后再试
2120202=研发商店: 模板发布流程状态变更顺序不正确
2120203=研发商店: 模板的可见范围不在插件{0}的可见范围之内,如有需要请联系插件的发布者
Expand Down

0 comments on commit b8b3362

Please sign in to comment.