Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Comment thread
msanft marked this conversation as resolved.
Outdated
must(cmd.MarkFlagFilename("policy", "rego"))
must(cmd.MarkFlagFilename("settings", "json"))
must(cmd.MarkFlagFilename("manifest", "json"))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Comment thread
msanft marked this conversation as resolved.
Outdated
return spec
})
return ret
Expand All @@ -343,6 +350,16 @@ func isCoordinator(resource any) bool {
return false
}

func patchCoordinatorAllowInsecure(resource any) {
r, ok := resource.(*applyappsv1.StatefulSetApplyConfiguration)
if !ok || !isCoordinator(resource) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isCoordinator needs to additionally check r.Spec.Template.Spec != nil

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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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)

Expand Down Expand Up @@ -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") == "" {
Comment thread
msanft marked this conversation as resolved.
Outdated
Comment thread
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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -961,6 +1004,7 @@ type generateFlags struct {
injectImageStore bool
insecureEnableDebugShell bool
calculatePodMemory bool
allowInsecureRuntimes bool
outputFile string
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1093,6 +1141,7 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) {
injectImageStore: injectImageStore,
insecureEnableDebugShell: insecureEnableDebugShell,
calculatePodMemory: calculatePodMemory,
allowInsecureRuntimes: allowInsecureRuntimes,
outputFile: outputFile,
}, nil
}
Expand Down
Loading
Loading