@ -396,23 +396,34 @@ func TestLeader_Vault_PrimaryCA_IntermediateRenew(t *testing.T) {
provider , _ := getCAProviderWithLock ( s1 )
intermediatePEM , err := provider . ActiveIntermediate ( )
require . NoError ( err )
_, err = connect . ParseCert ( intermediatePEM )
intermediateCert, err : = connect . ParseCert ( intermediatePEM )
require . NoError ( err )
// Check that the state store has the correct intermediate
store := s1 . caManager . delegate . State ( )
_ , activeRoot , err := store . CARootActive ( nil )
require . NoError ( err )
require . Equal ( intermediatePEM , s1 . caManager . getLeafSigningCertFromRoot ( activeRoot ) )
require . Equal ( connect . HexString ( intermediateCert . SubjectKeyId ) , activeRoot . SigningKeyID )
// Wait for dc1's intermediate to be refreshed.
// It is possible that test fails when the blocking query doesn't return.
retry . Run ( t , func ( r * retry . R ) {
provider , _ = getCAProviderWithLock ( s1 )
newIntermediatePEM , err := provider . ActiveIntermediate ( )
r . Check ( err )
_ , err = connect . ParseCert ( intermediatePEM )
r . Check ( err )
if newIntermediatePEM == intermediatePEM {
r . Fatal ( "not a renewed intermediate" )
}
intermediateCert , err = connect . ParseCert ( newIntermediatePEM )
r . Check ( err )
intermediatePEM = newIntermediatePEM
} )
_ , activeRoot , err = store . CARootActive ( nil )
require . NoError ( err )
require . Equal ( intermediatePEM , s1 . caManager . getLeafSigningCertFromRoot ( activeRoot ) )
require . Equal ( connect . HexString ( intermediateCert . SubjectKeyId ) , activeRoot . SigningKeyID )
// Get the root from dc1 and validate a chain of:
// dc1 leaf -> dc1 intermediate -> dc1 root
@ -439,6 +450,8 @@ func TestLeader_Vault_PrimaryCA_IntermediateRenew(t *testing.T) {
// Check that the leaf signed by the new intermediate can be verified using the
// returned cert chain (signed intermediate + remote root).
intermediatePool := x509 . NewCertPool ( )
// TODO: do not explicitly add the intermediatePEM, we should have it available
// from leafPEM. Use connect.ParseLeafCerts to do the right thing.
intermediatePool . AppendCertsFromPEM ( [ ] byte ( intermediatePEM ) )
rootPool := x509 . NewCertPool ( )
rootPool . AppendCertsFromPEM ( [ ] byte ( caRoot . RootCert ) )
@ -515,10 +528,10 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
secondaryProvider , _ := getCAProviderWithLock ( s2 )
intermediatePEM , err := secondaryProvider . ActiveIntermediate ( )
require . NoError ( err )
c ert, err := connect . ParseCert ( intermediatePEM )
intermediateC ert, err := connect . ParseCert ( intermediatePEM )
require . NoError ( err )
currentCertSerialNumber := c ert. SerialNumber
currentCertAuthorityKeyId := c ert. AuthorityKeyId
currentCertSerialNumber := intermediateC ert. SerialNumber
currentCertAuthorityKeyId := intermediateC ert. AuthorityKeyId
// Capture the current root
var originalRoot * structs . CARoot
@ -532,6 +545,12 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
waitForActiveCARoot ( t , s1 , originalRoot )
waitForActiveCARoot ( t , s2 , originalRoot )
store := s2 . fsm . State ( )
_ , activeRoot , err := store . CARootActive ( nil )
require . NoError ( err )
require . Equal ( intermediatePEM , s2 . caManager . getLeafSigningCertFromRoot ( activeRoot ) )
require . Equal ( connect . HexString ( intermediateCert . SubjectKeyId ) , activeRoot . SigningKeyID )
// Wait for dc2's intermediate to be refreshed.
// It is possible that test fails when the blocking query doesn't return.
// When https://github.com/hashicorp/consul/pull/3777 is merged
@ -548,8 +567,13 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
currentCertAuthorityKeyId = cert . AuthorityKeyId
r . Fatal ( "not a renewed intermediate" )
}
intermediateCert = cert
} )
_ , activeRoot , err = store . CARootActive ( nil )
require . NoError ( err )
require . Equal ( intermediatePEM , s2 . caManager . getLeafSigningCertFromRoot ( activeRoot ) )
require . Equal ( connect . HexString ( intermediateCert . SubjectKeyId ) , activeRoot . SigningKeyID )
// Get the root from dc1 and validate a chain of:
// dc2 leaf -> dc2 intermediate -> dc1 root
@ -570,17 +594,19 @@ func TestLeader_SecondaryCA_IntermediateRenew(t *testing.T) {
leafPEM , err := secondaryProvider . Sign ( leafCsr )
require . NoError ( err )
c ert, err = connect . ParseCert ( leafPEM )
intermediateC ert, err = connect . ParseCert ( leafPEM )
require . NoError ( err )
// Check that the leaf signed by the new intermediate can be verified using the
// returned cert chain (signed intermediate + remote root).
intermediatePool := x509 . NewCertPool ( )
// TODO: do not explicitly add the intermediatePEM, we should have it available
// from leafPEM. Use connect.ParseLeafCerts to do the right thing.
intermediatePool . AppendCertsFromPEM ( [ ] byte ( intermediatePEM ) )
rootPool := x509 . NewCertPool ( )
rootPool . AppendCertsFromPEM ( [ ] byte ( caRoot . RootCert ) )
_ , err = c ert. Verify ( x509 . VerifyOptions {
_ , err = intermediateC ert. Verify ( x509 . VerifyOptions {
Intermediates : intermediatePool ,
Roots : rootPool ,
} )