|
65 | 65 |
|
66 | 66 | cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -list #{keystore_argument} -storepass #{new_resource.keystore_passwd} -rfc -alias \"#{new_resource.cert_alias}\"")
|
67 | 67 | cmd.run_command
|
68 |
| - keystore_cert = cmd.stdout.match(/^[-]+BEGIN.*END(\s|\w)+[-]+$/m).to_s |
| 68 | + normalized_stdout = cmd.stdout |
| 69 | + normalized_stdout = normalized_stdout.gsub(/\r\n/, "\n") if platform?('windows') |
| 70 | + keystore_cert = normalized_stdout.match(/^[-]+BEGIN.*END(\s|\w)+[-]+$/m).to_s |
69 | 71 |
|
70 | 72 | keystore_cert_digest = keystore_cert.empty? ? nil : OpenSSL::Digest::SHA512.hexdigest(OpenSSL::X509::Certificate.new(keystore_cert).to_der)
|
71 | 73 | certfile_digest = OpenSSL::Digest::SHA512.hexdigest(OpenSSL::X509::Certificate.new(certdata).to_der)
|
|
109 | 111 | action :remove do
|
110 | 112 | keystore_argument = keystore_argument(new_resource.java_version, new_resource.cacerts, new_resource.keystore_path)
|
111 | 113 |
|
112 |
| - cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -list #{keystore_argument} -storepass #{new_resource.keystore_passwd} -v | grep \"#{new_resource.cert_alias}\"") |
| 114 | + cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -list #{keystore_argument} -storepass #{new_resource.keystore_passwd} -v -alias \"#{new_resource.cert_alias}\"") |
113 | 115 | cmd.run_command
|
114 |
| - has_key = !cmd.stdout[/Alias name: #{new_resource.cert_alias}/].nil? |
115 |
| - does_not_exist = cmd.stdout[/Alias <#{new_resource.cert_alias}> does not exist/].nil? |
116 |
| - Chef::Application.fatal!("Error querying keystore for existing certificate: #{cmd.exitstatus}", cmd.exitstatus) unless (cmd.exitstatus == 0) || does_not_exist |
| 116 | + alias_exists = !cmd.stdout[/Alias name: #{new_resource.cert_alias}/].nil? |
| 117 | + alias_missing = !cmd.stdout[/Alias <#{new_resource.cert_alias}> does not exist/].nil? |
| 118 | + # Only raise if command failed AND alias wasn't just missing |
| 119 | + if cmd.exitstatus != 0 && !alias_missing |
| 120 | + Chef::Application.fatal!("Error querying keystore for existing certificate: #{cmd.exitstatus}", cmd.exitstatus) |
| 121 | + end |
117 | 122 |
|
118 |
| - if has_key |
| 123 | + if alias_exists |
119 | 124 | converge_by("remove certificate #{new_resource.cert_alias} from #{new_resource.keystore_path}") do
|
120 | 125 | cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -delete -alias \"#{new_resource.cert_alias}\" #{keystore_argument} -storepass #{new_resource.keystore_passwd}")
|
121 | 126 | cmd.run_command
|
|
0 commit comments