Skip to content

Use certwatcher to support mounting cert-manager certificates. #134

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

Merged
merged 9 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
go-version: 1.17.x
- name: Checkout code
uses: actions/checkout@v2
- name: Test
Expand Down
62 changes: 49 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
module github.com/aws/amazon-eks-pod-identity-webhook

go 1.13
go 1.17

require (
github.com/evanphx/json-patch v4.4.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/google/go-cmp v0.5.2 // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.7.0
gopkg.in/square/go-jose.v2 v2.5.1
k8s.io/api v0.18.8
k8s.io/apimachinery v0.18.9-rc.0
k8s.io/client-go v0.18.8
k8s.io/api v0.23.0
k8s.io/apimachinery v0.23.0
k8s.io/client-go v0.23.0
k8s.io/klog v1.0.0
sigs.k8s.io/yaml v1.2.0
sigs.k8s.io/controller-runtime v0.11.0
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.28.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.0 // indirect
)
901 changes: 789 additions & 112 deletions go.sum

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)

var webhookVersion = "v0.1.0"
Expand Down Expand Up @@ -148,6 +150,8 @@ func main() {
// Expose other debug paths
}

// setup signal handler to be passed to certwatcher and http server
signalHandlerCtx := signals.SetupSignalHandler()
tlsConfig := &tls.Config{}

if *inCluster {
Expand Down Expand Up @@ -185,11 +189,18 @@ func main() {
return certificate, nil
}
} else {
certificate, err := tls.LoadX509KeyPair(*tlsCertFile, *tlsKeyFile)
watcher, err := certwatcher.New(*tlsCertFile, *tlsKeyFile)
if err != nil {
klog.Fatalf("failed to load TLS cert and key: %v", err)
klog.Fatalf("Error initializing certwatcher: %q", err)
}
tlsConfig.Certificates = []tls.Certificate{certificate}

go func() {
if err := watcher.Start(signalHandlerCtx); err != nil {
klog.Fatalf("Error starting certwatcher: %q", err)
}
}()

tlsConfig.GetCertificate = watcher.GetCertificate
}

klog.Info("Creating server")
Expand All @@ -198,7 +209,8 @@ func main() {
Handler: mux,
TLSConfig: tlsConfig,
}
handler.ShutdownOnTerm(server, time.Duration(10)*time.Second)

handler.ShutdownFromContext(signalHandlerCtx, server, time.Duration(10)*time.Second)

