Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,18 @@ docker::registry_auth::registries:
version: '<docker_version>'
```

To keep the password out of the catalog stored in PuppetDB and out of reports, pass it as `Sensitive`
(it is then supplied via `docker login --password-stdin` from a Sensitive exec command).
This is Linux only, cannot be combined with `email` on Docker <= 1.10, and does not support `Deferred` values.
The password is briefly visible to local users in the process list (`ps`) while `docker login` runs:

```puppet
docker::registry { 'example.docker.io:5000':
username => 'user',
password => Sensitive('secret'),
}
```

If using Docker V1.11 or later, the docker login email flag has been deprecated. See the [docker_change_log](https://docs.docker.com/release-notes/docker-engine/#1110-2016-04-13).

Add the following code to the manifest file:
Expand Down
44 changes: 35 additions & 9 deletions manifests/registry.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
#
# @param password
# Password for authentication to private Docker registry. Leave undef if
# auth is not required.
# auth is not required. May be given as Sensitive: on Linux the password is
# then fed to `docker login --password-stdin` from a Sensitive exec command
# instead of the exec environment, and the receipt hash is Sensitive too, so
# it is neither stored in the catalog (PuppetDB) nor shown in reports. A
# Sensitive password is rejected on Windows and together with `email` on
# Docker <= 1.10. `Deferred` values are not supported. During the login the
# password is briefly visible to local users in the process list (`ps`).
#
# @param pass_hash
# The hash to be used for receipt. If left as undef, a hash will be generated
Expand All @@ -38,7 +44,7 @@
Optional[String] $server = $title,
Enum[present,absent] $ensure = 'present',
Optional[String] $username = undef,
Optional[String] $password = undef,
Optional[Variant[String, Sensitive[String]]] $password = undef,
Optional[String] $pass_hash = undef,
Optional[String] $email = undef,
String $local_user = 'root',
Expand All @@ -50,6 +56,18 @@

$docker_command = $docker::params::docker_command

$_password = if $password =~ Sensitive { $password.unwrap } else { $password }
$_sensitive = ($password =~ Sensitive) and ($facts['os']['family'] != 'windows')

if $password =~ Sensitive {
if $facts['os']['family'] == 'windows' {
fail('docker::registry: a Sensitive password is not supported on Windows (it would be exposed in the exec environment)')
}
if $email != undef and $version != undef and $version =~ /1[.][1-9]0?/ {
fail('docker::registry: a Sensitive password cannot be combined with `email` on Docker <= 1.10 (it needs the password in the environment)')
}
}

if $facts['os']['family'] == 'windows' {
$exec_environment = ["PATH=${facts['docker_program_files_path']}/Docker/",]
$exec_timeout = 3000
Expand All @@ -76,12 +94,17 @@
}

if $ensure == 'present' {
if $username != undef and $password != undef and $email != undef and $version != undef and $version =~ /1[.][1-9]0?/ {
if $username != undef and $_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=${password}"
} elsif $username != undef and $password != undef {
$auth_environment = "password=${_password}"
} elsif $username != undef and $_password != undef and $_sensitive {
# Password only in the (Sensitive) command, never in the environment.
$_password_quoted = regsubst($_password, "'", "'\\\\''", 'G')
$auth_cmd = "printf '%s' '${_password_quoted}' | ${docker_command} login -u '${username}' --password-stdin ${server}"
$auth_environment = ''
} elsif $username != undef and $_password != undef {
$auth_cmd = "${docker_command} login -u '${username}' -p \"${password_env}\" ${server}"
$auth_environment = "password=${password}"
$auth_environment = "password=${_password}"
} else {
$auth_cmd = "${docker_command} login ${server}"
$auth_environment = ''
Expand All @@ -93,7 +116,10 @@

$docker_auth = "${title}${auth_environment}${auth_cmd}${local_user}"

if $auth_environment != '' {
if $_sensitive {
# $docker_auth embeds the password; only needed at runtime on Windows.
$exec_env = $exec_environment
} elsif $auth_environment != '' {
$exec_env = concat($exec_environment, $auth_environment, "docker_auth=${docker_auth}")
} else {
$exec_env = concat($exec_environment, "docker_auth=${docker_auth}")
Expand All @@ -116,7 +142,7 @@

file { "/${_local_user_home}/registry-auth-puppet_receipt_${server_strip}_${local_user}":
ensure => $ensure,
content => $_pass_hash,
content => if $_sensitive { Sensitive($_pass_hash) } else { $_pass_hash },
owner => $local_user,
group => $local_user,
notify => Exec["${title} auth"],
Expand Down Expand Up @@ -149,7 +175,7 @@

exec { "${title} auth":
environment => Deferred('docker::env', [$exec_env]),
command => Deferred('sprintf', [$_auth_command]),
command => if $_sensitive { Sensitive($_auth_command) } else { Deferred('sprintf', [$_auth_command]) },
user => $exec_user,
path => $exec_path,
timeout => $exec_timeout,
Expand Down
42 changes: 42 additions & 0 deletions spec/defines/registry_sensitive_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'docker::registry', type: :define do
ubuntu_facts = on_supported_os.find { |os, _| os.start_with?('ubuntu-22') }.last
windows_os_facts = on_supported_os.find { |os, _| os.start_with?('windows-2022') }.last
let(:title) { 'example.com' }
let(:base) { { 'username' => 'u', 'password' => RSpec::Puppet::Sensitive.new('pw'), 'pass_hash' => 'h', 'receipt' => false } }

context 'on Ubuntu with email and Docker 1.10' do
let(:facts) { ubuntu_facts }
let(:pre_condition) { 'class { "docker": }' }
let(:params) { base.merge('email' => 'a@b.c', 'version' => '1.10') }

it { is_expected.to compile.and_raise_error(%r{cannot be combined with `email`}) }
end

context 'on Ubuntu with receipt => true' do
let(:facts) { ubuntu_facts }
let(:pre_condition) { 'class { "docker": }' }
let(:params) { base.merge('version' => '17.06', 'receipt' => true) }

it { is_expected.to compile }
it { is_expected.to contain_file('//root/registry-auth-puppet_receipt_example.com_root').with_content(sensitive('h')) }

it {
is_expected.to contain_exec('example.com auth').with(
'environment' => [],
'command' => sensitive(%q(printf '%s' 'pw' | docker login -u 'u' --password-stdin example.com || (rm -f "//root/registry-auth-puppet_receipt_example.com_root"; exit 1;))),
)
}
end

context 'on Windows' do
let(:facts) { windows_facts.merge('docker_home_dirs' => { 'root' => '/root' }).merge(windows_os_facts) }
let(:pre_condition) { 'class { "docker": docker_ee => true }' }
let(:params) { base }

it { is_expected.to compile.and_raise_error(%r{not supported on Windows}) }
end
end
11 changes: 11 additions & 0 deletions spec/defines/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
'pass_hash' => 'test1234',
'receipt' => false
},
'with username => user1, and password => Sensitive(secret)' => {
'ensure' => 'present',
'username' => 'user1',
'password' => RSpec::Puppet::Sensitive.new("se'cret"),
'version' => '17.06',
'pass_hash' => 'test1234',
'receipt' => false
},
'with username => user1, and password => secret and local_user => testuser' => {
'username' => 'user1',
'password' => 'secret',
Expand Down Expand Up @@ -106,6 +114,9 @@

context "on #{os}" do
tests.each do |title, local_params|
# Sensitive passwords are rejected on Windows (see guard specs below)
next if os.include?('windows') && local_params['password'].respond_to?(:unwrap)

context title do
params = {
'server' => title,
Expand Down
13 changes: 10 additions & 3 deletions spec/shared_examples/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
receipt = params['receipt']

docker_command = defaults['docker_command']
sensitive_pw = password.respond_to?(:unwrap) && facts[:os]['family'] != 'windows'
password = password.unwrap if password.respond_to?(:unwrap)

if facts[:os]['family'] == 'windows'
exec_environment = ["PATH=#{facts['docker_program_files_path']}/Docker/"]
Expand All @@ -34,6 +36,9 @@
if username != :undef && password != :undef && email != :undef && version != :undef && version =~ %r{1[.][1-9]0?}
auth_cmd = "#{docker_command} login -u '#{username}' -p \"#{password_env}\" -e '#{email}' #{server}"
auth_environment = "password=#{password}"
elsif username != :undef && password != :undef && sensitive_pw
auth_cmd = "printf '%s' '#{password.gsub("'", "'\\\\''")}' | #{docker_command} login -u '#{username}' --password-stdin #{server}"
auth_environment = ''
elsif username != :undef && password != :undef
auth_cmd = "#{docker_command} login -u '#{username}' -p \"#{password_env}\" #{server}"
auth_environment = "password=#{password}"
Expand All @@ -48,7 +53,9 @@

docker_auth = "#{title}#{auth_environment}#{auth_cmd}#{local_user}"

exec_env = if auth_environment == ''
exec_env = if sensitive_pw
exec_environment
elsif auth_environment == ''
exec_environment << "docker_auth=#{docker_auth}"
else
exec_environment << auth_environment << "docker_auth=#{docker_auth}"
Expand Down Expand Up @@ -97,7 +104,7 @@
it {
expect(subject).to contain_file('/${local_user_home}/registry-auth-puppet_receipt_${server_strip}_${local_user}').with(
'ensure' => ensure_value,
'content' => _pass_hash,
'content' => sensitive_pw ? sensitive(_pass_hash) : _pass_hash,
'owner' => local_user,
'group' => local_user,
).that_notifies(
Expand All @@ -112,7 +119,7 @@
it {
expect(subject).to contain_exec("#{title} auth").with(
'environment' => exec_env,
'command' => auth_command,
'command' => sensitive_pw ? sensitive(auth_command) : auth_command,
'user' => exec_user,
'path' => exec_path,
'timeout' => exec_timeout,
Expand Down
Loading