|
| 1 | +package generate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/project-copacetic/copacetic/pkg/buildkit" |
| 10 | + "github.com/project-copacetic/copacetic/pkg/types" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "golang.org/x/term" |
| 13 | + |
| 14 | + // Register connection helpers for buildkit. |
| 15 | + _ "github.com/moby/buildkit/client/connhelper/dockercontainer" |
| 16 | + _ "github.com/moby/buildkit/client/connhelper/kubepod" |
| 17 | + _ "github.com/moby/buildkit/client/connhelper/nerdctlcontainer" |
| 18 | + _ "github.com/moby/buildkit/client/connhelper/podmancontainer" |
| 19 | + _ "github.com/moby/buildkit/client/connhelper/ssh" |
| 20 | +) |
| 21 | + |
| 22 | +type generateArgs struct { |
| 23 | + appImage string |
| 24 | + report string |
| 25 | + patchedTag string |
| 26 | + suffix string |
| 27 | + workingFolder string |
| 28 | + timeout time.Duration |
| 29 | + scanner string |
| 30 | + ignoreError bool |
| 31 | + outputContext string |
| 32 | + format string |
| 33 | + output string |
| 34 | + bkOpts buildkit.Opts |
| 35 | + platform []string |
| 36 | + loader string |
| 37 | +} |
| 38 | + |
| 39 | +func NewGenerateCmd() *cobra.Command { |
| 40 | + ga := generateArgs{} |
| 41 | + generateCmd := &cobra.Command{ |
| 42 | + Use: "generate", |
| 43 | + Short: "Generate a Docker build context tar stream for patching container images", |
| 44 | + Long: `Generate creates a tar stream containing a Dockerfile and patch layer that can be piped to 'docker build'. |
| 45 | +This command produces a build context with the patch diff layer and a Dockerfile that applies the patch.`, |
| 46 | + Example: ` # Generate patch context and pipe to docker build |
| 47 | + copa generate -i ubuntu:22.04 -r trivy.json | docker build -t ubuntu:22.04-patched - |
| 48 | + |
| 49 | + # Generate patch context without vulnerability report (update all packages) |
| 50 | + copa generate -i alpine:3.18 | docker build -t alpine:3.18-patched - |
| 51 | + |
| 52 | + # Save context to file |
| 53 | + copa generate -i alpine:3.18 -r scan.json --output-context patch.tar`, |
| 54 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 55 | + // Check if stdout is a TTY when not writing to file |
| 56 | + if ga.outputContext == "" && term.IsTerminal(int(os.Stdout.Fd())) { |
| 57 | + return fmt.Errorf("refusing to write tar stream to terminal. Use --output-context to save to file or redirect stdout") |
| 58 | + } |
| 59 | + |
| 60 | + opts := &types.Options{ |
| 61 | + Image: ga.appImage, |
| 62 | + Report: ga.report, |
| 63 | + PatchedTag: ga.patchedTag, |
| 64 | + Suffix: ga.suffix, |
| 65 | + WorkingFolder: ga.workingFolder, |
| 66 | + Timeout: ga.timeout, |
| 67 | + Scanner: ga.scanner, |
| 68 | + IgnoreError: ga.ignoreError, |
| 69 | + OutputContext: ga.outputContext, |
| 70 | + Format: ga.format, |
| 71 | + Output: ga.output, |
| 72 | + BkAddr: ga.bkOpts.Addr, |
| 73 | + BkCACertPath: ga.bkOpts.CACertPath, |
| 74 | + BkCertPath: ga.bkOpts.CertPath, |
| 75 | + BkKeyPath: ga.bkOpts.KeyPath, |
| 76 | + Platforms: ga.platform, |
| 77 | + Loader: ga.loader, |
| 78 | + } |
| 79 | + return Generate(context.Background(), opts) |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + flags := generateCmd.Flags() |
| 84 | + flags.StringVarP(&ga.appImage, "image", "i", "", "Application image name and tag to patch") |
| 85 | + flags.StringVarP(&ga.report, "report", "r", "", "Vulnerability report file path (optional)") |
| 86 | + flags.StringVarP(&ga.patchedTag, "tag", "t", "", "Tag for the patched image") |
| 87 | + flags.StringVarP(&ga.suffix, "tag-suffix", "", "patched", "Suffix for the patched image (if no explicit --tag provided)") |
| 88 | + flags.StringVarP(&ga.workingFolder, "working-folder", "w", "", "Working folder, defaults to system temp folder") |
| 89 | + flags.StringVarP(&ga.bkOpts.Addr, "addr", "a", "", "Address of buildkitd service, defaults to local docker daemon with fallback to "+buildkit.DefaultAddr) |
| 90 | + flags.StringVarP(&ga.bkOpts.CACertPath, "cacert", "", "", "Absolute path to buildkitd CA certificate") |
| 91 | + flags.StringVarP(&ga.bkOpts.CertPath, "cert", "", "", "Absolute path to buildkit client certificate") |
| 92 | + flags.StringVarP(&ga.bkOpts.KeyPath, "key", "", "", "Absolute path to buildkit client key") |
| 93 | + flags.DurationVar(&ga.timeout, "timeout", 5*time.Minute, "Timeout for the operation, defaults to '5m'") |
| 94 | + flags.StringVarP(&ga.scanner, "scanner", "s", "trivy", "Scanner that generated the report, defaults to 'trivy'") |
| 95 | + flags.BoolVar(&ga.ignoreError, "ignore-errors", false, "Ignore errors during patching") |
| 96 | + flags.StringVarP(&ga.format, "format", "f", "openvex", "Output format, defaults to 'openvex'") |
| 97 | + flags.StringVarP(&ga.output, "output", "o", "", "Output file path") |
| 98 | + flags.StringSliceVar(&ga.platform, "platform", nil, |
| 99 | + "Target platform(s) for multi-arch images when no report directory is provided (e.g., linux/amd64,linux/arm64). "+ |
| 100 | + "Valid platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6. "+ |
| 101 | + "If platform flag is used, only specified platforms are patched and the rest are preserved. If not specified, all platforms present in the image are patched.") |
| 102 | + flags.StringVarP(&ga.loader, "loader", "l", "", "Loader to use for loading images. Options: 'docker', 'podman', or empty for auto-detection based on buildkit address") |
| 103 | + flags.StringVar(&ga.outputContext, "output-context", "", "Path to save the generated tar context (instead of stdout)") |
| 104 | + |
| 105 | + if err := generateCmd.MarkFlagRequired("image"); err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + |
| 109 | + return generateCmd |
| 110 | +} |
0 commit comments