Skip to content

Commit 09afa5d

Browse files
committed
Update install and remove actions of java_certificate to function on Windows
1 parent f2e340a commit 09afa5d

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This file is used to list changes made in each version of the Java cookbook.
44

55
## Unreleased
66

7+
- Update `install` and `remove` actions of `java_certificate` to function on Windows
8+
79
## 12.1.1 - *2024-12-05*
810

911
## 12.1.0 - *2024-12-03*

resources/certificate.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565

6666
cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -list #{keystore_argument} -storepass #{new_resource.keystore_passwd} -rfc -alias \"#{new_resource.cert_alias}\"")
6767
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
6971

7072
keystore_cert_digest = keystore_cert.empty? ? nil : OpenSSL::Digest::SHA512.hexdigest(OpenSSL::X509::Certificate.new(keystore_cert).to_der)
7173
certfile_digest = OpenSSL::Digest::SHA512.hexdigest(OpenSSL::X509::Certificate.new(certdata).to_der)
@@ -109,13 +111,16 @@
109111
action :remove do
110112
keystore_argument = keystore_argument(new_resource.java_version, new_resource.cacerts, new_resource.keystore_path)
111113

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}\"")
113115
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
117122

118-
if has_key
123+
if alias_exists
119124
converge_by("remove certificate #{new_resource.cert_alias} from #{new_resource.keystore_path}") do
120125
cmd = Mixlib::ShellOut.new("#{new_resource.java_home}/bin/keytool -delete -alias \"#{new_resource.cert_alias}\" #{keystore_argument} -storepass #{new_resource.keystore_passwd}")
121126
cmd.run_command

0 commit comments

Comments
 (0)