Docker Registry采用token认证实践

本文详细介绍了Docker Registry采用token认证的过程,包括从Client向Registry发起连接,Registry的未认证响应,再到Client与认证服务器的交互,最终完成认证。实践部分涉及认证服务的后端存储、Registry和Nginx代理的配置,以及测试环节的docker login、docker pull/push操作。
摘要由CSDN通过智能技术生成

token认证过程

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

图解

1.docker client 尝试到registry中进行push/pull操作;
2.registry会返回401未认证信息给client(未认证的前提下),同时返回的信息中还包含了到哪里去认证的信息;
3.client发送认证请求到认证服务器(authorization service);
4.认证服务器(authorization service)返回token;
5.client携带这附有token的请求,尝试请求registry;
6.registry接受了认证的token并且使得client继续操作;

详细介绍6个步骤

Step 1,Client 向registry 发起连接

通常,Docker Client在进行pull/push操作时,会先尝试连接docker registry。
Note: 当你访问远程的registry时,会用到tls验证域名的有效性(证书),否则会出现如下错误:

FATA[0000] Error response from daemon: v1 ping attempt failed with error:
Get https://registry.example.com/v1/_ping: tls: oversized record received with length 20527. 
If this private registry supports only HTTP or HTTPS with an unknown CA certificate,please add 
`--insecure-registry registry.example.com` to the daemon's arguments.
In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag;
simply place the CA certificate at /etc/docker/certs.d/registry.example.com/ca.crt

在docker的启动中加入下面的命令,来忽略对registry域名证书的审核:

--insecure-registry registry.example.com

Step 2,未认证响应(Unauthorized response)

Registry server会返回401并且会附带Authentication endpoint:

$ curl https://registry.example.com/v2 -k -IL
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.7
Date: Sun, 22 Nov 2015 09:01:42 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Docker-Distribution-Api-Version: registry/2.0
Location: /v2/

HTTP/1.1 401 Unauthorized
Server: nginx/1.4.7
Date: Sun, 22 Nov 2015 09:01:42 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 87
Connection: keep-alive
Docker-Distribution-Api-Version: registry/2.0
Www-Authenticate: Bearer realm="https://registry.example.com:5001/auth",service="Docker registry"
X-Content-Type-Options: nosniff

Authentication server返回的头信息中包含了如何去或许token,它有几个重要的查询参数:
realm: 描述了认证的enpoint。
realm=“https://registry.example.com:5001/auth

service: 描述了持有资源服务器的名称。
service=“Docker registry”

scope: 描述了client端操作资源的方法(push/pull)
scope=“repository:shuyun/hello:push”

account: 描述了操作的账号
account=admin

Step 3&4,认证endpoint通讯

这2步描述了client与认证服务2者的验证过程,需要明确的是,你需要知道:client发送请求到认证服务器签署token,请求信息中包含的基本身份验证信息将于服务器中的用户列表做匹配,然后根据请求中的scope要操作的范围、方法进而进行匹配,最后服务器匹配成功后将token进行签名,并且将token返回给客户端。

Step 5&6,最后沟通

Client尝试与registry连接(这次带了token信息),registry接到请求后验证token,继而开始pull/push操作。

方案

开源auth_server认证服务
认证服务后端存储采用mongodb
registry都采用nginx代理
四个组件都通过容器化部署

实践

目录层级说明

在这里插入图片描述

CA创建

CA部分是为了实现TLS,实现镜像层文件的安全传输,我们需要创建一份根CA和多份不同域名的CA证书。

在这里插入图片描述

mongodb存储

Mongodb作为DB,存储用户认证和访问权限相关信息。为什么用mongodb,而不用已有的mysql,因为我们使用了一个开源的token认证服务,该开源工具可以采用mongodb作为后端存储,但是不支持mysql。
后续,我们还会采用到最新发布的navicat premium 12,新增了mongodb客户端连接工具,使用navicat向mongodb存储一些测试数据。

mongodb容器启动配置

容器化启动compose配置如下:

  mongodb:
    image: mongo:3.2
    expose:
      - "27017"
    network_mode: "host"
    volumes:
      - /data/mongodb:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=mongoadmin
      - MONGO_INITDB_ROOT_PASSWORD=mongoadmin
    container_name: mongodb
    restart: always

建库并写入数据

1. 新建集合
users
registry_acl
2. 添

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值