2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-07 06:56:25 -05:00
|
|
|
"errors"
|
2022-03-22 11:03:15 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-08-07 05:40:48 -04:00
|
|
|
"path/filepath"
|
2022-08-01 10:51:34 -04:00
|
|
|
"strconv"
|
2022-11-29 09:58:01 -05:00
|
|
|
"strings"
|
2023-06-12 07:45:34 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/debugd/internal/debugd"
|
2022-11-29 09:58:01 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/debugd/internal/debugd/logcollector"
|
2023-01-20 04:11:41 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/debugd/internal/filetransfer"
|
|
|
|
"github.com/edgelesssys/constellation/v2/debugd/internal/filetransfer/streamer"
|
2022-09-21 07:47:57 -04:00
|
|
|
pb "github.com/edgelesssys/constellation/v2/debugd/service"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-06-12 07:45:34 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/grpclog"
|
2023-01-20 04:11:41 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
2023-06-12 07:45:34 -04:00
|
|
|
const deployEndpointTimeout = 20 * time.Minute
|
|
|
|
|
2022-08-24 07:43:23 -04:00
|
|
|
func newDeployCmd() *cobra.Command {
|
|
|
|
deployCmd := &cobra.Command{
|
|
|
|
Use: "deploy",
|
2022-11-11 02:44:36 -05:00
|
|
|
Short: "Deploys a self-compiled bootstrapper binary on the current constellation",
|
|
|
|
Long: `Deploys a self-compiled bootstrapper binary on the current constellation.
|
2022-08-24 07:43:23 -04:00
|
|
|
Uses config provided by --config and reads constellation config from its default location.
|
|
|
|
If required, you can override the IP addresses that are used for a deployment by specifying "--ips" and a list of IP addresses.
|
|
|
|
Specifying --bootstrapper will upload the bootstrapper from the specified path.`,
|
|
|
|
RunE: runDeploy,
|
2023-08-07 05:40:48 -04:00
|
|
|
Example: "cdbg deploy\ncdbg deploy -C /path/to/workspace --bindir $(pwd)\ncdbg deploy -C /path/to/workspace --bootstrapper /path/to/bootstrapper --ips 192.0.2.1,192.0.2.2,192.0.2.3",
|
2022-08-24 07:43:23 -04:00
|
|
|
}
|
|
|
|
deployCmd.Flags().StringSlice("ips", nil, "override the ips that the bootstrapper will be uploaded to (defaults to ips from constellation config)")
|
2023-08-07 05:40:48 -04:00
|
|
|
deployCmd.Flags().String("bindir", "", "override the base path that binaries are read from")
|
|
|
|
deployCmd.Flags().String("bootstrapper", "bootstrapper", "override the path to the bootstrapper binary uploaded to instances")
|
|
|
|
deployCmd.Flags().String("upgrade-agent", "upgrade-agent", "override the path to the upgrade-agent binary uploaded to instances")
|
2022-11-16 10:43:54 -05:00
|
|
|
deployCmd.Flags().StringToString("info", nil, "additional info to be passed to the debugd, in the form --info key1=value1,key2=value2")
|
2023-01-20 04:11:41 -05:00
|
|
|
deployCmd.Flags().Int("verbosity", 0, logger.CmdLineVerbosityDescription)
|
2022-08-24 07:43:23 -04:00
|
|
|
return deployCmd
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func runDeploy(cmd *cobra.Command, _ []string) error {
|
2023-01-20 04:11:41 -05:00
|
|
|
verbosity, err := cmd.Flags().GetInt("verbosity")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log := logger.New(logger.PlainLog, logger.VerbosityFromInt(verbosity))
|
2023-01-31 05:45:31 -05:00
|
|
|
force, err := cmd.Flags().GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting force flag: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
fs := afero.NewOsFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
streamer := streamer.New(fs)
|
|
|
|
transfer := filetransfer.New(log, streamer, filetransfer.ShowProgress)
|
2023-08-07 05:40:48 -04:00
|
|
|
constellationConfig, err := config.New(fileHandler, constants.ConfigFilename, attestationconfigapi.NewFetcher(), force)
|
2023-02-07 06:56:25 -05:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2023-02-07 06:56:25 -05:00
|
|
|
return err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
return deploy(cmd, fileHandler, constellationConfig, transfer, log)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-06-12 07:45:34 -04:00
|
|
|
func deploy(cmd *cobra.Command, fileHandler file.Handler, constellationConfig *config.Config,
|
|
|
|
transfer fileTransferer,
|
|
|
|
log *logger.Logger,
|
|
|
|
) error {
|
2023-08-07 05:40:48 -04:00
|
|
|
binDir, err := cmd.Flags().GetString("bindir")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 06:25:27 -04:00
|
|
|
bootstrapperPath, err := cmd.Flags().GetString("bootstrapper")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
upgradeAgentPath, err := cmd.Flags().GetString("upgrade-agent")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-11-22 12:47:08 -05:00
|
|
|
if constellationConfig.IsReleaseImage() {
|
2023-01-20 04:11:41 -05:00
|
|
|
log.Infof("WARNING: Constellation image does not look like a debug image. Are you using a debug image?")
|
2022-09-07 07:07:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !constellationConfig.IsDebugCluster() {
|
2023-01-20 04:11:41 -05:00
|
|
|
log.Infof("WARNING: The Constellation config has debugCluster set to false.")
|
|
|
|
log.Infof("cdbg will likely not work unless you manually adjust the firewall / load balancing rules.")
|
|
|
|
log.Infof("If you create the cluster with a debug image, you should also set debugCluster to true.")
|
2022-07-26 10:52:14 -04:00
|
|
|
}
|
|
|
|
|
2022-08-24 07:38:50 -04:00
|
|
|
ips, err := cmd.Flags().GetStringSlice("ips")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-24 07:38:50 -04:00
|
|
|
if len(ips) == 0 {
|
|
|
|
var idFile clusterIDsFile
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.ReadJSON(constants.ClusterIDsFilename, &idFile); err != nil {
|
2022-08-24 07:38:50 -04:00
|
|
|
return fmt.Errorf("reading cluster IDs file: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-08-24 07:38:50 -04:00
|
|
|
ips = []string{idFile.IP}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 09:58:01 -05:00
|
|
|
info, err := cmd.Flags().GetStringToString("info")
|
2022-11-16 10:43:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-29 09:58:01 -05:00
|
|
|
if err := checkInfoMap(info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
files := []filetransfer.FileStat{
|
|
|
|
{
|
2023-08-07 05:40:48 -04:00
|
|
|
SourcePath: prependBinDir(binDir, bootstrapperPath),
|
2023-01-20 04:11:41 -05:00
|
|
|
TargetPath: debugd.BootstrapperDeployFilename,
|
|
|
|
Mode: debugd.BinaryAccessMode,
|
|
|
|
OverrideServiceUnit: "constellation-bootstrapper",
|
|
|
|
},
|
|
|
|
{
|
2023-08-07 05:40:48 -04:00
|
|
|
SourcePath: prependBinDir(binDir, upgradeAgentPath),
|
2023-01-20 04:11:41 -05:00
|
|
|
TargetPath: debugd.UpgradeAgentDeployFilename,
|
|
|
|
Mode: debugd.BinaryAccessMode,
|
|
|
|
OverrideServiceUnit: "constellation-upgrade-agent",
|
|
|
|
},
|
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
for _, ip := range ips {
|
2022-03-22 11:03:15 -04:00
|
|
|
input := deployOnEndpointInput{
|
2023-01-20 04:11:41 -05:00
|
|
|
debugdEndpoint: ip,
|
|
|
|
infos: info,
|
|
|
|
files: files,
|
|
|
|
transfer: transfer,
|
|
|
|
log: log,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if err := deployOnEndpoint(cmd.Context(), input); err != nil {
|
2023-06-12 07:45:34 -04:00
|
|
|
return fmt.Errorf("deploying endpoint on %q: %w", ip, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 05:40:48 -04:00
|
|
|
func prependBinDir(bindDir, binary string) string {
|
|
|
|
if len(bindDir) == 0 || filepath.IsAbs(binary) {
|
|
|
|
return binary
|
|
|
|
}
|
|
|
|
return filepath.Join(bindDir, binary)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type deployOnEndpointInput struct {
|
2023-01-20 04:11:41 -05:00
|
|
|
debugdEndpoint string
|
|
|
|
files []filetransfer.FileStat
|
|
|
|
infos map[string]string
|
|
|
|
transfer fileTransferer
|
|
|
|
log *logger.Logger
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-11 02:44:36 -05:00
|
|
|
// deployOnEndpoint deploys a custom built bootstrapper binary to a debugd endpoint.
|
2022-03-22 11:03:15 -04:00
|
|
|
func deployOnEndpoint(ctx context.Context, in deployOnEndpointInput) error {
|
2023-06-12 07:45:34 -04:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, deployEndpointTimeout)
|
|
|
|
defer cancel()
|
2023-01-20 04:11:41 -05:00
|
|
|
in.log.Infof("Deploying on %v", in.debugdEndpoint)
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2023-06-12 07:45:34 -04:00
|
|
|
client, closeAndWaitFn, err := newDebugdClient(ctx, in.debugdEndpoint, in.log)
|
2022-11-16 10:43:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating debugd client: %w", err)
|
|
|
|
}
|
2023-06-12 07:45:34 -04:00
|
|
|
|
|
|
|
defer closeAndWaitFn()
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
if err := setInfo(ctx, in.log, client, in.infos); err != nil {
|
2022-11-16 10:43:54 -05:00
|
|
|
return fmt.Errorf("sending info: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
if err := uploadFiles(ctx, client, in); err != nil {
|
2022-11-16 10:43:54 -05:00
|
|
|
return fmt.Errorf("uploading bootstrapper: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 07:45:34 -04:00
|
|
|
type closeAndWait func()
|
|
|
|
|
|
|
|
// newDebugdClient creates a new gRPC client for the debugd service and logs the connection state changes.
|
|
|
|
func newDebugdClient(ctx context.Context, ip string, log *logger.Logger) (pb.DebugdClient, closeAndWait, error) {
|
2022-11-16 10:43:54 -05:00
|
|
|
conn, err := grpc.DialContext(
|
|
|
|
ctx,
|
|
|
|
net.JoinHostPort(ip, strconv.Itoa(constants.DebugdPort)),
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2023-06-12 07:45:34 -04:00
|
|
|
log.GetClientUnaryInterceptor(),
|
|
|
|
log.GetClientStreamInterceptor(),
|
2022-11-16 10:43:54 -05:00
|
|
|
)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-11-16 10:43:54 -05:00
|
|
|
return nil, nil, fmt.Errorf("connecting to other instance via gRPC: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-06-12 07:45:34 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
grpclog.LogStateChangesUntilReady(ctx, conn, log, &wg, func() {})
|
|
|
|
closeAndWait := func() {
|
|
|
|
conn.Close()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
return pb.NewDebugdClient(conn), closeAndWait, nil
|
2022-11-16 10:43:54 -05:00
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
func setInfo(ctx context.Context, log *logger.Logger, client pb.DebugdClient, infos map[string]string) error {
|
|
|
|
log.Infof("Setting info with length %d", len(infos))
|
2022-11-16 10:43:54 -05:00
|
|
|
|
|
|
|
var infosPb []*pb.Info
|
|
|
|
for key, value := range infos {
|
|
|
|
infosPb = append(infosPb, &pb.Info{Key: key, Value: value})
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &pb.SetInfoRequest{Info: infosPb}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
status, err := client.SetInfo(ctx, req, grpc.WaitForReady(true))
|
|
|
|
if err != nil {
|
2022-11-16 10:43:54 -05:00
|
|
|
return fmt.Errorf("setting info: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
switch status.Status {
|
|
|
|
case pb.SetInfoStatus_SET_INFO_SUCCESS:
|
|
|
|
log.Infof("Info set")
|
|
|
|
case pb.SetInfoStatus_SET_INFO_ALREADY_SET:
|
|
|
|
log.Infof("Info already set")
|
|
|
|
default:
|
|
|
|
log.Warnf("Unknown status %v", status.Status)
|
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
func uploadFiles(ctx context.Context, client pb.DebugdClient, in deployOnEndpointInput) error {
|
|
|
|
in.log.Infof("Uploading files")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
stream, err := client.UploadFiles(ctx, grpc.WaitForReady(true))
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-06-29 09:26:29 -04:00
|
|
|
return fmt.Errorf("starting bootstrapper upload to instance %v: %w", in.debugdEndpoint, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
|
|
|
|
in.transfer.SetFiles(in.files)
|
|
|
|
if err := in.transfer.SendFiles(stream); err != nil {
|
|
|
|
return fmt.Errorf("sending files to %v: %w", in.debugdEndpoint, err)
|
|
|
|
}
|
2022-04-20 10:11:48 -04:00
|
|
|
|
|
|
|
uploadResponse, closeErr := stream.CloseAndRecv()
|
|
|
|
if closeErr != nil {
|
2023-01-20 04:11:41 -05:00
|
|
|
return fmt.Errorf("closing upload stream after uploading files to %v: %w", in.debugdEndpoint, closeErr)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
switch uploadResponse.Status {
|
|
|
|
case pb.UploadFilesStatus_UPLOAD_FILES_SUCCESS:
|
|
|
|
in.log.Infof("Upload successful")
|
|
|
|
case pb.UploadFilesStatus_UPLOAD_FILES_ALREADY_FINISHED:
|
|
|
|
in.log.Infof("Files already uploaded")
|
|
|
|
case pb.UploadFilesStatus_UPLOAD_FILES_UPLOAD_FAILED:
|
|
|
|
return fmt.Errorf("uploading files to %v failed: %v", in.debugdEndpoint, uploadResponse)
|
|
|
|
case pb.UploadFilesStatus_UPLOAD_FILES_ALREADY_STARTED:
|
|
|
|
return fmt.Errorf("upload already started on %v", in.debugdEndpoint)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown upload status %v", uploadResponse.Status)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 09:58:01 -05:00
|
|
|
func checkInfoMap(info map[string]string) error {
|
|
|
|
logPrefix, logFields := logcollector.InfoFields()
|
|
|
|
for k := range info {
|
|
|
|
if !strings.HasPrefix(k, logPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
subkey := strings.TrimPrefix(k, logPrefix)
|
|
|
|
|
|
|
|
if _, ok := logFields[subkey]; !ok {
|
|
|
|
return fmt.Errorf("invalid subkey %q for info key %q", subkey, fmt.Sprintf("%s.%s", logPrefix, k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
type fileTransferer interface {
|
|
|
|
SendFiles(stream filetransfer.SendFilesStream) error
|
|
|
|
SetFiles(files []filetransfer.FileStat)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-08-24 07:38:50 -04:00
|
|
|
|
|
|
|
type clusterIDsFile struct {
|
|
|
|
ClusterID string
|
|
|
|
OwnerID string
|
|
|
|
IP string
|
|
|
|
}
|