Skip to content

Commit 7e59b2d

Browse files
committed
fill spec.tls.caCertificate in route with intermediate ca certificates chain
Signed-off-by: Jean-René ROUET <[email protected]>
1 parent 4b4145a commit 7e59b2d

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

internal/controller/sync.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919
import (
2020
"context"
2121
"crypto"
22+
"crypto/x509"
2223
"fmt"
2324
"net"
2425
"net/url"
@@ -313,7 +314,7 @@ func (r *RouteController) buildNextCert(ctx context.Context, route *routev1.Rout
313314
// Get the canonical hostname(s) of the Route (from .spec.host or .spec.subdomain)
314315
dnsNames = getRouteHostnames(route)
315316
if len(dnsNames) == 0 {
316-
err := fmt.Errorf("Route is not yet initialized with a hostname")
317+
err := fmt.Errorf("route is not yet initialized with a hostname")
317318
r.eventRecorder.Event(route, corev1.EventTypeWarning, ReasonMissingHostname, fmt.Sprint(err))
318319
return nil, err
319320
}
@@ -331,7 +332,7 @@ func (r *RouteController) buildNextCert(ctx context.Context, route *routev1.Rout
331332
ip := net.ParseIP(i)
332333
if ip == nil {
333334
r.eventRecorder.Event(route, corev1.EventTypeWarning, ReasonInvalidValue, fmt.Sprintf("Ignoring unparseable IP SAN %q", i))
334-
r.log.V(1).Error(nil, "ignoring unparseble IP address on route", "rawIP", i)
335+
r.log.V(1).Error(nil, "ignoring unparseable IP address on route", "rawIP", i)
335336
continue
336337
}
337338

@@ -347,7 +348,7 @@ func (r *RouteController) buildNextCert(ctx context.Context, route *routev1.Rout
347348
ur, err := url.Parse(u)
348349
if err != nil {
349350
r.eventRecorder.Event(route, corev1.EventTypeWarning, ReasonInvalidValue, fmt.Sprintf("Ignoring malformed URI SAN %q", u))
350-
r.log.V(1).Error(err, "ignoring unparseble URI SAN on route", "uri", u)
351+
r.log.V(1).Error(err, "ignoring unparseable URI SAN on route", "uri", u)
351352
continue
352353
}
353354

@@ -560,24 +561,34 @@ func (r *RouteController) populateRoute(ctx context.Context, route *routev1.Rout
560561
// final Sanity checks
561562
var key crypto.Signer
562563

563-
// get private key, signed certificate and ca chain certficates from Secret
564+
// get private key, signed certificate and ca chain certificates from Secret
564565
k, err := utilpki.DecodePrivateKeyBytes(secret.Data["tls.key"])
565566
if err != nil {
566567
return err
567568
}
568569
key = k
569570

570571
certificates, err := utilpki.DecodeX509CertificateSetBytes(secret.Data["tls.crt"])
571-
572-
certificate := certificates[0]
573572
if err != nil {
574573
return err
575574
}
576-
matches, err := utilpki.PublicKeyMatchesCertificate(key.Public(), certificate)
577-
if err != nil {
578-
return err
575+
576+
var certificate *x509.Certificate
577+
var caCertificates []*x509.Certificate
578+
579+
for _, cert := range certificates {
580+
matches, err := utilpki.PublicKeyMatchesCertificate(key.Public(), cert)
581+
if err != nil {
582+
return err
583+
}
584+
if matches {
585+
certificate = cert
586+
} else {
587+
caCertificates = append(caCertificates, cert)
588+
}
579589
}
580-
if !matches {
590+
591+
if certificate == nil {
581592
return fmt.Errorf("key does not match certificate (route: %s/%s)", route.Namespace, route.Name)
582593
}
583594

@@ -598,12 +609,13 @@ func (r *RouteController) populateRoute(ctx context.Context, route *routev1.Rout
598609
}
599610
route.Spec.TLS.Certificate = string(encodedCert)
600611

601-
encodedCAs, err := utilpki.EncodeX509Chain(certificates[1:])
602-
if err != nil {
603-
return err
612+
if caCertificates != nil && len(caCertificates) > 0 {
613+
encodedCAs, err := utilpki.EncodeX509Chain(caCertificates)
614+
if err != nil {
615+
return err
616+
}
617+
route.Spec.TLS.CACertificate = string(encodedCAs)
604618
}
605-
route.Spec.TLS.CACertificate = string(encodedCAs)
606-
607619
_, err = r.routeClient.RouteV1().Routes(route.Namespace).Update(ctx, route, metav1.UpdateOptions{})
608620
return err
609621
}

0 commit comments

Comments
 (0)