curl 访问 private registry 的 api 获取所有镜像

    司用 harbar(版本 v1.2.2, v2.x 的版本没有这个问题,v2.3.4 上面测试过滴) 搭建了 docker 的 registry,想一目了然的知道有哪些镜像,但是又不想登录到 harbar 的 ui 在 n 个项目中一个一个看 , 故采用 curl 命令来参看所有的镜像,结果告知 UNAUTHORIZED, 用户名和密码不用怀疑,肯定对的哈。

$ curl -s --user "$U" 172.30.3.149/v2/_catalog  | jq
{
  "errors": [
    {
      "code": "UNAUTHORIZED",
      "message": "authentication required",
      "detail": [
        {
          "Type": "registry",
          "Class": "",
          "Name": "catalog",
          "Action": "*"
        }
      ]
    }
  ]
}

查阅了 Token Authentication Specification ,终找到解决之道。

$ curl -i -s --user "$U" 172.30.3.149/v2/_catalog 
HTTP/1.1 401 Unauthorized
Server: nginx/1.11.13
Date: Thu, 28 Oct 2021 10:04:39 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 145
Connection: keep-alive
Docker-Distribution-Api-Version: registry/2.0
Set-Cookie: beegosessionID=430baef41ded23499b5147002b854089; Path=/; HttpOnly
Www-Authenticate: Bearer realm="http://172.30.3.149/service/token",service="harbor-registry",scope="registry:catalog:*"

{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"registry","Class":"","Name":"catalog","Action":"*"}]}]}

原来认证的地址是 Www-Authenticate: Bearer realm="http://172.30.3.149/service/token",service="harbor-registry",scope="registry:catalog:*",那重新拼接一下 URL 呗。

url="http://172.30.3.149/service/token?service=harbor-registry&scope=registry:catalog:*"

# 重新拼接 url
$ url="http://172.30.3.149/service/token?service=harbor-registry&scope=registry:catalog:*"
# 获取 token
$ curl  -s --user "$U" $url
{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlBMWE06V0tSSDpJNVVHOkJNSDU6WUgyRjpSQlNIOlJYQVY6SzRCRjpXWVhOOkpJSzY6RjRRRjpJQzNSIn0.eyJpc3MiOiJoYXJib3ItdG9rZW4taXNzdWVyIiwic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

token 在手, registry 的一切我有。

$ curl -s -H "Authorization: Bearer $T" 172.30.3.149/v2/_catalog
### 此处省略镜像输出,但是可以看到肯定是有镜像的。
$ curl -s  -H "Authorization: Bearer $T" 172.30.3.149/v2/_catalog | jq | wc -l
100

啊哈。到此为止了吧,目的已经达到了,也可以用获取到的 token 去访问任意的 registry api 哟。

你可能感兴趣的:(docker,容器,运维,linux,私有仓库)