2022-05-02 07:21:07 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
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-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/util"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var diskUUIDRegexp = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
|
|
|
|
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-05-06 11:51:41 -04:00
|
|
|
cmd.Flags().String("disk-uuid", "", "disk UUID of the encrypted state disk (required)")
|
2022-05-04 02:50:50 -04:00
|
|
|
must(cmd.MarkFlagRequired("disk-uuid"))
|
2022-05-09 11:02:47 -04:00
|
|
|
cmd.Flags().String("master-secret", constants.MasterSecretFilename, "path to base64-encoded master secret")
|
2022-05-02 07:21:07 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRecover(cmd *cobra.Command, args []string) error {
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
|
|
|
recoveryClient := &proto.KeyClient{}
|
|
|
|
defer recoveryClient.Close()
|
|
|
|
return recover(cmd.Context(), cmd, fileHandler, recoveryClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func recover(ctx context.Context, cmd *cobra.Command, fileHandler file.Handler, recoveryClient recoveryClient) error {
|
|
|
|
flags, err := parseRecoverFlags(cmd, fileHandler)
|
|
|
|
if 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)
|
|
|
|
|
|
|
|
config, err := readConfig(cmd.OutOrStdout(), fileHandler, flags.configPath, provider)
|
|
|
|
if err != nil {
|
2022-05-02 07:21:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
validators, err := cloudcmd.NewValidators(provider, config)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Print(validators.WarningsIncludeInit())
|
|
|
|
|
2022-05-06 07:56:02 -04:00
|
|
|
if err := recoveryClient.Connect(flags.endpoint, validators.V()); err != nil {
|
2022-05-02 07:21:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
diskKey, err := deriveStateDiskKey(flags.masterSecret, flags.diskUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := recoveryClient.PushStateDiskKey(ctx, diskKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Pushed recovery key.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRecoverFlags(cmd *cobra.Command, fileHandler file.Handler) (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 {
|
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
2022-05-06 07:56:02 -04:00
|
|
|
endpoint, err = validateEndpoint(endpoint, constants.CoordinatorPort)
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, err
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
diskUUID, err := cmd.Flags().GetString("disk-uuid")
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
|
|
|
if match := diskUUIDRegexp.MatchString(diskUUID); !match {
|
|
|
|
return recoverFlags{}, errors.New("flag '--disk-uuid' isn't a valid LUKS UUID")
|
|
|
|
}
|
|
|
|
diskUUID = strings.ToLower(diskUUID)
|
|
|
|
|
|
|
|
masterSecretPath, err := cmd.Flags().GetString("master-secret")
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
|
|
|
masterSecret, err := readMasterSecret(fileHandler, masterSecretPath)
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return recoverFlags{
|
2022-05-13 05:56:43 -04:00
|
|
|
endpoint: endpoint,
|
|
|
|
diskUUID: diskUUID,
|
|
|
|
masterSecret: masterSecret,
|
|
|
|
configPath: configPath,
|
2022-05-02 07:21:07 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type recoverFlags struct {
|
2022-05-13 05:56:43 -04:00
|
|
|
endpoint string
|
|
|
|
diskUUID string
|
|
|
|
masterSecret []byte
|
|
|
|
configPath string
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMasterSecret reads a base64 encoded master secret from file.
|
|
|
|
func readMasterSecret(fileHandler file.Handler, filename string) ([]byte, error) {
|
|
|
|
// Try to read the base64 secret from file
|
|
|
|
encodedSecret, err := fileHandler.Read(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(string(encodedSecret))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return decoded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deriveStateDiskKey derives a state disk key from a master secret and a disk UUID.
|
|
|
|
func deriveStateDiskKey(masterKey []byte, diskUUID string) ([]byte, error) {
|
|
|
|
return util.DeriveKey(masterKey, []byte("Constellation"), []byte("key"+diskUUID), constants.StateDiskKeyLength)
|
|
|
|
}
|