【发布时间】:2022-12-16 12:06:37
【问题描述】:
我以这段代码为例,使用基于 App Role 的身份验证来访问 Vault。因为秘密是我想使用包装令牌来更安全
import unittest
from hvac import Client
URL = "https://p.vault.myfine-company.de"
JENKINS_TOKEN = "mylovelytoken"
def test_ci_startup(self):
# Jenkins authentifies with token as secure instance
jenkins_client = Client(url=URL, token=JENKINS_TOKEN)
# fetch the role_id and stores this somewhere in the image of the app
resp = jenkins_client.auth.approle.read_role_id(role_name='workshop')
role_id = resp["data"]["role_id"]
# get a wrapped secret_id and passes this to the starting app
result = jenkins_client.write(path='auth/approle/role/workshop/secret-id',wrap_ttl="2s")
unwrap_token = result['wrap_info']['token']
# No the app comes in place
app_client = Client(url=URL) # , token=JENKINS_TOKEN)
# unwrap the secret_id
unwrap_response = app_client.sys.unwrap(unwrap_token) # !!! Here I get permission denied
secret_id = unwrap_response['data']['secret_id']
# use role_id and secret_id to login
login_result = app_client.auth.approle.login(role_id=role_id, secret_id=secret_id)
client_token = login_result['auth']['client_token']
# Read the database credential
read_response = app_client.secrets.kv.v2.read_secret_version(path='test/webapp')
self.assertEqual("users", read_response['data']['data']['db_name'])
return
不幸的是,当我尝试使用 app_client.sys.unwrap(unwrap_token) 解包 secret_id 时,会出现 403“权限被拒绝” 当我将 app_client-Connection 与 app_client = Client(url=URL), token=JENKINS_TOKEN) 一起使用时,一切正常。但这当然不是应该使用基于 AppRole 的身份验证的方式。所有这些都基于以下教程和最佳实践:
https://developer.hashicorp.com/vault/tutorials/recommended-patterns/pattern-approle https://developer.hashicorp.com/vault/tutorials/auth-methods/approle?in=vault%2Fauth-methods
我觉得跟政策有点关系。但是我还没有找到解决方案。
【问题讨论】:
-
是的,客户端需要使用授权令牌展开的关联策略进行身份验证。该政策应该在您在问题底部链接的那些教程中。
-
是的,本教程按预期工作。我可以在第一个窗口中获取一个 wrapped_token,TTL 为 10s。然后使用另一个窗口解包,获取 secret_id,将其与 role_id 一起使用以获得 VAULT_TOKEN,最后使用此令牌读取秘密。这只是我的 python 东西得到了
permission denied -
好的,我现在看到了“拉”方法实现的问题:第一个客户端被授权解包,而不是第二个。第一个客户端解开令牌并传递给第二个客户端进行身份验证。第二个客户端根本没有经过身份验证,因此无法解包。
-
@MattSchuchard 看到我的回答。第二个客户端解包令牌并使用它来获取
secret-id。在这种情况下,应用程序没有将secret-id存储在某处
标签: authentication hashicorp-vault