Skip to content

Commit 3afae5a

Browse files
committed
initial work from dynamic-authority
Signed-off-by: Erik Godding Boye <[email protected]>
1 parent fc173b9 commit 3afae5a

14 files changed

+1449
-0
lines changed

go.mod

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
11
module github.com/cert-manager/webhook-cert-lib
22

33
go 1.22.0
4+
5+
require (
6+
github.com/onsi/ginkgo/v2 v2.22.0
7+
github.com/onsi/gomega v1.36.0
8+
k8s.io/api v0.31.3
9+
k8s.io/apimachinery v0.31.3
10+
k8s.io/client-go v0.31.3
11+
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
12+
sigs.k8s.io/controller-runtime v0.19.2
13+
)
14+
15+
require (
16+
github.com/beorn7/perks v1.0.1 // indirect
17+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
18+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
19+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
20+
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
21+
github.com/fsnotify/fsnotify v1.7.0 // indirect
22+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
23+
github.com/go-logr/logr v1.4.2 // indirect
24+
github.com/go-logr/zapr v1.3.0 // indirect
25+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
26+
github.com/go-openapi/jsonreference v0.20.2 // indirect
27+
github.com/go-openapi/swag v0.22.4 // indirect
28+
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
29+
github.com/gogo/protobuf v1.3.2 // indirect
30+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
31+
github.com/golang/protobuf v1.5.4 // indirect
32+
github.com/google/gnostic-models v0.6.8 // indirect
33+
github.com/google/go-cmp v0.6.0 // indirect
34+
github.com/google/gofuzz v1.2.0 // indirect
35+
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect
36+
github.com/google/uuid v1.6.0 // indirect
37+
github.com/imdario/mergo v0.3.6 // indirect
38+
github.com/josharian/intern v1.0.0 // indirect
39+
github.com/json-iterator/go v1.1.12 // indirect
40+
github.com/mailru/easyjson v0.7.7 // indirect
41+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
42+
github.com/modern-go/reflect2 v1.0.2 // indirect
43+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
44+
github.com/pkg/errors v0.9.1 // indirect
45+
github.com/prometheus/client_golang v1.19.1 // indirect
46+
github.com/prometheus/client_model v0.6.1 // indirect
47+
github.com/prometheus/common v0.55.0 // indirect
48+
github.com/prometheus/procfs v0.15.1 // indirect
49+
github.com/spf13/pflag v1.0.5 // indirect
50+
github.com/x448/float16 v0.8.4 // indirect
51+
go.uber.org/multierr v1.11.0 // indirect
52+
go.uber.org/zap v1.26.0 // indirect
53+
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
54+
golang.org/x/net v0.30.0 // indirect
55+
golang.org/x/oauth2 v0.21.0 // indirect
56+
golang.org/x/sys v0.26.0 // indirect
57+
golang.org/x/term v0.25.0 // indirect
58+
golang.org/x/text v0.19.0 // indirect
59+
golang.org/x/time v0.3.0 // indirect
60+
golang.org/x/tools v0.26.0 // indirect
61+
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
62+
google.golang.org/protobuf v1.35.1 // indirect
63+
gopkg.in/inf.v0 v0.9.1 // indirect
64+
gopkg.in/yaml.v2 v2.4.0 // indirect
65+
gopkg.in/yaml.v3 v3.0.1 // indirect
66+
k8s.io/apiextensions-apiserver v0.31.0 // indirect
67+
k8s.io/klog/v2 v2.130.1 // indirect
68+
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
69+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
70+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
71+
sigs.k8s.io/yaml v1.4.0 // indirect
72+
)

go.sum

Lines changed: 194 additions & 0 deletions
Large diffs are not rendered by default.

