diff options
Diffstat (limited to 'keyword/policys/policy.robot')
| -rw-r--r-- | keyword/policys/policy.robot | 562 |
1 files changed, 562 insertions, 0 deletions
diff --git a/keyword/policys/policy.robot b/keyword/policys/policy.robot new file mode 100644 index 0000000..180ef50 --- /dev/null +++ b/keyword/policys/policy.robot @@ -0,0 +1,562 @@ +*** Settings ***
+Library yaml
+Library Collections
+Library String
+Library json
+Library RequestsLibrary
+Resource ../../variable/common_variable.txt
+#Resource functional_keywords.robot
+Resource process_policy_body.robot
+Resource ../common/common.robot
+Resource ../objects/object.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} DictionaryToQueryParams ${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" GetPolicyIds ${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}
+DeletePoliciebyList
+ [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}
+
+DeletePolicyByIds
+ [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}
+
+DeleteAllTypePolicyByIds
+ [Documentation]
+ ... policyIds 策略id list [[1,2,3],[4,5,6],[7,8,9]]分别为安全策略id,代理策略id
+ ... policyType 策略类型 tsg_security等
+ [Arguments] ${policyIds}
+ ${policyType} Create List ['tsg_security','pxy_manipulation','pxy_manipulation','active_defence','inline_device','wannat','pxy_tcp_option']
+ ${var} Set Variable 0
+ FOR ${param} IN @{policyIds}
+ ${varvalue} Get From List ${policyType}
+ ${rescode} DeletePolicyByIds ${param} ${varvalue}
+ END
+ ${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} DictionaryToQueryParams ${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}
+
+
+###############################################################################
+#数据分离,测试数据来自对应的yaml文件
+PolilcysByTemplate
+ [Documentation] 根据测试数据文件,使用对应公共模板,创建策略及策略相关数据:策略信息,对象信息,引用文件信息,返回本策略创建的策略及策略相关数据,用于teamdow删除
+ ... 入参:${dataFilePath}数据文件,全路径文件名 ${keyword}测试用例名称
+ ... data数据文件格式参照:other/data/security/http/demo_data.yaml
+ ... 待扩展:策略验证
+ ... 待扩展:功能端效果验证数据,如果存在对用数据则进行功能端效果验证和日志验证,如果不存在则不进行功能端效果验证和日志验证;
+ ... 返回数据:对象idlist${objectIds},格式:[107582, 107583]
+ ... 返回数据:策略id+类型list${policyIds},格式:[{'objectId': 107582, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'},{'objectId': 107583, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'}]
+ [Arguments] ${dataFilePath} ${keyword}
+ ${yamlData}= Get File ${dataFilePath}
+ ${loadedData}= yaml.Safe Load ${yamlData}
+ ${policyList} Get From Dictionary ${loadedData} ${keyword}_data
+ ${objectIds} Create List
+ ${policyIds} Create List
+ Comment 循环创建策略
+ FOR ${key} IN @{policyList}
+ LOG passssssssss
+ ${objectId} ${policyId} Run Keyword And Continue On Failure CreatePolicysAndObjects ${key} ${keyword}
+ #返回的对象和策略信息添加到对象和策略列表
+ Run Keyword If "${objectId}" !="None" AppendListToList ${objectIds} ${objectId}
+ Run Keyword If "${policyId}" !="None" AppendListToList ${policyIds} ${policyId}
+ END
+ [Return] ${objectIds} ${policyIds}
+
+
+CreatePolicysAndObjects
+ [Documentation] 组织单个策略数据,${policyModeJson}来自初始化加载的全局变量策略模板;${policyData}策略模板文件路径 从数据文件中获取的单个策略数据,还包含对象数据,引用文件等内容
+ ... 入参:${policyData},从数据文件获取的单个策略数据: ${testName}测试用例名称
+ ... 返回参数${objectId}格式:[107582, 107583]
+ ... 返回参数:${policyId} [{'objectId': 107582, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'},{'objectId': 107583, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'}]
+ [Arguments] ${policyData} ${testName}
+ Comment 获取模板
+ Comment ${policyModeJson}策略模板取自全局变量
+ ${policyJson} Set Variable ${policyModeJson}
+ Comment 用data数据替换模板内容生成策略新增数据
+ ${condation} Get From Dictionary ${policyData} condation
+ ${objectIds} ${sourceList} ${destinationList} ${filterList} CreateCondation ${condation}
+ ${sourceList} json.Dumps ${sourceList}
+ ${destinationList} json.Dumps ${destinationList}
+ ${filterList} json.Dumps ${filterList}
+ #${policyJson} = Run Keyword If "${sourceList}" != "${EMPTY}" Replace String ${policyJson} "source": null "sourceList":${sourceList}
+ #... ELSE Set Variable ${policyJson}
+ #${policyJson} = Run Keyword If "${destinationList}" != "${EMPTY}" Replace String ${policyJson} "destination": null "destinationList":${sourceList}
+ #... ELSE Set Variable ${policyJson}
+ #${policyJson} = Run Keyword If "${filterList}" != "${EMPTY}" Replace String ${policyJson} "filterList": null "filterList":${filterList}
+ #... ELSE Set Variable ${policyJson}
+ ${policyJson} Replace String ${policyJson} "source": null "source":${sourceList}
+ ${policyJson} Replace String ${policyJson} "destination": null "destination":${destinationList}
+ ${policyJson} Replace String ${policyJson} "filterList": null "filterList":${filterList}
+
+ #${policyInfo} Get From Dictionary ${policyData} condation
+ Comment 处理策略数据: "opAction": "add" "returnData": 1 "policyName": "autotest" "policyType": "tsg_security" "action": "allow" "userTags": "" "doBlacklist": 0 "doLog": 1 "policyDesc": "autotest" "effectiveRange": "userRegion": "appIdObjects": "appSelectorObjects": "isValid": 1 "scheduleId":
+ #转json替换
+ #${policyJson} json.Dumps ${ipMode}
+ #JSON处理
+ ${return} ${opAction} Run Keyword And Ignore Error Get From Dictionary ${policyData} opAction
+ #${opAction} Run Keyword If "${return}"!="FAIL" Set Variable ${opAction} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${opAction}" != "${EMPTY}" Replace String ${policyJson} "opAction": "add" "opAction": "${opAction}"
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${returnData} Run Keyword And Ignore Error Get From Dictionary ${policyData} returnData
+ #${returnData} Run Keyword If "${return}"!="FAIL" Set Variable ${returnData} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${returnData}" != "${EMPTY}" Replace String ${policyJson} "returnData": 1 "returnData": "${returnData}"
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${policyName} Run Keyword And Ignore Error Get From Dictionary ${policyData} policyName
+ #${policyName} Run Keyword If "${return}"!="FAIL" Set Variable ${policyName} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${policyName}" != "None" Replace String ${policyJson} "policyName": "autotest" "policyName": "${policyName}"
+ ... ELSE Replace String ${policyJson} "policyName": "autotest" "policyName": "${testName}"
+
+ ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policyData} policyType
+ #${policyType} Run Keyword If "${return}"!="FAIL" Set Variable ${policyType} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${policyType}" != "None" Replace String ${policyJson} "policyType": "tsg_security" "policyType": "${policyType}"
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${action} Run Keyword And Ignore Error Get From Dictionary ${policyData} action
+ #${action} Run Keyword If "${return}"!="FAIL" Set Variable ${action} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${action}" != "None" Replace String ${policyJson} "action": "allow" "action": "${action}"
+ ... ELSE Set Variable ${policyJson}}
+
+ ${return} ${method} Run Keyword And Ignore Error Get From Dictionary ${policyData} method
+ #${method} Run Keyword If "${return}"!="FAIL" Set Variable ${method} ELSE Set Variable ${EMPTY}
+
+ ${return} ${userTags} Run Keyword And Ignore Error Get From Dictionary ${policyData} userTags
+ #${userTags} Run Keyword If "${return}"!="FAIL" Set Variable ${userTags} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${userTags}" != "None" Replace String ${policyJson} "userTags": null "userTags": ${userTags}
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${doBlacklist} Run Keyword And Ignore Error Get From Dictionary ${policyData} doBlacklist
+ #${doBlacklist} Run Keyword If "${return}"!="FAIL" Set Variable ${doBlacklist} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${doBlacklist}" != "None" Replace String ${policyJson} "doBlacklist": 0 "doBlacklist": ${doBlacklist}
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${doLog} Run Keyword And Ignore Error Get From Dictionary ${policyData} doLog
+ #${doLog} Run Keyword If "${return}"!="FAIL" Set Variable ${doLog} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${doLog}" != "None" Replace String ${policyJson} "doLog": 1 "doLog": ${doLog}
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${policyDesc} Run Keyword And Ignore Error Get From Dictionary ${policyData} policyDesc
+ #${policyDesc} Run Keyword If "${return}"!="FAIL" Set Variable ${policyDesc} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${policyDesc}" != "None" Replace String ${policyJson} "policyDesc": "autotest" "policyDesc": "${policyDesc}"
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${effectiveRange} Run Keyword And Ignore Error Get From Dictionary ${policyData} effectiveRange
+ #${effectiveRange} Run Keyword If "${return}"!="FAIL" Set Variable ${effectiveRange} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${effectiveRange}" != "None" Replace String ${policyJson} "effectiveRange": null "effectiveRange": ${effectiveRange}
+ ... ELSE Replace String ${policyJson} "effectiveRange": null "effectiveRange": {}
+
+ ${return} ${userRegion} Run Keyword And Ignore Error Get From Dictionary ${policyData} userRegion
+ #${userRegion} Run Keyword If "${return}"!="FAIL" Set Variable ${userRegion} ELSE Set Variable ${EMPTY}
+ #${policyJson} = Run Keyword If "${return}"!="FAIL" and "${userRegion}" != "None" Replace String ${policyJson} "userRegion": null "userRegion": ${userRegion}
+ #... ELSE Replace String ${policyJson} "userRegion": null "userRegion": {}
+ ${return} ${code} Run Keyword And Ignore Error Get From Dictionary ${policyData} code
+ ${return} ${message} Run Keyword And Ignore Error Get From Dictionary ${policyData} message
+ ${return} ${html_profile} Run Keyword And Ignore Error Get From Dictionary ${policyData} html_profile
+ ${message} = Run Keyword If "${return}"!="FAIL" and "${html_profile}" != "None" Set Variable ${html_profile}
+ ... ELSE Set Variable ${message}
+ ${messageKey} = Run Keyword If "${return}"!="FAIL" and "${html_profile}" != "None" Set Variable html_profile
+ ... ELSE Set Variable message
+ ${return} ${keyring} Run Keyword And Ignore Error Get From Dictionary ${policyData} keyring
+ ${keyring} = Run Keyword If "${return}"!="FAIL" and "${keyring}" != "None" Set Variable ${keyring}
+ ... ELSE Set Variable ""
+ ${return} ${decryption} Run Keyword And Ignore Error Get From Dictionary ${policyData} decryption
+ ${decryption} = Run Keyword If "${return}"!="FAIL" and "${decryption}" != "None" Set Variable ${decryption}
+ ... ELSE Set Variable ""
+ ${return} ${decrypt_mirror} Run Keyword And Ignore Error Get From Dictionary ${policyData} decrypt_mirror
+ ${decrypt_mirror} = Run Keyword If "${return}"!="FAIL" and "${decrypt_mirror}" != "None" Set Variable ${decrypt_mirror}
+ ... ELSE Set Variable ""
+ ${decrypt_mirror} json.Dumps ${decrypt_mirror}
+ ${return} ${appIdObjects} Run Keyword And Ignore Error Get From Dictionary ${policyData} appIdObjects
+ #${appIdObjects} Run Keyword If "${return}"!="FAIL" Set Variable ${appIdObjects} ELSE Set Variable ${EMPTY}
+ ${appIdObjectIds} Create List
+ Comment userRegionStr由:动作,协议,动作参数等决定,待完成;userregion在allow,monitor动作时多个协议为空;deny,多个协议时为子动作;Intercept,多个协议为空;http为,ssl为,判断appid对象的个数。
+ ${userRegionStr} Set Variable
+ FOR ${key} IN @{appIdObjects}
+ ${appObjId} Get From Dictionary ${objprotol} ${key}
+ Append To List ${appIdObjectIds} ${appObjId}
+
+ ${userRegionStr} Run Keyword If "${method}" == "block" or "${method}" == "alert" Set Variable {"protocol":"${key}","method": "${method}","code":${code},"${messageKey}":${message}}
+ ... ELSE IF "${action}" == "deny" Set Variable {"protocol":"${key}","method": "${method}"}
+ ... ELSE IF "${action}" == "intercept" and "${key}" == "ssl" Set Variable {"protocol":"SSL","keyring": ${keyring},"decryption":${decryption},"decrypt_mirror":${decrypt_mirror}}
+ ... ELSE Set Variable {"protocol":"HTTP"}
+ END
+ # Run Keyword If "${return}"!="FAIL" and "${appIdObjects}" != "${EMPTY}" Remove From List ${appIdObjectIds} 0
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${appIdObjects}" != "None" Replace String ${policyJson} "appIdObjects": [null] "appIdObjects": ${appIdObjectIds}
+ ... ELSE Set Variable ${policyJson}
+
+ ${policyJson} = Replace String ${policyJson} "userRegion": null "userRegion": ${userRegionStr}
+ ${policyJson} = Replace String ${policyJson} "userRegion": null "userRegion": {}
+
+ ${return} ${appSelectorObjects} Run Keyword And Ignore Error Get From Dictionary ${policyData} appSelectorObjects
+ ${appSelectorObjects} Run Keyword If "${return}"!="FAIL" Set Variable ${appSelectorObjects} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${appSelectorObjects}" != "None" Replace String ${policyJson} "appSelectorObjects": "add" "appSelectorObjects": ${appSelectorObjects}
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${isValid} Run Keyword And Ignore Error Get From Dictionary ${policyData} isValid
+ ${appSelectorObjects} Run Keyword If "${return}"!="FAIL" Set Variable ${isValid} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${isValid}" != "None" Replace String ${policyJson} "isValid": 1 "isValid": ${isValid}
+ ... ELSE Set Variable ${policyJson}
+
+ ${return} ${scheduleId} Run Keyword And Ignore Error Get From Dictionary ${policyData} scheduleId
+ ${scheduleId} Run Keyword If "${return}"!="FAIL" Set Variable ${scheduleId} ELSE Set Variable ${EMPTY}
+ ${policyJson} = Run Keyword If "${return}"!="FAIL" and "${scheduleId}" != "None" Replace String ${policyJson} "scheduleId": [null] "scheduleId": ${scheduleId}
+ ... ELSE Set Variable ${policyJson}
+
+ ${policyJson} = Replace String ${policyJson} [null] []
+ ${policyJson} json.loads ${policyJson}
+ ${policyIds} CreatePolilcy ${policyJson}
+ [Return] ${objectIds} ${policyIds}
+CreatePolilcy
+ [Documentation]
+ ... 场景策略:secuiryt、proxy策略
+ ... 数据分离,测试数据来自对应的yaml文件
+ ... 入参 ${policyList},创建参数的dictionary
+ ... 返回参数;${policyIds} 策略id:格式:[{'policyType': 'tsg_security', 'policyIds': [52291]}]
+ [Arguments] ${policyList}
+ Comment 参数 ${filepath}读取文件路径 ${filename}读取文件名 ${keywords}获取数据关键字
+ Comment 创建策略
+
+ ${returnData} Get From Dictionary ${policyList} returnData
+ ${bodyJson} json.Dumps ${policyList}
+ #${bodyJson} = Replace String ${bodyJson} [null] []
+ ${response} BasePostRequestForV2 ${policyUrl} ${bodyJson} ${version2}
+ ${policyIds} Run Keyword If "${returnData}" == "1" GetPolicyIds ${response}
+ ... ELSE Create List
+ ${rescode} Set Variable ${response['code']}
+ Should Be Equal As Strings ${rescode} 200
+ #${policyIds} json.loads ${json}
+ ${policyIds} Run Keyword IF "${createPolicyIds}" != "${EMPTY}" AppendListToList ${policyIds} ${createPolicyIds}
+ ... ELSE Set Variable ${policyIds}
+ SET GLOBAL VARIABLE ${createPolicyIds} ${policyIds}
+ [Return] ${policyIds}
+CreateCondation
+ [Documentation] 解析策略条件,根据key:protocolField值判断其在source、destation、filter中
+ ... 入参${objectListData}所有的condation:"condation": 格式参照如下
+ ... "condation":
+ ... -
+ ... "protocolField": "TSG_SECURITY_SOURCE_ADDR"
+ ... "objectType": "ip"
+ ... "objectSubType": "endpoint"
+ ... "objList":
+ ... - "addItemList":
+ ... -
+ ... "ip": "255.255.255.255"
+ ... "port": "0-65535"
+ ... -
+ ... "ip": "255.255.255.255"
+ ... "port": "1-65535"
+ ... - "addItemList":
+ ... -
+ ... "ip": "255.255.255.1-255.255.255.255"
+ ... "port": "0-65535"
+ ... -
+ ... "ip": "255.255.255.2/24"
+ ... "port": "1-65535"
+ ... 返回参数:${objectIdsList} 格式:[107582, 107583]
+ ... ${sourceList} ${destinationList} ${filterList} 策略中的对象id,源、目的、filterlist条件 ,格式参照:[{'objectId': 107582, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'},{'objectId': 107582, 'protocolField': 'TSG_SECURITY_SOURCE_ADDR'}]
+ [Arguments] ${objectListData}
+ Comment 循环读取对象list
+ ${sourceList} create List
+ ${destinationList} create List
+ ${filterList} create List
+ ${objectIdsList} create List
+ #S${objectListDataType} = Evaluate type(${objectListData})
+ FOR ${key} IN @{objectListData}
+ ${protocolField} Get From Dictionary ${key} protocolField
+ ${objectIds} ${objectList} CreateObjectList ${key}
+ ${len}= Get Length ${objectIdsList}
+ ${objectIdsList} Run Keyword If ${len} != 0 AppendListToList ${objectIdsList} ${objectIds} ELSE Set Variable ${objectIds}
+ Run Keyword If "${protocolField}" == "TSG_SECURITY_SOURCE_ADDR" or "${protocolField}" == "None" or "${protocolField}" == "TSG_SECURITY_SOURCE_LOCATION" or "${protocolField}" == "TSG_SECURITY_SOURCE_ASN" or "${protocolField}" == "TSG_FILED_GTP_IMSI" or "${protocolField}" == "TSG_FILED_GTP_PHONE_NUMBER" or "${protocolField}" == "TSG_FILED_GTP_APN" Append To List ${sourceList} ${objectList}
+ ##############destination
+ ... ELSE IF "${protocolField}" == "TSG_SECURITY_DESTINATION_ADDR" or "${protocolField}" == "TSG_SECURITY_DESTINATION_LOCATION" or "${protocolField}" == "TSG_SECURITY_DESTINATION_ASN" Append To List ${destinationList} ${objectList}
+ #filter
+ ... ELSE Append To List ${filterList} ${objectList}
+ #... ELSE orangeFilter ${filterList} ${objectList}
+ #Run Keyword If "${protocolField}" == "TSG_SECURITY_SOURCE_ADDR" or "${protocolField}" == "subid" or "${protocolField}" == "TSG_SECURITY_SOURCE_IPLearnning" or "${protocolField}" == "TSG_SECURITY_SOURCE_LOCATION" or "${protocolField}" == "TSG_SECURITY_SOURCE_ASN" or "${protocolField}" == "TSG_FILED_GTP_IMSI" or "${protocolField}" == "TSG_FILED_GTP_PHONE_NUMBER" or "${protocolField}" == "TSG_FILED_GTP_APN" AppendListToList ${sourceList} ${objectList}
+ ##############destination
+ #... ELSE IF "${protocolField}" == "TSG_SECURITY_DESTINATION_ADDR" or "${protocolField}" == "TSG_SECURITY_DESTINATION_IPLearnning" or "${protocolField}" == "TSG_SECURITY_DESTINATION_LOCATION" or "${protocolField}" == "TSG_SECURITY_DESTINATION_ASN" AppendListToList ${destinationList} ${objectList}
+ #filter
+ #... ELSE AppendListToList ${filterList} ${objectList}
+ END
+ [Return] ${objectIdsList} ${sourceList} ${destinationList} ${filterList}
+
+
+
+CreatePolilcys
+ [Documentation]
+ ... 场景策略:secuiryt、proxy策略
+ ... 数据分离,测试数据来自对应的yaml文件
+ ... 参数 ${filepath}读取文件路径 ${filename}读取文件名 ${keywords}获取数据关键字
+ [Arguments] ${filepath} ${filename} ${keywords}
+ Comment 参数 ${filepath}读取文件路径 ${filename}读取文件名 ${keywords}获取数据关键字
+ Comment 创建策略
+ ${YAML}= Get File ${filepath}${/}${filename}
+ ${LOADED}= yaml.Safe Load ${YAML}
+ ${policyList} Get From Dictionary ${LOADED} ${keywords}
+ ${policyListType} = Evaluate type(${policyList})
+ #${retkeys} evaluate [one for one in ${ipList}]
+ #${dictType} = Evaluate type(${retkeys})
+ ${policyIds} Run Keyword If "${policyListType}" == "<class 'list'>" PolicyBatch ${policyList}
+ ... ELSE Set Variable ${EMPTY}
+ ${policyListType} = Evaluate type(${policyIds})
+ ${json} json.Dumps ${policyIds} ensure_ascii=False
+ ${json} = Replace String ${json} ["", [
+ ${policyIds} json.loads ${json}
+ [Return] ${policyIds}
+
+PolicyBatch
+ [Tags]
+ [Arguments] ${policyDictList}
+ Comment 参数${policyDictList}多个策略列表
+ Comment 循环批量创建策略
+ ${pIds} Create List ${EMPTY}
+ FOR ${policyDict} IN @{policyDictList}
+ ${rescode} ${ids} PolicyRequest ${policyDict}
+ AppendListToList ${pIds} ${ids}
+ END
+ [Return] ${pIds}
+
+PolicyRequest
+ [Tags]
+ [Arguments] ${policyList}
+ Comment 参数${policyList}策略dict
+ Comment 创建策略
+ ${returnData} Get From Dictionary ${policyList} returnData
+ ${bodyJson} json.Dumps ${policyList}
+ ${response} BasePostRequestForV2 ${policyUrl} ${bodyJson} ${version2}
+ ${policyIds} Run Keyword If "${returnData}" == "1" GetPolicyIds ${response}
+ ... ELSE Create List
+ ${rescode} Set Variable ${response['code']}
+ Should Be Equal As Strings ${rescode} 200
+ #S${dictType} = Evaluate type(${objectIds})
+ #log %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%${dictType}
+ [Return] ${rescode} ${policyIds}
+
+GetVerifyState
+ [Documentation]
+ ... 传入VerifyPolicies返回的resData值
+ ... 以及已知的被命中的策略id
+ [Arguments] ${resData} ${policyId}
+ ${len} Get Length ${resData['hitPolicyList']}
+ ${policyState} Create Dictionary
+ FOR ${n} IN RANGE ${len}
+ Set To Dictionary ${policyState} '${resData['hitPolicyList'][${n}]['policyId']}'=${resData['hitPolicyList'][${n}]['isExecutePolicy']}
+ END
+ log ${policyState}
+ ${State} Get From Dictionary ${policyState} '${policyId}'
+ Should Be Equal As Strings ${State} True
+
+
\ No newline at end of file |
