【问题标题】:Is it possible to ge the list of tenants a user is associated with in OpenStack using the keystoneclient?是否可以使用 keystoneclient 在 OpenStack 中获取用户关联的租户列表?
【发布时间】:2013-03-20 14:06:16
【问题描述】:

有人知道获取用户租户列表的方法吗?我知道我可以获取租户的用户,并且可以获取所有租户的列表,因此从技术上讲,我可以遍历所有租户并查找特定用户,但这似乎是一种繁琐的方法。

【问题讨论】:

    标签: openstack keystone


    【解决方案1】:

    不知道 keystone-client,但可以使用 keystone API 的第 3 版:

    GET /v3/users/['USER_ID']/projects

    【讨论】:

      【解决方案2】:

      这不是由 CLI 或 API 实现的。您可以列出令牌可以访问的所有租户,但不能按用户 ID 列出租户。

      Keystone 将用户与租户和角色相关联。所以基本上我们应该能够列出用户的所有角色,从而获得所有租户。但在实践中,你不能:

      Keystone 客户端确实有一个 user-role-list 子命令,但 tenant-id 是强制性的,如以下示例所示:

      $ keystone --token <...> --endpoint http://<...> user-role-list
      'Client' object has no attribute 'auth_tenant_id'
      
      $ keystone --token <...> --endpoint http://<...> user-role-list --user-id 0ab2b35d609d4994aa3100b13bcf9cb8
      'Client' object has no attribute 'auth_tenant_id'
      
      $ keystone --token <...> --endpoint http://<...> user-role-list --user-id 0ab2b35d609d4994aa3100b13bcf9cb8 --tenant-id 74ece217e4f543c5bd1387786fd9173c
      +----------------------------------+-------+----------------------------------+----------------------------------+
      |                id                |  name |             user_id              |            tenant_id             |
      +----------------------------------+-------+----------------------------------+----------------------------------+
      | 3ddf15ce213e4fa08f4d5769db4ee30b | admin | 0ab2b35d609d4994aa3100b13bcf9cb8 | 74ece217e4f543c5bd1387786fd9173c |
      +----------------------------------+-------+----------------------------------+----------------------------------+  
      

      Rest API 也是如此:

      /users/{user_id}/roles 在端口 35357 上返回 HTTP 501(在端口 5000 上返回 HTTP 404):

      $ curl -H "X-Auth-Token:..." http://localhost:35357/v2.0/users/aa1a4faf337544f8a29eb033fa895eef/roles | jq '.'
      {
        "error": {
          "title": "Not Implemented",
          "code": 501,
          "message": "User roles not supported: tenant ID required"
        }
      }
      

      如果你指定一个租户 id,它会起作用:

      $ curl -H "X-Auth-Token:..." http://localhost:35357/v2.0/tenants/8e0c523848e645be829c779bb9307290/users/aa1a4faf337544f8a29eb033fa895eef/roles | jq '.'
      {
        "roles": [
          {
            "id": "9fe2ff9ee4384b1894a90878d3e92bab",
            "name": "_member_",
            "description": "Default role for project membership",
            "enabled": "True"
          },
          {
            "name": "admin",
            "id": "3ddf15ce213e4fa08f4d5769db4ee30b"
          }
        ]
      }
      

      出于完整性目的,您可以使用 Rest API 通过 token 获取租户:

      $ curl -H "X-Auth-Token:<token here>" http://localhost:5000/v2.0/tenants/ | jq '.'
      {
        "tenants": [
          {
            "name": "Altair",
            "id": "51b8b30d4e574899b8fef6d819fda389",
            "enabled": true,
            "description": ""
          },
          {
            "name": "Aldebaran",
            "id": "92b1315b07f44afdaec920a868685b28",
            "enabled": true,
            "description": ""
          }
        ],
        "tenants_links": []
      }
      

      【讨论】:

      • 从技术上讲,您至少在 v2.0 api 中不需要指定租户来获取令牌。根据我列出的示例,有一个 keystone API(不是 keystoneclient )用于列出用户的租户。这不需要管理员令牌。
      • @Matt 也许我不清楚,但我解释说你不能按用户 ID 列出租户。您的示例按令牌列出租户。这是不同的。我编辑了我的帖子以添加一些说明,并添加一个带有 curl 的示例以列出令牌可以访问的所有租户。问候。
      【解决方案3】:

      使用 user3067622 的建议,从 Keystone 获取我的 Auth Token 后,以下语法有效:

      curl -v http://your.cloud.com:35357/v3/users/<user_UUID>/projects -X GET \
      -H 'Content-type: application/json' \
      -H 'Accept: application/json' \
      -H "X-Auth-Token: 27427040f887440c80ed6a697b294c47" | python -m json.tool | grep name
      

      【讨论】:

      • 我通过 CURL 173.234.161.218:5000/v3/users -h X-Auth-Token 创建用户: -d { "user": { "default_project_id": "55b686ef2fd148eaaa1f6ca77d1b2b89", "description": "Test Users ", "email": "er.rishishrivastava@gmail.com", "enabled": true, "name": "Rishi", "password": "123456" } } 但我无法通过这个用户登录“你没有任何项目的授权。”我必须做什么
      【解决方案4】:

      不是真的。但是您可以直接从 keystone API 中获取。

      例子:

          from keystoneclient.v2_0 import client
          from keystoneclient.v2_0 import tokens
      
          # keystone = client.Client(username=username, password=password, tenant_name=tenant_name, auth_url=auth_url)
          keystone = client.Client(username=username, password=password, auth_url=auth_url)
          token = keystone.auth_token
          headers = {'X-Auth-Token': token }
          tenant_url = auth_url
          tenant_url += '/tenants'
          r = requests.get(tenant_url, headers=headers)
          tenants_raw = r.raw.read(900000)
          tenant_data = json.loads(tenants_raw)
          success = 0
      

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 2017-08-09
        • 2011-08-13
        相关资源
        最近更新 更多