与我一起工作的一个外部组织允许我访问一个私有(auth令牌受保护的)码头注册中心,最终我希望能够使用docker的HTTP V2查询这个注册表,以便获得注册表中可用的所有存储库和/或图像的列表。
但在这样做之前,我首先想了解一些在公共注册表(如码头枢纽 )上构建这些类型的API查询的基本实践。因此,我在Docker上使用用户名和密码进行了注册我自己,还查阅了API V2文档,其中声明可以将API版本检查请求为:
GET /v2/
或以下列方式请求存储库列表:
GET /v2/_catalog
使用curl以及注册我的Docker Hub帐户时使用的用户名和密码,我试图在命令行构建一个GET请求:
stachyra> curl -u stachyra:<my_password> -X GET https://index.docker.io/v2/
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
stachyra> curl -u stachyra:<my_password> -X GET https://index.docker.io/v2/_catalog
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"registry","Class":"","Name":"catalog","Action":"*"}]}]}
当然,代替<my_password>
,我替换了我的实际帐户密码。
我从这个查询中得到的响应是一条巨大的json消息,列出了数千个存储库名称,但相反,API似乎拒绝了我的Docker凭据。
问题1:是否有正确的停靠中心注册表的URL (index.docker.io
)?(首先,我根据命令行工具docker info
返回的状态信息做出了这个假设,因此我有充分的理由认为它是正确的。)
问题2:假设注册表服务本身有正确的,为什么我的查询返回一个“未经授权”的错误代码?当我试图在hub.docker.com通过网络登录时,我的帐户凭证工作得很好,那么这两种情况之间有什么区别呢?
发布于 2021-08-04 16:11:01
我有正确的URL吗
index.docker.io
通过DockerHub托管Docker实现。hub.docker.com
承载富DockerHub特定的API接口。_catalog
API。为什么我的查询返回一个“未经授权的”错误代码?
为了使用Docker V2 API,需要为每个调用从https://auth.docker.io/token
生成一个JWT令牌,并且必须在index.docker.io
的DockerHub调用中使用该令牌作为Bearer令牌。
当我们访问像这样的DockerHub API:https://index.docker.io/v2/library/alpine/tags/list
时,它返回401,其中包含缺少的航班前呼叫的信息。我们在失败的请求中寻找www-authenticate
响应头。
例:www-authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/alpine:pull",error="invalid_token"
这意味着,我们需要显式调用下面的API来获得auth令牌。
https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/alpine:pull
https://auth.docker.io/token
在没有公开回购的情况下工作。要访问私有回购,我们需要在请求中添加基本的http auth。
https://<username>:<password>@auth.docker.io/token?service=registry.docker.io&scope=repository:<repo>:pull
注意:auth.docker.io
将生成一个令牌,即使请求无效(无效的creds或作用域或任何东西)。为了验证令牌,我们可以解析JWT (例如:从jwt.io)并检查有效负载中的access
字段,它应该包含请求的范围引用。
发布于 2020-03-05 15:37:49
下面是一个从注册表读取存储库的示例程序。我用它作为码头中心的学习辅助工具。
#!/bin/bash
set -e
# set username and password
UNAME="username"
UPASS="password"
# get token to be able to talk to Docker Hub
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of repos for that user account
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}"
https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}"
https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output list of all docker images
for i in ${FULL_IMAGE_LIST}
do
echo ${i}
done
(这来自描述如何使用API的关于码头地点的文章。)
本质上..。
Authorization: JWT <token>
传递。https://hub.docker.com/v2/repositories/<username>/
。发布于 2019-08-22 07:27:18
本站说我们不能:
Dockerhub托管公共存储库和私有存储库,但不公开编录端点以编程方式列出它们。
https://stackoverflow.com/questions/56193110
复制相似问题