Skip to content

Commit

Permalink
Do not reveal sensitive Data
Browse files Browse the repository at this point in the history
Allow Passwords to be of Puppet-Datatype Sensitive.
Return Sensitive, if Passwords were provided as Sensitive.
  • Loading branch information
cocker-cc committed May 22, 2024
1 parent dcd89b5 commit 8d6ee44
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 29 deletions.
12 changes: 6 additions & 6 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ Default value: `'0600'`

##### <a name="-openssl--certificate--x509--password"></a>`password`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

private key password. undef means no passphrase
will be used to encrypt private key.
Expand Down Expand Up @@ -953,7 +953,7 @@ Default value: `$title`

##### <a name="-openssl--export--pem_cert--in_pass"></a>`in_pass`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

PFX password

Expand Down Expand Up @@ -997,15 +997,15 @@ Default value: `present`

##### <a name="-openssl--export--pem_key--in_pass"></a>`in_pass`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

PFX container password

Default value: `undef`

##### <a name="-openssl--export--pem_key--out_pass"></a>`out_pass`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

PEM key password

Expand Down Expand Up @@ -1055,15 +1055,15 @@ Default value: `present`

##### <a name="-openssl--export--pkcs12--in_pass"></a>`in_pass`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

Private key password

Default value: `undef`

##### <a name="-openssl--export--pkcs12--out_pass"></a>`out_pass`

Data type: `Optional[String]`
Data type: `Optional[Variant[Sensitive[String], String]]`

PKCS12 password

Expand Down
6 changes: 5 additions & 1 deletion lib/puppet/provider/ssl_pkey/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def self.generate_key(resource)
def self.to_pem(resource, key)
if resource[:password]
cipher = OpenSSL::Cipher.new('des3')
key.to_pem(cipher, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(key.to_pem(cipher, resource[:password].unwrap))
else
key.to_pem(cipher, resource[:password])
end
else
key.to_pem
end
Expand Down
24 changes: 20 additions & 4 deletions lib/puppet/provider/x509_cert/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ def self.private_key(resource)
file = File.read(resource[:private_key])
case resource[:authentication]
when :dsa
OpenSSL::PKey::DSA.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::DSA.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::DSA.new(file, resource[:password])
end
when :rsa
OpenSSL::PKey::RSA.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::RSA.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::RSA.new(file, resource[:password])
end
when :ec
OpenSSL::PKey::EC.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::EC.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::EC.new(file, resource[:password])
end
else
raise Puppet::Error,
"Unknown authentication type '#{resource[:authentication]}'"
Expand Down Expand Up @@ -99,7 +111,11 @@ def create
'-out', resource[:path]
]
end
options << ['-passin', "pass:#{resource[:password]}"] if resource[:password]
if resource[:password].respond_to?(:unwrap)
options << ['-passin', "pass:#{resource[:password].unwrap}"]
elsif resource[:password]
options << ['-passin', "pass:#{resource[:password]}"]
end
options << ['-extensions', 'v3_req'] if resource[:req_ext] != :false
openssl options
end
Expand Down
24 changes: 20 additions & 4 deletions lib/puppet/provider/x509_request/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ def self.private_key(resource)
file = File.read(resource[:private_key])
case resource[:authentication]
when :dsa
OpenSSL::PKey::DSA.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::DSA.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::DSA.new(file, resource[:password])
end
when :rsa
OpenSSL::PKey::RSA.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::RSA.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::RSA.new(file, resource[:password])
end
when :ec
OpenSSL::PKey::EC.new(file, resource[:password])
if resource[:password].respond_to?(:unwrap)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(OpenSSL::PKey::EC.new(file, resource[:password].unwrap))
else
OpenSSL::PKey::EC.new(file, resource[:password])
end
else
raise Puppet::Error,
"Unknown authentication type '#{resource[:authentication]}'"
Expand Down Expand Up @@ -45,7 +57,11 @@ def create
'-out', resource[:path]
]

options << ['-passin', "pass:#{resource[:password]}"] if resource[:password]
if resource[:password]&.respond_to?(:unwrap)

Check failure on line 60 in lib/puppet/provider/x509_request/openssl.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Lint/RedundantSafeNavigation: Redundant safe navigation detected.
options << ['-passin', "pass:#{resource[:password].unwrap}"]
elsif resource[:password]
options << ['-passin', "pass:#{resource[:password]}"]
end
options << ['-nodes'] unless resource[:encrypted]

