管理测试过程中的凭证
概述:
凭证是测试过程中使用的关键数据。它们不仅对测试结果至关重要,而且要求高度的安全性和保密性。Testany平台的Testany Secrets Service(TSS)确保了凭证在整个测试过程中的端到端保护,防止其在存储、检索和执行时泄露。
理解 Testany Secrets Service
TSS定义:(又称TSS)Testany Secrets Service是一项专门的服务,设计用于从受信任的凭证保管库(如Azure Key Vault和AWS Secrets Manager)中获取机密信息。它支持安全检索敏感信息,如密钥和证书,确保数据的安全性并符合行业标准。该服务通过接收请求来提取由密钥标识的特定机密信息,并以请求的数据进行响应,从而在安全信息管理中发挥关键作用。(《术语表》)
在Testany平台的设计中,TSS与测试执行运行时模块(Execution Runtime) 采用一对多的关系集成。测试凭证的管理取决于运行时(Runtime)的部署方式,无论是Testany托管云还是用户自托管云。
如果您有一些需要在测试用例中使用的安全数据,如凭证,您可以使用Testany Secrets Service,通过凭证保管库在执行过程中启用测试检索凭证/安全数据。
管理凭证保管库
本章节主要面向负责管理测试凭证的工作区管理员。其他角色也可将其作为参考资料使用。
理解“凭证保管库”的概念
在Testany平台的设计中,所有用于测试的凭证必须且只能存储在符合所有相关信息安全法律法规的第三方服务提供的安全位置中。这些第三方服务可以是IaaS或SaaS提供商。
目前,与Testany Secrets Service集成的凭证存储包括:
AWS Secrets Manager
Azure Key Vault
为了确保在自动化测试执行过程中,运行测试程序能够安全且自动地读取存储在第三方凭证存储中的凭证,并防止凭证在整个测试生命周期中的泄露,用户需要使用Testany Secrets Service在测试脚本和这些凭证之间建立关联。有关详细步骤,请参阅管理测试用例 (testany.io)。
因此,“凭证保管库”是一个虚拟对象,表示Testany平台上第三方凭证存储,而“凭证”也是一个虚拟对象,代表存储在该凭证存储中的凭证。
创建“凭证保管库”
在开始之前…
确保您拥有需要创建凭证保管库的工作区的管理员权限。
为即将创建的凭证保管库对应的第三方凭证存储创建TSS访问权限(可以通过用户名和密码或IAM方法)。
分步骤指南
点击 Workspace settings

导航到“Credential”标签页,选择需要与将要创建的凭证保管库关联的运行时(Runtime)

点击 “Add credential safe” 按钮

- 当你选择的运行时(Runtime)是 “CloudPrime-Default”时
提供所需相应信息并点击 “Submit”

- 当你选择的运行时(Runtime)不是 “CloudPrime-Default”时
你会被要求提供凭证保管库的名称和URL

创建完成后,您将获得一个凭证保管库键值(key),该键值用于标识您想要使用的凭证保管库。该键值将在您的代码中使用。

更新“凭证保管库”
您可以通过点击此处编辑现有凭证保管库的名称和URL:

请注意,凭证保管库的键值(key)是不允许被修改的。
删除凭证保管库
您可以通过点击此处删除现有的凭证保管库:
如果您删除一个凭证保管库,保管库下创建的所有凭证也将被删除。所有引用这些凭证的测试将立即受到影响。

在凭证保管库下管理凭证密钥
凭证密钥是存储在您管理的凭证保管库中的凭证名称。
您需要在一个凭证保管库下提供密钥名称,然后您可以将该凭证绑定到多个测试用例。
在您的凭证存储(Azure Key Vault 或 AWS Secrets Manager)中添加凭证
在Azure Key Vault中, 使用如下命令创建一个凭证。 More details about Azure Key Vault
az keyvalult secret set --vault-name <YOUR-VAULT-NAME> --name client-secret --value <YOUR-SECRET-VALUE>
在AWS Secrets Manager中,凭证被像这样保存(
client-secret
)。 More details about AWS Secrets Manager
在Testany平台中添加凭证
点击“+”按钮,添加您将在凭证保管库中创建的密钥名称。
凭证名称:这是凭证密钥名称的显示名称。
类型:这是凭证的类型(key/secret/certificate)。
键值:这是凭证的密钥名称。它必须与您在“在凭证存储中添加凭证”步骤中创建的键值(如
client-secret
)名称相同。

更新凭证键值
通过点击“编辑”按钮,您可以编辑凭证密钥信息:

删除凭证键值
通过点击“删除”按钮,您可以删除您选择的凭证键值。请注意,所有引用该凭证键值的测试用例将立即受到影响。

在测试用例中引用凭证
要在测试用例中引用凭证,您需要确保Testany平台和您的测试代码都知道您正在使用的凭证。因此,您需要在两个地方进行配置:
在Testany平台上
在您的测试代码中
在Testany平台上的配置
将凭证绑定到特定的测试用例
进入测试用例详情页,点击edit:

点击 “Add a new secure key reference“, 并且选择 Workspace, Safe 和 Credential

点击 “Save”, 这样凭证 client-secret 就与当前的测试用例完成了绑定。

