0
votes

I have some struggle about the converge/compile phase of a chef recipe.

I want to update/create a java keystore, only_if the cert provided in a vault is different that the one in the keystore.

So I write temporary file from the vault on the system, use a ruby_block to compute the md5, and use this md5 on the only_if condition on the execute block that manage the keystore.

Here is the last code I have tried :

ruby_block "get private cert md5" do
 block do
   vault_md5 = Mixlib::ShellOut.new("openssl x509 -in /tmp/mycerrt.crt -fingerprint -md5 | head -1 | sed -e 's/MD5 Fingerprint=//'")
   vault_md5.run_command
   # get dynamically the only_if statement to update it.
   exec_r = run_context.resource_collection.find(:execute => "create p12 store")
   exec_r.only_if "[ \"#{cur_md5.stdout}\" -ne \"#{vault_md5.stdout}\" ]"
 end
end

execute "create p12 store" do
        command "openssl pkcs12 -export -in /tmp/mycerrt.crt -inkey /tmp/myKey -certfile /tmp/mycerrt.crt  -name priv -out /tmp/keystore.p12 -password pass:#{key['PrivateKeystorePassword']}"
        only_if "[ \"#{cur_md5.stdout}\" -eq '']"
        notifies :run, 'execute[convert keystore]', :immediately
        action :run
end

With this code the execute block if always skipped due to only_if. Thanks for your help

1
Mixlib::ShellOut doesn't need to be wrapped in a ruby_block, also where is cur_md5 run_command? - display name
Hi, I think in my case the Mixlib::ShellOut need to be wrapped because else it will be executed in the compile phase, and at this time the /tmp.mycerrt.crt file doesn't exist. the cur_md5 is run before and not wrapped in a ruby block because it test the keystore that always exist. - Damien Durant

1 Answers

0
votes

I find a solution to my issue, therefore I really don't know if it's the best ways to handle this kind of problematic.

Instead of dynamically try to change the only_statement, I focus on the run.

ruby_block "get private cert md5" do
 block do
   vault_md5 = Mixlib::ShellOut.new("openssl x509 -in /tmp/cert.crt -fingerprint -md5 | head -1 | sed -e 's/MD5 Fingerprint=//'")
   vault_md5.run_command
   exec_r = run_context.resource_collection.find(:execute => "create p12 store")
   if ( cur_md5.stdout != vault_md5.stdout )
      exec_r.action "run"
   end
 end

As result the execute blocks are driver by this ruby_block which could handle the compile phase generated data (here my local file generated)