pkg/authority/api.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
package authority
2+
3+
import (
4+
"crypto/tls"
5+
"errors"
6+
"time"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"k8s.io/apimachinery/pkg/labels"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
admissionregistrationv1ac "k8s.io/client-go/applyconfigurations/admissionregistration/v1"
13+
ctrl "sigs.k8s.io/controller-runtime"
14+
"sigs.k8s.io/controller-runtime/pkg/cache"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
)
17+
18+
const (
19+
// DynamicAuthoritySecretLabel will - if set to "true" - make the dynamic
20+
// authority CA controller inject and maintain a dynamic CA.
21+
// The label must be added to Secret resource that want to denote that they
22+
// can be directly injected into injectables that have a
23+
// `inject-dynamic-ca-from-secret` label.
24+
// If an injectable references a Secret that does NOT have this annotation,
25+
// the dynamic ca-injector will refuse to inject the secret.
26+
DynamicAuthoritySecretLabel = "cert-manager.io/allow-dynamic-ca-injection"
27+
// WantInjectFromSecretNamespaceLabel is the label that specifies that a
28+
// particular object wants injection of dynamic CAs from secret in
29+
// namespace.
30+
// Must be used in conjunction with WantInjectFromSecretNameLabel.
31+
WantInjectFromSecretNamespaceLabel = "cert-manager.io/inject-dynamic-ca-from-secret-namespace"
32+
// WantInjectFromSecretNameLabel is the label that specifies that a
33+
// particular object wants injection of dynamic CAs from secret with name.
34+
// Must be used in conjunction with WantInjectFromSecretNamespaceLabel.
35+
WantInjectFromSecretNameLabel = "cert-manager.io/inject-dynamic-ca-from-secret-name"
36+
37+
// TLSCABundleKey is used as a data key in Secret resources to store a CA
38+
// certificate bundle.
39+
TLSCABundleKey = "ca-bundle.crt"
40+
41+
// RenewCertificateSecretAnnotation is an annotation that can be set to
42+
// an arbitrary value on a certificate secret to trigger a renewal of the
43+
// certificate managed in the secret.
44+
RenewCertificateSecretAnnotation = "renew.cert-manager.io/requestedAt"
45+
// RenewHandledCertificateSecretAnnotation is an annotation that will be set on a
46+
// certificate secret whenever a new certificate is renewed using the
47+
// RenewCertificateSecretAnnotation annotation.
48+
RenewHandledCertificateSecretAnnotation = "renew.cert-manager.io/lastRequestedAt"
49+
)
50+
51+
type ApplyConfiguration interface {
52+
GetName() *string
53+
}
54+
55+
type Injectable interface {
56+
GroupVersionKind() schema.GroupVersionKind
57+
InjectCA(obj *unstructured.Unstructured, caBundle []byte) (ApplyConfiguration, error)
58+
}
59+
60+
type ValidatingWebhookCaBundleInject struct {
61+
}
62+
63+
func (i *ValidatingWebhookCaBundleInject) GroupVersionKind() schema.GroupVersionKind {
64+
return schema.GroupVersionKind{
65+
Group: "admissionregistration.k8s.io",
66+
Version: "v1",
67+
Kind: "ValidatingWebhookConfiguration",
68+
}
69+
}
70+
71+
func (i *ValidatingWebhookCaBundleInject) InjectCA(obj *unstructured.Unstructured, caBundle []byte) (ApplyConfiguration, error) {
72+
// TODO: Can we generalize this function for any resource based on a JSON path?
73+
74+
ac := admissionregistrationv1ac.ValidatingWebhookConfiguration(obj.GetName())
75+
76+
webhooks, _, err := unstructured.NestedSlice(obj.Object, "webhooks")
77+
if err != nil {
78+
return nil, err
79+
}
80+
for _, w := range webhooks {
81+
name, _, err := unstructured.NestedString(w.(map[string]any), "name")
82+
if err != nil {
83+
return nil, err
84+
}
85+
ac.WithWebhooks(
86+
admissionregistrationv1ac.ValidatingWebhook().
87+
WithName(name).
88+
WithClientConfig(admissionregistrationv1ac.WebhookClientConfig().
89+
WithCABundle(caBundle...),
90+
),
91+
)
92+
}
93+
94+
return ac, nil
95+
}
96+
97+
var _ Injectable = &ValidatingWebhookCaBundleInject{}
98+
99+
type Options struct {
100+
// The namespace used for certificate secrets.
101+
Namespace string
102+
103+
// The name of the Secret used to store CA certificates.
104+
CASecret string
105+
106+
// The amount of time the root CA certificate will be valid for.
107+
// This must be greater than LeafDuration.
108+
CADuration time.Duration
109+
110+
DNSNames []string
111+
112+
// The amount of time leaf certificates signed by this authority will be
113+
// valid for.
114+
// This must be less than CADuration.
115+
LeafDuration time.Duration
116+
117+
Injectables []Injectable
118+
}
119+
120+
type ServingCertificateOperator struct {
121+
Options Options
122+
123+
certificateHolder *CertificateHolder
124+
}
125+
126+
func (o *ServingCertificateOperator) ServingCertificate() func(config *tls.Config) {
127+
if o.certificateHolder == nil {
128+
o.certificateHolder = &CertificateHolder{}
129+
}
130+
return func(config *tls.Config) {
131+
config.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
132+
return o.certificateHolder.GetCertificate(info)
133+
}
134+
}
135+
}
136+
137+
// +kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=validatingwebhookconfigurations,verbs=get;list;watch;patch
138+
139+
func (o *ServingCertificateOperator) SetupWithManager(mgr ctrl.Manager) error {
140+
if o.certificateHolder == nil {
141+
return errors.New("ServingCertificate not invoked")
142+
}
143+
144+
if o.Options.CADuration == 0 {
145+
o.Options.CADuration = 7 * 24 * time.Hour
146+
}
147+
if o.Options.LeafDuration == 0 {
148+
o.Options.LeafDuration = 1 * 24 * time.Hour
149+
}
150+
if len(o.Options.Injectables) == 0 {
151+
o.Options.Injectables = []Injectable{
152+
&ValidatingWebhookCaBundleInject{},
153+
}
154+
}
155+
156+
cacheByObject := map[client.Object]cache.ByObject{
157+
&corev1.Secret{}: {
158+
Namespaces: map[string]cache.Config{
159+
o.Options.Namespace: {},
160+
},
161+
Label: labels.SelectorFromSet(labels.Set{
162+
DynamicAuthoritySecretLabel: "true",
163+
}),
164+
},
165+
}
166+
injectByObject := cache.ByObject{
167+
Label: labels.SelectorFromSet(labels.Set{
168+
WantInjectFromSecretNamespaceLabel: o.Options.Namespace,
169+
WantInjectFromSecretNameLabel: o.Options.CASecret,
170+
}),
171+
}
172+
for _, injectable := range o.Options.Injectables {
173+
cacheByObject[newUnstructured(injectable)] = injectByObject
174+
}
175+
controllerCache, err := cache.New(mgr.GetConfig(), cache.Options{
176+
HTTPClient: mgr.GetHTTPClient(),
177+
Scheme: mgr.GetScheme(),
178+
Mapper: mgr.GetRESTMapper(),
179+
ReaderFailOnMissingInformer: true,
180+
ByObject: cacheByObject,
181+
})
182+
if err := mgr.Add(controllerCache); err != nil {
183+
return err
184+
}
185+
186+
controllerClient, err := client.New(mgr.GetConfig(), client.Options{
187+
HTTPClient: mgr.GetHTTPClient(),
188+
Scheme: mgr.GetScheme(),
189+
Mapper: mgr.GetRESTMapper(),
190+
Cache: &client.CacheOptions{
191+
Reader: controllerCache,
192+
Unstructured: true,
193+
},
194+
})
195+
if err != nil {
196+
return err
197+
}
198+
199+
r := reconciler{
200+
Client: controllerClient,
201+
Cache: controllerCache,
202+
Opts: o.Options,
203+
}
204+
controllers := []dynamicAuthorityController{
205+
&CASecretReconciler{reconciler: r},
206+
&LeafCertReconciler{reconciler: r, certificateHolder: o.certificateHolder},
207+
}
208+
for _, injectable := range o.Options.Injectables {
209+
controllers = append(controllers, &InjectableReconciler{reconciler: r, Injectable: injectable})
210+
}
211+
for _, c := range controllers {
212+
if err := c.SetupWithManager(mgr); err != nil {
213+
return err
214+
}
215+
}
216+
217+
return nil
218+
}
219+
220+
type dynamicAuthorityController interface {
221+
SetupWithManager(ctrl.Manager) error
222+
}
223+
224+
func newUnstructured(injectable Injectable) *unstructured.Unstructured {
225+
obj := &unstructured.Unstructured{}
226+
obj.SetGroupVersionKind(injectable.GroupVersionKind())
227+
return obj
228+
}
229+
230+
func newUnstructuredList(injectable Injectable) *unstructured.UnstructuredList {
231+
obj := &unstructured.UnstructuredList{}
232+
obj.SetGroupVersionKind(injectable.GroupVersionKind())
233+
return obj
234+
}

0 commit comments

Comments
 (0)