Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fill spec.tls.caCertificate in route with intermediate ca certificate… #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions internal/controller/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"crypto"
"crypto/x509"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -313,7 +314,7 @@ func (r *RouteController) buildNextCert(ctx context.Context, route *routev1.Rout
// Get the canonical hostname(s) of the Route (from .spec.host or .spec.subdomain)
dnsNames = getRouteHostnames(route)
if len(dnsNames) == 0 {
err := fmt.Errorf("Route is not yet initialized with a hostname")
err := fmt.Errorf("route is not yet initialized with a hostname")
r.eventRecorder.Event(route, corev1.EventTypeWarning, ReasonMissingHostname, fmt.Sprint(err))
return nil, err
}
Expand All @@ -331,7 +332,7 @@ func (r *RouteController) buildNextCert(ctx context.Context, route *routev1.Rout
ip := net.ParseIP(i)
if ip == nil {
r.eventRecorder.Event(route, corev1.EventTypeWarning, ReasonInvalidValue, fmt.Sprintf("Ignoring unparseable IP SAN %q", i))
r.log.V(1).Error(nil, "ignoring unparseble IP address on route", "rawIP", i)
r.log.V(1).Error(nil, "ignoring unparseable IP address on route", "rawIP", i)
continue
}

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

Expand Down Expand Up @@ -560,22 +561,34 @@ func (r *RouteController) populateRoute(ctx context.Context, route *routev1.Rout
// final Sanity checks
var key crypto.Signer

// get private key and signed certificate from Secret
// get private key, signed certificate and ca chain certificates from Secret
k, err := utilpki.DecodePrivateKeyBytes(secret.Data["tls.key"])
if err != nil {
return err
}
key = k

certificate, err := utilpki.DecodeX509CertificateBytes(secret.Data["tls.crt"])
certificates, err := utilpki.DecodeX509CertificateSetBytes(secret.Data["tls.crt"])
if err != nil {
return err
}
matches, err := utilpki.PublicKeyMatchesCertificate(key.Public(), certificate)
if err != nil {
return err

var certificate *x509.Certificate
var caCertificates []*x509.Certificate

for _, cert := range certificates {
matches, err := utilpki.PublicKeyMatchesCertificate(key.Public(), cert)
if err != nil {
return err
}
if matches {
certificate = cert
} else {
caCertificates = append(caCertificates, cert)
}
}
if !matches {

if certificate == nil {
return fmt.Errorf("key does not match certificate (route: %s/%s)", route.Namespace, route.Name)
}

Expand All @@ -596,6 +609,13 @@ func (r *RouteController) populateRoute(ctx context.Context, route *routev1.Rout
}
route.Spec.TLS.Certificate = string(encodedCert)

if caCertificates != nil && len(caCertificates) > 0 {
encodedCAs, err := utilpki.EncodeX509Chain(caCertificates)
if err != nil {
return err
}
route.Spec.TLS.CACertificate = string(encodedCAs)
}
_, err = r.routeClient.RouteV1().Routes(route.Namespace).Update(ctx, route, metav1.UpdateOptions{})
return err
}
Expand Down