4
4
"context"
5
5
"crypto/x509"
6
6
"encoding/pem"
7
+ "os"
7
8
"testing"
8
9
"time"
9
10
@@ -12,10 +13,13 @@ import (
12
13
"github.com/moby/swarmkit/v2/api"
13
14
"github.com/moby/swarmkit/v2/ca"
14
15
"github.com/moby/swarmkit/v2/ca/testutils"
16
+ "github.com/sirupsen/logrus"
15
17
"github.com/stretchr/testify/assert"
16
18
"github.com/stretchr/testify/require"
17
19
"google.golang.org/grpc/codes"
18
20
"google.golang.org/grpc/status"
21
+
22
+ "github.com/moby/swarmkit/v2/log"
19
23
)
20
24
21
25
type rootCARotationTestCase struct {
@@ -315,15 +319,19 @@ func TestValidateCAConfigInvalidValues(t *testing.T) {
315
319
}
316
320
317
321
func runValidTestCases (t * testing.T , testcases []* rootCARotationTestCase , localRootCA * ca.RootCA ) {
322
+ logrus .SetLevel (logrus .DebugLevel )
323
+ logrus .SetOutput (os .Stdout )
324
+ ctx := log .WithLogger (context .Background (), log .L .WithField ("testname" , t .Name ()))
318
325
for _ , valid := range testcases {
326
+ casectx := log .WithField (ctx , "testcase" , valid .description )
319
327
cluster := & api.Cluster {
320
328
RootCA : * valid .rootCA .Copy (),
321
329
Spec : api.ClusterSpec {
322
330
CAConfig : valid .caConfig ,
323
331
},
324
332
}
325
333
secConfig := getSecurityConfig (t , localRootCA , cluster )
326
- result , err := validateCAConfig (context . Background () , secConfig , cluster )
334
+ result , err := validateCAConfig (casectx , secConfig , cluster )
327
335
require .NoError (t , err , valid .description )
328
336
329
337
// ensure that the cluster was not mutated
@@ -346,8 +354,12 @@ func runValidTestCases(t *testing.T, testcases []*rootCARotationTestCase, localR
346
354
// make sure the cross-signed cert is signed by the current root CA (and not an intermediate, if a root rotation is in progress)
347
355
parsedCross , err := helpers .ParseCertificatePEM (result .RootRotation .CrossSignedCACert ) // there should just be one
348
356
require .NoError (t , err )
357
+
358
+ log .G (casectx ).Debugf ("localRootCA:%s" , localRootCA .Certs )
359
+ log .G (casectx ).Debugf ("CACert:%s" , result .RootRotation .CACert )
360
+ log .G (casectx ).Debugf ("CrossSigned:%s" , result .RootRotation .CrossSignedCACert )
349
361
_ , err = parsedCross .Verify (x509.VerifyOptions {Roots : localRootCA .Pool })
350
- require .NoError (t , err , valid .description )
362
+ assert .NoError (t , err , valid .description )
351
363
352
364
// if we are expecting generated certs or root rotation, we can expect the expected root CA has a root rotation
353
365
result .RootRotation .CrossSignedCACert = valid .expectRootCA .RootRotation .CrossSignedCACert
@@ -365,14 +377,30 @@ func runValidTestCases(t *testing.T, testcases []*rootCARotationTestCase, localR
365
377
}
366
378
}
367
379
380
+ func printCert (t * testing.T , pemData []byte ) {
381
+ t .Helper ()
382
+
383
+ block , _ := pem .Decode (pemData )
384
+ cert , err := x509 .ParseCertificate (block .Bytes )
385
+ if err != nil {
386
+ t .Error (err )
387
+ }
388
+
389
+ cert .RawSubject = nil
390
+ cert .Raw = nil
391
+ cert .RawIssuer = nil
392
+ cert .RawSubjectPublicKeyInfo = nil
393
+ cert .RawTBSCertificate = nil
394
+ cert .Signature = nil
395
+ t .Logf ("%+v" , cert )
396
+ }
397
+
368
398
func TestValidateCAConfigValidValues (t * testing.T ) {
369
399
t .Parallel ()
370
400
localRootCA , err := ca .NewRootCA (testutils .ECDSA256SHA256Cert , testutils .ECDSA256SHA256Cert , testutils .ECDSA256Key ,
371
401
ca .DefaultNodeCertExpiration , nil )
372
402
require .NoError (t , err )
373
403
374
- parsedCert , err := helpers .ParseCertificatePEM (testutils .ECDSA256SHA256Cert )
375
- require .NoError (t , err )
376
404
parsedKey , err := helpers .ParsePrivateKeyPEM (testutils .ECDSA256Key )
377
405
require .NoError (t , err )
378
406
@@ -536,8 +564,7 @@ func TestValidateCAConfigValidValues(t *testing.T) {
536
564
537
565
// These all require a new root rotation because the desired cert is different, even if it has the same key and/or subject as the current
538
566
// cert or the current-to-be-rotated cert.
539
- renewedInitialCert , err := initca .RenewFromSigner (parsedCert , parsedKey )
540
- require .NoError (t , err )
567
+ time .Sleep (5 * time .Second )
541
568
parsedRotationCert , err := helpers .ParseCertificatePEM (rotationCert )
542
569
require .NoError (t , err )
543
570
parsedRotationKey , err := helpers .ParsePrivateKeyPEM (rotationKey )
@@ -554,49 +581,6 @@ func TestValidateCAConfigValidValues(t *testing.T) {
554
581
defer differentExtServer .Stop ()
555
582
require .NoError (t , differentExtServer .EnableCASigning ())
556
583
testcases = []* rootCARotationTestCase {
557
- {
558
- description : "desired cert being a renewed current cert and key results in a root rotation because the cert has changed" ,
559
- rootCA : initialLocalRootCA ,
560
- caConfig : api.CAConfig {
561
- SigningCACert : uglifyOnePEM (renewedInitialCert ),
562
- SigningCAKey : initialLocalRootCA .CAKey ,
563
- ForceRotate : 5 ,
564
- },
565
- expectRootCA : getRootCAWithRotation (expectedBaseRootCA , renewedInitialCert , initialLocalRootCA .CAKey , nil ),
566
- expectGeneratedCross : true ,
567
- },
568
- {
569
- description : "desired cert being a renewed current cert, external->internal results in a root rotation because the cert has changed" ,
570
- rootCA : initialExternalRootCA ,
571
- caConfig : api.CAConfig {
572
- SigningCACert : uglifyOnePEM (renewedInitialCert ),
573
- SigningCAKey : initialLocalRootCA .CAKey ,
574
- ForceRotate : 5 ,
575
- ExternalCAs : []* api.ExternalCA {
576
- {
577
- URL : initExtServer .URL ,
578
- },
579
- },
580
- },
581
- expectRootCA : getRootCAWithRotation (getExpectedRootCA (false ), renewedInitialCert , initialLocalRootCA .CAKey , nil ),
582
- expectGeneratedCross : true ,
583
- },
584
- {
585
- description : "desired cert being a renewed current cert, internal->external results in a root rotation because the cert has changed" ,
586
- rootCA : initialLocalRootCA ,
587
- caConfig : api.CAConfig {
588
- SigningCACert : append ([]byte ("\n \n " ), renewedInitialCert ... ),
589
- ForceRotate : 5 ,
590
- ExternalCAs : []* api.ExternalCA {
591
- {
592
- URL : initExtServer .URL ,
593
- CACert : uglifyOnePEM (renewedInitialCert ),
594
- },
595
- },
596
- },
597
- expectRootCA : getRootCAWithRotation (expectedBaseRootCA , renewedInitialCert , nil , nil ),
598
- expectGeneratedCross : true ,
599
- },
600
584
{
601
585
description : "desired cert being a renewed rotation RootCA cert + rotation key results in replaced root rotation because the cert has changed" ,
602
586
rootCA : getRootCAWithRotation (initialLocalRootCA , rotationCert , rotationKey , crossSigned ),
0 commit comments