This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dongxiaoyan-tsg-autotest/02-Keyword/tsg_bfapi/policy_object/ProcessPolicyBody.robot

321 lines
20 KiB
Plaintext
Raw Normal View History

*** Settings ***
Library json
Library Collections
Resource ../../../03-Variable/BifangApiVariable.txt
Resource ../../../03-Variable/PolicyObjectDefault.txt
Resource ../../tsg_common/ManagePolicyBody.robot
*** Keywords ***
# ... 特殊参数说明:
# ... effectiveRange=Nursurtan|Transtelecom&Astel,Almaty&Nursurtan,|Astel&Transtelecom
# ... 为字符串类型,每条生效区域信息以','分隔
# ... ①以'|'字符开头表示仅为isp信息effectiveRange=|Astel&Transtelecom
# ... ②非'|'字符开头表示仅为location信息或location和isp信息
# ... effectiveRange=Nursurtan仅为location信息
# ... effectiveRange=Nursurtan|Transtelecom&Astellocation和isp信息
# ... userRegion=protocol:HTTP,keyring:${1}
# ... 为字符串类型,每条动作参数以','分隔
# ... [key]:[value],...
# ... 在v1版本中referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN
# ... 为字符串类型,每条引用的策略对象信息以','分隔
# ... [objectId]|[protocolFileds],... 多个protocolFileds用'&'连接
# ... scheduleId=${3},${1}
# ... 为字符串类型,以','分隔
# ... 在v1版本接口中appObjectIdArray==${2},${3}
# ... 在v2版本接口中appIdObjects==${2},${3}
# ... 为字符串类型,以','分隔
# ...
# ... 注:数字类型变量为${0}、${1}、${...}
# ... 在v2版本中 无referenceObject取而代之的是sourcedestinationfilterList
# ... source= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN
# ... destination= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN
# ... filterList= ${265}|${257}:TSG_FIELD_HTTP_HOST,${266}|${299}:TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN
# ... filterList由多个数字用|分隔加上对上对应的protocolFiled,
# ... 一个filter中的protocolFiled是确定的一个protocolFiled对应多个objectId
WxPolicyListOperation
[Documentation]
... 描述入口opAction参数固定为'add'
... 参数returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。
... policyList: 必传,字典类型。字典内可选参数信息:
... policyObjectVersion: 必传v1,v2,涉及策略版本
... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt
... 多个策略处理
[Arguments] ${returnData} ${policyList} ${opAction} ${policyObjectVersion}
Log To Console Call WxPolicyListOperation
${policyListJson} Set Variable [
FOR ${policy} IN @{policyList}
${json} WxPolicyOrganize ${policy} ${opAction} ${policyObjectVersion}
${policyListJson} Set Variable ${policyListJson}${json},
END
${policyListJson}= Replace String Using Regexp ${policyListJson} ,$ ]
# 转为json结构并返回
${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList
${bodyJson} json.Dumps ${dict} ensure_ascii=False
${json} Replace String ${bodyJson} "replace:policyList" ${policyListJson}
Log Policy_Request_Body-${json}
[Return] ${json}
WxPolicyOperation
[Documentation]
... 描述入口opAction参数固定为'add'
... 参数returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。
... policyList: 必传,字典类型。字典内可选参数信息:
... policyObjectVersion: 必传v1,v2,涉及策略版本
... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt
... 单个策略处理
[Arguments] ${returnData} ${policy} ${opAction} ${policyObjectVersion}
Log To Console Call WxPolicyOperation
${json} WxPolicyOrganize ${policy} ${opAction} ${policyObjectVersion}
# 转为json结构并返回
${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList
${bodyJson} json.Dumps ${dict} ensure_ascii=False
${json} Replace String ${bodyJson} "replace:policyList" ${json}
Log Policy_Request_Body-${json}
[Return] ${json}
WxPolicyOrganize
[Documentation]
... 描述入口opAction参数固定为'add'
... policyList: 必传,字典类型。字典内可选参数信息:
... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt
... policyObjectVersion: 必传v1,v2,涉及策略版本
... 单个策略转换为json
[Arguments] ${policy} ${opAction} ${policyObjectVersion}
Log To Console Call WxPolicyOrganize
${emptyList} Create List
#v1版本接口与v2版本接口不同的字段
Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} source
Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} destination
Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} filterList
Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} appIdObjects
Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} referenceObject
Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} appObjectIdArray
# 取可选参数,为传入设置默认值
${return} ${policyName} Run Keyword And Ignore Error Get From Dictionary ${policy} policyName
Run Keyword If "${return}"=="FAIL" or "${policyName}"=="${EMPTY}" Set To Dictionary ${policy} policyName=${Default_PolicyName}
${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType
Run Keyword If "${return}"=="FAIL" or "${policyType}"=="${EMPTY}" Set To Dictionary ${policy} policyType=${Default_PolicyType}
#policyDesc 不是必填的选项
${return} ${policyDesc} Run Keyword And Ignore Error Get From Dictionary ${policy} policyDesc
Run Keyword If "${return}"=="FAIL" or "${policyDesc}"=="${EMPTY}" Set To Dictionary ${policy} policyDesc=${Default_PolicyDesc}
${return} ${action} Run Keyword And Ignore Error Get From Dictionary ${policy} action
Run Keyword If "${return}"=="FAIL" or "${action}"=="${EMPTY}" Set To Dictionary ${policy} action=${Default_Action}
#userTags 不是必填的选项
${return} ${userTags} Run Keyword And Ignore Error Get From Dictionary ${policy} userTags
Run Keyword If "${return}"=="FAIL" or "${userTags}"=="${EMPTY}" Set To Dictionary ${policy} userTags=${Default_UserTags}
${return} ${doLog} Run Keyword And Ignore Error Get From Dictionary ${policy} doLog
Run Keyword If "${return}"=="FAIL" or "${doLog}"=="${EMPTY}" Set To Dictionary ${policy} doLog=${Default_DoLog}
${return} ${isValid} Run Keyword And Ignore Error Get From Dictionary ${policy} isValid
Run Keyword If "${return}"=="FAIL" or "${isValid}"=="${EMPTY}" Set To Dictionary ${policy} isValid=${Default_IsValid}
${return} ${scheduleId} Run Keyword And Ignore Error Set Variable ${policy}[scheduleId]
${scheduleIdV} Run Keyword If "${return}"!="FAIL" and "${scheduleId}"!="${EMPTY}" and "${scheduleId}"!="${None}" Evaluate list(map(int, re.split('[,]', '${scheduleId}'))) re
... ELSE Set Variable ${emptyList}
Set To Dictionary ${policy} scheduleId=${scheduleIdV}
${return} ${appObjectIdArray} Run Keyword If "${policyObjectVersion}"=="v1" Run Keyword And Ignore Error Get From Dictionary ${policy} appObjectIdArray
... ELSE Run Keyword And Ignore Error Get From Dictionary ${policy} appIdObjects
${appObjectIdArrayV} Run Keyword If "${return}"=="FAIL" or "${appObjectIdArray}"=="${EMPTY}" Evaluate list(map(int, re.split('[,]', '${Default_AppObjectIdArray}'))) re
... ELSE Evaluate list(map(int, re.split('[,]', '${appObjectIdArray}'))) re
Run Keyword If "${policyObjectVersion}"=="v1" Set To Dictionary ${policy} appObjectIdArray=${appObjectIdArrayV}
... ELSE IF "${policyObjectVersion}"=="v2" Set To Dictionary ${policy} appIdObjects=${appObjectIdArrayV}
${policyId} Run Keyword If "${opAction}"=="add" Set Variable ${EMPTY}
... ELSE Get From Dictionary ${policy} policyId
# 固定值
Set To Dictionary ${policy} policyId=${policyId} doBlacklist=${0}
# 分别处理EffectiveRange、UserRegion和ReferenceObject参数
WxManageEffectiveRange ${policy}
#WxManageUserRegion ${policy}
Run Keyword If "${policyObjectVersion}"=="v1" WxManageReferenceObject ${policy} ELSE IF "${policyObjectVersion}"=="v2" WxManageObjectV2 ${policy}
#先取出useragion 再放回json
${return} ${userRegion} Run Keyword And Ignore Error Get From Dictionary ${policy} userRegion
Set To Dictionary ${policy} userRegion=${Default_UserRegion}
${userRegion} Run Keyword If "${return}"=="FAIL" or '${userRegion}'=="${EMPTY}" Set Variable ""
... ELSE Set Variable ${userRegion}
#针对主动防御特殊处理,主动防御不应用对象
${active_defence_emptyList} Create List
${return} ${policyType} Run Keyword And Ignore Error Set Variable ${policy}[policyType]
Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${policy} referenceObject=${active_defence_emptyList}
Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${policy} appObjectIdArray=${active_defence_emptyList}
#v版本主动防御
Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${policy} source=${active_defence_emptyList}
Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${policy} destination=${active_defence_emptyList}
Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${policy} filterList=${active_defence_emptyList}
Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${policy} appIdObjects=${active_defence_emptyList}
# 转为json结构并返回
${json} json.Dumps ${policy} ensure_ascii=False
${json} Replace String ${json} "method:rst" ${userRegion}
#${json} Replace String ${json} "effectiveRange:Empty" {}
Log Policy-${json}
[Return] ${json}
WxManageEffectiveRange
[Documentation]
... 处理EffectiveRange
... 加入effectiveRange={}的处理支持
[Arguments] ${policyList}
Log To Console Call WxManageEffectiveRange
${emptyList} Create List
${emptyListDouble} Create List ${emptyList}
${return} ${effectiveRange} Run Keyword And Ignore Error Get From Dictionary ${policyList} effectiveRange
${effectiveRange} Run Keyword If "${return}"=="FAIL" or "${effectiveRange}"=="${EMPTY}" Set Variable ${Default_EffectiveRange}
... ELSE Set Variable ${effectiveRange}
${value} Run Keyword If "${effectiveRange}"=="${EMPTY}" Create Dictionary tag_sets=${emptyListDouble}
... ELSE IF "${effectiveRange}"=="{}" Create Dictionary ELSE AnalysisEffectiveRange ${effectiveRange}
Set To Dictionary ${policyList} effectiveRange=${value}
WxManageReferenceObject
[Documentation]
... 处理引用对象字符串
... v1版本接口使用
[Arguments] ${policy}
Log To Console Call WxManageReferenceObject
${emptyList} Create List
#默认客户端条件类型clientip or clientsubid
${return} ${defaultClient} Run Keyword And Ignore Error Set Variable ${policy}[defalutClient]
${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type}
... ELSE Set Variable ${defaultClient}
Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObject ${emptyList}
Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObject ${emptyList}
#Run Keyword If '${addTestClentIPFlag}'=='1' ManageClientObject ${emptyList}
# Run Keyword If "${clientHostObj}"!="${None}" Append To List ${emptyList} ${clientHostObj}
${defaultObj} Create Dictionary
${return} ${referenceObject} Run Keyword And Ignore Error Get From Dictionary ${policy} referenceObject
${referenceObject} Run Keyword If "${return}"=="FAIL" or "${referenceObject}"=="${EMPTY}" Set Variable ${Default_ReferenceObject}
... ELSE Set Variable ${referenceObject}
${value} Run Keyword If "${referenceObject}"=="${EMPTY}" Create List ${defaultObj}
... ELSE AnalysisReferenceObject ${referenceObject}
# 若参数为空,设置默认结构
# FOR ${var} IN @{value}
# Run Keyword If ${var}!={} Append To List ${referenceObject} ${var}
# END
ObjectLoop ${value} ${emptyList}
Set To Dictionary ${policy} referenceObject=${emptyList}
ObjectLoop
[Arguments] ${value} ${valueList}
# 若参数为空,设置默认结构
FOR ${var} IN @{value}
Run Keyword If ${var}!={} Append To List ${valueList} ${var}
END
[Return] ${valueList}
WxManageObjectV2
[Documentation]
... 处理引用对象字符串
... v2版本接口使用
... source,destination,filterList
# 处理引用对象字符串
[Arguments] ${policy}
Log To Console Call WxManageObjectV2
#默认客户端条件类型clientip or clientsubid
${return} ${defaultClient} Run Keyword And Ignore Error Get From Dictionary ${policy} defalutClient
${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type}
... ELSE Set Variable ${defaultClient}
${sourceList} Create List
${destinationList} Create List
${filterList} Create List
Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObjectV2 ${sourceList}
Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObjectV2 ${sourceList}
AnalysisObjectV2 ${policy} source ${sourceList}
AnalysisObjectV2 ${policy} destination ${destinationList}
AnalysisObjectV2 ${policy} filterList ${filterList}
Set To Dictionary ${policy} source=${sourceList} destination=${destinationList} filterList=${filterList}
Log ${policy}
ManageClientIPObjectV2
[Arguments] ${emptyList}
# 增加引用测试终端IP策略对象
${obj1} Create Dictionary objectId=${testClentID} protocolField=TSG_SECURITY_SOURCE_ADDR
Append To List ${emptyList} ${obj1}
ManageClientSubIDObjectV2
[Arguments] ${emptyList}
# 增加引用测试终端IP策略对象
${protocolField} Set Variable
${obj1} Create Dictionary objectId=${testClentSubID} protocolField=${protocolField}
Append To List ${emptyList} ${obj1}
AnalysisObjectV2
[Arguments] ${policy} ${objectType} ${objList}
Log To Console Call AnalysisObjectV2
${policyObjectDefault} Run Keyword If "${objectType}"=="source" Set Variable ${Default_v2_SourceObject}
... ELSE IF "${objectType}"=="destination" Set Variable ${Default_v2_DestinationObject}
... ELSE IF "${objectType}"=="filterList" Set Variable ${Default_v2_FilterObject}
${return} ${objectList} Run Keyword And Ignore Error Get From Dictionary ${policy} ${objectType}
# source,destination,filterList
${objectList} Run Keyword If "${return}"=="FAIL" or "${objectList}"=="${EMPTY}" Set Variable ${policyObjectDefault}
... ELSE Set Variable ${objectList}
${list} Evaluate re.split('[,]', '${objectList}') re
FOR ${var} IN @{list}
${tempObj} Run Keyword If "${objectType}"=="filterList" and "${var}" != "" ManageFilter ${var}
... ELSE IF "${var}" != "" ManageObject ${var} ELSE Set Variable ${EMPTY}
Run Keyword If "${tempObj}" != "${EMPTY}" Append To List ${objList} ${tempObj}
END
Log ${objList}
ManageObject
[Documentation]
... source,destination处理
[Arguments] ${object}
Log To Console Call ManageObject
${objectList} Create List
#截取objectId
${objectId} Evaluate '${object}'[0:'${object}'.find('|')]
#截取protocolField
${protocolField} Evaluate '${object}'['${object}'.find('|')+1:]
${tempObj} Create Dictionary
${objectId} Run Keyword If '${objectId}'.isdigit() Evaluate int('${objectId}')
... ELSE Set Variable ${objectId}
Set To Dictionary ${tempObj} objectId=${objectId}
Set To Dictionary ${tempObj} protocolField=${protocolField}
[Return] ${tempObj}
ManageFilter
[Documentation]
... filterList处理
[Arguments] ${filter}
Log To Console Call ManageFilter
${filterObjectList} Create List
#截取objectId
${objectIds} Evaluate '${filter}'[0:'${filter}'.find(':')]
#截取protocolField
${protocolField} Evaluate '${filter}'['${filter}'.find(':')+1:]
#遍历 objectId
${list} Evaluate re.split('[|]', '${objectIds}') re
FOR ${var} IN @{list}
${tempObj} Create Dictionary
${var} Run Keyword If '${var}'.isdigit() Evaluate int('${var}')
... ELSE Set Variable ${var}
Set To Dictionary ${tempObj} objectId=${var}
Set To Dictionary ${tempObj} protocolField=${protocolField}
Append To List ${filterObjectList} ${tempObj}
END
${filterObject} Create Dictionary filter=${filterObjectList}
[Return] ${filterObject}
WxGet-PolicyIds
[Documentation]
... 获取结果中的策略ID
[Arguments] ${value}
${policyIds} Create List
FOR ${policy} IN @{value['data']['policyList']}
${policyType} = Evaluate type(${policy})
${policyIdList} Create List ${policy['policyId']}
${policyObj} Create Dictionary policyType=${policy['policyType']} policyIds=${policyIdList}
Append To List ${policyIds} ${policyObj}
#Log To Console ${policy['referenceObject']}
#${objectType} Evaluate type(${policy['referenceObject']})
#Log To Console ${objectType}
END
[Return] ${policyIds}