Skip to content

Commit 5c3d6e4

Browse files
authored
Merge pull request #266 from florindragos/docker_registry
Support private docker registries on windows
2 parents beb786f + c8436f9 commit 5c3d6e4

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

manifests/registry.pp

+64-21
Original file line numberDiff line numberDiff line change
@@ -47,55 +47,98 @@
4747

4848
$docker_command = $docker::params::docker_command
4949

50+
if $::osfamily == 'windows' {
51+
$exec_environment = ['PATH=C:/Program Files/Docker/']
52+
$exec_timeout = 3000
53+
$exec_path = ['c:/Windows/Temp/', 'C:/Program Files/Docker/']
54+
$exec_provider = 'powershell'
55+
$password_env = '$env:password'
56+
$exec_user = undef
57+
} else {
58+
$exec_environment = ['HOME=/root']
59+
$exec_path = ['/bin', '/usr/bin']
60+
$exec_timeout = 0
61+
$exec_provider = undef
62+
$password_env = "\${password}"
63+
$exec_user = $local_user
64+
}
65+
5066
if $ensure == 'present' {
5167
if $username != undef and $password != undef and $email != undef and $version != undef and $version =~ /1[.][1-9]0?/ {
52-
$auth_cmd = "${docker_command} login -u '${username}' -p \"\${password}\" -e '${email}' ${server}"
68+
$auth_cmd = "${docker_command} login -u '${username}' -p '${password_env}' -e '${email}' ${server}"
5369
$auth_environment = "password=${password}"
5470
}
5571
elsif $username != undef and $password != undef {
56-
$auth_cmd = "${docker_command} login -u '${username}' -p \"\${password}\" ${server}"
72+
$auth_cmd = "${docker_command} login -u '${username}' -p '${password_env}' ${server}"
5773
$auth_environment = "password=${password}"
5874
}
5975
else {
6076
$auth_cmd = "${docker_command} login ${server}"
61-
$auth_environment = undef
77+
$auth_environment = ''
6278
}
6379
}
6480
else {
6581
$auth_cmd = "${docker_command} logout ${server}"
66-
$auth_environment = undef
82+
$auth_environment = ''
6783
}
6884

85+
$docker_auth = "${title}${auth_environment}${auth_cmd}${local_user}"
6986
if $receipt {
70-
# no - with pw_hash
71-
$local_user_strip = regsubst($local_user, '-', '', 'G')
72-
73-
$_pass_hash = $pass_hash ? {
74-
Undef => pw_hash("${title}${auth_environment}${auth_cmd}${local_user}", 'SHA-512', $local_user_strip),
75-
default => $pass_hash
76-
}
7787

7888
# server may be an URI, which can contain /
7989
$server_strip = regsubst($server, '/', '_', 'G')
80-
$_auth_command = "${auth_cmd} || rm -f \"/root/registry-auth-puppet_receipt_${server_strip}_${local_user}\""
8190

82-
file { "/root/registry-auth-puppet_receipt_${server_strip}_${local_user}":
83-
ensure => $ensure,
84-
content => $_pass_hash,
85-
notify => Exec["${title} auth"],
91+
if $::osfamily != 'windows' {
92+
# no - with pw_hash
93+
$local_user_strip = regsubst($local_user, '-', '', 'G')
94+
95+
$_pass_hash = $pass_hash ? {
96+
Undef => pw_hash($docker_auth, 'SHA-512', $local_user_strip),
97+
default => $pass_hash
98+
}
99+
$_auth_command = "${auth_cmd} || rm -f \"/root/registry-auth-puppet_receipt_${server_strip}_${local_user}\""
100+
101+
file { "/root/registry-auth-puppet_receipt_${server_strip}_${local_user}":
102+
ensure => $ensure,
103+
content => $_pass_hash,
104+
notify => Exec["${title} auth"],
105+
}
106+
} else {
107+
$_auth_command = $auth_cmd
108+
$pw_hash_path = 'C:/Windows/Temp/compute_hash.ps1'
109+
$passfile = "C:/Windows/Temp/registry-auth-puppet_receipt_${server_strip}_${local_user}"
110+
file{ $pw_hash_path:
111+
ensure => present,
112+
force => true,
113+
content => template('docker/windows/compute_hash.ps1.erb'),
114+
notify => Exec['compute-hash']
115+
}
116+
exec { 'compute-hash':
117+
command => "& ${pw_hash_path}",
118+
provider => $exec_provider,
119+
refreshonly => true,
120+
logoutput => true,
121+
notify => Exec["${title} auth"],
122+
}
86123
}
87124
}
88125
else {
89126
$_auth_command = $auth_cmd
90127
}
91128

129+
if $auth_environment != '' {
130+
$exec_env = concat($exec_environment, $auth_environment, "docker_auth=${docker_auth}")
131+
} else {
132+
$exec_env = concat($exec_environment, "docker_auth=${docker_auth}")
133+
}
134+
92135
exec { "${title} auth":
93-
environment => $auth_environment,
136+
environment => $exec_env,
94137
command => $_auth_command,
95-
user => $local_user,
96-
cwd => '/root',
97-
path => ['/bin', '/usr/bin'],
98-
timeout => 0,
138+
user => $exec_user,
139+
path => $exec_path,
140+
timeout => $exec_timeout,
141+
provider => $exec_provider,
99142
refreshonly => $receipt,
100143
}
101144

spec/defines/registry_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@
4040

4141
context 'with ensure => present and username => user1, and password => secret and email => [email protected]' do
4242
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => '[email protected]', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
43-
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_environment('password=secret') }
43+
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p '${password}' localhost:5000").with_environment(/password=secret/) }
4444
end
4545

4646
context 'with ensure => present and username => user1, and password => secret and email => [email protected] and version < 1.11.0' do
4747
let(:params) { { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => '[email protected]', 'version' => '1.9.0', 'pass_hash' => 'test1234', 'receipt' => false } }
48-
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" -e '[email protected]' localhost:5000").with_environment('password=secret') }
48+
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p '${password}' -e '[email protected]' localhost:5000").with_environment(/password=secret/) }
4949
end
5050

5151
context 'with username => user1, and password => secret' do
5252
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
53-
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_environment('password=secret') }
53+
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p '${password}' localhost:5000").with_environment(/password=secret/) }
5454
end
5555

5656
context 'with username => user1, and password => secret and local_user => testuser' do
5757
let(:params) { { 'username' => 'user1', 'password' => 'secret', 'local_user' => 'testuser', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false } }
58-
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p \"${password}\" localhost:5000").with_user('testuser').with_environment('password=secret') }
58+
it { should contain_exec('localhost:5000 auth').with_command("docker login -u 'user1' -p '${password}' localhost:5000").with_user('testuser').with_environment(/password=secret/) }
5959
end
6060

6161
context 'with an invalid ensure value' do
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#file computes the 512SHA for a given string and writes it to a file
2+
3+
$String = $env:docker_auth
4+
$HashName = "SHA512"
5+
$StringBuilder = New-Object System.Text.StringBuilder
6+
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{
7+
[Void]$StringBuilder.Append($_.ToString("x2"))
8+
}
9+
10+
$StringBuilder.ToString() | Out-File <%= @passfile %>

0 commit comments

Comments
 (0)