Skip to content
Merged
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`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note here that Sensitive is Linux-only (and not usable with email on Docker <= 1.10), that Deferred values aren't supported, and that the password is briefly visible in the process list during login (see my comment on registry.pp:93).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2474d69: Linux-only, no email on Docker <= 1.10, no Deferred, and the brief ps exposure.

(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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#789 is only partly addressed.

Variant[String, Sensitive[String]] still rejects Sensitive[Deferred], and $username (L44) is unchanged. #789 is about Deferred(...) and Sensitive(Deferred(...)). pw_hash and the interpolation in this define run at compile time, so Deferred can't work in this design. Please reword "Addresses #789" in the description to say this covers Sensitive literal and Hiera values only, and mention the Deferred limitation in the param docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Reworded the description to "Partially addresses #789" (Sensitive literal/Hiera values only) and documented in the @param password docs and README that Deferred isn't supported (2474d69).

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')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Sensitive on Windows exposes the password.

$_sensitive is only "Sensitive and not Windows". On Windows $_password is unwrapped (L58) and goes into $exec_env (L112-113), so it lands in the catalog in clear text. Reproduced locally: environment => [..., "password=TOPSECRET", "docker_auth=...password=TOPSECRET..."]. The docs say Windows is handled like a plain String, but a user who passes Sensitive(...) will expect it to stay out of the catalog.

The same variable also causes the email + Docker <= 1.10 problem (see my comment on L109). Suggest failing loudly rather than silently leaking or dropping the password:

Suggested change
$_sensitive = ($password =~ Sensitive) and ($facts['os']['family'] != 'windows')
$_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)')
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch. Cherry-picked your commit as 85ea052: a Sensitive password now fails on Windows instead of leaking into the exec environment.


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}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs: the password is visible in ps for the duration of the login.

With printf '%s' '...' | docker login, the password is in the sh -c argv, so other local users can see it in ps. The old env var was only readable by the owner. This is still much better than PuppetDB and reports, but worth one sentence in the @param password docs (L15-21) and the README.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in the @param password docs and the README in 2474d69.

$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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: the password is dropped from the environment on the legacy email path.

With a Sensitive password, email and Docker <= 1.10, the first branch (L87-89) still builds -p "${password_env}" and sets auth_environment, but if $_sensitive { $exec_env = $exec_environment } here drops password=.... Reproduced on Ubuntu: environment => [] with command docker login -u 'u' -p "${password}" -e 'a@b.c' example.com. ${password} expands to an empty string, so the login fails or runs with an empty password.

The PR description says this path behaves exactly as before, which isn't true for a Sensitive password. The guard suggested on L59 covers it; the alternative is to keep Sensitive off this branch and unwrap like before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by the same guard in 85ea052: Sensitive + email on Docker <= 1.10 now fails instead of running with an empty password. I've also corrected the PR description.

# $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)' => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test gaps: this Sensitive case runs with receipt => false and also runs on Windows.

  • L71 uses 'receipt' => false, so the Sensitive file content (registry.pp:135) is never exercised.
  • This case also runs on every Windows platform, so it will start failing once the Windows guard (comment on registry.pp:59) is added. Skipping Sensitive cases on Windows inside the tests.each loop fixes that:
      tests.each do |title, local_params|
        # Sensitive passwords are rejected on Windows (see registry_sensitive_spec.rb)
        next if os.include?('windows') && local_params['password'].respond_to?(:unwrap)

The new spec/defines/registry_sensitive_spec.rb in the linked commit covers the two guards and receipt => true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered by your commit (85ea052): Windows is skipped for Sensitive cases and registry_sensitive_spec.rb adds the guard and receipt => true specs. I've left the stale shared-example branch for a follow-up, as you suggested.

'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