emissary-ingress / emissary

open source Kubernetes-native API gateway for microservices built on the Envoy Proxy
https://www.getambassador.io
Apache License 2.0
4.33k stars 682 forks source link

apiext: replace expired webhook certificates #5489

Closed superfrink closed 7 months ago

superfrink commented 7 months ago

Description

Currently, emissary-apiext will create a new webhook key and certificate when one does not exist and does not replace the certificate when it is expired. The change in this PR automatically replaces the key and certificate when it is expired.

Related Issues

Fixes https://github.com/emissary-ingress/emissary/issues/4442

Testing

Manual testing by scaling the emissary-apiext pods to zero and back up again. First with an expired certificate and second with a not-expired certificate.

$ kubectl -n emissary-system get secret emissary-ingress-webhook-ca -o yaml | grep tls.crt | awk '{print $2}' | base64 --decode | openssl x509 -noout -text | grep Not
            Not Before: Dec 23 00:57:19 2023 GMT
            Not After : Dec 23 00:59:19 2023 GMT

$ date
Fri 22 Dec 2023 17:59:37 MST

$ kubectl -n emissary-system scale --replicas=0 deployment emissary-apiext
deployment.apps/emissary-apiext scaled

$ kubectl -n emissary-system scale --replicas=1 deployment emissary-apiext
deployment.apps/emissary-apiext scaled

$ kubectl -n emissary-system get secret emissary-ingress-webhook-ca -o yaml | grep tls.crt | awk '{print $2}' | base64 --decode | openssl x509 -noout -text | grep Not
            Not Before: Dec 23 00:59:58 2023 GMT
            Not After : Dec 23 01:01:58 2023 GMT

$ date
Fri 22 Dec 2023 18:00:08 MST

$ kubectl -n emissary-system scale --replicas=0 deployment emissary-apiext
deployment.apps/emissary-apiext scaled

$ kubectl -n emissary-system scale --replicas=1 deployment emissary-apiext
deployment.apps/emissary-apiext scaled

$ kubectl -n emissary-system get secret emissary-ingress-webhook-ca -o yaml | grep tls.crt | awk '{print $2}' | base64 --decode | openssl x509 -noout -text | grep Not
            Not Before: Dec 23 00:59:58 2023 GMT
            Not After : Dec 23 01:01:58 2023 GMT

The new log message:

time="2023-12-23 00:59:53.5883" level=warning msg="Will try to replace cert not valid before 2023-12-23 00:57:19 +0000 UTC and after 2023-12-23 00:59:19 +0000 UTC" func=github.com/emissary-ingress/emissary/v3/pkg/apiext/internal.EnsureCA file="/go/pkg/apiext/internal/ca.go:69" CMD=apiext PID=1

The log message was not present when scaling up the pods and the certificate expiry date was still valid.

Checklist

LanceEa commented 7 months ago

@superfrink - Thanks for the PR. When the CA Cert gets renewed it also needs to patch the CRD's which previously was only being done on startup or new CRD's applied to cluster.

I had actually been working on a larger fix to this and just landed it which ensures the CA Cert is renewed and that the CABundle is properly injected in the CRD's when it changes. It also opens up the ability manage it externally via CertManager (which has been requested in the past).

https://github.com/emissary-ingress/emissary/pull/5494

Please feel free to try that out and let me know if you see any issues!