Skip to content

Commit 6ee9d93

Browse files
committed
fix: preserve ca-bundles injected from external providers (projectcapsule#1948)
* fix: preserve ca-bundles injected from external providers (projectcapsule#1948) Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
1 parent 89846da commit 6ee9d93

3 files changed

Lines changed: 103 additions & 16 deletions

File tree

internal/controllers/admission/mutating.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,27 @@ func (r *mutatingReconciler) reconcileConfiguration(
144144

145145
obj.SetAnnotations(annotations)
146146

147+
// Preserve existing CA Information (cert-manager)
148+
existingCABundles := mutatingWebhookCABundlesByName(obj.Webhooks)
149+
147150
obj.Webhooks = desiredHooks
148151

149-
caCert, err := tls.FetchCurrentCaBundleForAdmission(ctx, r.client, r.configuration, cfg.Client.CABundle)
150-
if err != nil {
151-
return err
152+
var caCert []byte
153+
154+
if r.configuration.EnableTLSConfiguration() {
155+
caCert, err = tls.FetchCurrentCaBundleForAdmission(ctx, r.client, r.configuration)
156+
if err != nil {
157+
return err
158+
}
159+
} else {
160+
caCert = cfg.Client.CABundle
152161
}
153162

154-
preserveMutatingWebhookCABundles(obj.Webhooks, caCert)
163+
if len(caCert) > 0 {
164+
preserveMutatingWebhookCABundles(obj.Webhooks, caCert)
165+
} else {
166+
restoreMutatingWebhookCABundles(obj.Webhooks, existingCABundles)
167+
}
155168

156169
return err
157170
})
@@ -222,10 +235,44 @@ func (r *mutatingReconciler) webhooks(
222235
return hooks, nil
223236
}
224237

238+
func mutatingWebhookCABundlesByName(
239+
hooks []admissionv1.MutatingWebhook,
240+
) map[string][]byte {
241+
out := make(map[string][]byte, len(hooks))
242+
243+
for _, hook := range hooks {
244+
if hook.Name == "" || len(hook.ClientConfig.CABundle) == 0 {
245+
continue
246+
}
247+
248+
out[hook.Name] = append([]byte(nil), hook.ClientConfig.CABundle...)
249+
}
250+
251+
return out
252+
}
253+
254+
func restoreMutatingWebhookCABundles(
255+
hooks []admissionv1.MutatingWebhook,
256+
existingCABundles map[string][]byte,
257+
) {
258+
for i := range hooks {
259+
existingCABundle := existingCABundles[hooks[i].Name]
260+
if len(existingCABundle) == 0 {
261+
continue
262+
}
263+
264+
hooks[i].ClientConfig.CABundle = append([]byte(nil), existingCABundle...)
265+
}
266+
}
267+
225268
func preserveMutatingWebhookCABundles(
226269
hooks []admissionv1.MutatingWebhook,
227270
caBundle []byte,
228271
) {
272+
if len(caBundle) == 0 {
273+
return
274+
}
275+
229276
for i := range hooks {
230277
hooks[i].ClientConfig.CABundle = append([]byte(nil), caBundle...)
231278
}

internal/controllers/admission/validating.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,27 @@ func (r *validatingReconciler) reconcileValidatingConfiguration(
146146

147147
obj.SetAnnotations(annotations)
148148

149+
// Preserve existing CA Information (cert-manager)
150+
existingCABundles := validatingWebhookCABundlesByName(obj.Webhooks)
151+
149152
obj.Webhooks = desiredHooks
150153

151-
caCert, err := tls.FetchCurrentCaBundleForAdmission(ctx, r.client, r.configuration, cfg.Client.CABundle)
152-
if err != nil {
153-
return err
154+
var caCert []byte
155+
156+
if r.configuration.EnableTLSConfiguration() {
157+
caCert, err = tls.FetchCurrentCaBundleForAdmission(ctx, r.client, r.configuration)
158+
if err != nil {
159+
return err
160+
}
161+
} else {
162+
caCert = cfg.Client.CABundle
154163
}
155164

156-
preserveValidatingWebhookCABundles(obj.Webhooks, caCert)
165+
if len(caCert) > 0 {
166+
preserveValidatingWebhookCABundles(obj.Webhooks, caCert)
167+
} else {
168+
restoreValidatingWebhookCABundles(obj.Webhooks, existingCABundles)
169+
}
157170

158171
return err
159172
})
@@ -224,10 +237,44 @@ func (r *validatingReconciler) validatingWebhooks(
224237
return hooks, nil
225238
}
226239

240+
func validatingWebhookCABundlesByName(
241+
hooks []admissionv1.ValidatingWebhook,
242+
) map[string][]byte {
243+
out := make(map[string][]byte, len(hooks))
244+
245+
for _, hook := range hooks {
246+
if hook.Name == "" || len(hook.ClientConfig.CABundle) == 0 {
247+
continue
248+
}
249+
250+
out[hook.Name] = append([]byte(nil), hook.ClientConfig.CABundle...)
251+
}
252+
253+
return out
254+
}
255+
256+
func restoreValidatingWebhookCABundles(
257+
hooks []admissionv1.ValidatingWebhook,
258+
existingCABundles map[string][]byte,
259+
) {
260+
for i := range hooks {
261+
existingCABundle := existingCABundles[hooks[i].Name]
262+
if len(existingCABundle) == 0 {
263+
continue
264+
}
265+
266+
hooks[i].ClientConfig.CABundle = append([]byte(nil), existingCABundle...)
267+
}
268+
}
269+
227270
func preserveValidatingWebhookCABundles(
228271
hooks []admissionv1.ValidatingWebhook,
229272
caBundle []byte,
230273
) {
274+
if len(caBundle) == 0 {
275+
return
276+
}
277+
231278
for i := range hooks {
232279
hooks[i].ClientConfig.CABundle = append([]byte(nil), caBundle...)
233280
}

internal/controllers/tls/utils.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,7 @@ func FetchCurrentCaBundleForAdmission(
141141
ctx context.Context,
142142
c client.Reader,
143143
cfg configuration.Configuration,
144-
configuredCABundle []byte,
145144
) ([]byte, error) {
146-
// Explicit configuration wins.
147-
if len(configuredCABundle) > 0 {
148-
return append([]byte(nil), configuredCABundle...), nil
149-
}
150-
151145
// Internal Capsule TLS enabled: source of truth is the TLS Secret.
152146
if cfg.EnableTLSConfiguration() {
153147
secret := &corev1.Secret{}
@@ -175,7 +169,6 @@ func FetchCurrentCaBundleForAdmission(
175169
return append([]byte(nil), caBundle...), nil
176170
}
177171

178-
// cert-manager / external injector mode:
179-
// return nil and preserve current webhook caBundle.
172+
// TLS Controller not enabled
180173
return nil, nil
181174
}

0 commit comments

Comments
 (0)