Showing with 29 additions and 3 deletions.
  1. +1 −1 Modulefile
  2. +28 −2 lib/puppet/provider/java_ks/keytool.rb
2 changes: 1 addition & 1 deletion Modulefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name 'puppetlabs-java_ks'
version '0.0.4'
version '0.0.5'
source 'https://github.com/puppetlabs/puppetlabs-java_ks.git'
author 'puppetlabs'
license 'ASL 2.0'
Expand Down
30 changes: 28 additions & 2 deletions lib/puppet/provider/java_ks/keytool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,27 @@ def create
'-importcert', '-noprompt',
'-alias', @resource[:name],
'-file', @resource[:certificate],
'-keystore', @resource[:target]
]
cmd << '-trustcacerts' if @resource[:trustcacerts] == :true

# The tmpfile will store input for the keytool command, and its path
# will be used as a basis for the temptarget path if needed
tmpfile = Tempfile.new("#{@resource[:name]}.")
if File.exists?(@resource[:target])

# In the event that a file exists but is zero length, the java keytool
# will explode spectacularly. Should the target be empty we work around
# this by using a temp file which we will later write into the empty
# target.
if File.zero?(@resource[:target])
temptarget = tmpfile.path + "#{@resource[:target].gsub('/', '_')}."
cmd << '-keystore' << temptarget
else
temptarget = false
cmd << '-keystore' << @resource[:target]
end

# Run the command with appropriate input
if File.exists?(@resource[:target]) and not temptarget
tmpfile.write(@resource[:password])
else
tmpfile.write("#{@resource[:password]}\n#{@resource[:password]}")
Expand All @@ -149,6 +165,16 @@ def create
:combine => true
)
tmpfile.close!

# If necessary, copy the generated keystore to the specified target
# (occurs if the target previously existed but was a zero-length file)
# and delete the temporary target file
if temptarget
File.open(@resource[:target], 'w') do |target|
target.write(File.read(temptarget))
end
File.delete(temptarget)
end
end
end

Expand Down