File: chain_key_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-java-ks 5.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ruby: 1,810; sh: 10; makefile: 4
file content (171 lines) | stat: -rw-r--r-- 5,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'managing intermediate certificates' do
  # rubocop:disable RSpec/InstanceVariable : Instance variables are inherited and thus cannot be contained within lets
  describe 'managing combined and seperate java chain keys' do
    include_context 'with common variables'

    it 'verifies keytool is setup', unless: os[:family] == 'windows' do
      i = 0
      loop do
        keytool_status = run_shell('keytool')
        break if keytool_status['exit_code'] == 0

        if i >= 8
          puts keytool_status
          exit 1
        else
          i += 1
          sleep 30.seconds
        end
      end
    end

    it 'creates two private key with chain certs' do
      pp = <<-MANIFEST
        java_ks { 'combined.example.com:#{@temp_dir}chain_combined_key.ks':
          ensure       => latest,
          certificate  => "#{@temp_dir}leafchain.pem",
          private_key  => "#{@temp_dir}leafkey.pem",
          password     => 'puppet',
          path         => #{@resource_path},
        }

        java_ks { 'seperate.example.com:#{@temp_dir}chain_key.ks':
          ensure       => latest,
          certificate  => "#{@temp_dir}leaf.pem",
          chain        => "#{@temp_dir}chain.pem",
          private_key  => "#{@temp_dir}leafkey.pem",
          password     => 'puppet',
          path         => #{@resource_path},
        }
      MANIFEST

      idempotent_apply(pp)
    end

    expectations_combined = [
      %r{Alias name: combined\.example\.com},
      %r{Entry type: (keyEntry|PrivateKeyEntry)},
      %r{Certificate chain length: 3},
      %r{^Serial number: 5.*^Serial number: 4.*^Serial number: 3}m,
    ]
    it 'verifies the private key #combined' do
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}chain_combined_key.ks -storepass puppet"), expect_failures: true) do |r|
        expectations_combined.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end
    end

    expectations_seperate = [
      %r{Alias name: seperate\.example\.com},
      %r{Entry type: (keyEntry|PrivateKeyEntry)},
      %r{Certificate chain length: 3},
      %r{^Serial number: 5.*^Serial number: 4.*^Serial number: 3}m,
    ]
    it 'verifies the private key #seperate' do
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}chain_key.ks -storepass puppet"), expect_failures: true) do |r|
        expectations_seperate.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end
    end

    it 'updates the two key chains' do
      pp = <<-MANIFEST
        java_ks { 'combined.example.com:#{@temp_dir}chain_combined_key.ks':
          ensure       => latest,
          certificate  => "#{@temp_dir}leafchain2.pem",
          private_key  => "#{@temp_dir}leafkey.pem",
          password     => 'puppet',
          path         => #{@resource_path},
        }

        java_ks { 'seperate.example.com:#{@temp_dir}chain_key.ks':
          ensure       => latest,
          certificate  => "#{@temp_dir}leaf.pem",
          chain        => "#{@temp_dir}chain2.pem",
          private_key  => "#{@temp_dir}leafkey.pem",
          password     => 'puppet',
          path         => #{@resource_path},
        }
      MANIFEST

      idempotent_apply(pp)

      expectations_combined = [
        %r{Alias name: combined\.example\.com},
        %r{Entry type: (keyEntry|PrivateKeyEntry)},
        %r{Certificate chain length: 2},
        %r{^Serial number: 5.*^Serial number: 6}m,
      ]
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}chain_combined_key.ks -storepass puppet"), expect_failures: true) do |r|
        expectations_combined.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end

      expectations_seperate = [
        %r{Alias name: seperate\.example\.com},
        %r{Entry type: (keyEntry|PrivateKeyEntry)},
        %r{Certificate chain length: 2},
        %r{^Serial number: 5.*Serial number: 6}m,
      ]
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}chain_key.ks -storepass puppet"), expect_failures: true) do |r|
        expectations_seperate.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end
    end
  end

  describe 'managing non existent java chain keys in noop' do
    include_context 'with common variables'

    it 'does not create a new keystore in noop' do
      pp = <<-MANIFEST
        $filenames = ["#{@temp_dir}noop_ca.pem",
                      "#{@temp_dir}noop_chain.pem",
                      "#{@temp_dir}noop_privkey.pem"]
        file { $filenames:
          ensure  => file,
          content => 'content',
        } ->
        java_ks { 'broker.example.com:#{@temp_dir}noop_chain_key.ks':
          ensure       => latest,
          certificate  => "#{@temp_dir}noop_ca.pem",
          chain        => "#{@temp_dir}noop_chain.pem",
          private_key  => "#{@temp_dir}noop_privkey.pem",
          password     => 'puppet',
          path         => #{@resource_path},
        }
      MANIFEST

      # in noop mode, when the dependent certificate files are not present in the system,
      # java_ks will not invoke openssl to validate their status, thus noop will succeed
      apply_manifest(pp, noop: true)
    end

    # verifies the dependent files are missing
    ['noop_ca.pem', 'noop_chain.pem', 'noop_privkey.pem'].each do |filename|
      describe filename do
        it "doesn't exist" do
          result = remote_file_exists?("#{@temp_dir}#{filename}")
          expect(result.exit_code).to be(1)
        end
      end
    end

    # verifies the keystore is not created
    describe 'noop_chain_key.ks' do
      it "doesn't exist" do
        result = remote_file_exists?("#{@temp_dir}noop_chain_key.ks")
        expect(result.exit_code).to be(1)
      end
    end
  end
  # rubocop:enable RSpec/InstanceVariable
end