Puppet Class: docker_registry_ha::web

Defined in:
modules/docker_registry_ha/manifests/web.pp

Summary

This class sets up nginx to proxy and provide access control

Overview

SPDX-License-Identifier: Apache-2.0

in front of a docker-registry.

There are 3 groups of users:

  • restricted-push - can push any image

  • restricted-read - can read restricted images

  • regular-push - can push non-restricted images

TODO: Refactor this to be a flexible ACL system, similar to etcd::tlsproxy

Parameters:

  • ci_restricted_user_password (String)

    password used by restricted ci

  • kubernetes_user_password (String)

    password used by kubernetes

  • ci_build_user_password (String)

    password for ci build

  • prod_build_user_password (String)

    password for production build user

  • password_salt (String)

    passowrd salt

  • allow_push_from (Array[Stdlib::Host])

    a list of hosts allowed to push

  • ssl_settings (Array[String])

    an array of ssl settings

  • ssl_paths (Hash) (defaults to: undef)

    the ssl file paths to use

  • jwt_allowed_ips (Array[Stdlib::IP::Address::Nosubnet]) (defaults to: [])

    A list of ips allowd to use jwt

  • jwt_keys_url (Stdlib::HTTPUrl) (defaults to: 'https://gitlab.wikimedia.org/oauth/discovery/keys')

    The jwt keys url

  • jwt_issuers (Array[String]) (defaults to: ['https://gitlab.wikimedia.org'])

    The jwt keys issuer

  • read_only_mode (Boolean) (defaults to: false)

    enable readonly mode

  • homepage (String) (defaults to: '/srv/homepage')

    the homepage doc root

  • nginx_cache (Boolean) (defaults to: true)

    enable nginx cache

  • deployment_hosts (Array[Stdlib::Host]) (defaults to: [])

    list of deployment hosts

  • kubernetes_hosts (Array[Stdlib::Host]) (defaults to: [])

    list of kubernetes hosts



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'modules/docker_registry_ha/manifests/web.pp', line 29

