如何使用HTTP API从gcr.io Docker注册表中列出图像和标签?

Jef*_*eff 5 node.js docker google-cloud-platform docker-registry google-container-registry

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

我首先使用google-auto-auth来获取具有scope的令牌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`
  }
})
Run Code Online (Sandbox Code Playgroud)

然后,我尝试使用此方法来调用v2/_catalog端点:

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

我得到以下错误:

{ 
  errors: [ { code: 'DENIED', message: 'Failed to retrieve projects.' } ] 
}
Run Code Online (Sandbox Code Playgroud)

足够公平,它必须提供我的项目ID,但是我应该在哪里提供呢?

只是为了看看我是否还能使其他任何东西正常工作,我尝试了:

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

我得到以下信息:

{ 
  errors: [ 
    { 
      code: 'NAME_INVALID', 
      message: 'Requested repository does not match bearer token resource: my-project-id/my-image' 
    } 
  ] 
}
Run Code Online (Sandbox Code Playgroud)

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

小智 5

第一个错误可能是因为您缺少列出项目的范围之一: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>:*
Run Code Online (Sandbox Code Playgroud)

请参阅此处的示例:https : //docs.docker.com/registry/spec/auth/token/#requesting-a-token


Jef*_*eff 5

与GCR的乔恩·约翰逊(Jon Johnson)进行了广泛的交流之后,我们终于弄清了问题所在。如果您赞成这个答案,请也赞成乔恩(Jon's),他超越了这一点,以解决此问题。

截止本文撰写之时,其中大部分内容尚未记录在案。

  • 需要使用registry:catalog:*范围。
  • 我的图像被推送到us.gcr.io,并将它们视为单独的注册表-我认为它们是镜子。
  • 服务帐户必须Project Viewer在Google Cloud IAM中具有角色。
  • 您可以使用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`
  },  
})
Run Code Online (Sandbox Code Playgroud)

使用令牌列出存储库

const client = axios.create({
  baseURL: `https://us.gcr.io/v2`,
  headers: {
    Authorization: `Bearer ${token}`
  }
})

client.get('/_catalog').then((response) => {
  console.log(response.data.repositories)
})
Run Code Online (Sandbox Code Playgroud)