File: ssl_provider_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,392 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (663 lines) | stat: -rw-r--r-- 25,240 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
require 'spec_helper'

describe Puppet::SSL::SSLProvider do
  include PuppetSpec::Files

  let(:global_cacerts) { [ cert_fixture('ca.pem'), cert_fixture('intermediate.pem') ] }
  let(:global_crls) { [ crl_fixture('crl.pem'), crl_fixture('intermediate-crl.pem') ] }
  let(:wrong_key) { OpenSSL::PKey::RSA.new(512) }

  context 'when creating an insecure context' do
    let(:sslctx) { subject.create_insecure_context }

    it 'has an empty list of trusted certs' do
      expect(sslctx.cacerts).to eq([])
    end

    it 'has an empty list of crls' do
      expect(sslctx.crls).to eq([])
    end

    it 'has an empty chain' do
      expect(sslctx.client_chain).to eq([])
    end

    it 'has a nil private key and cert' do
      expect(sslctx.private_key).to be_nil
      expect(sslctx.client_cert).to be_nil
    end

    it 'does not authenticate the server' do
      expect(sslctx.verify_peer).to eq(false)
    end

    it 'raises if the frozen context is modified' do
      expect {
        sslctx.cacerts = []
      }.to raise_error(/can't modify frozen/)
    end
  end

  context 'when creating an root ssl context with CA certs' do
    let(:config) { { cacerts: [], crls: [], revocation: false } }

    it 'accepts empty list of certs and crls' do
      sslctx = subject.create_root_context(**config)
      expect(sslctx.cacerts).to eq([])
      expect(sslctx.crls).to eq([])
    end

    it 'accepts valid root certs' do
      certs = [cert_fixture('ca.pem')]
      sslctx = subject.create_root_context(**config.merge(cacerts: certs))
      expect(sslctx.cacerts).to eq(certs)
    end

    it 'accepts valid intermediate certs' do
      certs = [cert_fixture('ca.pem'), cert_fixture('intermediate.pem')]
      sslctx = subject.create_root_context(**config.merge(cacerts: certs))
      expect(sslctx.cacerts).to eq(certs)
    end

    it 'accepts expired CA certs' do
      expired = [cert_fixture('ca.pem'), cert_fixture('intermediate.pem')]
      expired.each { |x509| x509.not_after = Time.at(0) }

      sslctx = subject.create_root_context(**config.merge(cacerts: expired))
      expect(sslctx.cacerts).to eq(expired)
    end

    it 'raises if the frozen context is modified' do
      sslctx = subject.create_root_context(**config)
      expect {
        sslctx.verify_peer = false
      }.to raise_error(/can't modify frozen/)
    end

    it 'verifies peer' do
      sslctx = subject.create_root_context(**config)
      expect(sslctx.verify_peer).to eq(true)
    end
  end

  context 'when creating a system ssl context' do
    it 'accepts empty list of CA certs' do
      sslctx = subject.create_system_context(cacerts: [])
      expect(sslctx.cacerts).to eq([])
    end

    it 'accepts valid root certs' do
      certs = [cert_fixture('ca.pem')]
      sslctx = subject.create_system_context(cacerts: certs)
      expect(sslctx.cacerts).to eq(certs)
    end

    it 'accepts valid intermediate certs' do
      certs = [cert_fixture('ca.pem'), cert_fixture('intermediate.pem')]
      sslctx = subject.create_system_context(cacerts: certs)
      expect(sslctx.cacerts).to eq(certs)
    end

    it 'accepts expired CA certs' do
      expired = [cert_fixture('ca.pem'), cert_fixture('intermediate.pem')]
      expired.each { |x509| x509.not_after = Time.at(0) }

      sslctx = subject.create_system_context(cacerts: expired)
      expect(sslctx.cacerts).to eq(expired)
    end

    it 'raises if the frozen context is modified' do
      sslctx = subject.create_system_context(cacerts: [])
      expect {
        sslctx.verify_peer = false
      }.to raise_error(/can't modify frozen/)
    end

    it 'trusts system ca store by default' do
      expect_any_instance_of(OpenSSL::X509::Store).to receive(:set_default_paths)

      subject.create_system_context(cacerts: [])
    end

    it 'trusts an external ca store' do
      path = tmpfile('system_cacerts')
      File.write(path, cert_fixture('ca.pem').to_pem)

      expect_any_instance_of(OpenSSL::X509::Store).to receive(:add_file).with(path)

      subject.create_system_context(cacerts: [], path: path)
    end

    it 'verifies peer' do
      sslctx = subject.create_system_context(cacerts: [])
      expect(sslctx.verify_peer).to eq(true)
    end

    it 'disable revocation' do
      sslctx = subject.create_system_context(cacerts: [])
      expect(sslctx.revocation).to eq(false)
    end

    it 'sets client cert and private key to nil' do
      sslctx = subject.create_system_context(cacerts: [])
      expect(sslctx.client_cert).to be_nil
      expect(sslctx.private_key).to be_nil
    end

    it 'includes the client cert and private key when requested' do
      Puppet[:hostcert] = fixtures('ssl/signed.pem')
      Puppet[:hostprivkey] = fixtures('ssl/signed-key.pem')
      sslctx = subject.create_system_context(cacerts: [], include_client_cert: true)
      expect(sslctx.client_cert).to be_an(OpenSSL::X509::Certificate)
      expect(sslctx.private_key).to be_an(OpenSSL::PKey::RSA)
    end

    it 'ignores non-existent client cert and private key when requested' do
      Puppet[:certname] = 'doesnotexist'
      sslctx = subject.create_system_context(cacerts: [], include_client_cert: true)
      expect(sslctx.client_cert).to be_nil
      expect(sslctx.private_key).to be_nil
    end

    it 'warns if the client cert does not exist' do
      Puppet[:certname] = 'missingcert'
      Puppet[:hostprivkey] = fixtures('ssl/signed-key.pem')

      expect(Puppet).to receive(:warning).with("Client certificate for 'missingcert' does not exist")
      subject.create_system_context(cacerts: [], include_client_cert: true)
    end

    it 'warns if the private key does not exist' do
      Puppet[:certname] = 'missingkey'
      Puppet[:hostcert] = fixtures('ssl/signed.pem')

      expect(Puppet).to receive(:warning).with("Private key for 'missingkey' does not exist")
      subject.create_system_context(cacerts: [], include_client_cert: true)
    end

    it 'raises if client cert and private key are mismatched' do
      Puppet[:hostcert] = fixtures('ssl/signed.pem')
      Puppet[:hostprivkey] = fixtures('ssl/127.0.0.1-key.pem')

      expect {
        subject.create_system_context(cacerts: [], include_client_cert: true)
      }.to raise_error(Puppet::SSL::SSLError,
        "The certificate for 'CN=signed' does not match its private key")
    end

    it 'trusts additional system certs' do
      path = tmpfile('system_cacerts')
      File.write(path, cert_fixture('ca.pem').to_pem)

      expect_any_instance_of(OpenSSL::X509::Store).to receive(:add_file).with(path)

      subject.create_system_context(cacerts: [], path: path)
    end

    it 'ignores empty files' do
      path = tmpfile('system_cacerts')
      FileUtils.touch(path)

      subject.create_system_context(cacerts: [], path: path)

      expect(@logs).to eq([])
    end

    it 'prints an error if it is not a file' do
      path = tmpdir('system_cacerts')

      subject.create_system_context(cacerts: [], path: path)

      expect(@logs).to include(an_object_having_attributes(level: :warning, message: /^The 'ssl_trust_store' setting does not refer to a file and will be ignored/))
    end
  end

  context 'when creating an ssl context with crls' do
    let(:config) { { cacerts: global_cacerts, crls: global_crls} }

    it 'accepts valid CRLs' do
      certs = [cert_fixture('ca.pem')]
      crls = [crl_fixture('crl.pem')]
      sslctx = subject.create_root_context(**config.merge(cacerts: certs, crls: crls))
      expect(sslctx.crls).to eq(crls)
    end

    it 'accepts valid CRLs for intermediate certs' do
      certs = [cert_fixture('ca.pem'), cert_fixture('intermediate.pem')]
      crls = [crl_fixture('crl.pem'), crl_fixture('intermediate-crl.pem')]
      sslctx = subject.create_root_context(**config.merge(cacerts: certs, crls: crls))
      expect(sslctx.crls).to eq(crls)
    end

    it 'accepts expired CRLs' do
      expired = [crl_fixture('crl.pem'), crl_fixture('intermediate-crl.pem')]
      expired.each { |x509| x509.last_update = Time.at(0) }

      sslctx = subject.create_root_context(**config.merge(crls: expired))
      expect(sslctx.crls).to eq(expired)
    end

    it 'verifies peer' do
      sslctx = subject.create_root_context(**config)
      expect(sslctx.verify_peer).to eq(true)
    end
  end

  context 'when creating an ssl context with client certs' do
    let(:client_cert) { cert_fixture('signed.pem') }
    let(:private_key) { key_fixture('signed-key.pem') }
    let(:config) { { cacerts: global_cacerts, crls: global_crls, client_cert: client_cert, private_key: private_key } }

    it 'raises if CA certs are missing' do
      expect {
        subject.create_context(**config.merge(cacerts: nil))
      }.to raise_error(ArgumentError, /CA certs are missing/)
    end

    it 'raises if CRLs are missing' do
      expect {
        subject.create_context(**config.merge(crls: nil))
      }.to raise_error(ArgumentError, /CRLs are missing/)
    end

    it 'raises if private key is missing' do
      expect {
        subject.create_context(**config.merge(private_key: nil))
      }.to raise_error(ArgumentError, /Private key is missing/)
    end

    it 'raises if client cert is missing' do
      expect {
        subject.create_context(**config.merge(client_cert: nil))
      }.to raise_error(ArgumentError, /Client cert is missing/)
    end

    it 'accepts RSA keys' do
      sslctx = subject.create_context(**config)
      expect(sslctx.private_key).to eq(private_key)
    end

    it 'accepts EC keys' do
      ec_key = ec_key_fixture('ec-key.pem')
      ec_cert = cert_fixture('ec.pem')
      sslctx = subject.create_context(**config.merge(client_cert: ec_cert, private_key: ec_key))
      expect(sslctx.private_key).to eq(ec_key)
    end

    it 'raises if private key is unsupported' do
      dsa_key = OpenSSL::PKey::DSA.new
      expect {
        subject.create_context(**config.merge(private_key: dsa_key))
      }.to raise_error(Puppet::SSL::SSLError, /Unsupported key 'OpenSSL::PKey::DSA'/)
    end

    it 'resolves the client chain from leaf to root' do
      sslctx = subject.create_context(**config)
      expect(
        sslctx.client_chain.map(&:subject).map(&:to_utf8)
      ).to eq(['CN=signed', 'CN=Test CA Subauthority', 'CN=Test CA'])
    end

    it 'raises if client cert signature is invalid' do
      client_cert.public_key = wrong_key.public_key
      client_cert.sign(wrong_key, OpenSSL::Digest::SHA256.new)
      expect {
        subject.create_context(**config.merge(client_cert: client_cert))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Invalid signature for certificate 'CN=signed'")
    end

    it 'raises if client cert and private key are mismatched' do
      expect {
        subject.create_context(**config.merge(private_key: wrong_key))
      }.to raise_error(Puppet::SSL::SSLError,
                       "The certificate for 'CN=signed' does not match its private key")
    end

    it "raises if client cert's public key has been replaced" do
      expect {
        subject.create_context(**config.merge(client_cert: cert_fixture('tampered-cert.pem')))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Invalid signature for certificate 'CN=signed'")
    end

    # This option is only available in openssl 1.1
    # OpenSSL 1.1.1h no longer reports expired root CAs when using "verify".
    # This regression was fixed in 1.1.1i, so only skip this test if we're on
    # the affected version.
    # See: https://github.com/openssl/openssl/pull/13585
    if Puppet::Util::Package.versioncmp(OpenSSL::OPENSSL_LIBRARY_VERSION.split[1], '1.1.1h') != 0
      it 'raises if root cert signature is invalid', if: defined?(OpenSSL::X509::V_FLAG_CHECK_SS_SIGNATURE) do
        ca = global_cacerts.first
        ca.sign(wrong_key, OpenSSL::Digest::SHA256.new)

        expect {
          subject.create_context(**config.merge(cacerts: global_cacerts))
        }.to raise_error(Puppet::SSL::CertVerifyError,
                         "Invalid signature for certificate 'CN=Test CA'")
      end
    end

    it 'raises if intermediate CA signature is invalid', unless: Puppet::Util::Platform.jruby? && RUBY_VERSION.to_f >= 2.6 do
      int = global_cacerts.last
      int.public_key = wrong_key.public_key if Puppet::Util::Platform.jruby?
      int.sign(wrong_key, OpenSSL::Digest::SHA256.new)

      expect {
        subject.create_context(**config.merge(cacerts: global_cacerts))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Invalid signature for certificate 'CN=Test CA Subauthority'")
    end

    it 'raises if CRL signature for root CA is invalid', unless: Puppet::Util::Platform.jruby? do
      crl = global_crls.first
      crl.sign(wrong_key, OpenSSL::Digest::SHA256.new)

      expect {
        subject.create_context(**config.merge(crls: global_crls))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Invalid signature for CRL issued by 'CN=Test CA'")
    end

    it 'raises if CRL signature for intermediate CA is invalid', unless: Puppet::Util::Platform.jruby? do
      crl = global_crls.last
      crl.sign(wrong_key, OpenSSL::Digest::SHA256.new)

      expect {
        subject.create_context(**config.merge(crls: global_crls))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Invalid signature for CRL issued by 'CN=Test CA Subauthority'")
    end

    it 'raises if client cert is revoked' do
      expect {
        subject.create_context(**config.merge(private_key: key_fixture('revoked-key.pem'), client_cert: cert_fixture('revoked.pem')))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "Certificate 'CN=revoked' is revoked")
    end

    it 'warns if intermediate issuer is missing' do
      expect(Puppet).to receive(:warning).with("The issuer 'CN=Test CA Subauthority' of certificate 'CN=signed' cannot be found locally")

      subject.create_context(**config.merge(cacerts: [cert_fixture('ca.pem')]))
    end

    it 'raises if root issuer is missing' do
      expect {
        subject.create_context(**config.merge(cacerts: [cert_fixture('intermediate.pem')]))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The issuer 'CN=Test CA' of certificate 'CN=Test CA Subauthority' is missing")
    end

    it 'raises if cert is not valid yet', unless: Puppet::Util::Platform.jruby? do
      client_cert.not_before = Time.now + (5 * 60 * 60)
      int_key = key_fixture('intermediate-key.pem')
      client_cert.sign(int_key, OpenSSL::Digest::SHA256.new)

      expect {
        subject.create_context(**config.merge(client_cert: client_cert))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The certificate 'CN=signed' is not yet valid, verify time is synchronized")
    end

    it 'raises if cert is expired', unless: Puppet::Util::Platform.jruby? do
      client_cert.not_after = Time.at(0)
      int_key = key_fixture('intermediate-key.pem')
      client_cert.sign(int_key, OpenSSL::Digest::SHA256.new)

      expect {
        subject.create_context(**config.merge(client_cert: client_cert))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The certificate 'CN=signed' has expired, verify time is synchronized")
    end

    it 'raises if crl is not valid yet', unless: Puppet::Util::Platform.jruby? do
      future_crls = global_crls
      # invalidate the CRL issued by the root
      future_crls.first.last_update = Time.now + (5 * 60 * 60)

      expect {
        subject.create_context(**config.merge(crls: future_crls))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The CRL issued by 'CN=Test CA' is not yet valid, verify time is synchronized")
    end

    it 'raises if crl is expired', unless: Puppet::Util::Platform.jruby? do
      past_crls = global_crls
      # invalidate the CRL issued by the root
      past_crls.first.next_update = Time.at(0)

      expect {
        subject.create_context(**config.merge(crls: past_crls))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The CRL issued by 'CN=Test CA' has expired, verify time is synchronized")
    end

    it 'raises if the root CRL is missing' do
      crls = [crl_fixture('intermediate-crl.pem')]
      expect {
        subject.create_context(**config.merge(crls: crls, revocation: :chain))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The CRL issued by 'CN=Test CA' is missing")
    end

    it 'raises if the intermediate CRL is missing' do
      crls = [crl_fixture('crl.pem')]
      expect {
        subject.create_context(**config.merge(crls: crls))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       "The CRL issued by 'CN=Test CA Subauthority' is missing")
    end

    it "doesn't raise if the root CRL is missing and we're just checking the leaf" do
      crls = [crl_fixture('intermediate-crl.pem')]
      subject.create_context(**config.merge(crls: crls, revocation: :leaf))
    end

    it "doesn't raise if the intermediate CRL is missing and revocation checking is disabled" do
      crls = [crl_fixture('crl.pem')]
      subject.create_context(**config.merge(crls: crls, revocation: false))
    end

    it "doesn't raise if both CRLs are missing and revocation checking is disabled" do
      subject.create_context(**config.merge(crls: [], revocation: false))
    end

    # OpenSSL < 1.1 does not verify basicConstraints
    it "raises if root CA's isCA basic constraint is false", unless: Puppet::Util::Platform.jruby? || OpenSSL::OPENSSL_VERSION_NUMBER < 0x10100000 do
      certs = [cert_fixture('bad-basic-constraints.pem'), cert_fixture('intermediate.pem')]

      # openssl 3 returns 79
      # define X509_V_ERR_NO_ISSUER_PUBLIC_KEY                 24
      # define X509_V_ERR_INVALID_CA                           79
      expect {
        subject.create_context(**config.merge(cacerts: certs, crls: [], revocation: false))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       /Certificate 'CN=Test CA' failed verification \((24|79)\): invalid CA certificate/)
    end

    # OpenSSL < 1.1 does not verify basicConstraints
    it "raises if intermediate CA's isCA basic constraint is false", unless: Puppet::Util::Platform.jruby? || OpenSSL::OPENSSL_VERSION_NUMBER < 0x10100000 do
      certs = [cert_fixture('ca.pem'), cert_fixture('bad-int-basic-constraints.pem')]

      expect {
        subject.create_context(**config.merge(cacerts: certs, crls: [], revocation: false))
      }.to raise_error(Puppet::SSL::CertVerifyError,
                       /Certificate 'CN=Test CA Subauthority' failed verification \((24|79)\): invalid CA certificate/)
    end

    it 'accepts CA certs in any order' do
      sslctx = subject.create_context(**config.merge(cacerts: global_cacerts.reverse))
      # certs in ruby+openssl 1.0.x are not comparable, so compare subjects
      expect(sslctx.client_chain.map(&:subject).map(&:to_utf8)).to contain_exactly('CN=Test CA', 'CN=Test CA Subauthority', 'CN=signed')
    end

    it 'accepts CRLs in any order' do
      sslctx = subject.create_context(**config.merge(crls: global_crls.reverse))
      # certs in ruby+openssl 1.0.x are not comparable, so compare subjects
      expect(sslctx.client_chain.map(&:subject).map(&:to_utf8)).to contain_exactly('CN=Test CA', 'CN=Test CA Subauthority', 'CN=signed')
    end

    it 'raises if the frozen context is modified' do
      sslctx = subject.create_context(**config)
      expect {
        sslctx.verify_peer = false
      }.to raise_error(/can't modify frozen/)
    end

    it 'verifies peer' do
      sslctx = subject.create_context(**config)
      expect(sslctx.verify_peer).to eq(true)
    end

    it 'does not trust the system ca store by default' do
      expect_any_instance_of(OpenSSL::X509::Store).to receive(:set_default_paths).never

      subject.create_context(**config)
    end

    it 'trusts the system ca store' do
      expect_any_instance_of(OpenSSL::X509::Store).to receive(:set_default_paths)

      subject.create_context(**config.merge(include_system_store: true))
    end
  end

  context 'when loading an ssl context' do
    let(:client_cert) { cert_fixture('signed.pem') }
    let(:private_key) { key_fixture('signed-key.pem') }
    let(:doesnt_exist) { '/does/not/exist' }

    before :each do
      Puppet[:localcacert] = file_containing('global_cacerts', global_cacerts.first.to_pem)
      Puppet[:hostcrl] = file_containing('global_crls', global_crls.first.to_pem)

      Puppet[:certname] = 'signed'
      Puppet[:privatekeydir] = tmpdir('privatekeydir')
      File.write(File.join(Puppet[:privatekeydir], 'signed.pem'), private_key.to_pem)

      Puppet[:certdir] = tmpdir('privatekeydir')
      File.write(File.join(Puppet[:certdir], 'signed.pem'), client_cert.to_pem)
    end

    it 'raises if CA certs are missing' do
      Puppet[:localcacert] = doesnt_exist

      expect {
        subject.load_context
      }.to raise_error(Puppet::Error, /The CA certificates are missing from/)
    end

    it 'raises if the CRL is missing' do
      Puppet[:hostcrl] = doesnt_exist

      expect {
        subject.load_context
      }.to raise_error(Puppet::Error, /The CRL is missing from/)
    end

    it 'does not raise if the CRL is missing and revocation is disabled' do
      Puppet[:hostcrl] = doesnt_exist

      subject.load_context(revocation: false)
    end

    it 'raises if the private key is missing' do
      Puppet[:privatekeydir] = doesnt_exist

      expect {
        subject.load_context
      }.to raise_error(Puppet::Error, /The private key is missing from/)
    end

    it 'raises if the client cert is missing' do
      Puppet[:certdir] = doesnt_exist

      expect {
        subject.load_context
      }.to raise_error(Puppet::Error, /The client certificate is missing from/)
    end

    context 'loading private keys', unless: RUBY_PLATFORM == 'java' do
      it 'loads the private key and client cert' do
        ssl_context = subject.load_context

        expect(ssl_context.private_key).to be_an(OpenSSL::PKey::RSA)
        expect(ssl_context.client_cert).to be_an(OpenSSL::X509::Certificate)
      end

      it 'loads a password protected key and client cert' do
        FileUtils.cp(File.join(PuppetSpec::FIXTURE_DIR, 'ssl', 'encrypted-key.pem'), File.join(Puppet[:privatekeydir], 'signed.pem'))

        ssl_context = subject.load_context(password: '74695716c8b6')

        expect(ssl_context.private_key).to be_an(OpenSSL::PKey::RSA)
        expect(ssl_context.client_cert).to be_an(OpenSSL::X509::Certificate)
      end

      it 'raises if the password is incorrect' do
        FileUtils.cp(File.join(PuppetSpec::FIXTURE_DIR, 'ssl', 'encrypted-key.pem'), File.join(Puppet[:privatekeydir], 'signed.pem'))

        expect {
          subject.load_context(password: 'wrongpassword')
        }.to raise_error(Puppet::SSL::SSLError, /Failed to load private key for host 'signed': Could not parse PKey/)
      end
    end

    it 'does not trust the system ca store by default' do
      expect_any_instance_of(OpenSSL::X509::Store).to receive(:set_default_paths).never

      subject.load_context
    end

    it 'trusts the system ca store' do
      expect_any_instance_of(OpenSSL::X509::Store).to receive(:set_default_paths)

      subject.load_context(include_system_store: true)
    end
  end

  context 'when verifying requests' do
    let(:csr) { request_fixture('request.pem') }

    it 'accepts valid requests' do
      private_key = key_fixture('request-key.pem')
      expect(subject.verify_request(csr, private_key.public_key)).to eq(csr)
    end

    it "raises if the CSR was signed by a private key that doesn't match public key" do
      expect {
        subject.verify_request(csr, wrong_key.public_key)
      }.to raise_error(Puppet::SSL::SSLError,
                       "The CSR for host 'CN=pending' does not match the public key")
    end

    it "raises if the CSR was tampered with" do
      csr = request_fixture('tampered-csr.pem')
      expect {
        subject.verify_request(csr, csr.public_key)
      }.to raise_error(Puppet::SSL::SSLError,
                       "The CSR for host 'CN=signed' does not match the public key")
    end
  end

  context 'printing' do
    let(:client_cert) { cert_fixture('signed.pem') }
    let(:private_key) { key_fixture('signed-key.pem') }
    let(:config) { { cacerts: global_cacerts, crls: global_crls, client_cert: client_cert, private_key: private_key } }

    it 'prints in debug' do
      Puppet[:log_level] = 'debug'

      ctx = subject.create_context(**config)
      subject.print(ctx)
      expect(@logs.map(&:message)).to include(
        /Verified CA certificate 'CN=Test CA' fingerprint/,
        /Verified CA certificate 'CN=Test CA Subauthority' fingerprint/,
        /Verified client certificate 'CN=signed' fingerprint/,
        /Using CRL 'CN=Test CA' authorityKeyIdentifier '(keyid:)?[A-Z0-9:]{59}' crlNumber '0'/,
        /Using CRL 'CN=Test CA Subauthority' authorityKeyIdentifier '(keyid:)?[A-Z0-9:]{59}' crlNumber '0'/
      )
    end
  end
end