openssl options
Expand Down
2 changes: 1 addition & 1 deletion manifests/certificate/x509.pp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
Variant[String, Integer] $key_owner = $owner,
Variant[String, Integer] $key_group = $group,
Stdlib::Filemode $key_mode = '0600',
Optional[String] $password = undef,
Optional[Variant[Sensitive[String], String]] $password = undef,
Boolean $force = true,
Boolean $encrypted = true,
Optional[Stdlib::Absolutepath] $ca = undef,
Expand Down
7 changes: 4 additions & 3 deletions manifests/export/pem_cert.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Stdlib::Absolutepath $pem_cert = $title,
Optional[Stdlib::Absolutepath] $pfx_cert = undef,
Optional[Stdlib::Absolutepath] $der_cert = undef,
Optional[String] $in_pass = undef,
Optional[Variant[Sensitive[String], String]] $in_pass = undef,

) {
#local variables
Expand All @@ -40,6 +40,7 @@
$module_opt = ''
}

$is_sensitive = ($in_pass =~ Sensitive)
$passin_opt = $in_pass ? {
undef => '',
default => "-nokeys -passin pass:'${in_pass}'",
Expand All @@ -52,10 +53,10 @@
"-in ${in_cert}",
"-out ${pem_cert}",
$passin_opt,
]
].join(' ')

exec { "Export ${in_cert} to ${pem_cert}":
command => inline_template('<%= @cmd.join(" ") %>'),
command => if $is_sensitive { Sensitive($cmd) } else { $cmd },
path => $facts['path'],
creates => $pem_cert,
}
Expand Down
9 changes: 5 additions & 4 deletions manifests/export/pem_key.pp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
Stdlib::Absolutepath $pfx_cert,
Stdlib::Absolutepath $pem_key = $title,
Enum['present', 'absent'] $ensure = present,
Optional[String] $in_pass = undef,
Optional[String] $out_pass = undef,
Optional[Variant[Sensitive[String], String]] $in_pass = undef,
Optional[Variant[Sensitive[String], String]] $out_pass = undef,
) {
if $ensure == 'present' {
$is_sensitive = ($in_pass =~ Sensitive or $out_pass =~ Sensitive)
$passin_opt = $in_pass ? {
undef => '',
default => "-passin pass:'${in_pass}'",
Expand All @@ -36,10 +37,10 @@
'-nocerts',
$passin_opt,
$passout_opt,
]
].join(' ')

exec { "Export ${pfx_cert} to ${pem_key}":
command => inline_template('<%= @cmd.join(" ") %>'),
command => if $is_sensitive { Sensitive($cmd) } else { $cmd },
path => $facts['path'],
creates => $pem_key,
}
Expand Down
13 changes: 7 additions & 6 deletions manifests/export/pkcs12.pp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
Stdlib::Absolutepath $cert,
Enum['present', 'absent'] $ensure = present,
Optional[String] $chaincert = undef,
Optional[String] $in_pass = undef,
Optional[String] $out_pass = undef,
Optional[Variant[Sensitive[String], String]] $in_pass = undef,
Optional[Variant[Sensitive[String], String]] $out_pass = undef,
) {
if $ensure == 'present' {
$is_sensitive = ($in_pass =~ Sensitive or $out_pass =~ Sensitive)
$pass_opt = $in_pass ? {
undef => '',
default => "-passin pass:${in_pass}",
default => "-passin pass:${in_pass.unwrap}",
}

$passout_opt = $out_pass ? {
undef => '',
default => "-passout pass:${out_pass}",
default => "-passout pass:${out_pass.unwrap}",
}

$chain_opt = $chaincert ? {
Expand All @@ -50,10 +51,10 @@
$chain_opt,
$pass_opt,
$passout_opt,
]
].join(' ')

exec { "Export ${name} to ${basedir}/${name}.p12":
command => inline_template('<%= @cmd.join(" ") %>'),
command => if $is_sensitive { Sensitive($cmd) } else { $cmd },
path => $facts['path'],
creates => "${basedir}/${name}.p12",
}
Expand Down

0 comments on commit 8d6ee44

Please sign in to comment.