将相应的代码加入您的测试脚本
在您的测试脚本中,您可以使用环境变量 TESTANY_SECRETS_SERVICE
来获取 TSS(Testany Secrets Service) 的URL, 并将 key
作为参数添加到您的URL中。
支持的参数:
key
: 这是存储在您的凭证保管库中的密钥键值。请勿在其中提供 secureUUID。safe_key
(保管库键值): “safe_key”表示您可以使用的特定密钥保管库实例。该实例由工作区管理员在“创建凭证保管库”步骤中创建。
您可以参考TSS API 获得更详细的信息:
openapi: 3.0.3
info:
title: Swagger Testany Secrets Service
description: |-
Testany Secrets Service API.
### Check out its awesome features:
* Retrieve secrets
* Retrieve certificates
* Retrieve keys
termsOfService: http://swagger.io/terms/
contact:
email: support@testany.io
license:
name: Testany
url: https://www.testany.com.cn
version: 2.0.0
servers:
- url: TESTANY_SECRETS_SERVICE
tags:
- name: Testany Secrets Service
description: Fetch Secrets
paths:
/:
get:
tags:
- Testany Secrets Service
summary: Fetch secrets from Trusted Certificate Safe
description: Fetch secrets from Trusted Certificate Safe, like Azure Key Vault, AWS Secrets Manager
operationId: retrieveSecrets
parameters:
- name: key
in: query
description: |
The key name in your Trusted Certificate Safe(TCS), like Azure KeyVault, AWS Secrets Manager etc. Please DO NOT provide the SecureUUID in this parameter. The key you **stored** in TCS is `<SecureUUID>-<key_name>`, this parameter should **ONLY** `<key_name>`, Testany will auto ingest the SecureUUID when retrieve secrets from TCS.
If the key name is used for multiple cases, the key need to be created under the credential safe which created by Workspace Admin. And the key need to bind to the case.
required: true
schema:
type: string
# - name: key_type
# deprecated: true
# in: query
# description: Default is **secrets** if not provide this parameter
# required: false
# schema:
# type: string
# enum:
# - secrets
# - keys
# - certificates
- name: group
deprecated: true
in: query
description: |
The group parameter is only used for TSS V1
Currently the group name configured by Testany. If you need use your specific Trusted Certificate Safe, like a new Azure Key Vault URL, please contact Testany Support Team. In the further, the group will be configured by your workspace admin.
required: true
schema:
type: string
- name: safe_key
in: query
description: |
Currently the safe_key name created by Workspace Admin. If you need use your specific Trusted Certificate Safe, like a new Azure Key Vault URL, please contact your Workspace Admin.
required: true
schema:
type: string
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
properties:
value:
type: string
description: |
Depends on what value you stored in your Trusted Certificates Safe
'400':
description: Invalid key type value
components:
schemas:
keys:
description: |
if key_type is keys, the response string is a json format string
properties:
key:
type: object
properties:
kid:
type: string
kty:
type: string
key_ops:
type: array
items:
type: string
n:
type: string
e:
type: string
attributes:
type: object
properties:
enabled:
type: string
created:
type: integer
example: 161010101
updated:
type: integer
example: 161010101
recoveryLevel:
type: string
certificates:
description: |
if key_type is certificates, the response string is a certificate pem string
example: |
-----BEGIN CERTIFICATE-----\nMIIDPDCCxxxxxxxxxxxxxxsFADAb\nMRkwFwxxxxxxxxxxxNDMwNloXDTI0\nMDkxMTA3NxxxxxxxxxxxxxDQYJ\nKoZIhxxxxxxxxxxxI0sldgU\nxxxxxxxxxxxlsqMK\n3hpTxxxxxxxxxqou3b\nFanxxxxxxxxxxxxxNqhh\nOMuEfjK7kvGxxxxxxxxxWRvCteW\natxxxxxxxxxxxxxxxwEAAaN8\nMHoxxxxxxxxxxxxxxxxxxBQUH\nAwExxxxxxxxxMB0G\nA1UdDgxxxxxxxxxxEA\nVgsjWlIxxxxxxxxA2EF\nS5vwLExxxxxxxxxYTUE\n/2ZQW+j+icoxxxxxxxxxNeh4g\nljtBpX4ugxxxxxxxmnS9jb\nSZxxxxxxxxxx0yFLOyiF5W2Yp\nk+JWxxxxxxxx7AiA==\n-----END CERTIFICATE-----\n
样例代码:
这时一个Postman Collection样例代码, 您可以在Postman的Pre-request script
中发送一个 GET
请求 以获得您所需的凭证
const postRequest = {
url: pm.environment.get("TESTANY_SECRETS_SERVICE")+'?key=client-secret&safe_key=XXX-CS-XXXX',
method: 'GET',
header: {
'Content-Type': 'application/json'
}
};
pm.sendRequest(postRequest, (error, response) => {
if(error){
console.log(error)
}
pm.test("Retrieve Secrets is OK", ()=> {
try{
pm.expect(error).to.equal(null);
pm.expect(response).to.have.property('code', 200);
pm.expect(response).to.have.property('status', 'OK');
pm.expect(response.json()).to.not.have.property('error')
var secret_value = JSON.parse(response.json()['value'])
pm.environment.set("my-secret", secret_value)
}catch(e) {
console.log(response.json())
throw new Error("Retrieve Secrets Error ["+ e.message +"]")
}
})
});
curl command
curl -X GET ${TESTANY_SECRETS_SERVICE}?key=client-secret&safe_key=XXX-CS-XXXX
附上一个Postman测试用例样例供参考: postman-with-credential.zip