Skip to content

Commit

Permalink
Moves back to using Tempfile.
Browse files Browse the repository at this point in the history
  Move back to Tempfile because Puppet::Util::FileType wasn't buying
  anything and made the provider not funciton on Windows.  Tests
  adjusted acordingly.
  • Loading branch information
ody committed May 4, 2012
1 parent de78d42 commit 34efb72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
35 changes: 21 additions & 14 deletions lib/puppet/provider/java_ks/keytool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ def to_pkcs12
'-inkey', @resource[:private_key],
'-name', @resource[:name]
]
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
tmpfile.write(@resource[:password])
tmpfile.flush
output = Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmpfile.remove
tmpfile.close!
return output
end

# Where we actually to the import of the file created using to_pkcs12.
def import_ks
tmppk12 = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmppk12 = Tempfile.new("#{@resource[:name]}.")
tmppk12.write(to_pkcs12)
tmppk12.flush
cmd = [
command(:keytool),
'-importkeystore', '-srcstoretype', 'PKCS12',
Expand All @@ -40,20 +42,21 @@ def import_ks
'-alias', @resource[:name]
]
cmd << '-trustcacerts' if @resource[:trustcacerts] == :true
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
if File.exists?(@resource[:target])
tmpfile.write("#{@resource[:password]}\n#{@resource[:password]}")
else
tmpfile.write("#{@resource[:password]}\n#{@resource[:password]}\n#{@resource[:password]}")
end
tmpfile.flush
Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmppk12.remove
tmpfile.remove
tmppk12.close!
tmpfile.close!
end

def exists?
Expand All @@ -64,15 +67,16 @@ def exists?
'-alias', @resource[:name]
]
begin
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
tmpfile.write(@resource[:password])
tmpfile.flush
Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmpfile.remove
tmpfile.close!
return true
rescue
return false
Expand Down Expand Up @@ -100,15 +104,16 @@ def current
'-keystore', @resource[:target],
'-alias', @resource[:name]
]
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
tmpfile.write(@resource[:password])
tmpfile.flush
output = Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmpfile.remove
tmpfile.close!
current = output.scan(/Certificate fingerprint \(MD5\): (.*)/)[0][0]
return current
end
Expand All @@ -129,19 +134,20 @@ def create
'-keystore', @resource[:target]
]
cmd << '-trustcacerts' if @resource[:trustcacerts] == :true
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
if File.exists?(@resource[:target])
tmpfile.write(@resource[:password])
else
tmpfile.write("#{@resource[:password]}\n#{@resource[:password]}")
end
tmpfile.flush
Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmpfile.remove
tmpfile.close!
end
end

Expand All @@ -152,15 +158,16 @@ def destroy
'-alias', @resource[:name],
'-keystore', @resource[:target]
]
tmpfile = Puppet::Util::FileType.filetype(:flat).new("/tmp/#{@resource[:name]}.#{rand(2 << 64)}")
tmpfile = Tempfile.new("#{@resource[:name]}.")
tmpfile.write(@resource[:password])
tmpfile.flush
Puppet::Util.execute(
cmd,
:stdinfile => tmpfile.path,
:failonfail => true,
:combine => true
)
tmpfile.remove
tmpfile.close!
end

# Being safe since I have seen some additions overwrite and some just throw errors.
Expand Down
7 changes: 4 additions & 3 deletions spec/provider/java_ks/keytool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
provider.stubs(:command).with(:keytool).returns('mykeytool')
provider.stubs(:command).with(:openssl).returns('myopenssl')

file = stub('file', :class => Puppet::Util::FileType::FileTypeFlat,
tempfile = stub('tempfile', :class => Tempfile,
:write => true,
:remove => true,
:flush => true,
:close! => true,
:path => '/tmp/testing.stuff'
)
Puppet::Util::FileType.filetype(:flat).stubs(:new).returns(file)
Tempfile.stubs(:new).returns(tempfile)
end

let(:resource) do
Expand Down

0 comments on commit 34efb72

Please sign in to comment.