2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
2022-11-28 04:27:33 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2022-10-11 07:57:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/sigstore"
|
2022-12-01 04:26:35 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
|
2022-08-31 05:59:07 -04:00
|
|
|
"github.com/manifoldco/promptui"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/talos-systems/talos/pkg/machinery/config/encoder"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newUpgradePlanCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "plan",
|
|
|
|
Short: "Plan an upgrade of a Constellation cluster",
|
|
|
|
Long: "Plan an upgrade of a Constellation cluster by fetching compatible image versions and their measurements.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: runUpgradePlan,
|
|
|
|
}
|
|
|
|
|
2022-09-11 10:24:15 -04:00
|
|
|
cmd.Flags().StringP("file", "f", "", "path to output file, or '-' for stdout (omit for interactive mode)")
|
2022-08-31 05:59:07 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUpgradePlan(cmd *cobra.Command, args []string) error {
|
2022-12-19 10:52:15 -05:00
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
defer log.Sync()
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
|
|
|
flags, err := parseUpgradePlanFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-19 10:52:15 -05:00
|
|
|
planner, err := cloudcmd.NewUpgrader(cmd.OutOrStdout(), log)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-01 04:26:35 -05:00
|
|
|
patchLister := versionsapi.New()
|
2022-10-11 07:57:52 -04:00
|
|
|
rekor, err := sigstore.NewRekor()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constructing Rekor client: %w", err)
|
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
cliVersion := getCurrentCLIVersion()
|
2022-08-31 05:59:07 -04:00
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
return upgradePlan(cmd, planner, patchLister, fileHandler, http.DefaultClient, rekor, flags, cliVersion)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// upgradePlan plans an upgrade of a Constellation cluster.
|
2022-11-29 05:39:07 -05:00
|
|
|
func upgradePlan(cmd *cobra.Command, planner upgradePlanner, patchLister patchLister,
|
2022-10-11 07:57:52 -04:00
|
|
|
fileHandler file.Handler, client *http.Client, rekor rekorVerifier, flags upgradePlanFlags,
|
2022-11-29 05:39:07 -05:00
|
|
|
cliVersion string,
|
2022-08-31 05:59:07 -04:00
|
|
|
) error {
|
2022-11-15 09:40:49 -05:00
|
|
|
conf, err := config.New(fileHandler, flags.configPath)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
2022-11-15 09:40:49 -05:00
|
|
|
return displayConfigValidationErrors(cmd.ErrOrStderr(), err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get current image version of the cluster
|
2022-11-15 09:40:49 -05:00
|
|
|
csp := conf.GetProvider()
|
2022-08-31 05:59:07 -04:00
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
version, err := getCurrentImageVersion(cmd.Context(), planner)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("checking current image version: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
// find compatible images
|
|
|
|
// image updates should always be possible for the current minor version of the cluster
|
|
|
|
// (e.g. 0.1.0 -> 0.1.1, 0.1.2, 0.1.3, etc.)
|
|
|
|
// additionally, we allow updates to the next minor version (e.g. 0.1.0 -> 0.2.0)
|
|
|
|
// if the CLI minor version is newer than the cluster minor version
|
|
|
|
currentImageMinorVer := semver.MajorMinor(version)
|
|
|
|
currentCLIMinorVer := semver.MajorMinor(cliVersion)
|
|
|
|
nextImageMinorVer, err := nextMinorVersion(currentImageMinorVer)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
2022-11-29 05:39:07 -05:00
|
|
|
return fmt.Errorf("calculating next image minor version: %w", err)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
|
|
|
|
var allowedMinorVersions []string
|
|
|
|
|
|
|
|
cliImageCompare := semver.Compare(currentCLIMinorVer, currentImageMinorVer)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case cliImageCompare < 0:
|
|
|
|
cmd.PrintErrln("Warning: CLI version is older than cluster image version. This is not supported.")
|
|
|
|
case cliImageCompare == 0:
|
|
|
|
allowedMinorVersions = []string{currentImageMinorVer}
|
|
|
|
case cliImageCompare > 0:
|
|
|
|
allowedMinorVersions = []string{currentImageMinorVer, nextImageMinorVer}
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateCandidates []string
|
|
|
|
for _, minorVer := range allowedMinorVersions {
|
2022-12-05 09:15:03 -05:00
|
|
|
versionList, err := patchLister.PatchVersionsOf(cmd.Context(), "-", "stable", minorVer, "image")
|
2022-11-29 05:39:07 -05:00
|
|
|
if err == nil {
|
|
|
|
updateCandidates = append(updateCandidates, versionList.Versions...)
|
|
|
|
}
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
// filter out versions that are not compatible with the current cluster
|
|
|
|
compatibleImages := getCompatibleImages(version, updateCandidates)
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
// get expected measurements for each image
|
2022-11-29 05:39:07 -05:00
|
|
|
upgrades, err := getCompatibleImageMeasurements(cmd.Context(), cmd, client, rekor, []byte(flags.cosignPubKey), csp, compatibleImages)
|
|
|
|
if err != nil {
|
2022-08-31 05:59:07 -04:00
|
|
|
return fmt.Errorf("fetching measurements for compatible images: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
if len(upgrades) == 0 {
|
|
|
|
cmd.PrintErrln("No compatible images found to upgrade to.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
// interactive mode
|
|
|
|
if flags.filePath == "" {
|
2022-11-10 04:27:24 -05:00
|
|
|
cmd.Printf("Current version: %s\n", version)
|
2022-08-31 05:59:07 -04:00
|
|
|
return upgradePlanInteractive(
|
|
|
|
&nopWriteCloser{cmd.OutOrStdout()},
|
|
|
|
io.NopCloser(cmd.InOrStdin()),
|
2022-11-15 09:40:49 -05:00
|
|
|
flags.configPath, conf, fileHandler,
|
2022-11-29 05:39:07 -05:00
|
|
|
upgrades,
|
2022-08-31 05:59:07 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write upgrade plan to stdout
|
|
|
|
if flags.filePath == "-" {
|
2022-11-29 05:39:07 -05:00
|
|
|
content, err := encoder.NewEncoder(upgrades).Encode()
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding compatible images: %w", err)
|
|
|
|
}
|
|
|
|
_, err = cmd.OutOrStdout().Write(content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write upgrade plan to file
|
2022-11-29 05:39:07 -05:00
|
|
|
return fileHandler.WriteYAML(flags.filePath, upgrades)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCompatibleImages trims the list of images to only ones compatible with the current cluster.
|
2022-11-29 05:39:07 -05:00
|
|
|
func getCompatibleImages(currentImageVersion string, images []string) []string {
|
|
|
|
var compatibleImages []string
|
2022-08-31 05:59:07 -04:00
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
for _, image := range images {
|
|
|
|
// check if image is newer than current version
|
|
|
|
if semver.Compare(image, currentImageVersion) <= 0 {
|
|
|
|
continue
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
compatibleImages = append(compatibleImages, image)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
return compatibleImages
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCompatibleImageMeasurements retrieves the expected measurements for each image.
|
2022-11-29 05:39:07 -05:00
|
|
|
func getCompatibleImageMeasurements(ctx context.Context, cmd *cobra.Command, client *http.Client, rekor rekorVerifier, pubK []byte,
|
|
|
|
csp cloudprovider.Provider, images []string,
|
|
|
|
) (map[string]config.UpgradeConfig, error) {
|
|
|
|
upgrades := make(map[string]config.UpgradeConfig)
|
|
|
|
for _, img := range images {
|
2022-12-09 05:51:38 -05:00
|
|
|
measurementsURL, err := measurementURL(csp, img, "measurements.json")
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
2022-11-29 05:39:07 -05:00
|
|
|
return nil, err
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-12-09 05:51:38 -05:00
|
|
|
signatureURL, err := measurementURL(csp, img, "measurements.json.sig")
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
2022-11-29 05:39:07 -05:00
|
|
|
return nil, err
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
var fetchedMeasurements measurements.M
|
|
|
|
hash, err := fetchedMeasurements.FetchAndVerify(
|
2022-11-28 04:27:33 -05:00
|
|
|
ctx, client,
|
|
|
|
measurementsURL,
|
|
|
|
signatureURL,
|
|
|
|
pubK,
|
|
|
|
measurements.WithMetadata{
|
2022-11-29 05:39:07 -05:00
|
|
|
CSP: csp,
|
|
|
|
Image: img,
|
2022-11-28 04:27:33 -05:00
|
|
|
},
|
|
|
|
)
|
2022-10-11 07:57:52 -04:00
|
|
|
if err != nil {
|
2022-11-29 05:39:07 -05:00
|
|
|
cmd.PrintErrf("Skipping image %q: %s\n", img, err)
|
|
|
|
continue
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
2022-10-11 07:57:52 -04:00
|
|
|
|
|
|
|
if err = verifyWithRekor(ctx, rekor, hash); err != nil {
|
2022-11-10 04:27:24 -05:00
|
|
|
cmd.PrintErrf("Warning: Unable to verify '%s' in Rekor.\n", hash)
|
|
|
|
cmd.PrintErrf("Make sure measurements are correct.\n")
|
2022-10-11 07:57:52 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
upgrades[img] = config.UpgradeConfig{
|
|
|
|
Image: img,
|
|
|
|
Measurements: fetchedMeasurements,
|
|
|
|
CSP: csp,
|
|
|
|
}
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
return upgrades, nil
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrentImageVersion retrieves the semantic version of the image currently installed in the cluster.
|
|
|
|
// If the cluster is not using a release image, an error is returned.
|
2022-11-29 05:39:07 -05:00
|
|
|
func getCurrentImageVersion(ctx context.Context, planner upgradePlanner) (string, error) {
|
|
|
|
_, imageVersion, err := planner.GetCurrentImage(ctx)
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
if !semver.IsValid(imageVersion) {
|
|
|
|
return "", fmt.Errorf("current image version is not a release image version: %q", imageVersion)
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
return imageVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentCLIVersion() string {
|
|
|
|
return "v" + constants.VersionInfo
|
2022-08-31 05:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseUpgradePlanFlags(cmd *cobra.Command) (upgradePlanFlags, error) {
|
|
|
|
configPath, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return upgradePlanFlags{}, err
|
|
|
|
}
|
|
|
|
filePath, err := cmd.Flags().GetString("file")
|
|
|
|
if err != nil {
|
|
|
|
return upgradePlanFlags{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upgradePlanFlags{
|
|
|
|
configPath: configPath,
|
|
|
|
filePath: filePath,
|
|
|
|
cosignPubKey: constants.CosignPublicKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func upgradePlanInteractive(out io.WriteCloser, in io.ReadCloser,
|
|
|
|
configPath string, config *config.Config, fileHandler file.Handler,
|
2022-11-29 05:39:07 -05:00
|
|
|
compatibleUpgrades map[string]config.UpgradeConfig,
|
2022-08-31 05:59:07 -04:00
|
|
|
) error {
|
|
|
|
var imageVersions []string
|
2022-11-29 05:39:07 -05:00
|
|
|
for k := range compatibleUpgrades {
|
2022-08-31 05:59:07 -04:00
|
|
|
imageVersions = append(imageVersions, k)
|
|
|
|
}
|
|
|
|
semver.Sort(imageVersions)
|
|
|
|
|
|
|
|
prompt := promptui.Select{
|
|
|
|
Label: "Select an image version to upgrade to",
|
|
|
|
Items: imageVersions,
|
|
|
|
Searcher: func(input string, index int) bool {
|
|
|
|
version := imageVersions[index]
|
|
|
|
trimmedVersion := strings.TrimPrefix(strings.Replace(version, ".", "", -1), "v")
|
|
|
|
input = strings.TrimPrefix(strings.Replace(input, ".", "", -1), "v")
|
|
|
|
return strings.Contains(trimmedVersion, input)
|
|
|
|
},
|
|
|
|
Size: 10,
|
|
|
|
Stdin: in,
|
|
|
|
Stdout: out,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, res, err := prompt.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(out, "Updating config to the following:")
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
fmt.Fprintf(out, "Image: %s\n", compatibleUpgrades[res].Image)
|
2022-08-31 05:59:07 -04:00
|
|
|
fmt.Fprintln(out, "Measurements:")
|
2022-11-29 05:39:07 -05:00
|
|
|
content, err := encoder.NewEncoder(compatibleUpgrades[res].Measurements).Encode()
|
2022-08-31 05:59:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding measurements: %w", err)
|
|
|
|
}
|
|
|
|
measurements := strings.TrimSuffix(strings.Replace("\t"+string(content), "\n", "\n\t", -1), "\n\t")
|
|
|
|
fmt.Fprintln(out, measurements)
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
config.Upgrade = compatibleUpgrades[res]
|
2022-08-31 05:59:07 -04:00
|
|
|
return fileHandler.WriteYAML(configPath, config, file.OptOverwrite)
|
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
func nextMinorVersion(version string) (string, error) {
|
|
|
|
major, minor, _, err := parseCanonicalSemver(version)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("v%d.%d", major, minor+1), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCanonicalSemver(version string) (major int, minor int, patch int, err error) {
|
|
|
|
version = semver.Canonical(version) // ensure version is in canonical form (vX.Y.Z)
|
|
|
|
num, err := fmt.Sscanf(version, "v%d.%d.%d", &major, &minor, &patch)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("parsing version: %w", err)
|
|
|
|
}
|
|
|
|
if num != 3 {
|
|
|
|
return 0, 0, 0, fmt.Errorf("parsing version: expected 3 numbers, got %d", num)
|
|
|
|
}
|
|
|
|
|
|
|
|
return major, minor, patch, nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 05:59:07 -04:00
|
|
|
type upgradePlanFlags struct {
|
|
|
|
configPath string
|
|
|
|
filePath string
|
|
|
|
cosignPubKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopWriteCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nopWriteCloser) Close() error { return nil }
|
|
|
|
|
|
|
|
type upgradePlanner interface {
|
|
|
|
GetCurrentImage(ctx context.Context) (*unstructured.Unstructured, string, error)
|
|
|
|
}
|
2022-11-29 05:39:07 -05:00
|
|
|
|
|
|
|
type patchLister interface {
|
2022-12-05 09:15:03 -05:00
|
|
|
PatchVersionsOf(ctx context.Context, ref, stream, minor, kind string) (*versionsapi.List, error)
|
2022-11-29 05:39:07 -05:00
|
|
|
}
|