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"
|
2023-02-07 06:56:25 -05: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"
|
2023-03-21 07:46:49 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-08-08 09:42:06 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cmd/pathprefix"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/recoverproto"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2023-05-03 05:11:53 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-09-21 07:47:57 -04:00
|
|
|
"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"
|
2023-03-02 09:08:31 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
|
2022-09-21 07:47:57 -04:00
|
|
|
"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",
|
2023-01-17 08:01:56 -05:00
|
|
|
Long: "Recover a Constellation cluster by sending a recovery key to an instance in the boot stage.\n\n" +
|
|
|
|
"This is only required if instances restart without other instances available for bootstrapping.",
|
2022-05-02 07:21:07 -04:00
|
|
|
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-05-02 07:21:07 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
type recoverCmd struct {
|
2023-06-01 07:55:46 -04:00
|
|
|
log debugLog
|
2023-06-07 10:16:32 -04:00
|
|
|
configFetcher attestationconfigapi.Fetcher
|
2023-08-08 09:42:06 -04:00
|
|
|
pf pathprefix.PathPrefixer
|
2023-01-04 04:46:29 -05:00
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
func runRecover(cmd *cobra.Command, _ []string) error {
|
2023-01-04 04:46:29 -05:00
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
defer log.Sync()
|
2022-05-02 07:21:07 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2023-05-03 05:11:53 -04:00
|
|
|
newDialer := func(validator atls.Validator) *dialer.Dialer {
|
|
|
|
return dialer.New(nil, validator, &net.Dialer{})
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2023-06-07 10:16:32 -04:00
|
|
|
r := &recoverCmd{log: log, configFetcher: attestationconfigapi.NewFetcher()}
|
2023-01-04 04:46:29 -05:00
|
|
|
return r.recover(cmd, fileHandler, 5*time.Second, &recoverDoer{log: r.log}, newDialer)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
func (r *recoverCmd) recover(
|
2022-09-26 03:57:40 -04:00
|
|
|
cmd *cobra.Command, fileHandler file.Handler, interval time.Duration,
|
2023-05-03 05:11:53 -04:00
|
|
|
doer recoverDoerInterface, newDialer func(validator atls.Validator) *dialer.Dialer,
|
2022-09-26 03:57:40 -04:00
|
|
|
) error {
|
2023-01-04 04:46:29 -05:00
|
|
|
flags, err := r.parseRecoverFlags(cmd, fileHandler)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Using flags: %+v", flags)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
var masterSecret uri.MasterSecret
|
2023-08-16 09:38:40 -04:00
|
|
|
r.log.Debugf("Loading master secret file from %s", r.pf.PrefixPrintablePath(constants.MasterSecretFilename))
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.ReadJSON(constants.MasterSecretFilename, &masterSecret); err != nil {
|
2022-07-29 03:52:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-16 09:38:40 -04:00
|
|
|
r.log.Debugf("Loading configuration file from %q", r.pf.PrefixPrintablePath(constants.ConfigFilename))
|
2023-08-04 07:53:51 -04:00
|
|
|
conf, err := config.New(fileHandler, constants.ConfigFilename, r.configFetcher, flags.force)
|
2023-02-07 06:56:25 -05:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
2022-05-23 09:01:39 -04:00
|
|
|
if err != nil {
|
2023-02-07 06:56:25 -05:00
|
|
|
return err
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
2023-02-07 06:56:25 -05:00
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
provider := conf.GetProvider()
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Got provider %s", provider.String())
|
2022-09-26 03:57:40 -04:00
|
|
|
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
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
conf.UpdateMAAURL(flags.maaURL)
|
|
|
|
r.log.Debugf("Creating aTLS Validator for %s", conf.GetAttestationConfig().GetVariant())
|
|
|
|
validator, err := cloudcmd.NewValidator(cmd, conf.GetAttestationConfig(), r.log)
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
2023-03-22 11:53:01 -04:00
|
|
|
return fmt.Errorf("creating new validator: %w", err)
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Created a new validator")
|
2022-09-26 03:57:40 -04:00
|
|
|
doer.setDialer(newDialer(validator), flags.endpoint)
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Set dialer for endpoint %s", flags.endpoint)
|
2023-03-02 09:08:31 -05:00
|
|
|
doer.setURIs(masterSecret.EncodeToURI(), uri.NoStoreURI)
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Set secrets")
|
|
|
|
if err := r.recoverCall(cmd.Context(), cmd.OutOrStdout(), interval, doer); err != nil {
|
2022-09-26 03:57:40 -04:00
|
|
|
if grpcRetry.ServiceIsUnavailable(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("recovering cluster: %w", err)
|
|
|
|
}
|
2022-09-14 07:25:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
func (r *recoverCmd) recoverCall(ctx context.Context, out io.Writer, interval time.Duration, doer recoverDoerInterface) error {
|
2022-09-26 03:57:40 -04:00
|
|
|
var err error
|
|
|
|
ctr := 0
|
|
|
|
for {
|
|
|
|
once := sync.Once{}
|
|
|
|
retryOnceOnFailure := func(err error) bool {
|
2023-01-18 07:10:24 -05:00
|
|
|
var retry bool
|
2022-09-26 03:57:40 -04:00
|
|
|
// retry transient GCP LB errors
|
|
|
|
if grpcRetry.LoadbalancerIsNotReady(err) {
|
2023-01-18 07:10:24 -05:00
|
|
|
retry = true
|
|
|
|
} else {
|
|
|
|
// 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)
|
|
|
|
})
|
2022-09-26 03:57:40 -04:00
|
|
|
}
|
|
|
|
|
2023-01-18 07:10:24 -05:00
|
|
|
r.log.Debugf("Encountered error (retriable: %t): %s", retry, err)
|
2022-09-26 03:57:40 -04:00
|
|
|
return retry
|
|
|
|
}
|
|
|
|
|
|
|
|
retrier := retry.NewIntervalRetrier(doer, interval, retryOnceOnFailure)
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Created new interval retrier")
|
2022-09-26 03:57:40 -04:00
|
|
|
err = retrier.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Fprintln(out, "Pushed recovery key.")
|
|
|
|
ctr++
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Retry counter is %d", ctr)
|
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)
|
2023-01-16 05:19:03 -05:00
|
|
|
setURIs(kmsURI, storageURI string)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type recoverDoer struct {
|
2023-01-16 05:19:03 -05:00
|
|
|
dialer grpcDialer
|
|
|
|
endpoint string
|
|
|
|
kmsURI string // encodes masterSecret
|
|
|
|
storageURI string
|
|
|
|
log debugLog
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
d.log.Debugf("Dialed recovery server")
|
2022-09-14 07:25:42 -04:00
|
|
|
defer conn.Close()
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
protoClient := recoverproto.NewAPIClient(conn)
|
2023-01-04 04:46:29 -05:00
|
|
|
d.log.Debugf("Created protoClient")
|
2022-09-14 07:25:42 -04:00
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
req := &recoverproto.RecoverMessage{
|
|
|
|
KmsUri: d.kmsURI,
|
|
|
|
StorageUri: d.storageURI,
|
2022-05-02 07:21:07 -04:00
|
|
|
}
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
_, err = protoClient.Recover(ctx, req)
|
2022-09-14 07:25:42 -04:00
|
|
|
if err != nil {
|
2023-01-16 05:19:03 -05:00
|
|
|
return fmt.Errorf("calling recover: %w", err)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
d.log.Debugf("Received confirmation")
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
func (d *recoverDoer) setURIs(kmsURI, storageURI string) {
|
|
|
|
d.kmsURI = kmsURI
|
|
|
|
d.storageURI = storageURI
|
2022-09-26 03:57:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type recoverFlags struct {
|
2023-08-08 09:42:06 -04:00
|
|
|
endpoint string
|
|
|
|
maaURL string
|
|
|
|
force bool
|
2022-09-26 03:57:40 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
func (r *recoverCmd) parseRecoverFlags(cmd *cobra.Command, fileHandler file.Handler) (recoverFlags, error) {
|
2023-08-08 09:42:06 -04:00
|
|
|
workDir, err := cmd.Flags().GetString("workspace")
|
2023-08-04 07:53:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, fmt.Errorf("parsing config path argument: %w", err)
|
|
|
|
}
|
2023-08-08 09:42:06 -04:00
|
|
|
r.log.Debugf("Workspace set to %q", workDir)
|
|
|
|
r.pf = pathprefix.New(workDir)
|
2023-08-04 07:53:51 -04:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
var idFile clusterid.File
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.ReadJSON(constants.ClusterIDsFilename, &idFile); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
|
2023-03-21 07:46:49 -04:00
|
|
|
return recoverFlags{}, err
|
|
|
|
}
|
|
|
|
|
2022-05-06 07:56:02 -04:00
|
|
|
endpoint, err := cmd.Flags().GetString("endpoint")
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Endpoint flag is %s", 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 == "" {
|
2023-03-21 07:46:49 -04:00
|
|
|
endpoint = idFile.IP
|
2022-09-26 03:57:40 -04:00
|
|
|
}
|
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
|
|
|
}
|
2023-01-04 04:46:29 -05:00
|
|
|
r.log.Debugf("Endpoint value after parsing is %s", endpoint)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2023-01-31 05:45:31 -05:00
|
|
|
force, err := cmd.Flags().GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return recoverFlags{}, fmt.Errorf("parsing force argument: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:21:07 -04:00
|
|
|
return recoverFlags{
|
2023-08-08 09:42:06 -04:00
|
|
|
endpoint: endpoint,
|
|
|
|
maaURL: idFile.AttestationURL,
|
|
|
|
force: force,
|
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) {
|
2023-01-16 05:19:03 -05:00
|
|
|
return crypto.DeriveKey(masterKey, salt, []byte(crypto.DEKPrefix+uuid), crypto.StateDiskKeyLength)
|
2022-09-14 07:25:42 -04:00
|
|
|
}
|
|
|
|
}
|