-
Notifications
You must be signed in to change notification settings - Fork 182
PoC: Fake import to verify KMS integration #1951
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
Changes from all commits
71962d8
eefd6e0
df55e94
01ed3d1
2afae7d
a11ec65
e603a1a
071e794
23f5281
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,3 +138,4 @@ spec: | |
| - hostPath: | ||
| path: /var/log/kube-apiserver | ||
| name: audit-dir | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,96 @@ | ||||||||||||||||||
| package targetconfigcontroller | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "context" | ||||||||||||||||||
| "fmt" | ||||||||||||||||||
|
|
||||||||||||||||||
| configv1 "github.com/openshift/api/config/v1" | ||||||||||||||||||
| configv1listers "github.com/openshift/client-go/config/listers/config/v1" | ||||||||||||||||||
| "github.com/openshift/library-go/pkg/operator/encryption/kms" | ||||||||||||||||||
| corev1 "k8s.io/api/core/v1" | ||||||||||||||||||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||||||||||||||
| "k8s.io/klog/v2" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| // copied from https://github.com/flavianmissi/cluster-kube-apiserver-operator/tree/kms-plugin-sidecars | ||||||||||||||||||
|
|
||||||||||||||||||
| const ( | ||||||||||||||||||
| // KMSPluginImageEnvVar is the environment variable that specifies the KMS plugin container image | ||||||||||||||||||
| // This should be set by the operator deployment | ||||||||||||||||||
| KMSPluginImageEnvVar = "KMS_PLUGIN_IMAGE" | ||||||||||||||||||
|
|
||||||||||||||||||
| // DefaultKMSPluginImage is the fallback image if KMS_PLUGIN_IMAGE is not set | ||||||||||||||||||
| DefaultKMSPluginImage = "quay.io/fmissi/aws-kms-plugin:0.1.0" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| // getKMSEncryptionConfig checks if KMS encryption is enabled and returns the configuration | ||||||||||||||||||
| // Returns: | ||||||||||||||||||
| // - kmsConfig: the KMS configuration if enabled, nil otherwise | ||||||||||||||||||
| // - enabled: true if KMS encryption is enabled | ||||||||||||||||||
| // - error: any error encountered while reading the config | ||||||||||||||||||
| func getKMSEncryptionConfig(ctx context.Context, apiserverLister configv1listers.APIServerLister) (*configv1.KMSConfig, bool, error) { | ||||||||||||||||||
| apiserver, err := apiserverLister.Get("cluster") | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| if apierrors.IsNotFound(err) { | ||||||||||||||||||
| klog.V(4).Info("APIServer config.openshift.io/cluster not found, KMS encryption not enabled") | ||||||||||||||||||
| return nil, false, nil | ||||||||||||||||||
| } | ||||||||||||||||||
| return nil, false, fmt.Errorf("failed to get APIServer config: %w", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check if encryption is configured | ||||||||||||||||||
| if apiserver.Spec.Encryption.Type != configv1.EncryptionTypeKMS { | ||||||||||||||||||
| klog.V(4).Infof("Encryption type is %q, not KMS - skipping KMS plugin injection", apiserver.Spec.Encryption.Type) | ||||||||||||||||||
| return nil, false, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // KMS type is set, must have KMS config | ||||||||||||||||||
| if apiserver.Spec.Encryption.KMS == nil { | ||||||||||||||||||
| return nil, false, fmt.Errorf("encryption type is KMS but kms config is nil") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| klog.Infof("KMS encryption enabled with type=%s, region=%s, keyARN=%s", | ||||||||||||||||||
| apiserver.Spec.Encryption.KMS.Type, | ||||||||||||||||||
| apiserver.Spec.Encryption.KMS.AWS.Region, | ||||||||||||||||||
| apiserver.Spec.Encryption.KMS.AWS.KeyARN) | ||||||||||||||||||
|
Comment on lines
+52
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against nil AWS KMS config before logging.
- klog.Infof("KMS encryption enabled with type=%s, region=%s, keyARN=%s",
- apiserver.Spec.Encryption.KMS.Type,
- apiserver.Spec.Encryption.KMS.AWS.Region,
- apiserver.Spec.Encryption.KMS.AWS.KeyARN)
+ klog.Infof("KMS encryption enabled with type=%s", apiserver.Spec.Encryption.KMS.Type)
+ if awsConfig := apiserver.Spec.Encryption.KMS.AWS; awsConfig != nil {
+ klog.Infof("KMS AWS config: region=%s, keyARN=%s", awsConfig.Region, awsConfig.KeyARN)
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| return apiserver.Spec.Encryption.KMS, true, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // injectKMSPlugin adds the KMS plugin sidecar container to the kube-apiserver pod | ||||||||||||||||||
| // if KMS encryption is enabled in the cluster APIServer config | ||||||||||||||||||
| func injectKMSPlugin(ctx context.Context, pod *corev1.Pod, apiserverLister configv1listers.APIServerLister, kmsPluginImage string) error { | ||||||||||||||||||
| // Check if KMS encryption is enabled | ||||||||||||||||||
| kmsConfig, enabled, err := getKMSEncryptionConfig(ctx, apiserverLister) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return fmt.Errorf("failed to check KMS encryption config: %w", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if !enabled { | ||||||||||||||||||
| klog.V(4).Info("KMS encryption not enabled, skipping sidecar injection") | ||||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Validate the image is set | ||||||||||||||||||
| if kmsPluginImage == "" { | ||||||||||||||||||
| kmsPluginImage = DefaultKMSPluginImage | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| klog.Infof("Injecting KMS plugin sidecar container (image: %s)", kmsPluginImage) | ||||||||||||||||||
|
|
||||||||||||||||||
| // Create container config for kube-apiserver | ||||||||||||||||||
| // kube-apiserver uses hostNetwork: true, so it accesses AWS credentials via IMDS | ||||||||||||||||||
| containerConfig := &kms.ContainerConfig{ | ||||||||||||||||||
| Image: kmsPluginImage, | ||||||||||||||||||
| UseHostNetwork: true, // Static pod with hostNetwork uses EC2 IMDS for AWS credentials | ||||||||||||||||||
| KMSConfig: kmsConfig, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Inject the KMS plugin sidecar container and volumes into the pod spec | ||||||||||||||||||
| if err := kms.AddKMSPluginToPodSpec(&pod.Spec, kmsConfig, containerConfig, true); err != nil { | ||||||||||||||||||
| return fmt.Errorf("failed to inject KMS plugin sidecar: %w", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| klog.Infof("Successfully injected KMS plugin sidecar container") | ||||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
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.
Remove trailing blank line.
The YAML file has an extra blank line at the end of the volumes section that violates linting rules.
Apply this diff to remove the trailing blank line:
- hostPath: path: /var/log/kube-apiserver name: audit-dir -📝 Committable suggestion
🧰 Tools
🪛 YAMLlint (1.37.1)
[warning] 141-141: too many blank lines (1 > 0)
(empty-lines)
🤖 Prompt for AI Agents