Skip to content

Commit

Permalink
Update java location and invoke-webrequest for windows tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Fields committed Dec 23, 2014
1 parent 984a1cb commit d6071d6
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 38 deletions.
18 changes: 11 additions & 7 deletions lib/puppet/provider/java_ks/keytool.rb
Expand Up @@ -22,19 +22,23 @@ def to_pkcs12(path)
else
chain_certs = []
end
pkcs12 = OpenSSL::PKCS12.create(@resource[:password], @resource[:name], pkey, x509_cert, chain_certs)
pkcs12 = OpenSSL::PKCS12.create(get_password, @resource[:name], pkey, x509_cert, chain_certs)
File.open(path, "wb") { |f| f.print pkcs12.to_der }
end

def password_file
def get_password
if @resource[:password_file].nil?
pword = @resource[:password]
@resource[:password]
else
file = File.open(@resource[:password_file], "r")
pword = file.read
file.close
pword = pword.chomp
file = File.open(@resource[:password_file], "r")
pword = file.read
file.close
pword.chomp
end
end

def password_file
pword = get_password

tmpfile = Tempfile.new("#{@resource[:name]}.")
if File.exists?(@resource[:target]) and not File.zero?(@resource[:target])
Expand Down
7 changes: 4 additions & 3 deletions spec/acceptance/basic_spec.rb
Expand Up @@ -3,15 +3,16 @@
describe 'prep nodes', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
it 'requires java', :unless => ["Solaris", "AIX"].include?(fact('osfamily')) do
java_source = ENV['JAVA_DOWNLOAD_SOURCE'] || "http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-windows-x64.exe"
java_major, java_minor = (ENV['JAVA_VERSION'] || '7u67').split('u')
pp = <<-EOS
if $::osfamily !~ /windows/ {
class { 'java': }
} else {
windows_java::jdk{'JDK 7u67':
windows_java::jdk{'JDK #{java_major}u#{java_minor}':
ensure => 'present',
install_name => 'Java SE Development Kit 7 Update 67 (64-bit)',
install_name => 'Java SE Development Kit #{java_major} Update #{java_minor} (64-bit)',
source => '#{java_source}',
install_path => 'C:\\Java\\jdk1.7.0_67',
install_path => 'C:\\Java\\jdk1.#{java_major}.0_#{java_minor}',
jre_install_path => 'C:\\Java\\jre',
}
}
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/chain_key_spec.rb
Expand Up @@ -17,7 +17,7 @@
certificate => "#{@temp_dir}ca.pem",
chain => "#{@temp_dir}chain.pem",
private_key => "#{@temp_dir}privkey.pem",
password => 'testpass',
password => 'puppet',
path => #{@resource_path},
}
EOS
Expand All @@ -26,7 +26,7 @@
end

it 'verifies the private key' do
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass testpass") do |r|
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass puppet") do |r|
expect(r.exit_code).to be_zero
expect(r.stdout).to match(/Alias name: broker\.example\.com/)
expect(r.stdout).to match(/Entry type: (keyEntry|PrivateKeyEntry)/)
Expand Down
18 changes: 9 additions & 9 deletions spec/acceptance/keystore_spec.rb
Expand Up @@ -16,7 +16,7 @@
ensure => #{@ensure_ks},
certificate => "#{@temp_dir}ca.pem",
target => '#{target}',
password => 'testpass',
password => 'puppet',
trustcacerts => true,
path => #{@resource_path},
}
Expand All @@ -26,7 +26,7 @@
end

it 'verifies the keystore' do
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass testpass") do |r|
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass puppet") do |r|
expect(r.exit_code).to be_zero
expect(r.stdout).to match(/Your keystore contains 1 entry/)
expect(r.stdout).to match(/Alias name: puppetca/)
Expand All @@ -36,26 +36,26 @@

it 'uses password_file' do
pp = <<-EOS
file { '/tmp/password':
file { '#{@temp_dir}password':
ensure => file,
content => 'puppet',
}
java_ks { 'puppetca2:keystore':
ensure => latest,
certificate => "/tmp/ca2.pem",
target => '/etc/keystore.ks',
password_file => '/tmp/password',
certificate => "#{@temp_dir}ca2.pem",
target => '#{target}',
password_file => '#{@temp_dir}password',
trustcacerts => true,
path => #{resource_path},
require => File['/tmp/password']
path => #{@resource_path},
require => File['#{@temp_dir}password']
}
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'verifies the keystore' do
shell("#{keytool_path}keytool -list -v -keystore /etc/keystore.ks -storepass puppet") do |r|
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass puppet") do |r|
expect(r.exit_code).to be_zero
expect(r.stdout).to match(/Your keystore contains 2 entries/)
expect(r.stdout).to match(/Alias name: puppetca2/)
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/private_key_spec.rb
Expand Up @@ -17,7 +17,7 @@
ensure => #{@ensure_ks},
certificate => "#{@temp_dir}ca.pem",
private_key => "#{@temp_dir}privkey.pem",
password => 'testpass',
password => 'puppet',
path => #{@resource_path},
}
EOS
Expand All @@ -26,7 +26,7 @@
end

it 'verifies the private key' do
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass testpass") do |r|
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass puppet") do |r|
expect(r.exit_code).to be_zero
expect(r.stdout).to match(/Alias name: broker\.example\.com/)
expect(r.stdout).to match(/Entry type: (keyEntry|PrivateKeyEntry)/)
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/truststore_spec.rb
Expand Up @@ -16,7 +16,7 @@
ensure => #{@ensure_ks},
certificate => "#{@temp_dir}ca.pem",
target => '#{target}',
password => 'testpass',
password => 'puppet',
trustcacerts => true,
path => #{@resource_path},
}
Expand All @@ -25,7 +25,7 @@
end

