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-09-14 07:25:42 -04:00
|
|
|
"errors"
|
2022-06-09 10:10:42 -04:00
|
|
|
"fmt"
|
2022-09-08 08:45:27 -04:00
|
|
|
"io"
|
2022-09-14 07:25:42 -04:00
|
|
|
"net"
|
2022-09-26 03:57:40 -04:00
|
|
|
"sync"
|
2022-09-14 07:25:42 -04:00
|
|
|
"time"
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/recoverproto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
|
|
|
|
grpcRetry "github.com/edgelesssys/constellation/v2/internal/grpc/retry"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/retry"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
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-09-26 03:57:40 -04:00
|
|
|
cmd.Flags().StringP("endpoint", "e", "", "endpoint of the instance, passed as HOST[:PORT]")
|
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-14 07:25:42 -04:00
|
|
|
newDialer := func(validator *cloudcmd.Validator) *dialer.Dialer {
|
|
|
|
return dialer.New(nil, validator.V(cmd), &net.Dialer{})
|
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
return recover(cmd, fileHandler, 5*time.Second, &recoverDoer{}, newDialer)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
func recover(
|
|
|
|
cmd *cobra.Command, fileHandler file.Handler, interval time.Duration,
|
|
|
|
doer recoverDoerInterface, newDialer func(validator *cloudcmd.Validator) *dialer.Dialer,
|
|
|
|
) error {
|
|
|
|
flags, err := parseRecoverFlags(cmd, fileHandler)
|
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-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-09-26 03:57:40 -04:00
|
|
|
provider := config.GetProvider()
|
|
|
|
if provider == cloudprovider.Azure {
|
|
|
|
interval = 20 * time.Second // Azure LB takes a while to remove unhealthy instances
|
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
validator, err := cloudcmd.NewValidator(provider, config)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
doer.setDialer(newDialer(validator), flags.endpoint)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
measurementSecret, err := attestation.DeriveMeasurementSecret(masterSecret.Key, masterSecret.Salt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
doer.setSecrets(getStateDiskKeyFunc(masterSecret.Key, masterSecret.Salt), measurementSecret)
|
2022-09-14 07:25:42 -04:00
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
if err := recoverCall(cmd.Context(), cmd.OutOrStdout(), interval, doer); err != nil {
|
|
|
|
if grpcRetry.ServiceIsUnavailable(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("recovering cluster: %w", err)
|
|
|
|
}
|
2022-09-14 07:25:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
func recoverCall(ctx context.Context, out io.Writer, interval time.Duration, doer recoverDoerInterface) error {
|
|
|
|
var err error
|
|
|
|
ctr := 0
|
|
|
|
for {
|
|
|
|
once := sync.Once{}
|
|
|
|
retryOnceOnFailure := func(err error) bool {
|
|
|
|
// retry transient GCP LB errors
|
|
|
|
if grpcRetry.LoadbalancerIsNotReady(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
retry := false
|
|
|
|
|
|
|
|
// retry connection errors once
|
|
|
|
// this is necessary because Azure's LB takes a while to remove unhealthy instances
|
|
|
|
once.Do(func() {
|
|
|
|
retry = grpcRetry.ServiceIsUnavailable(err)
|
|
|
|
})
|
|
|
|
return retry
|
|
|
|
}
|
|
|
|
|
|
|
|
retrier := retry.NewIntervalRetrier(doer, interval, retryOnceOnFailure)
|
|
|
|
err = retrier.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Fprintln(out, "Pushed recovery key.")
|
|
|
|
ctr++
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
|
|
|
|
if ctr > 0 {
|
|
|
|
fmt.Fprintf(out, "Recovered %d control-plane nodes.\n", ctr)
|
|
|
|
} else if grpcRetry.ServiceIsUnavailable(err) {
|
|
|
|
fmt.Fprintln(out, "No control-plane nodes in need of recovery found. Exiting.")
|
|
|
|
return nil
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2022-09-26 03:57:40 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type recoverDoerInterface interface {
|
|
|
|
Do(ctx context.Context) error
|
|
|
|
setDialer(dialer grpcDialer, endpoint string)
|
|
|
|
setSecrets(getDiskKey func(uuid string) ([]byte, error), measurementSecret []byte)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type recoverDoer struct {
|
|
|
|
dialer grpcDialer
|
|
|
|
endpoint string
|
|
|
|
measurementSecret []byte
|
|
|
|
getDiskKey func(uuid string) (key []byte, err error)
|
|
|
|
}
|
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
// Do performs the recover streaming rpc.
|
2022-09-14 07:25:42 -04:00
|
|
|
func (d *recoverDoer) Do(ctx context.Context) (retErr error) {
|
|
|
|
conn, err := d.dialer.Dial(ctx, d.endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing recovery server: %w", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
// set up streaming client
|
|
|
|
protoClient := recoverproto.NewAPIClient(conn)
|
|
|
|
recoverclient, err := protoClient.Recover(ctx)
|
|
|
|
if err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("creating client: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
defer func() {
|
2022-09-26 03:57:40 -04:00
|
|
|
_ = recoverclient.CloseSend()
|
2022-09-14 07:25:42 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// send measurement secret as first message
|
|
|
|
if err := recoverclient.Send(&recoverproto.RecoverMessage{
|
|
|
|
Request: &recoverproto.RecoverMessage_MeasurementSecret{
|
|
|
|
MeasurementSecret: d.measurementSecret,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("sending measurement secret: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
// receive disk uuid
|
|
|
|
res, err := recoverclient.Recv()
|
|
|
|
if err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("receiving disk uuid: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
stateDiskKey, err := d.getDiskKey(res.DiskUuid)
|
|
|
|
if err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("getting state disk key: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send disk key
|
|
|
|
if err := recoverclient.Send(&recoverproto.RecoverMessage{
|
|
|
|
Request: &recoverproto.RecoverMessage_StateDiskKey{
|
|
|
|
StateDiskKey: stateDiskKey,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("sending state disk key: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := recoverclient.Recv(); err != nil && !errors.Is(err, io.EOF) {
|
2022-09-26 03:57:40 -04:00
|
|
|
return fmt.Errorf("receiving confirmation: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2022-05-02 07:21:07 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
func (d *recoverDoer) setDialer(dialer grpcDialer, endpoint string) {
|
|
|
|
d.dialer = dialer
|
|
|
|
d.endpoint = endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *recoverDoer) setSecrets(getDiskKey func(string) ([]byte, error), measurementSecret []byte) {
|
|
|
|
d.getDiskKey = getDiskKey
|
|
|
|
d.measurementSecret = measurementSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
type recoverFlags struct {
|
|
|
|
endpoint string
|
|
|
|
secretPath string
|
|
|
|
configPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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-09-26 03:57:40 -04:00
|
|
|
if endpoint == "" {
|
|
|
|
endpoint, err = readIPFromIDFile(fileHandler)
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, fmt.Errorf("getting recovery endpoint: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 07:25:42 -04:00
|
|
|
endpoint, err = addPortIfMissing(endpoint, constants.RecoveryPort)
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
func getStateDiskKeyFunc(masterKey, salt []byte) func(uuid string) ([]byte, error) {
|
|
|
|
return func(uuid string) ([]byte, error) {
|
|
|
|
return crypto.DeriveKey(masterKey, salt, []byte(crypto.HKDFInfoPrefix+uuid), crypto.StateDiskKeyLength)
|
|
|
|
}
|
|
|
|
}
|