metricsServer := &http.Server{
Addr: metricsAddr,
Expand Down
15 changes: 8 additions & 7 deletions pkg/cert/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import (
"fmt"

"github.com/prometheus/client_golang/prometheus"
certificates "k8s.io/api/certificates/v1beta1"
certificates "k8s.io/api/certificates/v1"
clientset "k8s.io/client-go/kubernetes"
certificatesclient "k8s.io/client-go/kubernetes/typed/certificates/v1beta1"
"k8s.io/client-go/util/certificate"
)

// NewServerCertificateManager returns a certificate manager that stores TLS keys in Kubernetes Secrets
func NewServerCertificateManager(kubeClient clientset.Interface, namespace, secretName string, csr *x509.CertificateRequest) (certificate.Manager, error) {
clientFn := func(_ *tls.Certificate) (certificatesclient.CertificateSigningRequestInterface, error) {
return kubeClient.CertificatesV1beta1().CertificateSigningRequests(), nil
clientsetFn := func(_ *tls.Certificate) (clientset.Interface, error) {
return kubeClient, nil
}

certificateStore := NewSecretCertStore(
Expand All @@ -49,8 +48,8 @@ func NewServerCertificateManager(kubeClient clientset.Interface, namespace, secr
prometheus.MustRegister(certificateRotation)

m, err := certificate.NewManager(&certificate.Config{
ClientFn: clientFn,
Template: csr,
ClientsetFn: clientsetFn,
Template: csr,
Usages: []certificates.KeyUsage{
// https://tools.ietf.org/html/rfc5280#section-4.2.1.3
//
Expand All @@ -65,7 +64,9 @@ func NewServerCertificateManager(kubeClient clientset.Interface, namespace, secr
// authenticate itself to a TLS client.
certificates.UsageServerAuth,
},
SignerName: certificates.LegacyUnknownSignerName,
// Hard coding this since LegacyUnknownSignerName is no longer available in certificates/v1
// https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/#kubernetes-signers.
SignerName: "kubernetes.io/legacy-unknown",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not just unavailable, it's not allowed: https://github.com/kubernetes/kubernetes/blob/48da959dbff18bfef6e801bd8c8ab3c88b7a7650/pkg/apis/certificates/validation/validation.go#L202

I think we will need to deprecate in-cluster flag + print a clear error + exit gracefully if the kubernetes server version is 1.22 AND in-cluster is true. Instead of trying and failing to create invalid CSR. However, doing all that is out of scope of your PR. So I would like to accept the other changes and drop these changes to pkg/cert/request.go, if possible.

Copy link
Contributor Author

@colinhoglund colinhoglund Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @wongma7, and it totally makes sense to separate these. The only reason I changed pkg/cert/request.go is because of dependencies. certwatcher is only exported in a newer version of the controller-runtime, which resulted in an upgrade of k8s.io/client-go to v0.23.0, which is turn changed the underlying Config type for KeyUsage to certficates/v1.

I'm not entirely sure if there is a way to avoid these dependency updates for now, but would be open to any suggesstions.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thx for the explanation. Then I agree with merging this part as well, I don't see a way to avoid it either.

Copy link

@wongma7 wongma7 Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for self and others interested in the in-cluster=true case:

Given that this PR basically solves the in-cluster=false case since now webhook actually reload certs when they are renewed...

My plan for in-cluster=true is...

  1. Currently, in-cluster=true works for k8s <=1.21 but doesn't for >1.21. We have discussed this at length and there seems to be no way to make it work in >1.21* (without mis/abusing the CSR api) so we need to deprecate it ASAP (as 1.22 has been out for months now).
  2. Upon merging this, in-cluster=true won't work for ANY k8s version. Users who rely on it will need to make sure they use an older revision when building pod-identity-webhook.
  3. Immediately/soon after merging this, someone (me/colleague/contributor) should deprecate in-cluster=true with a note in README and major version release so it is clear & obvious that it won't work for ANY k8s version

Copy link
Contributor Author

@colinhoglund colinhoglund Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2. Upon merging this, in-cluster=true won't work for ANY k8s version. Users who rely on it will need to make sure they use an older revision when building pod-identity-webhook.

It probably makes sense to tag a release before merging this since the latest release is a bit old. Then once the followup work is done, a new release can be cut that introduces certwatcher and deprecates the --in-cluster flag at the same time.

CertificateStore: certificateStore,
CertificateRotation: certificateRotation,
})
Expand Down
23 changes: 6 additions & 17 deletions pkg/handler/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,19 @@ package handler
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"k8s.io/klog"
)

var term = syscall.SIGTERM

// ShutdownOnTerm will wait for SIGTERM or SIGINT and gracefully shuts down the
// http server or kill it after the specified timeout
func ShutdownOnTerm(server *http.Server, timeout time.Duration) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
signal.Notify(c, term)

func ShutdownFromContext(ctx context.Context, server *http.Server, timeout time.Duration) {
go func() {
<-c
klog.Infof("Received SIGTERM/SIGINT. Beginning shutdown")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
<-ctx.Done()

shutdownCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := server.Shutdown(ctx); err != http.ErrServerClosed {
<-ctx.Done()

if err := server.Shutdown(shutdownCtx); err != nil {
klog.Errorf("Error shutting server down: %v", err)
if err := server.Close(); err != nil {
klog.Fatalf("Error closing server: %v", err)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading