Add puppetlabs/certregen module
[mirror/dsa-puppet.git] / 3rdparty / modules / certregen / spec / acceptance / helpers.rb
1 require 'openssl'
2
3 # Time constants in seconds
4 HOUR =  60 * 60
5 DAY  =  24 * HOUR
6 YEAR = 365 * DAY
7
8 # Retrieve CA Certificate from the given host
9 #
10 # @param  [Host]           host   single Beaker::Host
11 #
12 # @return [OpenSSL::X509::Certificate]  Certificate object
13 def get_ca_cert_on(host)
14   if host[:roles].include? 'master' then
15     dir = on(host, puppet('config', 'print', 'cadir')).stdout.chomp
16     ca_path = "#{dir}/ca_crt.pem"
17   else
18     dir = on(host, puppet('config', 'print', 'certdir')).stdout.chomp
19     ca_path = "#{dir}/ca.pem"
20   end
21   on(host, "cat #{ca_path}") do |result|
22     cert = OpenSSL::X509::Certificate.new(result.stdout)
23     return cert
24   end
25 end
26
27 # Execute `date` command on host with optional arguments
28 # and get back a Ruby Time object
29 #
30 # @param [Host]            host   single Beaker::Host to run the command on
31 # @param [Array<String>]   args   Array of arguments to be appended to the
32 #                                 `date` command
33 # @return [Time]    Ruby Time object
34 def get_time_on(host, args = [])
35   arg_string = args.join(' ')
36   date = on(host, "date #{arg_string}").stdout.chomp
37   return Time.parse(date)
38 end
39
40 # Retrieve the CA enddate on a given host as a Ruby time object
41 #
42 # @param [Host]            host   single Beaker::Host to get CA enddate from
43 #
44 # @return [Time]    Ruby Time object, or nil if error
45 def get_ca_enddate_time_on(host)
46   cert = get_ca_cert_on(host)
47   return cert.not_after if cert
48   return nil
49 end
50
51 # Retrieve the current ca_serial value for `puppet certgen ca` on a given host
52 #
53 # @param [Host]            host   single Beaker::Host to get ca_serial from
54 #
55 # @return [String]    ca_serial in hexadecimal, or nil if error
56 def get_ca_serial_id_on(host)
57   cert = get_ca_cert_on(host)
58   return cert.serial.to_s(16) if cert
59   return nil
60 end
61
62 # Patch puppet to get around the date check validation.
63 #
64 # This method is used to patch puppet in order to prevent it from failing to
65 # create a CA if the system clock is turned back in time by years. The same
66 # method is used to reverse the patch with the `reverse` parameter.
67 #
68 # @param [Host]            host     single Beaker::Host to run the command on
69 # @param [String]          reverse  causes the patch to be reversed
70 def patch_puppet_date_check_on(host, reverse=nil)
71   reverse = '--reverse' if reverse
72   apply_manifest_on(host, 'package { "patch": ensure => present}')
73   interface_documentation_file = "/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/interface/documentation.rb"
74   patch =<<EOF
75 305c305
76 <           raise ArgumentError, "copyright with a year \#{fault} is very strange; did you accidentally add or subtract two years?"
77 ---
78 >           #raise ArgumentError, "copyright with a year \#{fault} is very strange; did you accidentally add or subtract two years?"
79 EOF
80   patch_file        = host.tmpfile('iface_doc_patch')
81   create_remote_file(host, patch_file, patch)
82   on(host, "patch #{reverse} #{interface_documentation_file} < #{patch_file}", :acceptable_exit_codes => [0,1])
83 end