-
Notifications
You must be signed in to change notification settings - Fork 21
[6/8] cli: support insecure manifests behind opt-in #2536
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
6c4f50d
38266fe
45dbf5d
e35e886
772ceb3
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 |
|---|---|---|
|
|
@@ -93,6 +93,7 @@ subcommands.`, | |
| cmd.Flags().Bool("insecure-enable-debug-shell-access", false, "enable the debug shell service in the pod CVM to get access from container to guest VM") | ||
| cmd.Flags().Bool("calculate-pod-memory", false, "calculate pod memory based on image layer sizes and container resource limits") | ||
| cmd.Flags().StringP("output", "o", "", "output file for generated YAML") | ||
| cmd.Flags().Bool("INSECURE", false, "allow generation for insecure (non-CC) runtimes (also requires the CONTRAST_ALLOW_INSECURE_RUNTIMES environment variable to be set)") | ||
| must(cmd.MarkFlagFilename("policy", "rego")) | ||
| must(cmd.MarkFlagFilename("settings", "json")) | ||
| must(cmd.MarkFlagFilename("manifest", "json")) | ||
|
|
@@ -144,6 +145,10 @@ func runGenerate(cmd *cobra.Command, args []string) error { | |
| usedPlatforms.Add(flags.referenceValuesPlatform) | ||
| } | ||
|
|
||
| if err := validateInsecurePlatforms(usedPlatforms, flags.allowInsecureRuntimes); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // generate a manifest by checking if a manifest exists and using that, | ||
| // or otherwise using a default. | ||
| var mnf *manifest.Manifest | ||
|
|
@@ -294,7 +299,7 @@ func runGenerate(cmd *cobra.Command, args []string) error { | |
| return nil | ||
| } | ||
|
|
||
| // mapContrastWorkloads applies the given function to all workloads with a Contrast runtime class. | ||
| // mapContrastWorkloads applies the given function to all workloads with the 'contrast-cc' or 'contrast-insecure' runtime class. | ||
| // The callback receives an apply configuration together with the file path and index the unstructured object has in the file map. | ||
| // Changes to the apply configuration are not applied to the original unstructured object. | ||
| func mapContrastWorkloads(fileMap map[string][]*unstructured.Unstructured, f func(res any, path string, idx int) (any, error)) error { | ||
|
|
@@ -325,7 +330,9 @@ func mapContrastWorkloads(fileMap map[string][]*unstructured.Unstructured, f fun | |
|
|
||
| func isContrastWorkload(resource any) (ret bool) { | ||
| kuberesource.MapPodSpec(resource, func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration { | ||
| ret = kuberesource.IsContrastPod(spec) | ||
| if kuberesource.IsContrastPod(spec) { | ||
| ret = true | ||
| } | ||
|
msanft marked this conversation as resolved.
Outdated
|
||
| return spec | ||
| }) | ||
| return ret | ||
|
|
@@ -343,6 +350,16 @@ func isCoordinator(resource any) bool { | |
| return false | ||
| } | ||
|
|
||
| func patchCoordinatorAllowInsecure(resource any) { | ||
| r, ok := resource.(*applyappsv1.StatefulSetApplyConfiguration) | ||
| if !ok || !isCoordinator(resource) { | ||
|
Collaborator
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.
|
||
| return | ||
| } | ||
| if len(r.Spec.Template.Spec.Containers) > 0 { | ||
| r.Spec.Template.Spec.Containers[0].WithEnv(kuberesource.NewEnvVar("CONTRAST_ALLOW_INSECURE", "1")) | ||
| } | ||
| } | ||
|
|
||
| func runVerifiers(fileMap map[string][]*unstructured.Unstructured, verifiers []verifier.Verifier) error { | ||
| var findings error | ||
| for _, v := range verifiers { | ||
|
|
@@ -428,7 +445,7 @@ func extractTargets(paths []string, configFile io.Writer, logger *slog.Logger) ( | |
| } | ||
| } | ||
| if len(fileMap) == 0 { | ||
| return nil, "", fmt.Errorf("no .yml/.yaml files with 'contrast-cc' runtime found") | ||
| return nil, "", fmt.Errorf("no .yml/.yaml files with 'contrast-cc' or 'contrast-insecure' runtime found") | ||
| } | ||
|
|
||
| extraData, err := kuberesource.EncodeUnstructured(extraResources) | ||
|
|
@@ -586,6 +603,9 @@ func patchTargets(fileMap map[string][]*unstructured.Unstructured, imageReplacem | |
| if flags.injectImageStore { | ||
| kuberesource.AddImageStore([]any{res}) | ||
| } | ||
| if flags.allowInsecureRuntimes { | ||
| patchCoordinatorAllowInsecure(res) | ||
| } | ||
|
Comment on lines
+606
to
+608
Collaborator
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. This may be overly cautious, but the flag alone should not be enough in an all-secure deployment. Maybe the if should additionally check that the usedPlatforms are all insecure. |
||
|
|
||
| kuberesource.PatchImages([]any{res}, replacements) | ||
|
|
||
|
|
@@ -631,6 +651,19 @@ func injectServiceMesh(resource any, memoryProfile kuberesource.MemoryProfile) e | |
| return nil | ||
| } | ||
|
|
||
| func validateInsecurePlatforms(usedPlatforms kuberesource.PlatformCollection, allowInsecure bool) error { | ||
| if !slices.ContainsFunc(usedPlatforms.Platforms(), platforms.IsInsecure) { | ||
| return nil | ||
| } | ||
| if !allowInsecure { | ||
| return fmt.Errorf("insecure runtime platforms detected but --INSECURE flag not set") | ||
| } | ||
| if os.Getenv("CONTRAST_ALLOW_INSECURE_RUNTIMES") == "" { | ||
|
msanft marked this conversation as resolved.
Outdated
msanft marked this conversation as resolved.
Outdated
|
||
| return fmt.Errorf("insecure runtime platforms detected but CONTRAST_ALLOW_INSECURE_RUNTIMES environment variable not set") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func validateOutputFile(outputFile string) error { | ||
| if outputFile == "" { | ||
| return nil | ||
|
|
@@ -758,7 +791,17 @@ func patchRuntimeClassName(defaultRuntimeHandler string) func(*applycorev1.PodSp | |
| if spec == nil || spec.RuntimeClassName == nil { | ||
| return spec, nil | ||
| } | ||
| if *spec.RuntimeClassName == "kata-cc-isolation" || *spec.RuntimeClassName == "contrast-cc" { | ||
| if *spec.RuntimeClassName == "kata-cc-isolation" || *spec.RuntimeClassName == "contrast-cc" || *spec.RuntimeClassName == "contrast-insecure" { | ||
| // Only allow the bare runtime class names if the default runtime handler is compatible. | ||
| // For example, `contrast-cc` should only resolve when `--reference-values` is set to a CC-enabled platform, | ||
| // and `contrast-insecure` should only resolve when `--reference-values` is set to an insecure platform. | ||
| if *spec.RuntimeClassName == "contrast-insecure" && !strings.HasPrefix(defaultRuntimeHandler, "contrast-insecure-") { | ||
| return nil, fmt.Errorf("bare 'contrast-insecure' runtime class requires --reference-values to be set to an insecure platform") | ||
| } | ||
| if (*spec.RuntimeClassName == "contrast-cc" || *spec.RuntimeClassName == "kata-cc-isolation") && | ||
| strings.HasPrefix(defaultRuntimeHandler, "contrast-insecure-") { | ||
| return nil, fmt.Errorf("bare %q runtime class is incompatible with insecure --reference-values platform %q", *spec.RuntimeClassName, defaultRuntimeHandler) | ||
| } | ||
| spec.RuntimeClassName = &defaultRuntimeHandler | ||
| if kuberesource.PodSpecRequiresGPU(spec) { | ||
| platform, err := platforms.FromRuntimeClassString(*spec.RuntimeClassName) | ||
|
|
@@ -773,7 +816,7 @@ func patchRuntimeClassName(defaultRuntimeHandler string) func(*applycorev1.PodSp | |
| } | ||
| return spec, nil | ||
| } | ||
| if !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc-") { | ||
| if !kuberesource.IsContrastPod(spec) { | ||
| return spec, nil | ||
| } | ||
| overridePlatform, err := platforms.FromRuntimeClassString(*spec.RuntimeClassName) | ||
|
|
@@ -961,6 +1004,7 @@ type generateFlags struct { | |
| injectImageStore bool | ||
| insecureEnableDebugShell bool | ||
| calculatePodMemory bool | ||
| allowInsecureRuntimes bool | ||
| outputFile string | ||
| } | ||
|
|
||
|
|
@@ -1066,6 +1110,10 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) { | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| allowInsecureRuntimes, err := cmd.Flags().GetBool("INSECURE") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| outputFile, err := cmd.Flags().GetString("output") | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -1093,6 +1141,7 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) { | |
| injectImageStore: injectImageStore, | ||
| insecureEnableDebugShell: insecureEnableDebugShell, | ||
| calculatePodMemory: calculatePodMemory, | ||
| allowInsecureRuntimes: allowInsecureRuntimes, | ||
| outputFile: outputFile, | ||
| }, nil | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.