summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNimesh-Msys <nimesh.patni@msystechnologies.com>2019-02-13 17:55:05 +0530
committerTim Smith <tsmith@chef.io>2019-03-04 10:00:04 -0800
commit4fd1eeb3ac1cd9fe57b318cee7425929a17f128a (patch)
tree79faf86aa5255378846c3c2cee42c13e266efc65
parentcc111ec75b22d0bd94301aa164a73a0734838473 (diff)
downloadchef-4fd1eeb3ac1cd9fe57b318cee7425929a17f128a.tar.gz
Minor fixes as per the review comments.
- Checking file formats by uisng ruby itself, instead of shelling it out. Signed-off-by: Nimesh-Msys <nimesh.patni@msystechnologies.com>
-rw-r--r--lib/chef/resource/windows_certificate.rb37
1 files changed, 20 insertions, 17 deletions
diff --git a/lib/chef/resource/windows_certificate.rb b/lib/chef/resource/windows_certificate.rb
index b5926da92b..ca838fd6dd 100644
--- a/lib/chef/resource/windows_certificate.rb
+++ b/lib/chef/resource/windows_certificate.rb
@@ -21,6 +21,7 @@ require "chef/util/path_helper"
require "chef/resource"
require "win32-certstore" if Chef::Platform.windows?
require "openssl"
+require "open3"
class Chef
class Resource
@@ -276,19 +277,19 @@ class Chef
# Uses powershell command to convert crt/der/cer/pfx & p7b certificates
# In PEM format and returns its certificate content
def convert_pem(ext)
- out = case ext
- when ".crt", ".cer", ".der"
- command = "openssl x509 -text -in #{new_resource.source} -outform PEM"
- command += " -inform DER" if binary_cert?
- powershell_out(command)
- when ".pfx"
- powershell_out("openssl pkcs12 -in #{new_resource.source} -nodes -passin pass:'#{new_resource.pfx_password}'")
- when ".p7b"
- powershell_out("openssl pkcs7 -print_certs -in #{new_resource.source} -outform PEM")
- else
- powershell_out("openssl x509 -text -inform #{ext.delete(".")} -in #{new_resource.source} -outform PEM")
- end
-
+ command = case ext
+ when ".crt", ".cer", ".der"
+ cmd = "openssl x509 -text -in #{new_resource.source} -outform PEM"
+ pem_cert? ? cmd : cmd + " -inform DER"
+ when ".pfx"
+ "openssl pkcs12 -in #{new_resource.source} -nodes -passin pass:'#{new_resource.pfx_password}'"
+ when ".p7b"
+ "openssl pkcs7 -print_certs -in #{new_resource.source} -outform PEM"
+ else
+ "openssl x509 -text -inform #{ext.delete('.')} -in #{new_resource.source} -outform PEM"
+ end
+
+ out = powershell_out(command)
if out.exitstatus == 0
format_raw_out(out.stdout)
else
@@ -303,12 +304,14 @@ class Chef
begin_cert + out[/#{begin_cert}(.*?)#{end_cert}/m, 1] + end_cert
end
- # Checks if the certificate is binary encoded or not
- def binary_cert?
- powershell_out("file -b --mime-encoding #{new_resource.source}").stdout.strip == "binary"
+ # Checks if the given certificate is a PEM certificate or not
+ def pem_cert?
+ details, status = Open3.capture2e("file", new_resource.source)
+ return false unless status.success?
+
+ details.rpartition(":").last.strip == "PEM certificate"
end
end
-
end
end
end