From 966c5806bf815370772da652f220f1975c4f3304 Mon Sep 17 00:00:00 2001 From: Jason St-Cyr Date: Wed, 23 Sep 2026 11:08:46 -0400 Subject: [PATCH] (TESTS-1066) docker::registry: accept Sensitive password Co-authored-by: Andreas Scholdan --- README.md | 10 ++++++++++ manifests/registry.pp | 34 +++++++++++++++++++++++--------- spec/defines/registry_spec.rb | 8 ++++++++ spec/shared_examples/registry.rb | 11 +++++++++-- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f6081d5cf..7d3c09442 100644 --- a/README.md +++ b/README.md @@ -922,6 +922,16 @@ docker::registry_auth::registries: version: '' ``` +To keep the password out of the catalog stored in PuppetDB and out of reports, pass it as `Sensitive` +(on Linux it is then supplied via `docker login --password-stdin` from a Sensitive exec command): + +```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: diff --git a/manifests/registry.pp b/manifests/registry.pp index a40cc8eb6..6741ec109 100644 --- a/manifests/registry.pp +++ b/manifests/registry.pp @@ -13,7 +13,12 @@ # # @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. On +# Windows (and for Docker <= 1.10 with `email`) the value is unwrapped and +# handled like a plain String. # # @param pass_hash # The hash to be used for receipt. If left as undef, a hash will be generated @@ -38,7 +43,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', @@ -50,6 +55,9 @@ $docker_command = $docker::params::docker_command + $_password = if $password =~ Sensitive { $password.unwrap } else { $password } + $_sensitive = ($password =~ Sensitive) and ($facts['os']['family'] != 'windows') + if $facts['os']['family'] == 'windows' { $exec_environment = ["PATH=${facts['docker_program_files_path']}/Docker/",] $exec_timeout = 3000 @@ -76,12 +84,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 = '' @@ -93,7 +106,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}") @@ -116,7 +132,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"], @@ -149,7 +165,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, diff --git a/spec/defines/registry_spec.rb b/spec/defines/registry_spec.rb index e30688c09..b0afef357 100644 --- a/spec/defines/registry_spec.rb +++ b/spec/defines/registry_spec.rb @@ -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', diff --git a/spec/shared_examples/registry.rb b/spec/shared_examples/registry.rb index df966185b..3377fa736 100644 --- a/spec/shared_examples/registry.rb +++ b/spec/shared_examples/registry.rb @@ -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/"] @@ -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}" @@ -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}" @@ -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,