it 'verifies the truststore' do
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass testpass") do |r|
shell("#{@keytool_path}keytool -list -v -keystore #{target} -storepass puppet") do |r|
expect(r.exit_code).to be_zero
expect(r.stdout).to match(/Your keystore contains 1 entry/)
expect(r.stdout).to match(/Alias name: puppetca/)
Expand Down
22 changes: 18 additions & 4 deletions spec/spec_helper_acceptance.rb
Expand Up @@ -44,6 +44,7 @@ def create_keys_for_test(host)
# Need to check for ruby path on puppet install, use vendor ruby and add it to the path durring execution
tmp_privky = "#{temp_dir}privkey.pem"
tmp_ca = "#{temp_dir}ca.pem"
tmp_ca2 = "#{temp_dir}ca2.pem"
tmp_chain = "#{temp_dir}chain.pem"
opensslscript =<<EOS
require 'openssl'
Expand All @@ -57,6 +58,17 @@ def create_keys_for_test(host)
ca.not_before = Time.now
ca.not_after = ca.not_before + 360
ca.sign(key, OpenSSL::Digest::SHA256.new)
key2 = OpenSSL::PKey::RSA.new 1024
ca2 = OpenSSL::X509::Certificate.new
ca2.serial = 2
ca2.public_key = key2.public_key
subj2 = '/CN=Test CA/ST=Denial/L=Springfield/O=Dis/CN=www.example.com'
ca2.subject = OpenSSL::X509::Name.parse subj2
ca2.issuer = ca2.subject
ca2.not_before = Time.now
ca2.not_after = ca2.not_before + 360
ca2.sign(key2, OpenSSL::Digest::SHA256.new)
chain = OpenSSL::X509::Certificate.new
chain.serial = 1
Expand All @@ -70,6 +82,7 @@ def create_keys_for_test(host)
File.open('#{tmp_privky}', 'w') { |f| f.write key.to_pem }
File.open('#{tmp_ca}', 'w') { |f| f.write ca.to_pem }
File.open('#{tmp_ca2}', 'w') { |f| f.write ca2.to_pem }
File.open('#{tmp_chain}', 'w') { |f| f.write chain.to_pem }
EOS
on host, "#{cmd} \"#{opensslscript}\""
Expand All @@ -93,8 +106,8 @@ def create_keys_for_test(host)
if host['platform'] =~ /windows/i
exec_puppet = <<EOS
exec{'Download':
command => 'powershell.exe -command \'(New-Object System.Net.Webclient).DownloadString("https://forge.puppetlabs.com")\'',
path => ['c:\windows\sysnative\WindowsPowershell\v1.0','c:\windows\system32\WindowsPowershell\v1.0'],
command => 'powershell.exe -command "Invoke-WebRequest https://forge.puppetlabs.com"',
path => ['c:\\windows\\sysnative\\WindowsPowershell\\v1.0','c:\\windows\\system32\\WindowsPowershell\\v1.0'],
}
EOS
on host, apply_manifest(exec_puppet)
Expand All @@ -109,6 +122,7 @@ def create_keys_for_test(host)

RSpec.shared_context 'common variables' do
before {
java_major, java_minor = (ENV['JAVA_VERSION'] || '7u67').split('u')
@ensure_ks = 'latest'
@temp_dir = '/tmp/'
@resource_path = "undef"
Expand All @@ -124,10 +138,10 @@ def create_keys_for_test(host)
@target = '/etc/truststore.ts'
when 'windows'
@ensure_ks = 'present'
@keytool_path = 'C:/Java/jdk1.7.0_60/bin/'
@keytool_path = "C:/Java/jdk1.#{java_major}.0_#{java_minor}/bin/"
@target = 'c:/truststore.ts'
@temp_dir = 'C:/tmp/'
@resource_path = "['C:/Java/jdk1.7.0_60/bin/']"
@resource_path = "['C:/Java/jdk1.#{java_major}.0_#{java_minor}/bin/']"
end
}
end
22 changes: 13 additions & 9 deletions spec/unit/puppet/provider/java_ks/keytool_spec.rb
Expand Up @@ -68,15 +68,15 @@
describe 'when importing a private key and certifcate' do
describe '#to_pkcs12' do
it 'converts a certificate to a pkcs12 file' do
provider.expects(:run_command).with([
'myopenssl', 'pkcs12', '-export', '-passout', 'stdin',
'-in', resource[:certificate],
'-inkey', resource[:private_key],
'-name', resource[:name],
'-out', '/tmp/testing.stuff'
],
any_parameters
)
provider.stubs(:get_password).returns(resource[:password])
File.stubs(:read).with(resource[:private_key]).returns('private key')
File.stubs(:read).with(resource[:certificate]).returns('certificate')
OpenSSL::PKey::RSA.expects(:new).with('private key').returns('priv_obj')
OpenSSL::X509::Certificate.expects(:new).with('certificate').returns('cert_obj')

pkcs_double = BogusPkcs.new()
pkcs_double.expects(:to_der)
OpenSSL::PKCS12.expects(:create).with(resource[:password],resource[:name],'priv_obj','cert_obj',[]).returns(pkcs_double)
provider.to_pkcs12('/tmp/testing.stuff')
end
end
Expand Down Expand Up @@ -129,3 +129,7 @@
end
end
end

class BogusPkcs

end

0 comments on commit d6071d6

Please sign in to comment.