2020-06-18 16:29:17 +08:00
*** Settings ***
Library Collections
Library RequestsLibrary
Resource ../../../03-Variable/BifangApiVariable.txt
Resource ../../../02-Keyword/tsg_bfapi/policy_file_interface/FunctionalKeywords.robot
Resource ProcessObjectBody.robot
Library Collections
Resource ../Common.robot
2020-12-24 20:02:22 +08:00
Library ../../../04-CustomLibrary/Library/fileOperations.py
Library DatabaseLibrary
2020-06-18 16:29:17 +08:00
*** Variables ***
2020-12-24 20:02:22 +08:00
${objectUrl} /policy/object
2021-04-29 15:13:42 +08:00
${categoryUrl} /category/dict
${applicationDictUrl} /application/update/dict
${fileUploaduRL} /system/feature/update/
2020-12-24 20:02:22 +08:00
2020-06-18 16:29:17 +08:00
*** Keywords ***
AddObjects
2020-12-24 20:02:22 +08:00
[Arguments] ${returnData} ${objectList}
[Documentation] 新增策略对象
2020-06-18 16:29:17 +08:00
... objectList,策略对象,可为list类型与dict类型
2020-07-24 12:43:07 +08:00
... addItemList自动过滤itemId
... updateItemList自动过滤isInitialize(update时该字段引发异常)
... ipItem格式为dict,自动过滤空的字段
2020-12-24 20:02:22 +08:00
... stringItem格式为dict,需要注意keywordArray字段应传入逗号分隔的字符串eg: keyword1,keyword2
2020-07-24 12:43:07 +08:00
... http_signature为代表的拓展关键字keywordArray字段也是以逗号分隔的
2020-06-18 16:29:17 +08:00
... returnData, 是否返回数据, 固定为1
${returnData}= Run Keyword If '${returnData}' == '${EMPTY}' Set Variable 1
... ELSE Set Variable ${returnData}
#必选参数判定
Should Not Be Empty ${objectList}
${dictType} = Evaluate type(${objectList})
2020-06-23 18:15:03 +08:00
${body} Run Keyword If "${dictType}" == "<class 'list'>" ObjectListOperation ${returnData} ${objectList} add
2020-12-24 20:02:22 +08:00
... ELSE IF "${dictType}" == "<class 'dict'>" ObjectOperation ${returnData} ${objectList} add
2020-06-18 16:29:17 +08:00
... ELSE Set Variable ${EMPTY}
${response} BasePostRequestForV2 ${objectUrl} ${body} ${version}
2020-12-24 20:02:22 +08:00
${objectIds} Run Keyword If "${returnData}" == "1" Get-ObjectIds ${response}
... ELSE Create List
${rescode} Set Variable ${response['code']}
2020-06-18 16:29:17 +08:00
Should Be Equal As Strings ${rescode} 200
[Return] ${rescode} ${objectIds}
2020-12-24 20:02:22 +08:00
2020-06-18 16:29:17 +08:00
UpdateObjects
2020-12-24 20:02:22 +08:00
[Arguments] ${returnData} ${objectList} ${opAction}
[Documentation] 更新策略对象
2020-06-18 16:29:17 +08:00
... objectList,策略对象,可为list类型与dict类型
2020-07-24 12:43:07 +08:00
... addItemList自动过滤itemId
... updateItemList自动过滤isInitialize(update时该字段引发异常)
... ipItem格式为dict,自动过滤空的字段
2020-12-24 20:02:22 +08:00
... stringItem格式为dict,需要注意keywordArray字段应传入逗号分隔的字符串eg: keyword1,keyword2
2020-07-24 12:43:07 +08:00
... http_signature为代表的拓展关键字keywordArray字段也是以逗号分隔的
2020-06-18 16:29:17 +08:00
... returnData, 是否返回数据, 固定为1
... opAction, 可为update, enable,disable
${returnData}= Run Keyword If '${returnData}' == '${EMPTY}' Set Variable 1
... ELSE Set Variable ${returnData}
#必选参数判定
Should Not Be Empty ${objectList}
${dictType} = Evaluate type(${objectList})
2020-06-23 18:15:03 +08:00
${body} Run Keyword If "${dictType}" == "<class 'list'>" ObjectListOperation ${returnData} ${objectList} ${opAction}
2020-12-24 20:02:22 +08:00
... ELSE IF "${dictType}" == "<class 'dict'>" ObjectOperation ${returnData} ${objectList} ${opAction}
2020-06-18 16:29:17 +08:00
... ELSE Set Variable ${EMPTY}
${response} BaseEditRequestForV2 ${objectUrl} ${body} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
2020-06-18 16:29:17 +08:00
Should Be Equal As Strings ${rescode} 200
[Return] ${rescode}
2021-04-29 15:13:42 +08:00
UpdateInstall
[Arguments] ${updateDict}
[Documentation] 更新策略对象
... objectList,策略对象,可为list类型与dict类型
... addItemList自动过滤itemId
... updateItemList自动过滤isInitialize(update时该字段引发异常)
... ipItem格式为dict,自动过滤空的字段
... stringItem格式为dict,需要注意keywordArray字段应传入逗号分隔的字符串eg: keyword1,keyword2
... http_signature为代表的拓展关键字keywordArray字段也是以逗号分隔的
... returnData, 是否返回数据, 固定为1
... opAction, 可为update, enable,disable
#必选参数判定
Should Not Be Empty ${updateDict}
${bodyJson} json.Dumps ${updateDict} ensure_ascii=False
${response} BaseEditRequestForV2 ${fileUploaduRL} ${body} ${version}
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
[Return] ${rescode}
2020-12-24 20:02:22 +08:00
2020-06-18 16:29:17 +08:00
GetObjectItems
2020-12-24 20:02:22 +08:00
[Arguments] ${params}
[Documentation] 获取策略对象中的item,策略对象单元
2020-06-18 16:29:17 +08:00
... 当updateItemList中有需要传入数据时
... 调用此关键字
#必选参数判定
2020-12-24 20:02:22 +08:00
Should Not Be Empty ${params}
2020-06-18 16:29:17 +08:00
${paramsStr} Dictionary-To-QueryParams ${params}
${response} BaseFormRequest /policy/items ${paramsStr} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
2020-06-18 16:29:17 +08:00
Should Be Equal As Strings ${rescode} 200
2020-12-24 20:02:22 +08:00
${data} Set Variable ${response['data']}
2020-06-18 16:29:17 +08:00
${itemIds} Create List
FOR ${item} IN @{data['list']}
Append To List ${itemIds} ${item['itemId']}
END
2020-06-28 17:35:34 +08:00
[Return] ${rescode} ${itemIds} ${data['list']}
2020-12-24 20:02:22 +08:00
2020-06-29 16:21:55 +08:00
GetObjectItems1
2020-12-24 20:02:22 +08:00
[Arguments] ${params}
[Documentation] 获取策略对象中的item,策略对象单元
2020-06-29 16:21:55 +08:00
... 当updateItemList中有需要传入数据时
... 调用此关键字
#必选参数判定
2020-12-24 20:02:22 +08:00
Should Not Be Empty ${params}
2020-06-29 16:21:55 +08:00
${paramsStr} Dictionary-To-QueryParams ${params}
${response} BaseFormRequest /policy/items ${paramsStr} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
2020-06-29 16:21:55 +08:00
Should Be Equal As Strings ${rescode} 200
2020-12-24 20:02:22 +08:00
${data} Set Variable ${response['data']}
2020-06-29 16:21:55 +08:00
[Return] ${rescode} ${data}
2020-12-24 20:02:22 +08:00
2020-06-28 17:35:34 +08:00
DeleteObjects
2020-12-24 20:02:22 +08:00
[Arguments] ${objectIds}
[Documentation] 删除策略对象
2020-06-28 17:35:34 +08:00
... params, 传入的删除字典
... 结构为[1,2,3]
${type} Evaluate isinstance(${objectIds},list)
2020-12-24 20:02:22 +08:00
Should Be True ${type}
2020-06-28 17:35:34 +08:00
${dict} Create Dictionary objectIds=${objectIds}
${json} json.Dumps ${dict} ensure_ascii=False
${response} BaseDeleteRequest /${version}${objectUrl} ${json}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
2020-06-28 17:35:34 +08:00
[Return] ${rescode}
2020-12-24 20:02:22 +08:00
2020-06-28 17:35:34 +08:00
ImportObject
2020-12-24 20:02:22 +08:00
[Arguments] ${filePath} ${fileName} ${objectType} ${objectSubType} ${objectName}
[Documentation] 策略对象导入
2020-06-28 17:35:34 +08:00
... filePath文件路径
... fileName文件名称
... objectType对象类型
... objectSubType对象子类型
... objectName对象名称
2020-06-29 17:02:46 +08:00
... jira CHON-12导入限制需求支持
2020-06-28 17:35:34 +08:00
Directory Should Exist ${path}
File Should Exist ${path}/${filePath}/${fileName}
File Should Not Be Empty ${path}/${filePath}/${fileName}
${size} Get File Size ${path}/${filePath}/${fileName}
Should Not Be Empty ${objectType}
2020-06-29 16:21:55 +08:00
#获取Item 的总数
${total} GetItemNum ${objectType} ${objectSubType}
#获取导入文件中的总行数
2020-12-24 20:02:22 +08:00
#${lines} CountLines ${path}/${filePath}/${fileName}
2020-06-29 16:21:55 +08:00
${lines} CountLines ${path}/${filePath}/${fileName}
#总数超过一定数据量测试不通过
2020-12-24 20:02:22 +08:00
${total1} Evaluate ${total}+${lines}
#链接数据库查询上限
Connect To Database Using Custom Params pymysql ${mysqlHost}
${objectLimit} query SELECT cfg_value FROM t_sys_cfg WHERE cfg_name='${objectType}_upper_limit'
${objectLimit} Set Variable ${objectLimit[0][0]}
${remaining} Evaluate ${objectLimit}-${total}
log ${objectType}剩余可导入量为:${remaining}
${testType} Evaluate type($objectLimit)
Disconnect From Database
2020-06-29 17:02:46 +08:00
#jira CHON-12导入限制需求支持
2020-12-24 20:02:22 +08:00
Run Keyword If "${objectType}"=="url" and ${total1}>${objectLimit} log 当前已经存在item个数${total},待导入个数${lines},总数超过${objectLimit}条!
... ELSE IF ${total1}>${objectLimit} log 当前已经存在item个数${total},待导入个数${lines},总数超过${objectLimit}条!
2020-06-28 17:35:34 +08:00
${objectSubType} Run Keyword If "${objectType}"=="ip" and "${objectSubType}"=="${EMPTY}" Set Variable endpoint
... ELSE Set Variable ${objectSubType}
${subfix} Fetch From Right ${fileName} .
${binFile} Run Keyword If "${subfix}"=="txt" Evaluate (r'${fileName}',open(r"${path}/${filePath}/${fileName}",'rb'),'text/plain')
... ELSE IF "${subfix}"=="csv" Evaluate (r'${fileName}',open(r"${path}/${filePath}/${fileName}",'rb'),'application/vnd.ms-excel')
... ELSE ${EMPTY}
${data} Create Dictionary objectType=${objectType}
2020-12-24 20:02:22 +08:00
Run Keyword If "${objectSubType}"!="${EMPTY}" Set To Dictionary ${data} objectSubType=${objectSubType}
Run Keyword If "${objectName}"!="${EMPTY}" Set To Dictionary ${data} objectName=${objectName}
2020-06-28 17:35:34 +08:00
${file} Create Dictionary file=${binFile}
${response} BaseMultipartPostRequest ${objectUrl}/batch ${data} ${file} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
[Return] ${rescode} ${objectType} ${response}
2021-04-29 15:13:42 +08:00
ImportApplication
[Arguments] ${filePath} ${fileName} ${feature} ${code}
[Documentation] 策略对象导入
... filePath文件路径
... fileName文件名称
... feature是特征
Directory Should Exist ${path}
File Should Exist ${path}/${filePath}/${fileName}
File Should Not Be Empty ${path}/${filePath}/${fileName}
${size} Get File Size ${path}/${filePath}/${fileName}
${binFile} Run Keyword If "${feature}"=="${EMPTY}" Evaluate (r'${fileName}',open(r"${path}/${filePath}/${fileName}",'rb'),'application/xml')
... ELSE IF "${feature}"=="app_library" or "${feature}"=="app_signature" Evaluate (r'${fileName}',open(r"${path}/${filePath}/${fileName}",'rb'),'application/octet-stream')
... ELSE ${EMPTY}
${file} Create Dictionary file=${binFile}
# ${response} BaseMultipartPostRequest ${objectUrl}/batch ${EMPTY} ${file} ${version}
${response} Run Keyword If "${feature}"=="${EMPTY}" BaseMultipartPostRequest ${applicationDictUrl} ${EMPTY} ${file} ${version}
... ELSE IF "${feature}"=="app_library" or "${feature}"=="app_signature" BaseMultipartPostRequest ${fileUploaduRL} ${EMPTY} ${file} ${version}
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} ${code}
2020-06-28 17:35:34 +08:00
ExportObject
[Arguments] ${objectType} ${objectSubType} ${objectIds} ${objectName}
2020-12-24 20:02:22 +08:00
[Documentation] 策略对象导出
... objectType ip,url等
... objectSubType 子类型
... objectIds 1,2,3
... objectName 字符串
2020-06-28 17:35:34 +08:00
Should Not Be Empty ${objectType}
${params} Create Dictionary objectType=${objectType}
Run Keyword If "${objectSubType}"!="${EMPTY}" Set To Dictionary ${params} objectSubType=${objectSubType}
Run Keyword If "${objectIds}"!="${EMPTY}" Set To Dictionary ${params} objectIds=${objectIds}
2020-12-24 20:02:22 +08:00
Run Keyword If "${objectName}"!="${EMPTY}" Set To Dictionary ${params} objectName=${objectName}
${response} BaseGetRequestReturnBinary ${objectUrl}/batch ${params} ${version}
2020-06-29 16:21:55 +08:00
[Return] ${response}
2020-12-24 20:02:22 +08:00
2020-06-29 16:21:55 +08:00
GetItemNum
[Arguments] ${objectType} ${objectSubType}
2020-12-24 20:02:22 +08:00
[Documentation] 获取策略对象单元总数
2020-06-29 16:21:55 +08:00
${getTotal} Create Dictionary pageNo=${1} pageSize=${1}
Run Keyword If "${objectType}"=="ip" and "${objectSubType}"=="endpoint" Set To Dictionary ${getTotal} itemType=IP
... ELSE IF "${objectType}"=="ip" and "${objectSubType}"=="geo_location" Set To Dictionary ${getTotal} itemType=geo_location
... ELSE IF "${objectType}"=="ip" and "${objectSubType}"=="as_number" Set To Dictionary ${getTotal} itemType=as_number
... ELSE IF "${objectType}"=="url" Set To Dictionary ${getTotal} itemType=URL
... ELSE Set To Dictionary ${getTotal} itemType=${objectType}
${rescode} ${data} GetObjectItems1 ${getTotal}
${total} Set Variable ${data['total']}
Run Keyword If "${objectType}"=="fqdn_category" Set To Dictionary ${getTotal} isInitialize=1
${rescode} ${data} Run Keyword If "${objectType}"=="fqdn_category" GetObjectItems1 ${getTotal}
... ELSE Set Variable ${rescode} ${data}
${total} Run Keyword If "${objectType}"=="fqdn_category" Evaluate ${total} - ${data['total']}
... ELSE Set Variable ${total}
[Return] ${total}
2020-12-24 20:02:22 +08:00
CountLines1
2020-06-29 16:21:55 +08:00
[Arguments] ${file}
2020-12-24 20:02:22 +08:00
[Documentation] 获取文件总行数
2020-06-29 16:21:55 +08:00
${count} Set Variable ${0}
${openFile} Evaluate enumerate(open(r"${file}",'r'))
FOR ${line} IN @{openFile}
2020-12-24 20:02:22 +08:00
${val} Run Keyword If "${line[0]}"=="0" Set Variable ${line[1]}
... ELSE Set Variable ${EMPTY}
${val2} Run Keyword If "${line[1]}"=="\n" Set Variable ${line[0]}
... ELSE Set Variable ${EMPTY}
log ${line}
log "${line[1]}"
2020-06-29 16:21:55 +08:00
${match} Get Regexp Matches ${val} ^-->
2020-12-24 20:02:22 +08:00
#${match2} Get Regexp Matches ${val} ^(\t)*$\n
2020-06-29 16:21:55 +08:00
${len} Get Length ${match}
2020-12-24 20:02:22 +08:00
#${len2} Get Length ${match2}
2020-06-29 16:21:55 +08:00
${count} Run Keyword If "${line[0]}"=="0" and ${len}>0 Set Variable ${count}
... ELSE Evaluate ${count}+1
END
2020-06-30 18:26:22 +08:00
[Return] ${count}
2020-12-24 20:02:22 +08:00
2020-06-30 18:26:22 +08:00
PolicProtocolFields
2020-12-24 20:02:22 +08:00
[Arguments] ${policyType} ${protocol} ${objectType}
[Documentation] 策略对象生效协议字段查询
2020-06-30 18:26:22 +08:00
... policyType策略类型
... protocol协议
... objectType对象类型
${dict} Create Dictionary
2020-12-24 20:02:22 +08:00
Run Keyword If "${policyType}"!="${EMPTY}" Set To Dictionary ${dict} policyType=${policyType}
Run Keyword If "${protocol}"!="${EMPTY}" Set To Dictionary ${dict} protocol=${protocol}
Run Keyword If "${objectType}"!="${EMPTY}" Set To Dictionary ${dict} objectType=${objectType}
2020-06-30 18:26:22 +08:00
${parmStr} Dictionary-To-QueryParams ${dict}
2020-12-24 20:02:22 +08:00
${response} BaseFormRequest ${objectUrl}/protocolFields ${parmStr} ${version}
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
2020-07-01 12:03:01 +08:00
[Return] ${response['data']}
2020-12-24 20:02:22 +08:00
2020-07-01 12:03:01 +08:00
ObjectReference
2020-12-24 20:02:22 +08:00
[Arguments] ${objectId}
[Documentation] 策略对象相关性查询
2020-07-01 12:03:01 +08:00
${response} BaseFormRequest ${objectUrl}/reference objectId=${objectId} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
2020-07-01 12:03:01 +08:00
[Return] ${response['data']}
2020-12-24 20:02:22 +08:00
2020-07-01 12:03:01 +08:00
AppIdTreeSearch
[Arguments] ${referenceObjectId}
2020-12-24 20:02:22 +08:00
[Documentation] App ID对象树查询
2020-07-01 12:03:01 +08:00
${response} BaseFormRequest ${objectUrl}/appId/tree referenceObjectId=${referenceObjectId} ${version}
2020-12-24 20:02:22 +08:00
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} 200
[Return] ${response['data']}
2021-04-29 15:13:42 +08:00
2021-05-17 09:57:31 +08:00
2021-04-29 15:13:42 +08:00
AddCategories
2021-05-17 09:57:31 +08:00
[Arguments] ${returnData} ${categoryList} ${opAction} ${testCode}=null
[Documentation] 新增Category
2021-04-29 15:13:42 +08:00
... returnData, 是否返回数据, 固定为1
Comment 此处思路给category单独的另一套关键字, 原因: 地址与json串名称与Object部分不同了
${returnData}= Run Keyword If '${returnData}' == '${EMPTY}' Set Variable 1
... ELSE Set Variable ${returnData}
#必选参数判定
Should Not Be Empty ${categoryList}
${dictType} = Evaluate type(${categoryList})
2021-05-17 09:57:31 +08:00
${body} Run Keyword If "${dictType}" == "<class 'list'>" CategoryListOperation ${returnData} ${categoryList} ${opAction}
... ELSE IF "${dictType}" == "<class 'dict'>" CategoryOperation ${returnData} ${categoryList} ${opAction}
... ELSE Set Variable ${EMPTY}
log ${body}
# ${response} BasePostRequestForV2 ${categoryUrl} ${body} ${version}
${response} Run Keyword If "${opAction}"=="add" BasePostRequestForV2 ${categoryUrl} ${body} ${version}
... ELSE IF "${opAction}"=="update" BaseEditRequestForV2 ${categoryUrl} ${body} ${version}
${CategoryObjIds} Run Keyword If "${returnData}" == "1" and ${testCode} == 200 and "${opAction}"=="add" Get-CategoryObjIds ${response}
... ELSE Set Variable ${EMPTY}
${CategoryIds} Run Keyword If "${returnData}" == "1" and ${testCode} == 200 and "${opAction}"=="add" Get-CategoryIds ${response}
2021-04-29 15:13:42 +08:00
... ELSE Set Variable ${EMPTY}
${rescode} Set Variable ${response['code']}
2021-05-17 09:57:31 +08:00
Should Be Equal As Strings ${rescode} ${testCode}
[Return] ${rescode} ${CategoryObjIds} ${CategoryIds}
2021-04-29 15:13:42 +08:00
QueryUpadateRecord
[Arguments] ${body} ${code}
${response} BaseGetRequestOK /${version}/system/feature/update body=${body}
log ${response}
${rescode} Set Variable ${response['code']}
2021-05-17 09:57:31 +08:00
Should Be Equal As Strings ${rescode} ${code}
2021-04-29 15:13:42 +08:00
[Return] ${rescode}
2021-05-17 09:57:31 +08:00
QueryCategory
#hbn
[Arguments] ${body} ${code}
${response} BaseGetRequest /${version}/category/dict body=${body}
log ${response}
${rescode} Set Variable ${response['code']}
Should Be Equal As Strings ${rescode} ${code}
# [Return] ${rescode} ${response}