class docker_registry_ha::web (
    String                               $ci_restricted_user_password,
    String                               $kubernetes_user_password,
    String                               $ci_build_user_password,
    String                               $prod_build_user_password,
    String                               $password_salt,
    Array[Stdlib::Host]                  $allow_push_from,
    Array[String]                        $ssl_settings,
    Hash                                 $ssl_paths            = undef,
    Array[Stdlib::IP::Address::Nosubnet] $jwt_allowed_ips      = [],
    Stdlib::HTTPUrl                      $jwt_keys_url         = 'https://gitlab.wikimedia.org/oauth/discovery/keys',
    Array[String]                        $jwt_issuers          = ['https://gitlab.wikimedia.org'],
    Boolean                              $read_only_mode       = false,
    String                               $homepage             = '/srv/homepage',
    Boolean                              $nginx_cache          = true,
    Array[Stdlib::Host]                  $deployment_hosts     = [],
    Array[Stdlib::Host]                  $kubernetes_hosts     = [],
) {

    # Legacy credentials
    file { '/etc/nginx/htpasswd.registry':
        ensure => absent,
    }

    # Push access to /restricted/
    $restricted_push_file = '/etc/nginx/restricted-push.htpasswd';
    $ci_restricted_user_hash = htpasswd($ci_restricted_user_password, $password_salt);
    file { $restricted_push_file:
        content => "ci-restricted:${ci_restricted_user_hash}",
        owner   => 'www-data',
        group   => 'www-data',
        mode    => '0440',
        before  => Service['nginx'],
        require => Package['nginx-common'],
    }

    # Read access to /restricted/
    $restricted_read_file = '/etc/nginx/restricted-read.htpasswd';
    $kubernetes_user_hash = htpasswd($kubernetes_user_password, $password_salt);
    file { $restricted_read_file:
        content => "kubernetes:${kubernetes_user_hash}\nci-restricted:${ci_restricted_user_hash}",
        owner   => 'www-data',
        group   => 'www-data',
        mode    => '0440',
        before  => Service['nginx'],
        require => Package['nginx-common'],
    }

    # Push access to /
    $regular_push_file = '/etc/nginx/regular-push.htpasswd';
    $ci_build_user_hash = htpasswd($ci_build_user_password, $password_salt);
    $prod_build_user_hash = htpasswd($prod_build_user_password, $password_salt);
    file { $regular_push_file:
        content => "ci-build:${ci_build_user_hash}\nprod-build:${prod_build_user_hash}\nci-restricted:${ci_restricted_user_hash}",
        owner   => 'www-data',
        group   => 'www-data',
        mode    => '0440',
        before  => Service['nginx'],
        require => Package['nginx-common'],
    }

    # Find k8s nodes that have auth credentials (for restricted/)
    $k8s_authenticated_nodes = Hash($kubernetes_hosts.map |$host| { [$host, ipresolve($host, 4)]}.sort)

    # Create a directory for nginx cache if enabled
    if $nginx_cache {
        $cache_dir_ensure = directory
        $cache_config_ensure = file
    } else {
        $cache_dir_ensure = absent
        $cache_config_ensure = absent
    }
    $nginx_cache_dir = '/var/cache/nginx-docker-registry'
    file { $nginx_cache_dir:
        ensure => $cache_dir_ensure,
        owner  => 'www-data',
        group  => 'www-data',
        mode   => '0775',
    }

    file {'/etc/nginx/registry-nginx-cache.conf':
        ensure  => $cache_config_ensure,
        mode    => '0744',
        owner   => 'root',
        group   => 'root',
        source  => 'puppet:///modules/docker_registry_ha/registry-nginx-cache.conf',
        require => Package['nginx-common'],
    }

    file { '/etc/nginx/nginx.conf':
        ensure  => present,
        source  => 'puppet:///modules/docker_registry_ha/nginx.conf',
        owner   => 'root',
        group   => 'root',
        mode    => '0644',
        before  => Service['nginx'],
        require => Package['nginx-common'],
    }

    # Create a separate cache and socket location for internal auth_request
    # subrequests (see templates/registry.nginx.conf.erb)
    $nginx_auth_cache_dir = '/var/cache/nginx-auth'
    file { $nginx_auth_cache_dir:
        ensure => directory,
        owner  => 'www-data',
        group  => 'www-data',
        mode   => '0700',
    }

    $nginx_auth_socket_dir = '/var/run/nginx-auth'
    $nginx_auth_socket = "${nginx_auth_socket_dir}/basic.sock"
    file { $nginx_auth_socket_dir:
        ensure => directory,
        owner  => 'www-data',
        group  => 'www-data',
        mode   => '0700',
    }

    # Add a systemctl override that will allow us to clean up the nginx auth socket
    # when we stop nginx.
    # See https://trac.nginx.org/nginx/ticket/753
    systemd::unit { 'nginx':
        ensure   => present,
        content  => "[Service]\nExecStopPost=/usr/bin/rm -f ${nginx_auth_socket}",
        restart  => true,
        override => true,
    }

    # If we're allowing any hosts to use JSON Web Token auth, provision the
    # JWT authenticator service.
    $jwt_authorizer_socket = "${nginx_auth_socket_dir}/jwt.sock"
    if (!empty($jwt_allowed_ips)) {
        $jwt_authorizer_ensure = 'present'
    } else {
        $jwt_authorizer_ensure = 'absent'
    }

    jwt_authorizer::service { 'docker-registry-ha-jwt':
        ensure              => $jwt_authorizer_ensure,
        listen              => "unix://${jwt_authorizer_socket}",
        owner               => 'www-data',
        group               => 'www-data',
        keys_url            => $jwt_keys_url,
        issuers             => $jwt_issuers,
        validation_template => 'puppet:///modules/docker_registry_ha/jwt-validations.tmpl',
    }

    profile::auto_restarts::service { 'docker-registry-ha-jwt':
        ensure => $jwt_authorizer_ensure,
    }

    nginx::site { 'registry':
        content => template('docker_registry_ha/registry-nginx.conf.erb'),
    }

    ensure_packages(['python3-docker-report'])

    file { '/usr/local/bin/registry-homepage-builder':
        mode    => '0744',
        owner   => 'root',
        group   => 'root',
        source  => 'puppet:///modules/docker_registry_ha/registry-homepage-builder.py',
        require => Package['python3-docker-report'],
    }

    file { '/usr/local/lib/registry-homepage-builder.css':
        mode   => '0644',
        owner  => 'root',
        group  => 'root',
        source => 'puppet:///modules/docker_registry_ha/registry-homepage-builder.css',
    }

    file { $homepage:
        ensure => directory,
        owner  => 'root',
        group  => 'root',
        mode   => '0755',
    }

    # Spread out jobs so they don't all run at the same time, leading to 504s from the registry
    $minute = Integer(seeded_rand(60, "${::fqdn}-build-homepage"))
    systemd::timer::job {'build-homepage':
        ensure      => 'present',
        description => 'Build docker-registry homepage',
        command     => "/usr/local/bin/registry-homepage-builder localhost:5000 ${homepage}",
        user        => 'root',
        interval    => {
            'start'    => 'OnCalendar',
            'interval' => "*-*-* *:${minute}:00", # every hour
        },
        require     => File['/usr/local/bin/registry-homepage-builder'],
    }
}