2023-07-26 11:29:03 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-08-23 04:35:42 -04:00
|
|
|
"context"
|
2023-07-26 11:29:03 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-08-23 04:35:42 -04:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
2023-07-26 11:29:03 -04:00
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
2023-07-26 11:29:03 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2023-08-04 07:53:51 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-07-26 11:29:03 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2023-10-16 09:05:29 -04:00
|
|
|
"github.com/spf13/pflag"
|
2023-07-26 11:29:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func newIAMUpgradeCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "upgrade",
|
|
|
|
Short: "Find and apply upgrades to your IAM profile",
|
|
|
|
Long: "Find and apply upgrades to your IAM profile.",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
}
|
|
|
|
cmd.AddCommand(newIAMUpgradeApplyCmd())
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIAMUpgradeApplyCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "apply",
|
|
|
|
Short: "Apply an upgrade to an IAM profile",
|
|
|
|
Long: "Apply an upgrade to an IAM profile.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: runIAMUpgradeApply,
|
|
|
|
}
|
2023-08-04 07:53:51 -04:00
|
|
|
cmd.Flags().BoolP("yes", "y", false, "run upgrades without further confirmation")
|
2023-07-26 11:29:03 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
type iamUpgradeApplyFlags struct {
|
|
|
|
rootFlags
|
|
|
|
yes bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *iamUpgradeApplyFlags) parse(flags *pflag.FlagSet) error {
|
|
|
|
if err := f.rootFlags.parse(flags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
yes, err := flags.GetBool("yes")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting 'yes' flag: %w", err)
|
|
|
|
}
|
|
|
|
f.yes = yes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
type iamUpgradeApplyCmd struct {
|
|
|
|
fileHandler file.Handler
|
|
|
|
log debugLog
|
2023-09-14 05:51:20 -04:00
|
|
|
configFetcher attestationconfigapi.Fetcher
|
2023-10-16 09:05:29 -04:00
|
|
|
flags iamUpgradeApplyFlags
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|
|
|
|
|
2023-07-26 11:29:03 -04:00
|
|
|
func runIAMUpgradeApply(cmd *cobra.Command, _ []string) error {
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2023-08-16 03:59:32 -04:00
|
|
|
upgradeID := generateUpgradeID(upgradeCmdKindIAM)
|
2023-08-23 04:35:42 -04:00
|
|
|
upgradeDir := filepath.Join(constants.UpgradeDir, upgradeID)
|
2023-09-14 05:51:20 -04:00
|
|
|
configFetcher := attestationconfigapi.NewFetcher()
|
2023-08-23 04:35:42 -04:00
|
|
|
iamMigrateCmd, err := cloudcmd.NewIAMUpgrader(
|
|
|
|
cmd.Context(),
|
|
|
|
constants.TerraformIAMWorkingDir,
|
|
|
|
upgradeDir,
|
|
|
|
terraform.LogLevelDebug,
|
|
|
|
fileHandler,
|
|
|
|
)
|
2023-07-26 11:29:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up IAM migration command: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up logger: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
i := iamUpgradeApplyCmd{
|
|
|
|
fileHandler: fileHandler,
|
|
|
|
log: log,
|
2023-09-14 05:51:20 -04:00
|
|
|
configFetcher: configFetcher,
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
if err := i.flags.parse(cmd.Flags()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-23 04:35:42 -04:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
return i.iamUpgradeApply(cmd, iamMigrateCmd, upgradeDir)
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func (i iamUpgradeApplyCmd) iamUpgradeApply(cmd *cobra.Command, iamUpgrader iamUpgrader, upgradeDir string) error {
|
|
|
|
conf, err := config.New(i.fileHandler, constants.ConfigFilename, i.configFetcher, i.flags.force)
|
2023-08-23 04:35:42 -04:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := cloudcmd.TerraformIAMUpgradeVars(conf, i.fileHandler)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting terraform variables: %w", err)
|
|
|
|
}
|
|
|
|
hasDiff, err := iamUpgrader.PlanIAMUpgrade(cmd.Context(), cmd.OutOrStderr(), vars, conf.GetProvider())
|
|
|
|
if err != nil {
|
2023-09-14 05:51:20 -04:00
|
|
|
return fmt.Errorf("planning terraform migrations: %w", err)
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
if !hasDiff && !i.flags.force {
|
2023-08-23 04:35:42 -04:00
|
|
|
cmd.Println("No IAM migrations necessary.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any Terraform migrations to apply, ask for confirmation
|
|
|
|
cmd.Println("The IAM upgrade requires a migration by applying an updated Terraform template. Please manually review the suggested changes.")
|
2023-10-16 09:05:29 -04:00
|
|
|
if !i.flags.yes {
|
2023-08-23 04:35:42 -04:00
|
|
|
ok, err := askToConfirm(cmd, "Do you want to apply the IAM upgrade?")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("asking for confirmation: %w", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
cmd.Println("Aborting upgrade.")
|
2023-09-14 05:51:20 -04:00
|
|
|
// User doesn't expect to see any changes in his workspace after aborting an "upgrade apply",
|
|
|
|
// therefore, roll back to the backed up state.
|
|
|
|
if err := iamUpgrader.RestoreIAMWorkspace(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"restoring Terraform workspace: %w, restore the Terraform workspace manually from %s ",
|
|
|
|
err,
|
|
|
|
filepath.Join(upgradeDir, constants.TerraformIAMUpgradeBackupDir),
|
|
|
|
)
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|
|
|
|
return errors.New("IAM upgrade aborted by user")
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 09:20:01 -05:00
|
|
|
i.log.Debug("Applying Terraform IAM migrations")
|
2023-08-23 04:35:42 -04:00
|
|
|
if err := iamUpgrader.ApplyIAMUpgrade(cmd.Context(), conf.GetProvider()); err != nil {
|
|
|
|
return fmt.Errorf("applying terraform migrations: %w", err)
|
2023-07-26 11:29:03 -04:00
|
|
|
}
|
2023-08-16 03:59:32 -04:00
|
|
|
|
2023-07-26 11:29:03 -04:00
|
|
|
cmd.Println("IAM profile successfully applied.")
|
2023-08-23 04:35:42 -04:00
|
|
|
|
2023-07-26 11:29:03 -04:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-23 04:35:42 -04:00
|
|
|
|
|
|
|
type iamUpgrader interface {
|
|
|
|
PlanIAMUpgrade(ctx context.Context, outWriter io.Writer, vars terraform.Variables, csp cloudprovider.Provider) (bool, error)
|
|
|
|
ApplyIAMUpgrade(ctx context.Context, csp cloudprovider.Provider) error
|
2023-09-14 05:51:20 -04:00
|
|
|
RestoreIAMWorkspace() error
|
2023-08-23 04:35:42 -04:00
|
|
|
}
|