How to build an controlled environment to distribute docker images based on user accounts

Docker itself, AWS (just to name the biggest docker hosts right now) and many more public / private repository servers are on the marked. But sometime there is need to host an own registry for docker images. One reason can be because we can, the other is for example to give individual pull / push rights to different images to different users and control the access also based on expiration dates.

Components and the big picture

For this setup we need several software components to work orchestrated together. Starting with the firewall to block all ports except the 443 for HTTPS, the nginxreverse proxy to terminate the SSL connection and protect the underlying services against direct access and also possible load balancing, the docker registry to host the images and at last but not least the docker token authenticator to identify users and give access to images (push and/or pull) based on their rights.

Docker introduced in the second version for the registry protocol the “Docker registry authentication scheme“. This basically transfers the access control to images to an outside system and uses the bearer token mechanism to communicate. The flow is to access an docker image is:

Docker daemon accesses the docker registry server as usual and gets a 401 Unauthorized in return with a “WWW-Authenticate” header pointing to the authentication server the registry server trusts.

Docker daemon contacts the authentication server with the given URL and the user identifies against the server.

The authentication server checks the access rights based on username, password, image name and access type (pull/push) and returns a bearer token signed with the private key.

Docker daemon accesses the docker registry again with the bearer token and the docker image request.

Docker registry server checks the bearer token based on the authentication server public key and grants access or doesn’t.

Firewall

Ubuntu ships with a very simple firewall control script called “Uncomplicated Firewall“. The script manages the iptable configuration and lets the user configure ports with a single line. If you access the server via SSH make sure you allow ssh access before you activate the firewall. I also recommend installing fail2ban to ban script hacking.

1

2

3

4

5

6

7

sudo apt update

sudo apt install-yufw fail2ban

ufw allow ssh#only necessary when you need remote access

ufw allow https

ufw allow http

ufw enable

ufw status

Nginx reverse proxy

We install Nginx also as a docker service because the update cycle is way faster compared to the software repository. The basic Nginx docker container is ready to be used and only needs the settings for http and https. Everything is handled via the https port but we also have http (port 80) open to have a redirect to https for everything with a 301 (moved permanently) return code.

1

2

3

4

5

6

7

8

FROM docker.io/nginx:latest

COPY default.conf/etc/nginx/conf.d/default.conf

COPY ssl.conf/etc/nginx/conf.d/ssl.conf

COPY cert/cert

EXPOSE80

EXPOSE443

This is a very simple Dockerfile to to add the ssl certificates and the http/https configuration. We could also mount the ssl and configuration in the docker-compose file and leave the images plain as it is. Both options are valid and just a flavour.

1

2

3

4

5

6

server{

listen80;

listen[::]:80;

server_name registry.23-5.euauth.23-5.eu;

return301https://$host$request_uri;

}

This is the http configuration for nginx. Accepting everything for http and returning a 301 (moved permanently) to the same server and path just with https.

SSL configuration

SSL configuration is a little bit more complicated as we also specify the ciphers and parameters for the encryption. As this topic is endless and very easy to screw up I personally relay on https://cipherli.st as a configuration source.

1

openssl dhparam-out dhparams.pem4096

The recommendation is to generate own Diffie–Hellman pool bigger than 2048 bit. This process can take a very long time. We add the result file together with our keys to the cert folder.

This configuration is based on the recommendation from cipherlist. Be aware one part of this setup is the Strict-Transport-Security with can cause a lot of long-time trouble if you mess it up. This completes the basic SSL setup.

And this configuration part for the registry server itself. Important here is the client_max_body_size parameter to make sure even bigger docker images are getting through. Older docker client versions getting a 404 because they can not be handled by the docker registry.

Lets encrypt

The easiest way to get a certificate is by using let’s encrypt. There are different ways how to receive a certificate, we just use a very simple one here with the standalone call. The certbot opens a mini web server on port 80 to handle the authentication request on its own. Therefore make sure the Nginx docker is not running.

Do the certificate request call for the auth and the registry certificate and copy the certificate and private key to your cert folder for the docker build to pick it up. Don’t forget the dhaprams.pem file.

Docker registry

Now as the server is configured and more or less secured, let’s configure the docker registry server and auth server. Docker inc. offers a docker registry docker container which is relatively easy to hande and to configure.

1

2

3

4

5

-REGISTRY_AUTH=token

-REGISTRY_AUTH_TOKEN_REALM=https://auth.23-5.eu/auth

-REGISTRY_AUTH_TOKEN_SERVICE="Docker registry"

-REGISTRY_AUTH_TOKEN_ISSUER="Acme auth server"

-REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE=/ssl/domain.crt

The configuration is done in the docker-compose file itself. The important information is the REALM, so the docker registry can redirect the client to the auth server with the issuer and the cert bundle from the referred auth server to check the bearer token later.

Docker Token Authenticator

Docker Inc. does not provide an auth server out of the box as done with the registry itself. This is basically left for the registry provider to build their own. Luckily Cesanta stepped up and build a nice configurable auth server to be used with the registry server. docker_auth has different ways of how to store information about the user.

Static list of users

Google Sign-In

Github Sign-In

LDAP bind

MongoDB user collection

External Program (gets login parameters and returns 0 or 1)

