2023-04-03 11:39:31 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
2023-04-03 11:39:31 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/extract"
|
|
|
|
"github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/sums"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newSumCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "sum",
|
|
|
|
Short: "Generate sha256sum file that pins an OCI image.",
|
|
|
|
RunE: runSum,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().String("oci-path", "", "Path to the OCI image to pin.")
|
|
|
|
cmd.Flags().String("output", "-", "Output file. If not set, the output is written to stdout.")
|
|
|
|
cmd.Flags().String("image-name", "", "Short name (suffix) of the OCI image to pin.")
|
2023-04-27 05:52:02 -04:00
|
|
|
cmd.Flags().String("repoimage-tag-file", "", "Tag file of the OCI image to pin.")
|
|
|
|
must(cmd.MarkFlagRequired("repoimage-tag-file"))
|
2023-04-03 11:39:31 -04:00
|
|
|
must(cmd.MarkFlagRequired("oci-path"))
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runSum(cmd *cobra.Command, _ []string) error {
|
|
|
|
flags, err := parseSumFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-08 09:20:01 -05:00
|
|
|
log := logger.NewTextLogger(flags.logLevel)
|
2024-04-03 09:49:03 -04:00
|
|
|
log.Debug("Using flags", "imageRepoTag", flags.imageRepoTag, "ociPath", flags.ociPath)
|
2023-04-03 11:39:31 -04:00
|
|
|
|
2023-04-27 05:52:02 -04:00
|
|
|
registry, prefix, name, tag, err := splitRepoTag(flags.imageRepoTag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("splitting repo tag: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
log.Debug(fmt.Sprintf("Generating sum file for OCI image %q.", name))
|
2023-04-03 11:39:31 -04:00
|
|
|
|
|
|
|
ociIndexPath := filepath.Join(flags.ociPath, "index.json")
|
|
|
|
index, err := os.Open(ociIndexPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening OCI index at %q: %w", ociIndexPath, err)
|
|
|
|
}
|
|
|
|
defer index.Close()
|
|
|
|
|
|
|
|
var out io.Writer
|
|
|
|
if flags.output == "-" {
|
|
|
|
out = cmd.OutOrStdout()
|
|
|
|
} else {
|
|
|
|
f, err := os.Create(flags.output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating output file %q: %w", flags.output, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
|
|
|
digest, err := extract.Digest(index)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("extracting OCI image digest: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-03 09:49:03 -04:00
|
|
|
log.Debug(fmt.Sprintf("OCI image digest: %q", digest))
|
2023-04-03 11:39:31 -04:00
|
|
|
|
|
|
|
refs := []sums.PinnedImageReference{
|
|
|
|
{
|
2023-04-27 05:52:02 -04:00
|
|
|
Registry: registry,
|
|
|
|
Prefix: prefix,
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
2023-04-03 11:39:31 -04:00
|
|
|
Digest: digest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sums.Create(refs, out); err != nil {
|
|
|
|
return fmt.Errorf("creating sum file: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Debug(fmt.Sprintf("Sum file created at %q 🤖", flags.output))
|
2023-04-03 11:39:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sumFlags struct {
|
2023-04-27 05:52:02 -04:00
|
|
|
ociPath string
|
|
|
|
output string
|
|
|
|
imageRepoTag string
|
2024-02-08 09:20:01 -05:00
|
|
|
logLevel slog.Level
|
2023-04-03 11:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseSumFlags(cmd *cobra.Command) (sumFlags, error) {
|
|
|
|
ociPath, err := cmd.Flags().GetString("oci-path")
|
|
|
|
if err != nil {
|
|
|
|
return sumFlags{}, err
|
|
|
|
}
|
|
|
|
output, err := cmd.Flags().GetString("output")
|
|
|
|
if err != nil {
|
|
|
|
return sumFlags{}, err
|
|
|
|
}
|
2023-04-27 05:52:02 -04:00
|
|
|
|
|
|
|
imageTagFile, err := cmd.Flags().GetString("repoimage-tag-file")
|
2023-04-03 11:39:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return sumFlags{}, err
|
|
|
|
}
|
2023-04-27 05:52:02 -04:00
|
|
|
tag, err := os.ReadFile(imageTagFile)
|
2023-04-03 11:39:31 -04:00
|
|
|
if err != nil {
|
2023-04-27 05:52:02 -04:00
|
|
|
return sumFlags{}, fmt.Errorf("reading image repotag file %q: %w", imageTagFile, err)
|
2023-04-03 11:39:31 -04:00
|
|
|
}
|
2023-04-27 05:52:02 -04:00
|
|
|
imageRepoTag := strings.TrimSpace(string(tag))
|
|
|
|
|
2023-04-03 11:39:31 -04:00
|
|
|
verbose, err := cmd.Flags().GetBool("verbose")
|
|
|
|
if err != nil {
|
|
|
|
return sumFlags{}, err
|
|
|
|
}
|
2024-02-08 09:20:01 -05:00
|
|
|
logLevel := slog.LevelInfo
|
2023-04-03 11:39:31 -04:00
|
|
|
if verbose {
|
2024-02-08 09:20:01 -05:00
|
|
|
logLevel = slog.LevelDebug
|
2023-04-03 11:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return sumFlags{
|
2023-04-27 05:52:02 -04:00
|
|
|
ociPath: ociPath,
|
|
|
|
output: output,
|
|
|
|
imageRepoTag: imageRepoTag,
|
|
|
|
logLevel: logLevel,
|
2023-04-03 11:39:31 -04:00
|
|
|
}, nil
|
|
|
|
}
|