-
Notifications
You must be signed in to change notification settings - Fork 100
THREESCALE-10236 Custom CA certificate support for operator deployment #1181
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| Copyright 2026 Red Hat, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package configuration | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/x509" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/client-go/tools/record" | ||
| "k8s.io/utils/ptr" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/builder" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| ) | ||
|
|
||
| const ( | ||
| CABundleConfigMapName = "threescale-ca-bundle" | ||
| CABundleConfigMapKey = "ca-bundle.crt" | ||
| ) | ||
|
|
||
| // CABundleWatcher watches the threescale-ca-bundle ConfigMap and updates the | ||
| // package-level CA pool via SetRootCAs on every successful reconcile. | ||
| type CABundleWatcher struct { | ||
| client.Client | ||
| Recorder record.EventRecorder | ||
| Namespace string | ||
| } | ||
|
|
||
| // SetupWithManager registers the controller with the manager. | ||
| func (r *CABundleWatcher) SetupWithManager(mgr ctrl.Manager) error { | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| Named("cabundlewatcher"). | ||
| // NeedLeaderElection is disabled so that every operator replica runs | ||
| // this controller, not just the leader. Each replica must load the CA | ||
| // bundle into its own in-process TLS config. | ||
| // | ||
| // controller-runtime replays a synthetic Add event for every object | ||
| // already in the informer cache when a controller starts, so a newly | ||
| // promoted leader would pick up the bundle automatically. Running on | ||
| // all replicas avoids the small window between manager start and leader | ||
| // promotion during which capability reconcilers could attempt outbound | ||
| // TLS calls before the bundle has been loaded. Those calls would fail | ||
| // and requeue, but disabling leader election here eliminates the window | ||
| // entirely. | ||
| WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}). | ||
| For(&corev1.ConfigMap{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool { | ||
| return object.GetNamespace() == r.Namespace && object.GetName() == CABundleConfigMapName | ||
| }))). | ||
| Complete(r) | ||
| } | ||
|
|
||
| // Reconcile fetches the CA bundle ConfigMap and updates the package-level | ||
| // TLS config. On an invalid bundle, the error is logged and recorded as a Warning event on | ||
| // the ConfigMap; the existing TLS config is left unchanged so capability | ||
| // controllers continue to operate with the last known good CA. | ||
| func (r *CABundleWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| logger := ctrl.LoggerFrom(ctx) | ||
|
|
||
| cm := &corev1.ConfigMap{} | ||
| err := r.Get(ctx, client.ObjectKey{Namespace: r.Namespace, Name: CABundleConfigMapName}, cm) | ||
| if err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| SetRootCAs(nil) | ||
| logger.Info("CA bundle ConfigMap not found; using system default CAs", "namespace", r.Namespace, "configmap", CABundleConfigMapName) | ||
| return ctrl.Result{}, nil | ||
| } | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| pool, parseErr := parseBundleFromConfigMap(cm) | ||
| if parseErr != nil { | ||
| logger.Error(parseErr, "CA bundle ConfigMap contains an invalid certificate bundle; keeping previous TLS config") | ||
| if r.Recorder != nil { | ||
| r.Recorder.Eventf(cm, corev1.EventTypeWarning, "InvalidCABundle", "%v", parseErr) | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| if pool == nil { | ||
| SetRootCAs(nil) | ||
| logger.Info("CA bundle ConfigMap key absent; using system default CAs", "namespace", r.Namespace, "configmap", CABundleConfigMapName, "key", CABundleConfigMapKey) | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| SetRootCAs(pool) | ||
| logger.Info("CA bundle updated and applied", "namespace", r.Namespace, "configmap", CABundleConfigMapName) | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // parseBundleFromConfigMap parses the CA bundle from a ConfigMap and returns a | ||
| // *x509.CertPool. Returns nil, nil if the key is absent (no custom bundle | ||
| // configured). Returns an error if the PEM data is present but invalid. | ||
| func parseBundleFromConfigMap(cm *corev1.ConfigMap) (*x509.CertPool, error) { | ||
| val, exists := cm.Data[CABundleConfigMapKey] | ||
| if !exists { | ||
| return nil, nil // no bundle configured — use system roots | ||
| } | ||
|
|
||
| if len(val) == 0 { | ||
| return nil, fmt.Errorf("InvalidCAFormat: key %q in ConfigMap %s/%s is empty", CABundleConfigMapKey, cm.Namespace, cm.Name) | ||
| } | ||
|
|
||
| certPool := x509.NewCertPool() | ||
| if !certPool.AppendCertsFromPEM([]byte(val)) { | ||
| return nil, fmt.Errorf("InvalidCAFormat: no valid PEM-encoded certificates found in CA bundle") | ||
| } | ||
|
|
||
| return certPool, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.