Skip to content

Commit 415897d

Browse files
authored
Fix CA regeneration (#1013)
When regenerating the TLS CA certificate we must be careful to keep the subject of the new CA exactly the same byte for byte. Otherwise the old CA is not considered in a cert pool when validating certificates issued by the new CA: https://github.com/golang/go/blob/497cb7c0c3042d3c6605b46a1bf35b7c3bc8b046/src/crypto/x509/cert_pool.go#L144 How the subject is rended into bytes from a pkix.Name struct is not guaranteed to be stable across go versions. We actually ran into this issue before and already filed a bug for this: golang/go#45882 Signed-off-by: Fabian Ruff <fabian.ruff@sap.com>
1 parent d0fe42e commit 415897d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pkg/util/certificates.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func (cf *CertificateFactory) UserCert(principal *models.Principal, apiURL strin
377377

378378
func loadOrCreateCA(kluster *v1.Kluster, name string, cert, key *string, certUpdates *[]CertUpdates) (*Bundle, error) {
379379
var existingKey *rsa.PrivateKey
380+
var existingSubject []byte
380381
regenerate := false
381382

382383
if name == "TLS" && *cert != "" {
@@ -400,14 +401,15 @@ func loadOrCreateCA(kluster *v1.Kluster, name string, cert, key *string, certUpd
400401
if !isRSAKey {
401402
return nil, errors.New("Key does not seem to be of type RSA")
402403
}
404+
existingSubject = caCert.RawSubject
403405
}
404406
}
405407

406408
if *cert != "" && *key != "" && !regenerate {
407409
return NewBundle([]byte(*key), []byte(*cert))
408410
}
409411

410-
caBundle, err := createCA(kluster.Name, name, existingKey)
412+
caBundle, err := createCA(kluster.Name, name, existingKey, existingSubject)
411413
if err != nil {
412414
return nil, err
413415
}
@@ -512,7 +514,7 @@ func ensureServerCertificate(ca *Bundle, cn string, dnsNames []string, ips []net
512514
return nil
513515
}
514516

515-
func createCA(klusterName, name string, existingKey *rsa.PrivateKey) (*Bundle, error) {
517+
func createCA(klusterName, name string, existingKey *rsa.PrivateKey, existingSubject []byte) (*Bundle, error) {
516518
var privateKey *rsa.PrivateKey
517519
var err error
518520

@@ -538,6 +540,9 @@ func createCA(klusterName, name string, existingKey *rsa.PrivateKey) (*Bundle, e
538540
BasicConstraintsValid: true,
539541
IsCA: true,
540542
}
543+
if existingSubject != nil && len(existingSubject) > 0 {
544+
tmpl.RawSubject = existingSubject
545+
}
541546

542547
certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &tmpl, &tmpl, privateKey.Public(), privateKey)
543548
if err != nil {

0 commit comments

Comments
 (0)