2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-02 07:21:07 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-08 08:45:27 -04:00
|
|
|
"context"
|
2022-06-09 10:10:42 -04:00
|
|
|
"fmt"
|
2022-09-08 08:45:27 -04:00
|
|
|
"io"
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-06-08 02:26:08 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/cloudcmd"
|
2022-06-07 05:05:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/proto"
|
2022-09-08 08:45:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
type recoveryClient interface {
|
|
|
|
Connect(endpoint string, validators atls.Validator) error
|
|
|
|
Recover(ctx context.Context, masterSecret, salt []byte) error
|
|
|
|
io.Closer
|
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
// NewRecoverCmd returns a new cobra.Command for the recover command.
|
|
|
|
func NewRecoverCmd() *cobra.Command {
|
2022-05-02 07:21:07 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "recover",
|
2022-05-09 11:02:47 -04:00
|
|
|
Short: "Recover a completely stopped Constellation cluster",
|
2022-05-04 03:13:46 -04:00
|
|
|
Long: "Recover a Constellation cluster by sending a recovery key to an instance in the boot stage." +
|
2022-05-02 07:21:07 -04:00
|
|
|
"\nThis is only required if instances restart without other instances available for bootstrapping.",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
RunE: runRecover,
|
|
|
|
}
|
2022-05-06 11:51:41 -04:00
|
|
|
cmd.Flags().StringP("endpoint", "e", "", "endpoint of the instance, passed as HOST[:PORT] (required)")
|
2022-05-06 07:56:02 -04:00
|
|
|
must(cmd.MarkFlagRequired("endpoint"))
|
2022-07-29 03:52:47 -04:00
|
|
|
cmd.Flags().String("master-secret", constants.MasterSecretFilename, "path to master secret file")
|
2022-05-02 07:21:07 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
func runRecover(cmd *cobra.Command, _ []string) error {
|
2022-05-02 07:21:07 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-09-08 08:45:27 -04:00
|
|
|
recoveryClient := &proto.RecoverClient{}
|
2022-05-02 07:21:07 -04:00
|
|
|
defer recoveryClient.Close()
|
2022-06-28 05:19:03 -04:00
|
|
|
return recover(cmd, fileHandler, recoveryClient)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 05:19:03 -04:00
|
|
|
func recover(cmd *cobra.Command, fileHandler file.Handler, recoveryClient recoveryClient) error {
|
2022-07-29 03:52:47 -04:00
|
|
|
flags, err := parseRecoverFlags(cmd)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
var masterSecret masterSecret
|
|
|
|
if err := fileHandler.ReadJSON(flags.secretPath, &masterSecret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
var stat state.ConstellationState
|
|
|
|
if err := fileHandler.ReadJSON(constants.StateFilename, &stat); err != nil {
|
2022-05-02 07:21:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
provider := cloudprovider.FromString(stat.CloudProvider)
|
2022-09-07 09:38:29 -04:00
|
|
|
config, err := readConfig(cmd.OutOrStdout(), fileHandler, flags.configPath)
|
2022-05-23 09:01:39 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return fmt.Errorf("reading and validating config: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
validators, err := cloudcmd.NewValidator(provider, config)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
if err := recoveryClient.Connect(flags.endpoint, validators.V(cmd)); err != nil {
|
2022-05-02 07:21:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
if err := recoveryClient.Recover(cmd.Context(), masterSecret.Key, masterSecret.Salt); err != nil {
|
2022-05-02 07:21:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Pushed recovery key.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
func parseRecoverFlags(cmd *cobra.Command) (recoverFlags, error) {
|
2022-05-06 07:56:02 -04:00
|
|
|
endpoint, err := cmd.Flags().GetString("endpoint")
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return recoverFlags{}, fmt.Errorf("parsing endpoint argument: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
endpoint, err = addPortIfMissing(endpoint, constants.BootstrapperPort)
|
2022-05-06 07:56:02 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return recoverFlags{}, fmt.Errorf("validating endpoint argument: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
masterSecretPath, err := cmd.Flags().GetString("master-secret")
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return recoverFlags{}, fmt.Errorf("parsing master-secret path argument: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 05:56:43 -04:00
|
|
|
configPath, err := cmd.Flags().GetString("config")
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return recoverFlags{}, fmt.Errorf("parsing config path argument: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return recoverFlags{
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint: endpoint,
|
|
|
|
secretPath: masterSecretPath,
|
|
|
|
configPath: configPath,
|
2022-05-02 07:21:07 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type recoverFlags struct {
|
2022-07-29 03:52:47 -04:00
|
|
|
endpoint string
|
|
|
|
secretPath string
|
|
|
|
configPath string
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|