beaker-pe 3.6.10 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9e801f6873a3ffac7ca895055bb548f263a0df1f5e2f98203afe4e6aa2128c6
4
- data.tar.gz: 2c8710970b09324ab563bacca5ed24ef64b670a68d039050792e9ff3d616773e
3
+ metadata.gz: 172fbe23694c21ce81470eb783654a2ba0a92adbde291050c649579b6d683bfc
4
+ data.tar.gz: 4231e9483c029121c8109f9af40b4eae8f7f284946fd0f5abdfaabee99693a33
5
5
  SHA512:
6
- metadata.gz: 62b396853cb2602f1bf6267341a190ea9e9a80d438d7d5694f1e3df1ba22585e4c64702f244f1714c91508f3876bb9e16f52703d196ae0518337a43006e6d929
7
- data.tar.gz: db71aeff009c49aef8b5b6e4d6695834e3738a6804175edea98da6026d251166a2d3e658192c7c22ff969e8a622557e6f5ddd8f2188a1001cf5e38606a5b2687
6
+ metadata.gz: baafc50d097c21968c2b80055066401de9c4f6d9f02e808e132c73b45072c6b24a56383afefb2ea102f0bb24325e735fd66b916af474f31927089fee0a36d3e0
7
+ data.tar.gz: 685da67abcd323a7b5440716e66fecdc7cd3eaff7e913877d81d2bda4146e6d53c0531447c815d1f56ebe9483a5f51ac53811364be3281e92f81b78747591503
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.7.0](https://github.com/puppetlabs/beaker-pe/tree/3.7.0) (2026-11-13)
4
+
5
+ [Full Changelog](https://github.com/puppetlabs/beaker-pe/compare/3.6.10...3.7.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - \(PE-48580\) Add external test PKI helpers to CAUtils [\#320](https://github.com/puppetlabs/beaker-pe/pull/320) ([jonathannewman](https://github.com/jonathannewman))
10
+
11
+ **Merged pull requests:**
12
+
13
+ - \(maint\) Pin net-ssh \< 8 for acceptance testing [\#321](https://github.com/puppetlabs/beaker-pe/pull/321) ([jonathannewman](https://github.com/jonathannewman))
14
+
3
15
  ## [3.6.10](https://github.com/puppetlabs/beaker-pe/tree/3.6.10) (2026-10-22)
4
16
 
5
17
  [Full Changelog](https://github.com/puppetlabs/beaker-pe/compare/3.6.9...3.6.10)
data/Gemfile CHANGED
@@ -13,6 +13,9 @@ def location_for(place, fake_version = nil)
13
13
  end
14
14
 
15
15
  group :acceptance_testing do
16
+ # beaker 4.x passes `verify_host_key: false` to net-ssh, which net-ssh 8 rejects
17
+ # (ArgumentError: verify_host_key: false has been removed, use :never).
18
+ gem "net-ssh", "< 8"
16
19
  gem "beaker", *location_for(ENV['BEAKER_VERSION'] || '~> 4.0')
17
20
  gem "beaker-vmpooler", *location_for(ENV['BEAKER_VMPOOLER_VERSION'] || '~> 1.3')
18
21
  gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.8')
@@ -1,5 +1,7 @@
1
1
  #Much of this is taken from PuppetSpec:SSL
2
2
  require "openssl"
3
+ require "securerandom"
4
+ require "shellwords"
3
5
 
4
6
  module Beaker
5
7
  module DSL
@@ -26,12 +28,41 @@ module Beaker
26
28
  # Intermediate Issuer: #{INT_CA_NAME}
27
29
  EOT
28
30
 
31
+ # One CA in an external PKI built by create_external_pki / single_tier_ca.
32
+ # revoke_in_pki and refresh_crl each store a new CRL object in crl; a CRL
33
+ # object obtained earlier is never modified.
34
+ PkiLevel = Struct.new(:name, :key, :cert, :crl)
35
+
36
+ # An external PKI, root first. bundle and crl_chain are always built from
37
+ # the current levels, issuing CA first and root last, which is the order
38
+ # pe_install::signing_ca and `puppetserver ca import` expect. Unlike the
39
+ # module-level bundle(*items), they are bare concatenated PEM with no
40
+ # explanatory header comments.
41
+ ExternalPki = Struct.new(:levels) do
42
+ def root
43
+ levels.first
44
+ end
45
+
46
+ def issuing
47
+ levels.last
48
+ end
49
+
50
+ def bundle
51
+ levels.reverse.map { |level| level.cert.to_pem }.join
52
+ end
53
+
54
+ def crl_chain
55
+ levels.reverse.map { |level| level.crl.to_pem }.join
56
+ end
57
+ end
58
+
29
59
  # Generate CA bundle with root and intermediate certs, as well as CRL chain and private key for
30
60
  # the intermediate CA, pushed to the host for use during PE install with pe_install::signing_ca
31
61
  #
32
62
  # @param [Host] host The host to create CA bundle files on. Defaults to global 'master' object.
33
63
  # @param [String] targetdir Location to save files on host, to be referenced in pe.conf for install.
34
64
  # @return [Hash] File names => where they were put on the host
65
+ # TODO PE-48617 targetdir is not shell-escaped and int_key is written with the remote umask; signing_ca_on shows the hardened form.
35
66
  def generate_ca_bundle_on(host = master, targetdir = '/tmp/ca_bundle')
36
67
  files = {}
37
68
  pki = create_chained_pki
@@ -47,7 +78,7 @@ EOT
47
78
  OpenSSL::PKey::RSA.new(length)
48
79
  end
49
80
 
50
- def self_signed_ca(key, name)
81
+ def self_signed_ca(key, name, extensions = CA_EXTENSIONS)
51
82
  cert = OpenSSL::X509::Certificate.new
52
83
 
53
84
  cert.public_key = key.public_key
@@ -61,7 +92,7 @@ EOT
61
92
  cert.not_after = not_before + FIVE_YEARS
62
93
 
63
94
  ext_factory = extension_factory_for(cert, cert)
64
- CA_EXTENSIONS.each do |ext|
95
+ extensions.each do |ext|
65
96
  extension = ext_factory.create_extension(*ext)
66
97
  cert.add_extension(extension)
67
98
  end
@@ -76,13 +107,14 @@ EOT
76
107
 
77
108
  csr.public_key = key.public_key
78
109
  csr.subject = OpenSSL::X509::Name.parse(name)
79
- csr.version = 2
110
+ # PKCS#10 v1 is encoded as 0; OpenSSL 3 rejects the former value of 2.
111
+ csr.version = 0
80
112
  csr.sign(key, DEFAULT_SIGNING_DIGEST)
81
113
 
82
114
  csr
83
115
  end
84
116
 
85
- def sign(ca_key, ca_cert, csr, extensions = NODE_EXTENSIONS)
117
+ def sign(ca_key, ca_cert, csr, extensions = NODE_EXTENSIONS, validity = FIVE_YEARS)
86
118
  cert = OpenSSL::X509::Certificate.new
87
119
 
88
120
  cert.public_key = csr.public_key
@@ -93,7 +125,7 @@ EOT
93
125
 
94
126
  not_before = just_now
95
127
  cert.not_before = not_before
96
- cert.not_after = not_before + FIVE_YEARS
128
+ cert.not_after = not_before + validity
97
129
 
98
130
  ext_factory = extension_factory_for(ca_cert, cert)
99
131
  extensions.each do |ext|
@@ -170,6 +202,268 @@ EOT
170
202
  }
171
203
  end
172
204
 
205
+ # Mint an external PKI on the test runner: a self-signed root plus
206
+ # levels - 1 intermediates, each with its own CRL.
207
+ #
208
+ # Levels are numbered from 0 (root) to levels - 1 (the issuing CA), the
209
+ # same numbering the other helpers take as `level:`.
210
+ #
211
+ # @param [Integer] levels Total CA levels, root included (>= 1).
212
+ # @param [Array<Integer,nil>] path_lens pathLenConstraint per level, root first;
213
+ # nil entries (and the default) leave that level unconstrained.
214
+ # @param [Hash{Integer=>String}] name_constraints level => nameConstraints value,
215
+ # e.g. 'permitted;DNS:example.com'. Added as a critical extension.
216
+ # @param [Hash{Integer=>String}] policy_constraints level => policyConstraints value,
217
+ # e.g. 'requireExplicitPolicy:0'. Added as a critical extension.
218
+ # @param [Hash{Integer=>String}] eku level => extendedKeyUsage value, e.g. 'serverAuth'.
219
+ # @return [ExternalPki] CAs are named /CN=external-root-ca-<id> and
220
+ # /CN=external-int-ca-<level>-<id>, where <id> is random per call so two
221
+ # PKIs on one runner are very unlikely to share a DN.
222
+ # @raise [ArgumentError] if levels is not an Integer >= 1, path_lens is not an
223
+ # Array with one entry per level, a path_lens entry is not nil or a
224
+ # non-negative Integer, or a constraint Hash has a key that is not a level.
225
+ def create_external_pki(levels:, path_lens: nil, name_constraints: {}, policy_constraints: {}, eku: {})
226
+ unless levels.is_a?(Integer) && levels >= 1
227
+ raise ArgumentError, "levels must be an Integer >= 1, got #{levels.inspect}"
228
+ end
229
+ path_lens = Array.new(levels) if path_lens.nil?
230
+ unless path_lens.is_a?(Array) && path_lens.length == levels
231
+ raise ArgumentError, "path_lens needs an Array with one entry per level (#{levels}), got #{path_lens.inspect}"
232
+ end
233
+ name_constraints ||= {}
234
+ policy_constraints ||= {}
235
+ eku ||= {}
236
+ { 'name_constraints' => name_constraints,
237
+ 'policy_constraints' => policy_constraints,
238
+ 'eku' => eku }.each do |arg, per_level|
239
+ raise ArgumentError, "#{arg} must be a Hash of level => value, got #{per_level.inspect}" unless per_level.is_a?(Hash)
240
+
241
+ bad = per_level.keys.reject { |k| k.is_a?(Integer) && k >= 0 && k < levels }
242
+ unless bad.empty?
243
+ raise ArgumentError, "#{arg} has keys outside the levels 0..#{levels - 1}: #{bad.inspect}"
244
+ end
245
+ end
246
+
247
+ id = SecureRandom.hex(3)
248
+ chain = []
249
+ levels.times do |i|
250
+ name = i.zero? ? "/CN=external-root-ca-#{id}" : "/CN=external-int-ca-#{i}-#{id}"
251
+ extensions = ca_extensions_for(path_lens[i],
252
+ name_constraints: name_constraints[i],
253
+ policy_constraints: policy_constraints[i],
254
+ eku: eku[i])
255
+ key = create_private_key
256
+ cert = if i.zero?
257
+ self_signed_ca(key, name, extensions)
258
+ else
259
+ parent = chain.last
260
+ sign(parent.key, parent.cert, create_csr(key, name), extensions)
261
+ end
262
+ chain << PkiLevel.new(name, key, cert, create_crl_for(cert, key))
263
+ end
264
+
265
+ ExternalPki.new(chain.freeze)
266
+ end
267
+
268
+ # A self-signed CA with its CRL, for single-tier fixtures (root = signer).
269
+ #
270
+ # @param [String] name A CN, or a full DN starting with '/'.
271
+ # @return [ExternalPki] with exactly one level
272
+ # @raise [ArgumentError] if name is empty
273
+ def single_tier_ca(name)
274
+ name = name.to_s
275
+ raise ArgumentError, 'name must not be empty' if name.empty?
276
+
277
+ dn = name.start_with?('/') ? name : "/CN=#{name}"
278
+ key = create_private_key
279
+ cert = self_signed_ca(key, dn)
280
+ ExternalPki.new([PkiLevel.new(dn, key, cert, create_crl_for(cert, key))].freeze)
281
+ end
282
+
283
+ # Mint a PE signing CA under the PKI's issuing CA (the last level, pki.issuing)
284
+ # and push the files PE needs (pe_install::signing_ca / `puppetserver ca import`)
285
+ # to the host. The directory is created mode 700 and the private key mode 600.
286
+ #
287
+ # @param [Host] host Host to write the files on.
288
+ # @param [ExternalPki] pki The PKI whose issuing CA signs the signer.
289
+ # @param [Integer,nil] signer_path_len Required. pathLenConstraint on the signer:
290
+ # nil leaves it unconstrained (the shape create_chained_pki produces), 0 forbids
291
+ # any CA below the signer, 1 allows one more CA level below it.
292
+ # @param [String] targetdir Directory on host to write into.
293
+ # @param [String] signer_name Signer DN. Defaults to INT_CA_NAME, keeping the
294
+ # /CN=intermediate-ca that generate_ca_bundle_on's signer has, for acceptance
295
+ # checks that match on that name.
296
+ # @return [Hash] same keys as generate_ca_bundle_on => paths on the host. The bundle
297
+ # and CRL chain files are plain PEM, without generate_ca_bundle_on's explanatory
298
+ # header comments.
299
+ def signing_ca_on(host, pki, signer_path_len:, targetdir: '/tmp/ca_bundle', signer_name: INT_CA_NAME)
300
+ issuer = pki.issuing
301
+ key = create_private_key
302
+ cert = sign(issuer.key, issuer.cert, create_csr(key, signer_name),
303
+ ca_extensions_for(signer_path_len))
304
+ crl = create_crl_for(cert, key)
305
+
306
+ contents = {
307
+ :root_cert => pki.root.cert.to_pem,
308
+ :int_cert => cert.to_pem,
309
+ :int_ca_bundle => cert.to_pem + pki.bundle,
310
+ :int_key => key.to_pem,
311
+ :int_crl_chain => crl.to_pem + pki.crl_chain,
312
+ }
313
+
314
+ dir = Shellwords.escape(targetdir)
315
+ on(host, "mkdir -p #{dir} && chmod 700 #{dir}", :acceptable_exit_codes => [0])
316
+ files = {}
317
+ contents.each do |name, pem|
318
+ create_remote_file(host, "#{targetdir}/#{name}", pem, :acceptable_exit_codes => [0])
319
+ files[name] = "#{targetdir}/#{name}"
320
+ end
321
+ on(host, "chmod 600 #{Shellwords.escape(files[:int_key])}", :acceptable_exit_codes => [0])
322
+ files
323
+ end
324
+
325
+ # Sign a CSR (for example from `external-ca csr`) as a CA certificate
326
+ # under the given level, keeping the CSR's subject and public key.
327
+ #
328
+ # @param [ExternalPki] pki The PKI whose level signs the CSR.
329
+ # @param [String,OpenSSL::X509::Request] csr_pem The CSR, PEM or parsed.
330
+ # @param [Integer] level Issuing level, 0 = root.
331
+ # @param [Numeric] validity Lifetime in seconds.
332
+ # @param [Integer,nil] path_len pathLenConstraint on the new CA, nil for none.
333
+ # @return [OpenSSL::X509::Certificate]
334
+ # @raise [ArgumentError] if level is outside the PKI, validity is not Numeric,
335
+ # path_len is not nil or a non-negative Integer, or the CSR signature does
336
+ # not verify.
337
+ def cross_sign_csr(pki, csr_pem, level:, validity:, path_len:)
338
+ issuer = pki_level(pki, level)
339
+ check_validity!(validity)
340
+ csr = csr_pem.is_a?(OpenSSL::X509::Request) ? csr_pem : OpenSSL::X509::Request.new(csr_pem)
341
+ raise ArgumentError, 'CSR signature does not verify' unless csr.verify(csr.public_key)
342
+
343
+ sign(issuer.key, issuer.cert, csr, ca_extensions_for(path_len), validity)
344
+ end
345
+
346
+ # Re-issue a certificate with the same subject, key and SKI and a new
347
+ # serial, signed by the given level. Extensions other than the AKI are
348
+ # copied from the original.
349
+ #
350
+ # @param [ExternalPki] pki
351
+ # @param [OpenSSL::X509::Certificate] cert Certificate to renew.
352
+ # @param [Integer] issuer_level Issuing level, 0 = root.
353
+ # @param [Numeric] validity Lifetime in seconds. Zero or negative gives an
354
+ # already-expired certificate, which is useful for expiry scenarios.
355
+ # @return [OpenSSL::X509::Certificate] Signed by issuer_level whether or not
356
+ # cert was originally issued there.
357
+ # @raise [ArgumentError] if issuer_level is outside the PKI or validity is not Numeric.
358
+ def reissue_same_key(pki, cert, issuer_level:, validity:)
359
+ issuer = pki_level(pki, issuer_level)
360
+ check_validity!(validity)
361
+ renewed = OpenSSL::X509::Certificate.new
362
+ renewed.version = 2
363
+ renewed.serial = rand(2**128)
364
+ renewed.subject = cert.subject
365
+ renewed.issuer = issuer.cert.subject
366
+ renewed.public_key = cert.public_key
367
+
368
+ not_before = just_now
369
+ renewed.not_before = not_before
370
+ renewed.not_after = not_before + validity
371
+
372
+ cert.extensions.each do |ext|
373
+ renewed.add_extension(ext) unless ext.oid == 'authorityKeyIdentifier'
374
+ end
375
+ aki = extension_factory_for(issuer.cert, renewed)
376
+ .create_extension('authorityKeyIdentifier', 'keyid:always', false)
377
+ renewed.add_extension(aki)
378
+
379
+ renewed.sign(issuer.key, DEFAULT_SIGNING_DIGEST)
380
+ renewed
381
+ end
382
+
383
+ # Publish an external revocation: add serial to a copy of the level's CRL,
384
+ # bump its CRL number and re-sign it. lastUpdate and nextUpdate are kept, so
385
+ # revoking on a CRL made expired by refresh_crl leaves it expired.
386
+ #
387
+ # @param [ExternalPki] pki
388
+ # @param [Integer] level Level whose CRL gets the entry, 0 = root.
389
+ # @param [Integer] serial Serial number of the certificate to revoke.
390
+ # @return [OpenSSL::X509::CRL] the new CRL (also stored on the level)
391
+ # @raise [ArgumentError] if level is outside the PKI
392
+ def revoke_in_pki(pki, level:, serial:)
393
+ target = pki_level(pki, level)
394
+ target.crl = revoke(serial, target.crl.dup, target.key)
395
+ end
396
+
397
+ # Re-issue the level's CRL with the given nextUpdate and a bumped CRL
398
+ # number, keeping existing revocations. A nextUpdate in the past yields
399
+ # an expired CRL; one just ahead yields a short-lived CRL.
400
+ #
401
+ # @param [ExternalPki] pki
402
+ # @param [Integer] level Level whose CRL is re-issued, 0 = root.
403
+ # @param [Time] next_update The new nextUpdate. If it is not after now,
404
+ # lastUpdate is set to an hour before it so lastUpdate stays earlier than
405
+ # nextUpdate; otherwise lastUpdate is one second before now.
406
+ # @return [OpenSSL::X509::CRL] the new CRL (also stored on the level)
407
+ # @raise [ArgumentError] if level is outside the PKI or next_update is not a Time
408
+ def refresh_crl(pki, level:, next_update:)
409
+ target = pki_level(pki, level)
410
+ unless next_update.is_a?(Time)
411
+ raise ArgumentError, "next_update must be a Time, got #{next_update.inspect}"
412
+ end
413
+
414
+ old = target.crl
415
+
416
+ crl = OpenSSL::X509::CRL.new
417
+ crl.version = 1
418
+ crl.issuer = target.cert.subject
419
+ crl.add_extension(
420
+ extension_factory_for(target.cert).create_extension(["authorityKeyIdentifier", "keyid:always", false]))
421
+ next_number = old.extensions.find { |e| e.oid == 'crlNumber' }.value.to_i + 1
422
+ crl.add_extension(
423
+ OpenSSL::X509::Extension.new("crlNumber", OpenSSL::ASN1::Integer(next_number)))
424
+ old.revoked.each { |entry| crl.add_revoked(entry) }
425
+
426
+ last_update = just_now
427
+ last_update = next_update - 3600 if next_update <= last_update + 1
428
+ crl.last_update = last_update
429
+ crl.next_update = next_update
430
+ crl.sign(target.key, DEFAULT_SIGNING_DIGEST)
431
+
432
+ target.crl = crl
433
+ end
434
+
435
+ # CA_EXTENSIONS with basicConstraints replaced by one carrying the given
436
+ # pathLenConstraint (nil = none, else a non-negative Integer), plus optional
437
+ # nameConstraints and policyConstraints (both critical) and extendedKeyUsage
438
+ # (non-critical). keyUsage, SKI and AKI are kept from CA_EXTENSIONS.
439
+ def ca_extensions_for(path_len, name_constraints: nil, policy_constraints: nil, eku: nil)
440
+ unless path_len.nil? || (path_len.is_a?(Integer) && path_len >= 0)
441
+ raise ArgumentError, "path_len must be nil or a non-negative Integer, got #{path_len.inspect}"
442
+ end
443
+
444
+ basic = path_len.nil? ? 'CA:TRUE' : "CA:TRUE,pathlen:#{path_len}"
445
+ extensions = [["basicConstraints", basic, true]] +
446
+ CA_EXTENSIONS.reject { |ext| ext[0] == "basicConstraints" }
447
+ extensions << ["nameConstraints", name_constraints, true] if name_constraints
448
+ extensions << ["policyConstraints", policy_constraints, true] if policy_constraints
449
+ extensions << ["extendedKeyUsage", eku, false] if eku
450
+ extensions
451
+ end
452
+
453
+ def pki_level(pki, level)
454
+ unless level.is_a?(Integer) && level >= 0 && level < pki.levels.length
455
+ raise ArgumentError, "level #{level.inspect} is outside the PKI (0..#{pki.levels.length - 1})"
456
+ end
457
+
458
+ pki.levels[level]
459
+ end
460
+
461
+ def check_validity!(validity)
462
+ return if validity.is_a?(Numeric)
463
+
464
+ raise ArgumentError, "validity must be Numeric seconds, got #{validity.inspect}"
465
+ end
466
+
173
467
  def just_now
174
468
  Time.now - 1
175
469
  end
@@ -3,7 +3,7 @@ module Beaker
3
3
  module PE
4
4
 
5
5
  module Version
6
- STRING = '3.6.10'
6
+ STRING = '3.7.0'
7
7
  end
8
8
 
9
9
  end
@@ -19,7 +19,7 @@ describe Beaker::DSL::InstallUtils::CAUtils do
19
19
  describe 'generate_ca_bundle_on' do
20
20
  let(:host) { make_host( 'unixhost', { :platform => 'linux'})}
21
21
  let(:bundledir) { '/tmp/ca_bundle' }
22
- let(:expected) {
22
+ let(:expected) {
23
23
  {
24
24
  :root_cert => "#{bundledir}/root_cert",
25
25
  :int_cert => "#{bundledir}/int_cert",
@@ -39,4 +39,607 @@ describe Beaker::DSL::InstallUtils::CAUtils do
39
39
  expect( subject.generate_ca_bundle_on(host,"#{bundledir}") ).to eq(expected)
40
40
  end
41
41
  end
42
- end
42
+
43
+ # Helpers shared by the external PKI specs below.
44
+ def pem_blocks(pem, kind)
45
+ pem.scan(/-----BEGIN #{kind}-----.*?-----END #{kind}-----/m)
46
+ end
47
+
48
+ def certs_in(pem)
49
+ pem_blocks(pem, 'CERTIFICATE').map { |p| OpenSSL::X509::Certificate.new(p) }
50
+ end
51
+
52
+ def crls_in(pem)
53
+ pem_blocks(pem, 'X509 CRL').map { |p| OpenSSL::X509::CRL.new(p) }
54
+ end
55
+
56
+ def ext_of(cert, oid)
57
+ cert.extensions.find { |e| e.oid == oid }
58
+ end
59
+
60
+ def crl_number(crl)
61
+ crl.extensions.find { |e| e.oid == 'crlNumber' }.value.to_i
62
+ end
63
+
64
+ def ski_of(cert)
65
+ ext_of(cert, 'subjectKeyIdentifier').value
66
+ end
67
+
68
+ def aki_of(cert)
69
+ ext_of(cert, 'authorityKeyIdentifier').value.sub(/\Akeyid:/, '').lines.first.strip
70
+ end
71
+
72
+ describe 'create_chained_pki (unchanged behaviour)' do
73
+ before(:each) do
74
+ allow(subject).to receive(:create_chained_pki).and_call_original
75
+ end
76
+
77
+ it 'keeps the signer CN as intermediate-ca, unconstrained, for verify_intermediate_ca.rb' do
78
+ pki = subject.create_chained_pki
79
+ expect(pki[:int_cert].subject.to_s).to eq('/CN=intermediate-ca')
80
+ expect(pki[:root_cert].subject.to_s).to eq('/CN=root-ca')
81
+ basic = ext_of(pki[:int_cert], 'basicConstraints')
82
+ expect(basic.value).to eq('CA:TRUE')
83
+ expect(basic).to be_critical
84
+ end
85
+
86
+ it 'encodes the CSR as PKCS#10 v1 (version 0) so OpenSSL 3 accepts it' do
87
+ csr = subject.create_csr(subject.create_private_key, '/CN=csr-version')
88
+ expect(csr.version).to eq(0)
89
+ expect(csr.verify(csr.public_key)).to be true
90
+ end
91
+
92
+ it 'still produces a verifiable chain with CRLs and the original keyUsage' do
93
+ pki = subject.create_chained_pki
94
+ expect(pki[:root_cert].verify(pki[:root_cert].public_key)).to be true
95
+ expect(pki[:int_cert].verify(pki[:root_cert].public_key)).to be true
96
+ expect(ext_of(pki[:int_cert], 'keyUsage').value).to eq('Certificate Sign, CRL Sign')
97
+ crls = crls_in(pki[:int_crl_chain])
98
+ expect(crls.map { |c| c.issuer.to_s }).to eq(['/CN=intermediate-ca', '/CN=root-ca'])
99
+ expect(crls.first.verify(pki[:int_cert].public_key)).to be true
100
+ expect(crls.last.verify(pki[:root_cert].public_key)).to be true
101
+ end
102
+
103
+ it 'keeps the five year validity on certificates' do
104
+ pki = subject.create_chained_pki
105
+ lifetime = pki[:int_cert].not_after - pki[:int_cert].not_before
106
+ expect(lifetime).to be_within(2).of(Beaker::DSL::InstallUtils::CAUtils::FIVE_YEARS)
107
+ end
108
+ end
109
+
110
+ describe 'create_external_pki' do
111
+ it 'rejects fewer than one level' do
112
+ expect { subject.create_external_pki(levels: 0) }.to raise_error(ArgumentError, /levels/)
113
+ end
114
+
115
+ it 'rejects a path_lens array whose length does not match levels' do
116
+ expect { subject.create_external_pki(levels: 2, path_lens: [1]) }.to raise_error(ArgumentError, /path_lens/)
117
+ end
118
+
119
+ it 'rejects a path_lens that is not an Array' do
120
+ expect { subject.create_external_pki(levels: 1, path_lens: 1) }.to raise_error(ArgumentError, /path_lens/)
121
+ end
122
+
123
+ it 'rejects a negative or non-Integer pathlen' do
124
+ expect { subject.create_external_pki(levels: 1, path_lens: [-1]) }.to raise_error(ArgumentError, /path_len/)
125
+ expect { subject.create_external_pki(levels: 1, path_lens: [1.5]) }.to raise_error(ArgumentError, /path_len/)
126
+ expect { subject.create_external_pki(levels: 1, path_lens: ['1']) }.to raise_error(ArgumentError, /path_len/)
127
+ end
128
+
129
+ it 'rejects constraint keys that are not levels instead of ignoring them' do
130
+ expect { subject.create_external_pki(levels: 2, name_constraints: { 5 => 'permitted;DNS:example.com' }) }
131
+ .to raise_error(ArgumentError, /name_constraints.*\[5\]/)
132
+ expect { subject.create_external_pki(levels: 2, policy_constraints: { '1' => 'requireExplicitPolicy:0' }) }
133
+ .to raise_error(ArgumentError, /policy_constraints.*"1"/)
134
+ expect { subject.create_external_pki(levels: 2, eku: { 2 => 'serverAuth' }) }
135
+ .to raise_error(ArgumentError, /eku/)
136
+ end
137
+
138
+ it 'rejects a constraint argument that is not a Hash' do
139
+ expect { subject.create_external_pki(levels: 1, eku: []) }.to raise_error(ArgumentError, /eku must be a Hash/)
140
+ end
141
+
142
+ it 'returns a PKI whose level list cannot be modified' do
143
+ expect(subject.create_external_pki(levels: 2).levels).to be_frozen
144
+ end
145
+
146
+ it 'defaults every level to an unconstrained pathlen' do
147
+ pki = subject.create_external_pki(levels: 2)
148
+ pki.levels.each do |level|
149
+ basic = ext_of(level.cert, 'basicConstraints')
150
+ expect(basic).to be_critical
151
+ expect(basic.value).to eq('CA:TRUE')
152
+ end
153
+ end
154
+
155
+ it 'makes the root self-signed and each intermediate issued by the level above' do
156
+ pki = subject.create_external_pki(levels: 3)
157
+ root, int1, int2 = pki.levels
158
+ expect(root.cert.issuer.to_s).to eq(root.cert.subject.to_s)
159
+ expect(int1.cert.issuer.to_s).to eq(root.cert.subject.to_s)
160
+ expect(int2.cert.issuer.to_s).to eq(int1.cert.subject.to_s)
161
+ expect(pki.levels.map { |l| l.cert.subject.to_s }.uniq.length).to eq(3)
162
+ end
163
+
164
+ it 'encodes pathlen 0, 1 and nil as asked on each level, always critical' do
165
+ pki = subject.create_external_pki(levels: 3, path_lens: [nil, 1, 0])
166
+ values = pki.levels.map { |l| ext_of(l.cert, 'basicConstraints') }
167
+ expect(values.map(&:critical?)).to eq([true, true, true])
168
+ expect(values.map(&:value)).to eq(['CA:TRUE', 'CA:TRUE, pathlen:1', 'CA:TRUE, pathlen:0'])
169
+ end
170
+
171
+ it 'sets keyUsage keyCertSign and cRLSign on every level' do
172
+ pki = subject.create_external_pki(levels: 2)
173
+ pki.levels.each do |level|
174
+ usage = ext_of(level.cert, 'keyUsage')
175
+ expect(usage).to be_critical
176
+ expect(usage.value).to eq('Certificate Sign, CRL Sign')
177
+ end
178
+ end
179
+
180
+ it 'adds nameConstraints and policyConstraints only on the requested level' do
181
+ pki = subject.create_external_pki(
182
+ levels: 3,
183
+ name_constraints: { 1 => 'permitted;DNS:example.com' },
184
+ policy_constraints: { 2 => 'requireExplicitPolicy:0' }
185
+ )
186
+ oids = pki.levels.map { |l| l.cert.extensions.map(&:oid) }
187
+ expect(oids.map { |o| o.include?('nameConstraints') }).to eq([false, true, false])
188
+ expect(oids.map { |o| o.include?('policyConstraints') }).to eq([false, false, true])
189
+ expect(ext_of(pki.levels[1].cert, 'nameConstraints').value).to match(/example\.com/)
190
+ expect(ext_of(pki.levels[1].cert, 'nameConstraints')).to be_critical
191
+ expect(ext_of(pki.levels[2].cert, 'policyConstraints').value).to match(/Require Explicit Policy:0/)
192
+ expect(ext_of(pki.levels[2].cert, 'policyConstraints')).to be_critical
193
+ end
194
+
195
+ it 'adds extendedKeyUsage only on the requested level' do
196
+ pki = subject.create_external_pki(levels: 2, eku: { 1 => 'serverAuth' })
197
+ expect(ext_of(pki.levels[0].cert, 'extendedKeyUsage')).to be_nil
198
+ expect(ext_of(pki.levels[1].cert, 'extendedKeyUsage').value).to match(/TLS Web Server Authentication/)
199
+ end
200
+
201
+ it 'gives every level a CRL that verifies against its own issuer' do
202
+ pki = subject.create_external_pki(levels: 3)
203
+ pki.levels.each do |level|
204
+ expect(level.crl.issuer.to_s).to eq(level.cert.subject.to_s)
205
+ expect(level.crl.verify(level.key)).to be true
206
+ expect(level.crl.verify(level.cert.public_key)).to be true
207
+ end
208
+ end
209
+
210
+ it 'orders bundle issuing CA first and root last' do
211
+ pki = subject.create_external_pki(levels: 3)
212
+ bundle = certs_in(pki.bundle)
213
+ expect(bundle.map { |c| c.subject.to_s }).to eq(pki.levels.reverse.map { |l| l.cert.subject.to_s })
214
+ expect(bundle.last.issuer.to_s).to eq(bundle.last.subject.to_s)
215
+ end
216
+
217
+ it 'orders crl_chain issuing CA first and root last' do
218
+ pki = subject.create_external_pki(levels: 3)
219
+ crls = crls_in(pki.crl_chain)
220
+ expect(crls.map { |c| c.issuer.to_s }).to eq(pki.levels.reverse.map { |l| l.cert.subject.to_s })
221
+ end
222
+
223
+ it 'exposes root and issuing level conveniences' do
224
+ pki = subject.create_external_pki(levels: 3)
225
+ expect(pki.root).to equal(pki.levels.first)
226
+ expect(pki.issuing).to equal(pki.levels.last)
227
+ end
228
+ end
229
+
230
+ describe 'signing_ca_on' do
231
+ let(:host) { make_host('unixhost', { :platform => 'linux' }) }
232
+ let(:targetdir) { '/tmp/external_ca' }
233
+ let(:written) { {} }
234
+ let(:pki) { subject.create_external_pki(levels: 2, path_lens: [nil, 1]) }
235
+
236
+ before(:each) do
237
+ allow(subject).to receive(:on)
238
+ allow(subject).to receive(:create_remote_file) do |_host, path, content, *|
239
+ written[path] = content
240
+ end
241
+ end
242
+
243
+ it 'requires signer_path_len to be given explicitly' do
244
+ expect { subject.signing_ca_on(host, pki, targetdir: targetdir) }.to raise_error(ArgumentError, /signer_path_len/)
245
+ end
246
+
247
+ it 'creates the target directory mode 700 and restricts the private key to mode 600' do
248
+ expect(subject).to receive(:on).with(host, "mkdir -p #{targetdir} && chmod 700 #{targetdir}", :acceptable_exit_codes => [0]).ordered
249
+ expect(subject).to receive(:on).with(host, "chmod 600 #{targetdir}/int_key", :acceptable_exit_codes => [0]).ordered
250
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
251
+ end
252
+
253
+ it 'shell-escapes the target directory' do
254
+ expect(subject).to receive(:on).with(host, 'mkdir -p /tmp/a\\ b && chmod 700 /tmp/a\\ b', :acceptable_exit_codes => [0])
255
+ expect(subject).to receive(:on).with(host, 'chmod 600 /tmp/a\\ b/int_key', :acceptable_exit_codes => [0])
256
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: '/tmp/a b')
257
+ end
258
+
259
+ it 'rejects a negative signer_path_len' do
260
+ expect { subject.signing_ca_on(host, pki, signer_path_len: -1, targetdir: targetdir) }
261
+ .to raise_error(ArgumentError, /path_len/)
262
+ end
263
+
264
+ it 'creates the target directory and returns the paths setup/intermediate_ca.rb uses' do
265
+ files = subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
266
+ expect(files).to eq(
267
+ :root_cert => "#{targetdir}/root_cert",
268
+ :int_cert => "#{targetdir}/int_cert",
269
+ :int_ca_bundle => "#{targetdir}/int_ca_bundle",
270
+ :int_key => "#{targetdir}/int_key",
271
+ :int_crl_chain => "#{targetdir}/int_crl_chain"
272
+ )
273
+ expect(written.keys).to match_array(files.values)
274
+ end
275
+
276
+ it 'mints a signer named intermediate-ca under the issuing CA' do
277
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
278
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
279
+ expect(signer.subject.to_s).to eq('/CN=intermediate-ca')
280
+ expect(signer.issuer.to_s).to eq(pki.issuing.cert.subject.to_s)
281
+ expect(signer.verify(pki.issuing.cert.public_key)).to be true
282
+ end
283
+
284
+ [[1, 'CA:TRUE, pathlen:1'], [0, 'CA:TRUE, pathlen:0'], [nil, 'CA:TRUE']].each do |len, expected|
285
+ it "encodes signer_path_len #{len.inspect} as #{expected.inspect}" do
286
+ subject.signing_ca_on(host, pki, signer_path_len: len, targetdir: targetdir)
287
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
288
+ basic = ext_of(signer, 'basicConstraints')
289
+ expect(basic).to be_critical
290
+ expect(basic.value).to eq(expected)
291
+ end
292
+ end
293
+
294
+ it 'writes a bundle with the signer first and the PKI bundle after it' do
295
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
296
+ bundle = certs_in(written["#{targetdir}/int_ca_bundle"])
297
+ expect(bundle.map { |c| c.subject.to_s }).to eq(['/CN=intermediate-ca'] + certs_in(pki.bundle).map { |c| c.subject.to_s })
298
+ end
299
+
300
+ it 'writes a crl chain with the signer CRL first, verified by the signer key' do
301
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
302
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
303
+ crls = crls_in(written["#{targetdir}/int_crl_chain"])
304
+ expect(crls.map { |c| c.issuer.to_s }).to eq(['/CN=intermediate-ca'] + crls_in(pki.crl_chain).map { |c| c.issuer.to_s })
305
+ expect(crls.first.verify(signer.public_key)).to be true
306
+ end
307
+
308
+ it 'writes the signer private key matching the signer certificate' do
309
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
310
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
311
+ key = OpenSSL::PKey::RSA.new(written["#{targetdir}/int_key"])
312
+ expect(signer.check_private_key(key)).to be true
313
+ end
314
+
315
+ it 'produces a chain that verifies a leaf issued by the signer (3-level PKI, signer pathlen 1)' do
316
+ subject.signing_ca_on(host, pki, signer_path_len: 1, targetdir: targetdir)
317
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
318
+ key = OpenSSL::PKey::RSA.new(written["#{targetdir}/int_key"])
319
+ leaf_key = subject.create_private_key
320
+ leaf = subject.sign(key, signer, subject.create_csr(leaf_key, '/CN=leaf'))
321
+
322
+ store = OpenSSL::X509::Store.new
323
+ store.add_cert(pki.root.cert)
324
+ expect(store.verify(leaf, [signer, pki.issuing.cert])).to be true
325
+ end
326
+
327
+ it 'chain verification fails when the issuing CA pathlen 0 forbids the signer CA below it' do
328
+ tight = subject.create_external_pki(levels: 2, path_lens: [nil, 0])
329
+ subject.signing_ca_on(host, tight, signer_path_len: 0, targetdir: targetdir)
330
+ signer = OpenSSL::X509::Certificate.new(written["#{targetdir}/int_cert"])
331
+ key = OpenSSL::PKey::RSA.new(written["#{targetdir}/int_key"])
332
+ leaf = subject.sign(key, signer, subject.create_csr(subject.create_private_key, '/CN=leaf'))
333
+
334
+ store = OpenSSL::X509::Store.new
335
+ store.add_cert(tight.root.cert)
336
+ expect(store.verify(leaf, [signer, tight.issuing.cert])).to be false
337
+ expect(store.error).to eq(OpenSSL::X509::V_ERR_PATH_LENGTH_EXCEEDED)
338
+ end
339
+ end
340
+
341
+ describe 'single_tier_ca' do
342
+ it 'returns a self-signed CA whose CRL verifies' do
343
+ pki = subject.single_tier_ca('single-tier')
344
+ expect(pki.levels.length).to eq(1)
345
+ cert = pki.root.cert
346
+ expect(cert.subject.to_s).to eq('/CN=single-tier')
347
+ expect(cert.issuer.to_s).to eq(cert.subject.to_s)
348
+ expect(cert.verify(cert.public_key)).to be true
349
+ expect(ext_of(cert, 'basicConstraints').value).to eq('CA:TRUE')
350
+ expect(pki.root.crl.verify(pki.root.key)).to be true
351
+ end
352
+
353
+ it 'has the root as the whole bundle and crl chain' do
354
+ pki = subject.single_tier_ca('single-tier')
355
+ expect(certs_in(pki.bundle).map(&:to_der)).to eq([pki.root.cert.to_der])
356
+ expect(crls_in(pki.crl_chain).map(&:to_der)).to eq([pki.root.crl.to_der])
357
+ end
358
+
359
+ it 'rejects an empty name' do
360
+ expect { subject.single_tier_ca('') }.to raise_error(ArgumentError, /name/)
361
+ expect { subject.single_tier_ca(nil) }.to raise_error(ArgumentError, /name/)
362
+ end
363
+
364
+ it 'accepts a full DN as the name' do
365
+ pki = subject.single_tier_ca('/O=Example/CN=other-ca')
366
+ expect(pki.root.cert.subject.to_s).to eq('/O=Example/CN=other-ca')
367
+ end
368
+ end
369
+
370
+ describe 'cross_sign_csr' do
371
+ let(:pki) { subject.create_external_pki(levels: 2, path_lens: [nil, 2]) }
372
+ let(:csr_key) { subject.create_private_key }
373
+ let(:csr) { subject.create_csr(csr_key, '/CN=pe-signer') }
374
+
375
+ it 'keeps the CSR subject and public key and takes the issuer DN' do
376
+ cert = subject.cross_sign_csr(pki, csr.to_pem, level: 1, validity: 86_400, path_len: 1)
377
+ expect(cert.subject.to_s).to eq('/CN=pe-signer')
378
+ expect(cert.public_key.to_der).to eq(csr_key.public_key.to_der)
379
+ expect(cert.issuer.to_s).to eq(pki.levels[1].cert.subject.to_s)
380
+ expect(cert.verify(pki.levels[1].cert.public_key)).to be true
381
+ end
382
+
383
+ it 'sets the AKI to the issuer SKI' do
384
+ cert = subject.cross_sign_csr(pki, csr.to_pem, level: 1, validity: 86_400, path_len: 1)
385
+ expect(aki_of(cert)).to eq(ski_of(pki.levels[1].cert))
386
+ end
387
+
388
+ it 'applies the requested pathlen as a critical CA basicConstraints' do
389
+ [[1, 'CA:TRUE, pathlen:1'], [0, 'CA:TRUE, pathlen:0'], [nil, 'CA:TRUE']].each do |len, expected|
390
+ cert = subject.cross_sign_csr(pki, csr.to_pem, level: 1, validity: 86_400, path_len: len)
391
+ basic = ext_of(cert, 'basicConstraints')
392
+ expect(basic).to be_critical
393
+ expect(basic.value).to eq(expected)
394
+ end
395
+ end
396
+
397
+ it 'applies the requested validity' do
398
+ cert = subject.cross_sign_csr(pki, csr.to_pem, level: 0, validity: 3_600, path_len: nil)
399
+ expect(cert.not_after - cert.not_before).to be_within(2).of(3_600)
400
+ end
401
+
402
+ it 'can sign under the root level' do
403
+ cert = subject.cross_sign_csr(pki, csr.to_pem, level: 0, validity: 3_600, path_len: nil)
404
+ expect(cert.issuer.to_s).to eq(pki.root.cert.subject.to_s)
405
+ end
406
+
407
+ it 'accepts an OpenSSL CSR object as well as PEM' do
408
+ cert = subject.cross_sign_csr(pki, csr, level: 1, validity: 3_600, path_len: nil)
409
+ expect(cert.subject.to_s).to eq('/CN=pe-signer')
410
+ end
411
+
412
+ it 'refuses a CSR whose signature does not verify' do
413
+ other = subject.create_private_key
414
+ bad = OpenSSL::X509::Request.new
415
+ bad.public_key = other.public_key
416
+ bad.subject = OpenSSL::X509::Name.parse('/CN=forged')
417
+ bad.version = 0
418
+ bad.sign(csr_key, Beaker::DSL::InstallUtils::CAUtils::DEFAULT_SIGNING_DIGEST)
419
+ expect { subject.cross_sign_csr(pki, bad.to_pem, level: 1, validity: 3_600, path_len: nil) }
420
+ .to raise_error(ArgumentError, /signature/)
421
+ end
422
+
423
+ it 'refuses a validity that is not Numeric seconds' do
424
+ expect { subject.cross_sign_csr(pki, csr.to_pem, level: 1, validity: '3600', path_len: nil) }
425
+ .to raise_error(ArgumentError, /validity/)
426
+ end
427
+
428
+ it 'refuses an invalid path_len' do
429
+ expect { subject.cross_sign_csr(pki, csr.to_pem, level: 1, validity: 3_600, path_len: -2) }
430
+ .to raise_error(ArgumentError, /path_len/)
431
+ end
432
+
433
+ it 'refuses a level outside the PKI' do
434
+ expect { subject.cross_sign_csr(pki, csr.to_pem, level: 5, validity: 3_600, path_len: nil) }
435
+ .to raise_error(ArgumentError, /level/)
436
+ end
437
+ end
438
+
439
+ describe 'reissue_same_key' do
440
+ let(:pki) { subject.create_external_pki(levels: 2, path_lens: [nil, 2]) }
441
+ let(:csr_key) { subject.create_private_key }
442
+ let(:original) do
443
+ subject.cross_sign_csr(pki, subject.create_csr(csr_key, '/CN=pe-signer').to_pem,
444
+ level: 1, validity: 3_600, path_len: 1)
445
+ end
446
+
447
+ it 'keeps subject, key and SKI and gets a new serial' do
448
+ renewed = subject.reissue_same_key(pki, original, issuer_level: 1, validity: 7_200)
449
+ expect(renewed.subject.to_s).to eq(original.subject.to_s)
450
+ expect(renewed.public_key.to_der).to eq(original.public_key.to_der)
451
+ expect(ski_of(renewed)).to eq(ski_of(original))
452
+ expect(renewed.serial).not_to eq(original.serial)
453
+ end
454
+
455
+ it 'is signed by the issuer level and carries its AKI' do
456
+ renewed = subject.reissue_same_key(pki, original, issuer_level: 1, validity: 7_200)
457
+ expect(renewed.issuer.to_s).to eq(pki.levels[1].cert.subject.to_s)
458
+ expect(renewed.verify(pki.levels[1].cert.public_key)).to be true
459
+ expect(aki_of(renewed)).to eq(ski_of(pki.levels[1].cert))
460
+ end
461
+
462
+ it 'keeps the original basicConstraints and keyUsage' do
463
+ renewed = subject.reissue_same_key(pki, original, issuer_level: 1, validity: 7_200)
464
+ expect(ext_of(renewed, 'basicConstraints').value).to eq('CA:TRUE, pathlen:1')
465
+ expect(ext_of(renewed, 'basicConstraints')).to be_critical
466
+ expect(ext_of(renewed, 'keyUsage').value).to eq(ext_of(original, 'keyUsage').value)
467
+ end
468
+
469
+ it 'applies the new validity' do
470
+ renewed = subject.reissue_same_key(pki, original, issuer_level: 1, validity: 7_200)
471
+ expect(renewed.not_after - renewed.not_before).to be_within(2).of(7_200)
472
+ end
473
+
474
+ it 'can reissue the self-signed root under the root level' do
475
+ renewed = subject.reissue_same_key(pki, pki.root.cert, issuer_level: 0, validity: 7_200)
476
+ expect(renewed.subject.to_s).to eq(pki.root.cert.subject.to_s)
477
+ expect(renewed.issuer.to_s).to eq(renewed.subject.to_s)
478
+ expect(renewed.verify(pki.root.cert.public_key)).to be true
479
+ expect(renewed.serial).not_to eq(pki.root.cert.serial)
480
+ end
481
+
482
+ it 'refuses a validity that is not Numeric seconds' do
483
+ expect { subject.reissue_same_key(pki, original, issuer_level: 1, validity: nil) }
484
+ .to raise_error(ArgumentError, /validity/)
485
+ end
486
+
487
+ it 'can reissue under a different issuer level than the original' do
488
+ renewed = subject.reissue_same_key(pki, original, issuer_level: 0, validity: 7_200)
489
+ expect(renewed.issuer.to_s).to eq(pki.root.cert.subject.to_s)
490
+ expect(aki_of(renewed)).to eq(ski_of(pki.root.cert))
491
+ end
492
+ end
493
+
494
+ describe 'revoke_in_pki' do
495
+ let(:pki) { subject.create_external_pki(levels: 3) }
496
+
497
+ it 'adds the serial to the level CRL and raises the CRL number' do
498
+ before_number = crl_number(pki.levels[1].crl)
499
+ crl = subject.revoke_in_pki(pki, level: 1, serial: 4242)
500
+ expect(crl.revoked.map { |r| r.serial.to_i }).to eq([4242])
501
+ expect(crl_number(crl)).to eq(before_number + 1)
502
+ expect(pki.levels[1].crl).to equal(crl)
503
+ end
504
+
505
+ it 'leaves the other levels CRLs alone' do
506
+ others = [0, 2].map { |i| pki.levels[i].crl.to_der }
507
+ subject.revoke_in_pki(pki, level: 1, serial: 4242)
508
+ expect([0, 2].map { |i| pki.levels[i].crl.to_der }).to eq(others)
509
+ end
510
+
511
+ it 'signs the CRL with the level key and republishes it in crl_chain' do
512
+ subject.revoke_in_pki(pki, level: 1, serial: 4242)
513
+ expect(pki.levels[1].crl.verify(pki.levels[1].cert.public_key)).to be true
514
+ chain = crls_in(pki.crl_chain)
515
+ expect(chain.map { |c| c.revoked.length }).to eq([0, 1, 0])
516
+ end
517
+
518
+ it 'accumulates multiple revocations and bumps the number each time' do
519
+ subject.revoke_in_pki(pki, level: 1, serial: 1)
520
+ crl = subject.revoke_in_pki(pki, level: 1, serial: 2)
521
+ expect(crl.revoked.map { |r| r.serial.to_i }).to eq([1, 2])
522
+ expect(crl_number(crl)).to eq(2)
523
+ end
524
+
525
+ it 'refuses a level outside the PKI' do
526
+ expect { subject.revoke_in_pki(pki, level: 9, serial: 1) }.to raise_error(ArgumentError, /level/)
527
+ end
528
+ end
529
+
530
+ describe 'refresh_crl' do
531
+ let(:pki) { subject.create_external_pki(levels: 2) }
532
+
533
+ it 'yields an expired CRL for a past next_update, with a raised CRL number' do
534
+ before_number = crl_number(pki.levels[1].crl)
535
+ crl = subject.refresh_crl(pki, level: 1, next_update: Time.now - 3_600)
536
+ expect(crl.next_update).to be < Time.now
537
+ expect(crl.last_update).to be < crl.next_update
538
+ expect(crl_number(crl)).to eq(before_number + 1)
539
+ expect(crl.verify(pki.levels[1].cert.public_key)).to be true
540
+ end
541
+
542
+ it 'yields a short-lived CRL for a near next_update' do
543
+ crl = subject.refresh_crl(pki, level: 1, next_update: Time.now + 120)
544
+ expect(crl.next_update - Time.now).to be_within(5).of(120)
545
+ expect(crl.last_update).to be <= Time.now
546
+ end
547
+
548
+ it 'preserves existing revocations' do
549
+ subject.revoke_in_pki(pki, level: 1, serial: 77)
550
+ crl = subject.refresh_crl(pki, level: 1, next_update: Time.now + 3_600)
551
+ expect(crl.revoked.map { |r| r.serial.to_i }).to eq([77])
552
+ end
553
+
554
+ it 'republishes the refreshed CRL in crl_chain' do
555
+ crl = subject.refresh_crl(pki, level: 1, next_update: Time.now - 3_600)
556
+ expect(crls_in(pki.crl_chain).first.to_der).to eq(crl.to_der)
557
+ end
558
+
559
+ it 'refuses a next_update that is not a Time' do
560
+ expect { subject.refresh_crl(pki, level: 1, next_update: 3600) }.to raise_error(ArgumentError, /next_update/)
561
+ end
562
+
563
+ it 'puts lastUpdate an hour before a next_update that is not after now' do
564
+ next_update = Time.now
565
+ crl = subject.refresh_crl(pki, level: 1, next_update: next_update)
566
+ expect(crl.last_update).to be_within(2).of(next_update - 3_600)
567
+ expect(crl.last_update).to be < crl.next_update
568
+ end
569
+
570
+ it 'puts lastUpdate one second before now for a next_update after now' do
571
+ crl = subject.refresh_crl(pki, level: 1, next_update: Time.now + 3_600)
572
+ expect(crl.last_update).to be_within(3).of(Time.now - 1)
573
+ end
574
+
575
+ it 'stays expired when revoke_in_pki later re-signs the CRL' do
576
+ subject.refresh_crl(pki, level: 1, next_update: Time.now - 3_600)
577
+ crl = subject.revoke_in_pki(pki, level: 1, serial: 5)
578
+ expect(crl.next_update).to be < Time.now
579
+ expect(crl.revoked.map { |r| r.serial.to_i }).to eq([5])
580
+ end
581
+
582
+ it 'refuses a level outside the PKI' do
583
+ expect { subject.refresh_crl(pki, level: 4, next_update: Time.now) }.to raise_error(ArgumentError, /level/)
584
+ end
585
+ end
586
+
587
+ describe 'revoke_in_pki aliasing' do
588
+ it 'leaves a previously obtained CRL object unmodified' do
589
+ pki = subject.create_external_pki(levels: 2)
590
+ old = pki.levels[1].crl
591
+ subject.revoke_in_pki(pki, level: 1, serial: 99)
592
+ expect(old.revoked).to be_empty
593
+ expect(crl_number(old)).to eq(0)
594
+ expect(pki.levels[1].crl).not_to equal(old)
595
+ end
596
+ end
597
+
598
+ describe 'an external PKI under OpenSSL::X509::Store with CRL checking' do
599
+ let(:pki) { subject.create_external_pki(levels: 3, path_lens: [nil, 1, 0]) }
600
+ let(:leaf) do
601
+ issuing = pki.issuing
602
+ subject.sign(issuing.key, issuing.cert, subject.create_csr(subject.create_private_key, '/CN=leaf'))
603
+ end
604
+ let(:intermediates) { [pki.levels[2].cert, pki.levels[1].cert] }
605
+
606
+ def store_for(pki)
607
+ store = OpenSSL::X509::Store.new
608
+ store.add_cert(pki.root.cert)
609
+ store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK | OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
610
+ pki.levels.each { |level| store.add_crl(level.crl) }
611
+ store
612
+ end
613
+
614
+ it 'verifies a leaf through root, pathlen 1 intermediate and pathlen 0 issuing CA' do
615
+ expect(store_for(pki).verify(leaf, intermediates)).to be true
616
+ end
617
+
618
+ it 'rejects the leaf once revoke_in_pki publishes its serial at the issuing level' do
619
+ subject.revoke_in_pki(pki, level: 2, serial: leaf.serial)
620
+ store = store_for(pki)
621
+ expect(store.verify(leaf, intermediates)).to be false
622
+ expect(store.error).to eq(OpenSSL::X509::V_ERR_CERT_REVOKED)
623
+ end
624
+
625
+ it 'rejects the chain when an upstream CA is revoked at the level above it' do
626
+ subject.revoke_in_pki(pki, level: 1, serial: pki.levels[2].cert.serial)
627
+ store = store_for(pki)
628
+ expect(store.verify(leaf, intermediates)).to be false
629
+ expect(store.error).to eq(OpenSSL::X509::V_ERR_CERT_REVOKED)
630
+ end
631
+
632
+ it 'rejects the leaf when the issuing CA CRL has expired via refresh_crl' do
633
+ subject.refresh_crl(pki, level: 2, next_update: Time.now - 3_600)
634
+ store = store_for(pki)
635
+ expect(store.verify(leaf, intermediates)).to be false
636
+ expect(store.error).to eq(OpenSSL::X509::V_ERR_CRL_HAS_EXPIRED)
637
+ end
638
+
639
+ it 'accepts the leaf again after refresh_crl re-issues a valid CRL' do
640
+ subject.refresh_crl(pki, level: 2, next_update: Time.now - 3_600)
641
+ subject.refresh_crl(pki, level: 2, next_update: Time.now + 3_600)
642
+ expect(store_for(pki).verify(leaf, intermediates)).to be true
643
+ end
644
+ end
645
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beaker-pe
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.10
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppetlabs