Prune intermediates before appending new one

This commit is contained in:
Kyle Havlovitz 2022-09-01 14:24:30 -07:00
parent 7150ccad85
commit 0c2fb7252d
1 changed files with 8 additions and 5 deletions

View File

@ -1098,9 +1098,13 @@ func setLeafSigningCert(caRoot *structs.CARoot, pem string) error {
return fmt.Errorf("error parsing leaf signing cert: %w", err)
}
if err := pruneExpiredIntermediates(caRoot); err != nil {
return err
}
caRoot.IntermediateCerts = append(caRoot.IntermediateCerts, pem)
caRoot.SigningKeyID = connect.EncodeSigningKeyID(cert.SubjectKeyId)
return pruneExpiredIntermediates(caRoot)
return nil
}
// pruneExpiredIntermediates removes expired intermediate certificates
@ -1108,15 +1112,14 @@ func setLeafSigningCert(caRoot *structs.CARoot, pem string) error {
func pruneExpiredIntermediates(caRoot *structs.CARoot) error {
var newIntermediates []string
now := time.Now()
for i, intermediatePEM := range caRoot.IntermediateCerts {
for _, intermediatePEM := range caRoot.IntermediateCerts {
cert, err := connect.ParseCert(intermediatePEM)
if err != nil {
return fmt.Errorf("error parsing leaf signing cert: %w", err)
}
// Only keep the intermediate cert if it's still valid, or if it's the most
// recently added (and thus the active signing cert).
if cert.NotAfter.After(now) || i == len(caRoot.IntermediateCerts) {
// Only keep the intermediate cert if it's still valid.
if cert.NotAfter.After(now) {
newIntermediates = append(newIntermediates, intermediatePEM)
}
}