-
Notifications
You must be signed in to change notification settings - Fork 84
Add support for injecting CA certificates into Secrets #265
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
Open
mnencia
wants to merge
1
commit into
openshift:main
Choose a base branch
from
mnencia:inject-ca-into-secrets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,81 @@ | ||
| package cabundleinjector | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1" | ||
| listers "k8s.io/client-go/listers/core/v1" | ||
| "k8s.io/klog/v2" | ||
|
|
||
| apiannotations "github.com/openshift/api/annotations" | ||
| "github.com/openshift/library-go/pkg/controller/factory" | ||
| "github.com/openshift/service-ca-operator/pkg/controller/api" | ||
| ) | ||
|
|
||
| type secretCABundleInjector struct { | ||
| client kcoreclient.SecretsGetter | ||
| lister listers.SecretLister | ||
| caBundle string | ||
|
|
||
| filterFn func(secret *corev1.Secret) bool | ||
| } | ||
|
|
||
| func newSecretInjectorConfig(config *caBundleInjectorConfig) controllerConfig { | ||
| informer := config.kubeInformers.Core().V1().Secrets() | ||
|
|
||
| syncer := &secretCABundleInjector{ | ||
| client: config.kubeClient.CoreV1(), | ||
| lister: informer.Lister(), | ||
| caBundle: string(config.caBundle), | ||
| } | ||
|
|
||
| return controllerConfig{ | ||
| name: "SecretCABundleInjector", | ||
| sync: syncer.Sync, | ||
| informer: informer.Informer(), | ||
| annotationsChecker: annotationsChecker( | ||
| api.InjectCABundleAnnotationName, | ||
| ), | ||
| namespaced: true, | ||
| } | ||
| } | ||
|
|
||
| func (bi *secretCABundleInjector) Sync(ctx context.Context, syncCtx factory.SyncContext) error { | ||
| namespace, name := namespacedObjectFromQueueKey(syncCtx.QueueKey()) | ||
|
|
||
| secret, err := bi.lister.Secrets(namespace).Get(name) | ||
| if apierrors.IsNotFound(err) { | ||
| return nil | ||
| } else if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if bi.filterFn != nil && !bi.filterFn(secret) { | ||
| return nil | ||
| } | ||
|
|
||
| // skip updating when the CA bundle is already there | ||
| if data, ok := secret.Data[api.InjectionDataKey]; ok && | ||
| string(data) == bi.caBundle && len(secret.Data) == 1 { | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| klog.Infof("updating secret %s/%s with the service signing CA bundle", secret.Namespace, secret.Name) | ||
|
|
||
| // make a copy to avoid mutating cache state | ||
| secretCopy := secret.DeepCopy() | ||
| secretCopy.Data = map[string][]byte{api.InjectionDataKey: []byte(bi.caBundle)} | ||
| // set the owning-component unless someone else has claimed it. | ||
| if len(secretCopy.Annotations[apiannotations.OpenShiftComponent]) == 0 { | ||
| secretCopy.Annotations[apiannotations.OpenShiftComponent] = api.OwningJiraComponent | ||
| secretCopy.Annotations[apiannotations.OpenShiftDescription] = fmt.Sprintf("Secret is added/updated with a data item containing the CA signing bundle that can be used to verify service-serving certificates") | ||
| } | ||
|
|
||
| _, err = bi.client.Secrets(secretCopy.Namespace).Update(ctx, secretCopy, metav1.UpdateOptions{}) | ||
| return err | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix nil
Annotationspanic and reconsider overwriting all Secret dataTwo things to flag here:
secretCopy.Annotationsmay benilfor a freshly-created Secret. Writing to a nil map will panic:Initialize the map before writing:
Datais fully overwritten (design/semantics):secretCopy.Data = map[string][]byte{...}drops all existing keys and, together with thelen(secret.Data) == 1skip condition, forces injected Secrets to contain onlyservice-ca.crt. That’s consistent with the new e2e tests (which assertlen(Data)==1and that extra keys get “stomped”), but it is stricter than the README wording (“adds or updates a data item”) and prevents co-locating other material in the same Secret.If you want parity with a more typical “only manage
InjectionDataKey” pattern (and to better match the README text), consider instead:and relaxing the
len(secret.Data) == 1checks inSync/tests to only care thatInjectionDataKeymatches the operator’s bundle.🤖 Prompt for AI Agents