【问题标题】:How to list images and tags from the gcr.io Docker Registry using the HTTP API?如何使用 HTTP API 从 gcr.io Docker Registry 中列出图像和标签?
【发布时间】:2017-07-14 15:43:53
【问题描述】:

我正在尝试从 Node.js 中的 Google Container Registry (gcr.io) 中获取可用图像及其标签的列表。

我首先使用google-auto-auth 来选择一个范围为https://www.googleapis.com/auth/devstorage.read_write 的令牌, 然后我将该令牌换成gcr.io 令牌,如下所示:

axios.get('https://gcr.io/v2/token?service=gcr.io', {
  auth: {
    username: '_token',
    password: token // token I got from `google-auto-auth`
  }
})

然后我尝试使用它来调用v2/_catalog 端点:

axios.get('https://gcr.io/v2/_catalog', {
  headers: {
    Authorization: `Bearer ${gcrToken}`
  }
})

我收到以下错误:

{ 
  errors: [ { code: 'DENIED', message: 'Failed to retrieve projects.' } ] 
}

很公平,它必须需要我的项目 ID,但我应该在哪里提供呢?

只是想看看我是否可以让其他任何东西正常工作,我尝试了:

axios.get('https://gcr.io/v2/my-project-id/my-image/tags/list', {
  headers: {
    Authorization: `Bearer ${gcrToken}`
  }
})

我得到以下回复:

{ 
  errors: [ 
    { 
      code: 'NAME_INVALID', 
      message: 'Requested repository does not match bearer token resource: my-project-id/my-image' 
    } 
  ] 
}

如何从 gcr.io 读取图像信息?

【问题讨论】:

    标签: node.js docker google-cloud-platform docker-registry google-container-registry


    【解决方案1】:

    在与 GCR 的 Jon Johnson 进行广泛沟通后,我们终于找出了问题所在。如果您支持这个答案,请也支持 Jon 的回答,他为解决这个问题付出了很多努力。

    在撰写本文时,大部分内容都没有记录。

    • 需要使用registry:catalog:*范围。
    • 我的图片被推送到us.gcr.io,他们将它们视为单独的注册表——我以为它们是镜像。
    • 服务帐号必须在 Google Cloud IAM 中具有 Project Viewer 角色。
    • 您可以使用 GCR 令牌以及 Google Cloud 令牌。不过,虽然 GCR 令牌不能与 Basic base64(_token:<token>) 一起使用,但 Google Cloud 令牌可以。

    获取 GCR 令牌

    // Updated host
    axios.get('https://us.gcr.io/v2/token?service=gcr.io', {
      params: {
        service: 'us.gcr.io',
        scope: `registry:catalog:*`
      },
      auth: {
        username: '_token',
        password: token // token I got from `google-auto-auth`
      },  
    })
    

    使用令牌列出存储库

    const client = axios.create({
      baseURL: `https://us.gcr.io/v2`,
      headers: {
        Authorization: `Bearer ${token}`
      }
    })
    
    client.get('/_catalog').then((response) => {
      console.log(response.data.repositories)
    })
    

    【讨论】:

    • 即使我将角色 roles/browser 而不是 roles/viewer 分配给服务帐户,它也对我有用。这是一个“较弱”的角色,因此从安全角度来看可能是一个更安全的选择。
    • 您还必须在包含 docker 图像的项目中启用云资源管理器 API (cloudresourcemanager.googleapis.com),否则您将遇到 403。
    【解决方案2】:

    第一个错误可能是因为您缺少列出项目的范围之一: https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#authorization

    您收到第二个错误,因为您缺少令牌交换中的范围。

    你想要这样的东西:

    https://gcr.io/v2/token?service=gcr.io&scope=repository:<my-project-id/my-image>:*
    

    在此处查看示例:https://docs.docker.com/registry/spec/auth/token/#requesting-a-token

    【讨论】:

    • 我尝试将https://www.googleapis.com/auth/cloudplatformprojects.readonly 添加为范围,同样的错误。
    • 这出乎意料。请随时通过 gcr-contact@google.com 与我们联系,我们可以帮助您进一步调试。
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多