Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reveal sensitive Data #187

Closed
wants to merge 1 commit into from
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: 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]
Copy link
Member

Choose a reason for hiding this comment

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

For this block I was thinking about refactoring it (prior to your change):

cls = case resource[:authentication]
      when :dsa
        OpenSSL::PKey::DSA
      when :rsa
        OpenSSL::PKey::RSA
      when :ec
        OpenSSL::PKey::EC
      else
        raise
      end

cls.new(file, resource[:password])

Then with your change it becomes

cls = case resource[:authentication]
      when :dsa
        OpenSSL::PKey::DSA
      when :rsa
        OpenSSL::PKey::RSA
      when :ec
        OpenSSL::PKey::EC
      else
        raise
      end

if resource[:password].respond_to?(:unwrap)
  Puppet::Pops::Types::PSensitiveType::Sensitive.new(cls.new(file, resource[:password].unwrap))
else
  cls.new(file, resource[:password])
end

But realistically, why don't we use OpenSSL::Pkey.read(file, resource[:password]) to let OpenSSL do the heavy lifting? Is that method unavailable in older versions? If so, how old?

Copy link
Member

Choose a reason for hiding this comment

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

#189 does that.

Copy link
Author

Choose a reason for hiding this comment

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

I am no Expert with OpenSSL. I suggest to leave that Change in #189

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 @@
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 @@
'-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
Loading