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"
|
|
|
|
"fmt"
|
2022-11-16 10:43:54 -05:00
|
|
|
"io"
|
2022-03-22 11:03:15 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2022-08-01 10:51:34 -04:00
|
|
|
"strconv"
|
2022-11-29 09:58:01 -05:00
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/debugd/internal/bootstrapper"
|
|
|
|
"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"
|
2022-09-21 07:47:57 -04:00
|
|
|
pb "github.com/edgelesssys/constellation/v2/debugd/service"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
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"
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
Example: "cdbg deploy\ncdbg deploy --config /path/to/config\ncdbg deploy --bootstrapper /path/to/bootstrapper --ips 192.0.2.1,192.0.2.2,192.0.2.3 --config /path/to/config",
|
|
|
|
}
|
|
|
|
deployCmd.Flags().StringSlice("ips", nil, "override the ips that the bootstrapper will be uploaded to (defaults to ips from constellation config)")
|
2022-08-31 06:25:27 -04:00
|
|
|
deployCmd.Flags().String("bootstrapper", "./bootstrapper", "override the path to the bootstrapper 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")
|
2022-08-24 07:43:23 -04:00
|
|
|
return deployCmd
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func runDeploy(cmd *cobra.Command, args []string) error {
|
2022-05-13 05:56:43 -04:00
|
|
|
configName, err := cmd.Flags().GetString("config")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("parsing config path argument: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-08-31 06:25:27 -04:00
|
|
|
constellationConfig, err := config.FromFile(fileHandler, configName)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:25:27 -04:00
|
|
|
return deploy(cmd, fileHandler, constellationConfig, bootstrapper.NewFileStreamer(afero.NewOsFs()))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:25:27 -04:00
|
|
|
func deploy(cmd *cobra.Command, fileHandler file.Handler, constellationConfig *config.Config, reader fileToStreamReader) error {
|
|
|
|
bootstrapperPath, err := cmd.Flags().GetString("bootstrapper")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-22 12:47:08 -05:00
|
|
|
if constellationConfig.IsReleaseImage() {
|
2022-09-07 07:07:32 -04:00
|
|
|
log.Println("WARNING: Constellation image does not look like a debug image. Are you using a debug image?")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !constellationConfig.IsDebugCluster() {
|
|
|
|
log.Println("WARNING: The Constellation config has debugCluster set to false.")
|
|
|
|
log.Println("cdbg will likely not work unless you manually adjust the firewall / load balancing rules.")
|
|
|
|
log.Println("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
|
|
|
|
if err := fileHandler.ReadJSON(constants.ClusterIDsFileName, &idFile); err != nil {
|
|
|
|
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
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
for _, ip := range ips {
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
input := deployOnEndpointInput{
|
2022-11-16 10:43:54 -05:00
|
|
|
debugdEndpoint: ip,
|
2022-11-29 09:58:01 -05:00
|
|
|
infos: info,
|
2022-08-31 06:25:27 -04:00
|
|
|
bootstrapperPath: bootstrapperPath,
|
2022-06-29 09:26:29 -04:00
|
|
|
reader: reader,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if err := deployOnEndpoint(cmd.Context(), input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type deployOnEndpointInput struct {
|
2022-06-29 09:26:29 -04:00
|
|
|
debugdEndpoint string
|
|
|
|
bootstrapperPath string
|
2022-11-16 10:43:54 -05:00
|
|
|
infos map[string]string
|
2022-06-29 09:26:29 -04:00
|
|
|
reader fileToStreamReader
|
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 {
|
|
|
|
log.Printf("Deploying on %v\n", in.debugdEndpoint)
|
2022-11-16 10:43:54 -05:00
|
|
|
|
|
|
|
client, closer, err := newDebugdClient(ctx, in.debugdEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating debugd client: %w", err)
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
if err := setInfo(ctx, client, in.infos); err != nil {
|
|
|
|
return fmt.Errorf("sending info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uploadBootstrapper(ctx, client, in); err != nil {
|
|
|
|
return fmt.Errorf("uploading bootstrapper: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDebugdClient(ctx context.Context, ip string) (pb.DebugdClient, io.Closer, error) {
|
|
|
|
conn, err := grpc.DialContext(
|
|
|
|
ctx,
|
|
|
|
net.JoinHostPort(ip, strconv.Itoa(constants.DebugdPort)),
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
)
|
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
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
|
|
|
|
return pb.NewDebugdClient(conn), conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setInfo(ctx context.Context, client pb.DebugdClient, infos map[string]string) error {
|
|
|
|
log.Printf("Setting info with length %d", len(infos))
|
|
|
|
|
|
|
|
var infosPb []*pb.Info
|
|
|
|
for key, value := range infos {
|
|
|
|
infosPb = append(infosPb, &pb.Info{Key: key, Value: value})
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &pb.SetInfoRequest{Info: infosPb}
|
|
|
|
|
|
|
|
if _, err := client.SetInfo(ctx, req, grpc.WaitForReady(true)); err != nil {
|
|
|
|
return fmt.Errorf("setting info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Info set")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadBootstrapper(ctx context.Context, client pb.DebugdClient, in deployOnEndpointInput) error {
|
|
|
|
log.Println("Uploading bootstrapper")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-10-20 03:56:45 -04:00
|
|
|
stream, err := client.UploadBootstrapper(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
|
|
|
}
|
2022-06-29 09:26:29 -04:00
|
|
|
streamErr := in.reader.ReadStream(in.bootstrapperPath, stream, debugd.Chunksize, true)
|
2022-04-20 10:11:48 -04:00
|
|
|
|
|
|
|
uploadResponse, closeErr := stream.CloseAndRecv()
|
|
|
|
if closeErr != nil {
|
2022-06-29 09:26:29 -04:00
|
|
|
return fmt.Errorf("closing upload stream after uploading bootstrapper to %v: %w", in.debugdEndpoint, closeErr)
|
2022-04-20 10:11:48 -04:00
|
|
|
}
|
2022-06-29 09:26:29 -04:00
|
|
|
if uploadResponse.Status == pb.UploadBootstrapperStatus_UPLOAD_BOOTSTRAPPER_FILE_EXISTS {
|
|
|
|
log.Println("Bootstrapper was already uploaded")
|
2022-04-20 10:11:48 -04:00
|
|
|
return nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-06-29 09:26:29 -04:00
|
|
|
if uploadResponse.Status != pb.UploadBootstrapperStatus_UPLOAD_BOOTSTRAPPER_SUCCESS || streamErr != nil {
|
|
|
|
return fmt.Errorf("uploading bootstrapper to instance %v failed: %v / %w", in.debugdEndpoint, uploadResponse, streamErr)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-11-16 10:43:54 -05:00
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
log.Println("Uploaded bootstrapper")
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type fileToStreamReader interface {
|
2022-06-29 09:26:29 -04:00
|
|
|
ReadStream(filename string, stream bootstrapper.WriteChunkStream, chunksize uint, showProgress bool) error
|
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
|
|
|
|
}
|