当前位置:网站首页>通过Shell实现MeterSphere Signature签名
通过Shell实现MeterSphere Signature签名
2022-07-20 00:41:00 【FIT2CLOUD飞致云】
编者注:
本文为公众号博主Wolfberry的原创文章。
原文链接:
https://mp.weixin.qq.com/s/4Nj8isNI2l0zInqxSDsvvw
本文将分享在MeterSphere开源持续测试平台中如何通过Shell生成Signature签名,并实现对MeterSphere API的调用操作。
最近在将MeterSphere与DevOps的CI/CD集成起来,主要是通过CI/CD流水线触发MeterSphere中的自动化接口用例和场景用例,第一步需要解决调用API所涉及的鉴权认证。
对于Java的实现,可以参考公众号文章:《在MeterSphere中使用预执行脚本功能生成接口认证签名》。然而在有些情况下,对MeterSphere API的调用需要通过Shell脚本来实现,即通过Shell生成Signature签名并调用接口。
根据上面链接中文章的描述,在通过API调用MeterSphere接口时,需要在请求头中传入accessKey和Signature。其中,accessKey为创建API Key时生成的Access Key,Signature通过如下加密算法得出:
通过Shell生成Signature
除了上述方法,我们还可以通过Shell来生成Signature。对于Shell,显然没有高级语言那样有丰富的类库,但Shell也能通过丰富的Linux命令和工具实现复杂的功能。对于本案例,可以利用OpenSSL命令生成该加密签名:
openssl enc -e -aes-128-cbc -base64 -K <加密密钥> -iv <加密向量>
另外,Shell也能十分便捷地用一条命令就能得到加密字符串所需的13位Unix时间戳,用符号“|” 将accessKey与Unix时间戳连接起来,就可以得到加密字符串。
操作示例
根据指定的MeterSphere项目名称,返回相应的项目ID:
■ 传入accessKey、secretKey、projectName和MeterSphere服务器地址,实时生成Signature:
################## Sample script #######################!/bin/bashaccessKey=$1secretKey=$2projectName=$3HOST=$4 keySpec=$(echo -n "${secretKey}" | od -A n -t x1 | tr -d ' ')iv=$(echo -n "${accessKey}" | od -A n -t x1 | tr -d ' ') currentTimeMillis=$[$(date +%s%N)/1000000]# use currentTimeMillis=$(date +s%000) instead for docker containers built from base image alpine or busybox. seed=${accessKey}\|$currentTimeMillissignature=$(printf %s "${seed}" | openssl enc -e -aes-128-cbc -base64 -K ${keySpec} -iv ${iv}) #Verify the signature by calling API through curl command to get project list.RESULT=$(curl -k -s -H "accesskey: ${accessKey}" -H "signature: ${signature}" -H "Content-Type: application/json" https://${HOST}/project/listAll) #Format and parse the returned JSON using jqprojectId=$(printf '%s\n' "${RESULT}" | jq '.data[] | select(.name == "'"${projectName}"'")' |jq .id) echo "Project ID of ${projectName} is: ${projectId}"################## End of the script ######################
■ 执行上述脚本:
$ sh getProjectId.sh <accessKey> <secretKey> <projectName> cloud.metersphere.com
■ 得到返回结果:
Project ID of Sample-Project is: "e82f625a-aca6-4c72-9ac8-1234567890ms"
这证明接口已能成功调通,对于CI/CD流水线而言,只需将调用的API换成相应的触发测试计划或测试场景的接口即可。在需要的时候,也可以根据MeterSphere的Swagger接口文档,调用其他接口,实现更为强大而丰富的功能。
边栏推荐
猜你喜欢
怎么使用mysql的导出语句
typeof与keyof
Installation de MySQL par centos8
10. Démarrage rapide du moteur
Lua code add comment rule
API策略因何成为企业数字化转型的制胜法宝?
如何获取 ASP.NET Core 当前启动地址?
[romance understood by technical talents] tidb community has prepared a "Tanabata Festival" gift for your partner, and everyone has a share!
Developers must read: 2022 mobile application operation growth insight white paper
EL & JSTL: summary of El expression
随机推荐
High single kV load caused by tidb read hotspot
要想成功进行数字化转型 首席信息官应遵循的三个原则
CCTV news news news channel "Zhengzhou opens catering quota invoice by hand"_ People's network
El cascade selector dynamic loading data and echo data method (the most complete summary) < rootbaby>
Developers must read: 2022 mobile application operation growth insight white paper
2022 Hangdian Zhongchao
CCTV news news news channel of Guangzhou catering manual tearing quota invoice_ People's network
EasyCVR视频广场设备列表无法滚动和加载是什么原因?
Two duplicate operations with different functions in blender
Detailed explanation of JDBC function classes
11、gin中间件
模型压缩-方案(一)-参数共享:ALBERT、BERT-of-Theseus
央视新闻《苏州开住宿手撕定额发票》新闻频道_人民网
Luogu p4305 does not repeat numbers
Edge 开发者沙龙|一小时精通Edge扩展开发
央视新闻《天津开住宿手撕定额发票》新闻频道_人民网
[SWPU2019]Web1-1|SQL注入
CCTV news "Ningbo opens catering quota invoice by hand" news channel_ People's network
Chapter 8: arithmetic prime sequence in interval [%d,%d], improved arithmetic prime sequence
11. Gin Middleware