225 lines
12 KiB
Plaintext
225 lines
12 KiB
Plaintext
*** Settings ***
|
||
Library Collections
|
||
Library RequestsLibrary
|
||
Resource ../../../03-Variable/BifangApiVariable.txt
|
||
Resource ../../../02-Keyword/tsg_bfapi/policy_file_interface/FunctionalKeywords.robot
|
||
Resource ProcessPolicyBody.robot
|
||
Resource ../Common.robot
|
||
*** Variables ***
|
||
${policyUrl} /policy/compile
|
||
*** Keywords ***
|
||
#get查询策略,传入字典对象,该方法将字典对象自动拼接成get请求查询条件
|
||
QueryPolicies
|
||
[Documentation]
|
||
... 查询策略,params为参数字典,version为接口版本号
|
||
[Arguments] ${params} ${version}
|
||
Log To Console Call QueryPolicies
|
||
Dictionary Should Contain Key ${params} policyType
|
||
${paramsStr} Dictionary-To-QueryParams ${params}
|
||
${response} BaseGetRequestForV2 ${policyUrl} ${paramsStr} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
[Return] ${rescode} ${response}
|
||
|
||
AddPolicies
|
||
[Documentation]
|
||
... 新增策略
|
||
... returnData:是否返回数据,这里需要设置为1
|
||
... policyList:策略列表,支持dict与list两种形式
|
||
... version:接口版本号
|
||
... 支持v1版本接口与v2版本接口
|
||
... v1版本特殊参数说明:
|
||
... referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN
|
||
... 为字符串类型,每条引用的策略对象信息以','分隔
|
||
... [objectId]|[protocolFileds],... ,多个protocolFileds用'&'连接
|
||
... appObjectIdArray==${2},${3}
|
||
... 为字符串类型,以','分隔
|
||
... v2版本特殊参数说明:
|
||
... v2版本中无referenceObject,取而代之的是source,destination,filterList
|
||
... source= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI
|
||
... destination= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI
|
||
... filterList= ${265}:${257}|TSG_FIELD_HTTP_HOST,${266}:${299}|TSG_FIELD_SSL_SNI
|
||
... filterList由多个数字用‘:’分隔,加上|对上对应的protocolFiled,
|
||
... 一个filter中的protocolFiled是确定的,一个protocolFiled对应多个objectId
|
||
... v2版本不支持protocolFiled之间的&符号连接,因为接口中protocolFiled不是数组结构
|
||
... appIdObjects==${2},${3}
|
||
... 20200701修订:分隔符号在Variables中定义,可替换
|
||
[Arguments] ${returnData} ${policyList} ${version}
|
||
Log To Console Call AddPolicies
|
||
${returnData}= Run Keyword If '${returnData}' == '${EMPTY}' Set Variable 1
|
||
... ELSE Set Variable ${returnData}
|
||
#控制台打印参数
|
||
#Log To Console ${policyList}
|
||
#必选参数判定
|
||
Should Not Be Empty ${policyList}
|
||
${dictType} = Evaluate type(${policyList})
|
||
${body} Run Keyword If "${dictType}" == "<class 'list'>" PolicyListDataOperation ${returnData} ${policyList} add ${version}
|
||
... ELSE IF "${dictType}" == "<class 'dict'>" PolicyDataOperation ${returnData} ${policyList} add ${version}
|
||
... ELSE Set Variable ${EMPTY}
|
||
#${body} Set Variable {"opAction":"add","policyList":{"policyName":"wx-test","policyType":"tsg_security","action":"deny","userTags":"","doBlacklist":0,"doLog":1,"policyDesc":"","effectiveRange":{"tag_sets":[[]]},"userRegion":{"protocol":"SSL","method":"drop"},"source":[{"objectId":3101,"protocolField":"TSG_SECURITY_SOURCE_ADDR"}],"destination":[],"filterList":[{"filter":[{"objectId":3404,"protocolField":"TSG_FIELD_SSL_CN"}]}],"appIdObjects":[3],"isValid":0,"scheduleId":[]}}
|
||
${response} BasePostRequestForV2 ${policyUrl} ${body} ${version}
|
||
${policyIds} Run Keyword If "${returnData}" == "1" Get-PolicyIds ${response}
|
||
... ELSE Create List
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${rescode} ${policyIds}
|
||
UpdatePolicies
|
||
[Documentation]
|
||
... 与新增类似,update多一个参数opAction
|
||
... opAction可为update,enable,disable
|
||
[Arguments] ${returnData} ${policyList} ${version} ${opAction}
|
||
Log To Console Call UpdatePolicies
|
||
${returnData}= Run Keyword If '${returnData}' == '${EMPTY}' Set Variable 1
|
||
... ELSE Set Variable ${returnData}
|
||
#必选参数判定
|
||
Should Not Be Empty ${policyList}
|
||
${dictType} = Evaluate type(${policyList})
|
||
${body} Run Keyword If "${dictType}" == "<class 'list'>" PolicyListDataOperation ${returnData} ${policyList} ${opAction} ${version}
|
||
... ELSE IF "${dictType}" == "<class 'dict'>" PolicyDataOperation ${returnData} ${policyList} ${opAction} ${version}
|
||
... ELSE Set Variable ${EMPTY}
|
||
${response} BaseEditRequestForV2 ${policyUrl} ${body} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${rescode}
|
||
DeletePolicies
|
||
[Documentation]
|
||
... 删除策略
|
||
... params结构可为List或者dict
|
||
... dict结构
|
||
... dict data:{'policyType':'tsg_security','policyIds':[1,2,3]}
|
||
... list 结构
|
||
... list data:[{'policyType':'tsg_security','policyIds':[1,2,3]},{'policyType':'pxy_manipulation','policyIds':[4,5,6]}]
|
||
[Arguments] ${params}
|
||
${paramsType} Evaluate type(${params})
|
||
Run Keyword And Return If "${paramsType}" == "<class 'dict'>" DeletePolicyWithParamDict ${params}
|
||
FOR ${param} IN @{params}
|
||
${rescode} DeletePolicyWithParamDict ${param}
|
||
END
|
||
[Return] ${rescode}
|
||
DeletePolicy1
|
||
[Documentation]
|
||
... policyIds 策略id list [1,2,3]
|
||
... policyType 策略类型 tsg_security等
|
||
[Arguments] ${policyIds} ${policyType}
|
||
${params} Create Dictionary policyIds=${policyIds} policyType=${policyType}
|
||
${response} BaseDeleteRequest /${version}${policyUrl} ${params}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${rescode}
|
||
ImportPolicy
|
||
[Documentation]
|
||
... 策略导入
|
||
... jira CHON-12导入限制需求支持
|
||
[Arguments] ${filePath} ${fileName} ${policyType}
|
||
Directory Should Exist ${path}
|
||
File Should Exist ${path}/${filePath}/${fileName}
|
||
File Should Not Be Empty ${path}/${filePath}/${fileName}
|
||
#获取导入策略总数
|
||
${file} Get File ${path}/${filePath}/${fileName}
|
||
${fileJson} json.Loads ${file}
|
||
#待导入的策略总数
|
||
${totalNum} Get From Dictionary ${fileJson} totalNum
|
||
#查询已经导入的非内置策略的数量
|
||
${totalExists} GetPolicyTotal 10
|
||
${calTotal} Evaluate ${totalNum} + ${totalExists}
|
||
Connect To Database Using Custom Params pymysql ${mysqlHost}
|
||
${policyLimit} query SELECT cfg_value FROM t_sys_cfg WHERE cfg_name='policy_upper_limit'
|
||
${policyLimit} Set Variable ${policyLimit[0][0]}
|
||
${remaining} Evaluate ${policyLimit}-${totalExists}
|
||
log 策略剩余可导入量为:${remaining}
|
||
${testType} Evaluate type($policyLimit)
|
||
Disconnect From Database
|
||
#jira CHON-12导入限制需求支持
|
||
Run Keyword If ${calTotal}>${policyLimit} log 当前已经存在策略条数${totalExists},待导入条数${totalNum},总数超过${policyLimit}条!
|
||
${binFile} Evaluate (r'import_policy.json',open(r"${path}/${filePath}/${fileName}",'rb'),'application/json')
|
||
Log ${binFile}
|
||
${data} Create Dictionary policyType=${policyType}
|
||
${file} Create Dictionary file=${binFile}
|
||
${response} BaseMultipartPostRequest ${policyUrl}/batch ${data} ${file} ${version2}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${rescode} ${totalNum}
|
||
ExportPolicy
|
||
[Documentation]
|
||
... 策略导出
|
||
... policyType tsg_security等
|
||
... policyIds 1,2,3
|
||
... policyName 字符串
|
||
[Arguments] ${policyType} ${policyIds} ${policyName}
|
||
Should Not Be Empty ${policyType}
|
||
${params} Create Dictionary policyType=${policyType}
|
||
Run Keyword If "${policyIds}"!="${EMPTY}" Set To Dictionary ${params} policyIds=${policyIds}
|
||
Run Keyword If "${policyName}"!="${EMPTY}" Set To Dictionary ${params} policyName=${policyName}
|
||
${paramsStr} Dictionary-To-QueryParams ${params}
|
||
${response} BaseFormRequest1 ${policyUrl}/batch ${paramsStr} ${version}
|
||
[Return] ${response}
|
||
WriteFile
|
||
[Arguments] ${filePath} ${fileName} ${content}
|
||
Create File ${path}/${filePath}/${fileName}
|
||
${type} Evaluate type(${content})
|
||
${content} Run Keyword If "${type}" == "<class 'dict'>" json.Dumps ${content}
|
||
... ELSE Set Variable ${content}
|
||
Append To File ${path}/${filePath}/${fileName} ${content}
|
||
VerifyPolicies
|
||
[Documentation]
|
||
... 策略验证
|
||
... 支持多个策略验证与单个策略验证
|
||
... 单个策略验证时verifyList 是一个dict
|
||
... 多个策略验证时是一个list
|
||
... atrributeValue字段可为三种格式
|
||
... 1:dict
|
||
... 2:json
|
||
... 3.字符串,ip的value对应格式为ip|port|addrType|protocol,注意addrType,protocol为数字
|
||
... signature 的value对应格式为district|string
|
||
... string的格式就是string
|
||
... 以上三种方式可以混合搭配使用
|
||
[Arguments] ${verifyList}
|
||
Log To Console Call VerifyPolicies
|
||
#必选参数判定
|
||
Should Not Be Empty ${verifyList}
|
||
${dictType} = Evaluate type(${verifyList})
|
||
${body} Run Keyword If "${dictType}" == "<class 'list'>" VerifyPolicyList ${verifyList}
|
||
... ELSE IF "${dictType}" == "<class 'dict'>" VerifyPolicyObj ${verifyList}
|
||
... ELSE Set Variable ${EMPTY}
|
||
${body} Set Variable {"verifyList":${body}}
|
||
${response} BasePostRequestForV2 /policy/verify ${body} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${rescode} ${response['data']}
|
||
GetPolicyTotal
|
||
[Documentation]
|
||
... 获取已经存在的策略总量
|
||
... 排除内置策略
|
||
... initializeNum内置策略数量
|
||
[Arguments] ${initializeNum}
|
||
${response} BaseFormRequest ${policyUrl}/statistics ${EMPTY} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
${total} Set Variable ${response['data']['policyNum']}
|
||
${total} Evaluate ${total} - ${initializeNum}
|
||
[Return] ${total}
|
||
SortPolicies
|
||
[Documentation]
|
||
... 将策略排序
|
||
... policyList
|
||
... 支持list dict两种结构
|
||
... dict结构:policyType=tsg_security policyId=${1517} afterPolicyId=${1518}
|
||
[Arguments] ${policyList}
|
||
Should Not Be Empty ${policyList}
|
||
${isList} Evaluate isinstance(${policyList},list)
|
||
${policyJson} Run Keyword If "${isList}"=="True" SortPolicyList ${policyList}
|
||
... ELSE SortPolicyObject ${policyList}
|
||
|
||
${response} BasePostRequestForV2 ${policyUrl}/moveto {"policyList":${policyJson}} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
[Return] ${response}
|
||
StatisticsPolicies
|
||
[Documentation]
|
||
... 获取策略总量
|
||
... 不需要传参
|
||
[Arguments]
|
||
${response} BaseFormRequest ${policyUrl}/statistics ${EMPTY} ${version}
|
||
${rescode} Set Variable ${response['code']}
|
||
Should Be Equal As Strings ${rescode} 200
|
||
${data} Set Variable ${response['data']}
|
||
[Return] ${data} |