File: keystore_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 (196 lines) | stat: -rw-r--r-- 6,653 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'managing java keystores' do
  # rubocop:disable RSpec/InstanceVariable : Instance variables are inherited and thus cannot be contained within lets
  include_context 'with common variables'

  describe 'basic tests' do
    it 'creates a keystore' do
      command = "rm #{@temp_dir}keystore.ks"
      command = interpolate_powershell(command) if os[:family] == 'windows'
      run_shell(command, expect_failures: true)
      pp_one = <<-MANIFEST
        java_ks { 'puppetca:keystore':
          ensure       => latest,
          certificate  => "#{@temp_dir}ca.pem",
          target       => '#{@temp_dir}keystore.ks',
          password     => 'puppet',
          trustcacerts => true,
          path         => #{@resource_path},
        }
      MANIFEST

      idempotent_apply(pp_one)
    end

    expectations = [
      %r{Your keystore contains 1 entry},
      %r{Alias name: puppetca},
      %r{CN=Test CA},
    ]
    it 'verifies the keytore' do
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}keystore.ks -storepass puppet")) do |r|
        expect(r.exit_code).to be_zero
        expectations.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end
    end

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

      idempotent_apply(pp_two)
    end

    it 'recreates a keystore if password fails' do
      pp_three = <<-MANIFEST

        java_ks { 'puppetca:#{@temp_dir}keystore':
          ensure              => latest,
          certificate         => "#{@temp_dir}ca.pem",
          target              => '#{@temp_dir}keystore.ks',
          password            => 'pepput',
          password_fail_reset => true,
          trustcacerts        => true,
          path                => #{@resource_path},
      }
      MANIFEST

      idempotent_apply(pp_three)
    end

    it 'verifies the keystore again' do
      run_shell(keytool_command("-list -v -keystore #{@temp_dir}keystore.ks -storepass pepput")) do |r|
        expect(r.exit_code).to be_zero
        expectations.each do |expect|
          expect(r.stdout).to match(expect)
        end
      end
    end
  end

  unless os[:family] == 'ubuntu' && os[:release].start_with?('18.04')
    describe 'storetype' do
      it 'creates a keystore' do
        pp = <<-MANIFEST
          java_ks { 'puppetca:#{@temp_dir}keystore':
            ensure       => latest,
            certificate  => "#{@temp_dir}ca.pem",
            target       => '#{@temp_dir}keystore.ks',
            password     => 'pepput',
            trustcacerts => true,
            path         => #{@resource_path},
            storetype    => 'jks',
          }
        MANIFEST

        idempotent_apply(pp)
      end

      expectations = [
        %r{Your keystore contains 1 entry},
        %r{Alias name: puppetca},
        %r{CN=Test CA},
      ]
      it 'verifies the keytore' do
        run_shell(keytool_command("-list -v -keystore #{@temp_dir}keystore.ks -storepass pepput")) do |r|
          expect(r.exit_code).to be_zero
          expectations.each do |expect|
            expect(r.stdout).to match(expect)
          end
        end
      end
    end
  end

  describe 'with der certificates' do
    it 'creates a keystore' do
      command = "rm #{@temp_dir}keystore.ks"
      command = interpolate_powershell(command) if os[:family] == 'windows'
      run_shell(command, expect_failures: true)
      pp_one = <<-MANIFEST
        java_ks { 'puppetcader:keystore':
          ensure       => latest,
          certificate  => "#{@temp_dir}ca.der",
          target       => '#{@temp_dir}keystore.ks',
          password     => 'puppet',
          trustcacerts => true,
          path         => #{@resource_path},
        }
      MANIFEST

      idempotent_apply(pp_one)
    end

    it 'adds a certificate and key' do
      pp_two = <<-MANIFEST
        java_ks { 'puppetcader_privkey:keystore':
          ensure       => latest,
          certificate  => "#{@temp_dir}ca.der",
          private_key  => "#{@temp_dir}privkey.pem",
          target       => '#{@temp_dir}keystore.ks',
          password     => 'puppet',
          trustcacerts => true,
          path         => #{@resource_path},
        }
      MANIFEST

      idempotent_apply(pp_two)
    end

    expectations = [
      %r{Your keystore contains 2 entries},
      %r{Alias name: puppetcader},
      %r{Alias name: puppetcader_privkey},
      %r{CN=Test CA},
    ]

    context 'when running on Linux', unless: os[:family] == 'windows' do
      it 'verifies the keystore' do
        run_shell(keytool_command("-list -v -keystore #{@temp_dir}keystore.ks -storepass puppet")) do |r|
          expect(r.exit_code).to be_zero
          expectations.each do |expect|
            expect(r.stdout).to match(expect)
          end
        end
      end
    end

    # On Windows, the keystore command warns about using a proprietary format when using DER formatted certs. We should
    # not take this as a failure, but also, we should also not blindly ignore all STDERR from the result either. If we
    # get an exit code of 1, we'll check to see if the STDERR message was the cert format warning and still pass the
    # test. We will still catch any errors that occur and are not related
    context 'when running on Windows', if: os[:family] == 'windows' do
      it 'verifies the keystore' do
        run_shell(keytool_command("-list -v -keystore #{@temp_dir}keystore.ks -storepass puppet"), expect_failures: true) do |r|
          expect(r.exit_code).to be_between(0, 1)
          expectations.each do |expect|
            expect(r.stdout).to match(expect)
          end
          # Pattern below ensures that it's the only warning printed out by anchoring to the end of line ($). This is to
          # handle the case that multiple warnings should ever occur - a looser match could potentially hide additional
          # errors
          expect(r.stderr.chomp).to match(%r{The JKS keystore.*pkcs12"\.$}) if r.exit_code == 1
        end
      end
    end
  end
  # rubocop:enable RSpec/InstanceVariable
end