In our case the way to go is the MongoDB user collection as we can control for each user individually who has access to which image and easily change it on the fly by modifying the user data in the DB itself.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

server:# Server settings.

# Address to listen on.

addr:":5001"

token:

issuer:"Acme auth server"# Must match issuer in the Registry config.

expiration:900

certificate:"/ssl/domain.crt"

key:"/ssl/domain.key"

mongo_auth:

dial_info:

addrs:["authdb"]

timeout:"10s"

database:"23-5"

username:"ansi"

password_file:"/config/mongopass.txt"

enabled_tls:false

collection:"users"

acl_mongo:

dial_info:

addrs:["authdb"]

timeout:"10s"

database:"23-5"

username:"ansi"

password_file:"/config/mongopass.txt"

enabled_tls:false

collection:"acl"

cache_ttl:"10s"

This is the configuration file for the auth server. Mainly 4 parts.

Server

Witch port to listen on

Nginx handles the TLS termination, therefore, this server has no TLS handling.

Token

Use the same issuer as configured in the registry server itself and provide the certificate files for signing the bearer token.

Mongo_auth

Where the user information is stored, the password is saved in a simple ASCII file and how to access the MongoDB. In our case, as we are behind a firewall in a docker network we don’t use TLS to access thMongoDBDB.

ACL_Mongo

Beside the user information, the AccessControlList (ACL) can also be stored in a MongoDB. Same configuration as the mongo_auth but there is a cache information as this information is stored in memory and refreshed every 10 seconds.

The mongoDB was initialized by the docker-compose file with an admin user “root” and passwd “example”. We use this account to create a new database called “23-5” and set a new user there with username “ansi” and passwd “test”. This database stores all user and acls. The docker registry users by themselves are stored with an bencrypted password. and some labels. Bencrypt a passwd with:

1

2

sudo apt install apache2-tools

htpasswd-nB USERNAME

Beside username and password, we can also store labels of all kind to a given user. This allows us to use these labels for the ACLs again. So in our case, the ACLs defines all docker images with a given name (the name is stored in the label with read-only or full access) to access images based on their label. In our case, the user “waldi” has full access to all docker images with “test/*” and only read access to everything in “prod/*” but nothing else. ACLs have a seq number in which they were processed. The first patching ACL will be used.

Labels can be combined so for example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

ACL:

{

"match":{"name":"${labels:project}/${labels:group}-${labels:tier}"},

"actions":["push","pull"],

"comment":"Contrived multiple label match rule"

}

USER:

{

"username":"busy-guy",

"password":"$2y$05$B.x.......CbCGtjFl7S33aCUHNBxbq",

"labels":{

"group":[

"web",

"webdev"

],

"project":[

"website",

"api"

],

"tier":[

"frontend",

"backend"

]

}

}

Would give push and pull access to the docker image

1

website/webdev-backend

These variables can be checked for the ACL:

${account} the account name aka username

${name} the repository name “*” can be used. So for example “prod/*” gives access to “prod/server”

Generating bearer SSL key

In order to sign a bearer token we need a key. This can be a self signed key done with openssl:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

openssl req\

-newkey rsa:4096\

-days365\

-nodes-keyout domain.key\

-out domain.csr\

-subj"/C=EU/ST=Germany/L=Berlin/O=23-5/CN=auth.23-5.eu"

openssl x509\

-signkey domain.key\

-indomain.csr\

-req-days365-out domain.crt

openssl req\

-x509\

-nodes\

-days365\

-newkey rsa:2048\

-keyout server.key\

-out server.pem

Docker-compose

We can configure and start the auth and registry server and nginx with one docker-compose file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

version:'3'

services:

nginx:

restart:always

build:

context:nginx

ports:

-80:80

-443:443

mongoclient:

image:docker.io/mongoclient/mongoclient:latest

restart:always

depends_on:

-authdb

ports:

-3000:3000

environment:

-TZ=Europe/Berlin

-STARTUP_DELAY=1

authdb:

image:docker.io/mongo:4.1

restart:always

volumes:

-/root/auth_db:/data/db

environment:

-TZ=Europe/Berlin

-MONGO_INITDB_ROOT_USERNAME=root

-MONGO_INITDB_ROOT_PASSWORD=example

ports:

-27017:27017

command:--bind_ip0.0.0.0

dockerauth:

image:docker.io/cesanta/docker_auth:1

volumes:

-/root/auth_server/config:/config:ro

-/root/auth_server/ssl:/ssl:ro

command:--v=2--alsologtostderr/config/auth_config.yml

restart:always

environment:

-TZ=Europe/Berlin

registry:

image:docker.io/registry:2

volumes:

-/root/auth_server/ssl:/ssl:ro

-/root/docker_registry/data:/var/lib/registry

restart:always

environment:

-TZ=Europe/Berlin

-REGISTRY_AUTH=token

-REGISTRY_AUTH_TOKEN_REALM=https://auth.23-5.eu/auth

-REGISTRY_AUTH_TOKEN_SERVICE="Docker registry"

-REGISTRY_AUTH_TOKEN_ISSUER="Acme auth server"

-REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE=/ssl/domain.crt

I also added a mongoclient docker container to have easy access to the mongodb server. Please be aware this one is not secured by the nginx reverse proxy and is only for testing. You can also access the mongodb with command line: