-
Notifications
You must be signed in to change notification settings - Fork 311
/
registry.pp
168 lines (155 loc) · 5.68 KB
/
registry.pp
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
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
# Define a custom function for deferred
function docker::deferred_password($password) {
# Add your logic here
# Example: return encrypt_function($password)
return $password
}
# @summary
# Module to configure private docker registries from which to pull Docker images
#
# @param server
# The hostname and port of the private Docker registry. Ex: dockerreg:5000
#
# @param ensure
# Whether or not you want to login or logout of a repository
#
# @param username
# Username for authentication to private Docker registry.
# auth is not required.
#
# @param password
# Password for authentication to private Docker registry. Leave undef if
# auth is not required.
#
# @param pass_hash
# The hash to be used for receipt. If left as undef, a hash will be generated
#
# @param email
# Email for registration to private Docker registry. Leave undef if
# auth is not required.
#
# @param local_user
# The local user to log in as. Docker will store credentials in this
# users home directory
#
# @param local_user_home
# The local user home directory.
#
# @param receipt
# Required to be true for idempotency
#
# @param version
#
define docker::registry (
Optional[String] $server = $title,
Enum[present,absent] $ensure = 'present',
Optional[String] $username = undef,
Optional[String] $password = undef,
Optional[String] $pass_hash = undef,
Optional[String] $email = undef,
String $local_user = 'root',
Optional[String] $local_user_home = undef,
Optional[String] $version = $docker::version,
Boolean $receipt = true,
) {
include docker::params
$docker_command = $docker::params::docker_command
# Use Deferred to encrypt the password
$deferred_password = Deferred('docker::deferred_password', [$password])
if $facts['os']['family'] == 'windows' {
$exec_environment = ["PATH=${facts['docker_program_files_path']}/Docker/",]
$exec_timeout = 3000
$exec_path = ["${facts['docker_program_files_path']}/Docker/",]
$exec_provider = 'powershell'
$password_env = '$env:password'
$exec_user = undef
} else {
$exec_environment = []
$exec_path = ['/bin', '/usr/bin',]
$exec_timeout = 0
$exec_provider = undef
$password_env = "\${password}"
$exec_user = $local_user
if $local_user_home {
$_local_user_home = $local_user_home
} else {
# set sensible default
$_local_user_home = ($local_user == 'root') ? {
true => '/root',
default => "/home/${local_user}",
}
}
}
if $ensure == 'present' {
if $username != undef and $deferred_password != undef and $email != undef and $version != undef and $version =~ /1[.][1-9]0?/ {
$auth_cmd = "${docker_command} login -u '${username}' -p \"${password_env}\" -e '${email}' ${server}"
$auth_environment = "password=${deferred_password}"
} elsif $username != undef and $deferred_password != undef {
$auth_cmd = "${docker_command} login -u '${username}' -p \"${password_env}\" ${server}"
$auth_environment = "password=${deferred_password}"
} else {
$auth_cmd = "${docker_command} login ${server}"
$auth_environment = ''
}
} else {
$auth_cmd = "${docker_command} logout ${server}"
$auth_environment = ''
}
$docker_auth = "${title}${auth_environment}${auth_cmd}${local_user}"
if $auth_environment != '' {
$exec_env = concat($exec_environment, $auth_environment, "docker_auth=${docker_auth}")
} else {
$exec_env = concat($exec_environment, "docker_auth=${docker_auth}")
}
if $receipt {
if $facts['os']['family'] != 'windows' {
# server may be an URI, which can contain /
$server_strip = regsubst($server, '/', '_', 'G')
# no - with pw_hash
$local_user_strip = regsubst($local_user, '[-_]', '', 'G')
$_pass_hash = $pass_hash ? {
Undef => pw_hash($docker_auth, 'SHA-512', $local_user_strip),
default => $pass_hash
}
$_auth_command = "${auth_cmd} || (rm -f \"/${_local_user_home}/registry-auth-puppet_receipt_${server_strip}_${local_user}\"; exit 1;)"
file { "/${_local_user_home}/registry-auth-puppet_receipt_${server_strip}_${local_user}":
ensure => $ensure,
content => $_pass_hash,
owner => $local_user,
group => $local_user,
notify => Exec["${title} auth"],
}
} else {
# server may be an URI, which can contain /
$server_strip = regsubst($server, '[/:]', '_', 'G')
$passfile = "${facts['docker_user_temp_path']}/registry-auth-puppet_receipt_${server_strip}_${local_user}"
$_auth_command = "if (-not (${auth_cmd})) { Remove-Item -Path ${passfile} -Force -Recurse -EA SilentlyContinue; exit 1 } else { exit 0 }" # lint:ignore:140chars
if $ensure == 'absent' {
file { $passfile:
ensure => $ensure,
notify => Exec["${title} auth"],
}
} elsif $ensure == 'present' {
exec { 'compute-hash':
command => template('docker/windows/compute_hash.ps1.erb'),
environment => $exec_env,
provider => $exec_provider,
logoutput => true,
unless => template('docker/windows/check_hash.ps1.erb'),
notify => Exec["${title} auth"],
}
}
}
} else {
$_auth_command = $auth_cmd
}
exec { "${title} auth":
environment => $exec_env,
command => $_auth_command,
user => $exec_user,
path => $exec_path,
timeout => $exec_timeout,
provider => $exec_provider,
refreshonly => $receipt,
}
}