【发布时间】:2013-11-30 19:08:57
【问题描述】:
我想使用 Horizon 从 keystone 获取身份验证令牌,然后想将该身份验证令牌传递给我支持的代码。
我不知道如何获得这个,请帮助我。
我阅读了许多文章和博客博客,但我无法找到答案。请为我指明正确的方向。
【问题讨论】:
标签: python django openstack openstack-horizon
我想使用 Horizon 从 keystone 获取身份验证令牌,然后想将该身份验证令牌传递给我支持的代码。
我不知道如何获得这个,请帮助我。
我阅读了许多文章和博客博客,但我无法找到答案。请为我指明正确的方向。
【问题讨论】:
标签: python django openstack openstack-horizon
【讨论】:
转到您已安装 Keystone 服务的节点。打开 vi /etc/keystone/keystone.conf
检查以 admin_token 开头的第三行。它应该是一个很长的随机字符串:
admin_token = 05131394ad6b49c56f217
那是你的基石令牌。使用python:
>>> from keystoneclient.v2_0.client as ksclient
>>> keystone = ksclient.Client(auth_url="http://service-stack.linxsol.com:35357/v2.0", username="admin", password="123456", tenant_name="admin")
当然,您可以根据自己的选择更改 auth_url、 *用户名、密码* 和 tenant_name。现在您可以使用 keystone 来执行所有的 api 任务:
keystone.tenants.list()
keystone.users.list()
keystone.roles.list()
或使用 dir(keystone) 列出所有可用选项。
您可以按如下方式重复使用令牌:
auth_ref = keystone.auth_ref or token = ksclient.get_raw_token_from_identity_service(auth_url="http://service-stack.linxsol.com:35357/v2.0", username="admin", password="123456", tenant_name="admin")
但请记住,它返回一个字典和一个原始令牌,而不是您在上面看到的令牌形式。
更多信息请查看python-keystoneclient。
希望对你有帮助。
【讨论】:
最简单的方法是使用 Rest 客户端登录并从响应中获取令牌。我喜欢 Firefox RESTClient 插件,但你可以使用任何你想要的客户端。
向 Openstack Identity URL 发布请求:
POST keystone_ip:port/version/tokens
(例如127.0.0.1:5000/v2.0/tokens)
带有标题:
Content-Type: application/json
和身体:
{
"auth": {
"tenantName": "enter_your_tenantname",
"passwordCredentials": {
"username": "enter_your_username",
"password": "enter_your_password"
}
}
}
注意:如果您不确定正确的身份(keystone)URL 是什么,您可以手动登录到 Horizon 并查找 API 端点列表。
响应正文将包含如下内容:
{
"token": {
"issued_at": "2014-02-25T08:34:56.068363",
"expires": "2014-02-26T08:34:55Z",
"id": "529e3a0e1c375j498315c71d08134837"
}
}
在新的休息调用中使用返回的令牌 ID 作为标头。例如,要获取服务器列表,请使用 request:
GET compute_endpoint_ip:port/v2/tenant_id/servers
带有标题:
X-Auth-Token: 529e3a0e1c375j498315c71d08134837
Content-Type: application/json
【讨论】:
您可以使用python-keystoneclient。要对用户进行身份验证,请使用例如
username='admin'
password='1234'
tenant_name='admin'
auth_url='http://127.0.0.1:5000/v2.0'
keystone = client.Client(username=username, password=password, tenant_name=tenant_name, auth_url=auth_url)
一旦用户通过身份验证,就会生成一个令牌。客户端上的 auth_ref 属性(本例中为 keystone 变量)将为您提供一个类似字典的结构,其中包含您需要的有关令牌的所有信息,这将使您能够重新使用令牌或将其传递到您的后端案例。
token_dict = keystone.auth_ref
现在,token_dict 是您可以传递给后端的变量。
【讨论】:
作为一个例子来说明如何做到这一点:
import keystoneclient.v2_0.client as ksclient
# authenticate with keystone to get a token
keystone = ksclient.Client(auth_url="http://192.168.10.5:35357/v2.0",
username="admin",
password="admin",
tenant_name="admin")
token = keystone.auth_ref['token']['id']
# use this token for whatever other services you are accessing.
print token
【讨论】:
首先你必须安装 python-keystoneclient。
要生成令牌,您可以使用以下代码,在这里我想提一下,您可以使用您的服务器 url 更改身份验证 url,但端口号将相同,
from keystoneclient.v2_0 import client
username='admin'
password='1234'
tenant_name='demo'
auth_url='http://10.0.2.15:5000/v2.0' # Or auth_url='http://192.168.206.133:5000/v2.0'
如果您的用户名、密码或租户名称错误,您将收到keystoneclient.openstack.common.apiclient.exceptions.Unauthorized: Invalid user / password
keystone = client.Client(username=username, password=password,tenant_name=tenant_name, auth_url=auth_url)
token_dict = keystone.auth_ref
token_dict
【讨论】: