美文网首页
Docker registry 关于Nginx认证代理

Docker registry 关于Nginx认证代理

作者: zerolinke | 来源:发表于2016-07-12 14:51 被阅读580次

Nginx 认证代理

用例

人们已经习惯依赖nginx代理去认证他们的用户和服务并且想要Registry的通信业通过这个方式。

通常,包含了后端LDAP/AD和SSO机制为了他们的互联网http门户。

备选方案

如果你仅仅想要认证你的registry,并且很乐于去维护用户的访问级别,你真的应该考虑下registry原生的基本认证功能。

解决方法

用这里介绍的方法,你要实现docker engines的基本认证在你的registry前。

我们用一个简单的htpasswd文件作为一个简单的例子,其它的nginx认证后端应该很容易实现这样的事情。

我们还要实现对push的限制(限制为一个用户群体)。

限制

虽然这样的模式给你能力去使用任何你想要的认证后端通过代理服务器内部实现二次认证机制,但是同样这也需要你移除TLS从Registry自身。

此外,引入一个外部的http层在你的通信管道将使得部署维护调试更加复杂,而且可能产生一些问题。

例如,Amazon的Elastic Load Balancer(ELB)在HTTPS模式下已经设置了下面的客户端头部:

X-Real-IP
X-Forwarded-For
X-Forwarded-Proto

所以如果你有一个nginx在它之后,应该移除下面配置的这些:

X-Real-IP         $remote_addr; # pass on real client's IP
X-Forwarded-For   $proxy_add_x_forwarded_for;
X-Forwarded-Proto $scheme;

否则,nginx将重置ELB的值,请求将不会被正确的路由。更多信息,参考#970.

配置

在此读一下这些需求。

准备好了?

--

创建需要的目录

mkdir -p auth
mkdir -p data

创建你将用到的主要的nginx配置。

cat <<EOF > auth/nginx.conf
events {
    worker_connections  1024;
}

http {

  upstream docker-registry {
    server registry:5000;
  }

  ## Set a variable to help us decide if we need to add the
  ## 'Docker-Distribution-Api-Version' header.
  ## The registry always sets this header.
  ## In the case of nginx performing auth, the header will be unset
  ## since nginx is auth-ing before proxying.
  map \$upstream_http_docker_distribution_api_version \$docker_distribution_api_version {
    'registry/2.0' '';
    default registry/2.0;
  }

  server {
    listen 443 ssl;
    server_name myregistrydomain.com;

    # SSL
    ssl_certificate /etc/nginx/conf.d/domain.crt;
    ssl_certificate_key /etc/nginx/conf.d/domain.key;

    # Recommendations from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;

    # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
    chunked_transfer_encoding on;

    location /v2/ {
      # Do not allow connections from docker 1.5 and earlier
      # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
      if (\$http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*\$" ) {
        return 404;
      }

      # To add basic authentication to v2 use auth_basic setting.
      auth_basic "Registry realm";
      auth_basic_user_file /etc/nginx/conf.d/nginx.htpasswd;

      ## If $docker_distribution_api_version is empty, the header will not be added.
      ## See the map directive above where this variable is defined.
      add_header 'Docker-Distribution-Api-Version' \$docker_distribution_api_version always;

      proxy_pass                          http://docker-registry;
      proxy_set_header  Host              \$http_host;   # required for docker client's sake
      proxy_set_header  X-Real-IP         \$remote_addr; # pass on real client's IP
      proxy_set_header  X-Forwarded-For   \$proxy_add_x_forwarded_for;
      proxy_set_header  X-Forwarded-Proto \$scheme;
      proxy_read_timeout                  900;
    }
  }
}
EOF

现在创建密码文件为 “testuser”和“testpassword”

docker run --rm --entrypoint htpasswd registry:2 -bn testuser testpassword > auth/nginx.htpasswd

复制你的证书文件

cp domain.crt auth
cp domain.key auth

现在创建你的compose文件

cat <<EOF > docker-compose.yml
nginx:
  image: "nginx:1.9"
  ports:
    - 5043:443
  links:
    - registry:registry
  volumes:
    - ./auth:/etc/nginx/conf.d
    - ./auth/nginx.conf:/etc/nginx/nginx.conf:ro

registry:
  image: registry:2
  ports:
    - 127.0.0.1:5000:5000
  volumes:
    - `pwd`./data:/var/lib/registry
EOF

启动和停止

现在,启动你的stack:

docker-compose up -d

使用一个合法的”push“用户登陆(使用testuser和testpassword),然后标识并推送你的第一个镜像:

docker login -u=testuser -p=testpassword -e=root@example.ch myregistrydomain.com:5043
docker tag ubuntu myregistrydomain.com:5043/test
docker push myregistrydomain.com:5043/test
docker pull myregistrydomain.com:5043/test

相关文章

网友评论

      本文标题:Docker registry 关于Nginx认证代理

      本文链接:https://www.haomeiwen.com/subject/eixsjttx.html