2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/debugd/bootstrapper"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/debugd/cdbg/config"
|
|
|
|
"github.com/edgelesssys/constellation/debugd/cdbg/state"
|
|
|
|
"github.com/edgelesssys/constellation/debugd/debugd"
|
|
|
|
depl "github.com/edgelesssys/constellation/debugd/debugd/deploy"
|
|
|
|
pb "github.com/edgelesssys/constellation/debugd/service"
|
2022-05-13 10:06:57 -04:00
|
|
|
configc "github.com/edgelesssys/constellation/internal/config"
|
2022-04-06 04:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-03-22 11:03:15 -04:00
|
|
|
statec "github.com/edgelesssys/constellation/internal/state"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
|
|
|
var deployCmd = &cobra.Command{
|
|
|
|
Use: "deploy",
|
2022-06-29 09:26:29 -04:00
|
|
|
Short: "Deploys a self-compiled bootstrapper binary and SSH keys on the current constellation",
|
|
|
|
Long: `Deploys a self-compiled bootstrapper binary and SSH keys on the current constellation.
|
2022-05-13 05:56:43 -04:00
|
|
|
Uses config provided by --config and reads constellation config from its default location.
|
2022-03-22 11:03:15 -04:00
|
|
|
If required, you can override the IP addresses that are used for a deployment by specifying "--ips" and a list of IP addresses.
|
2022-06-29 09:26:29 -04:00
|
|
|
Specifying --bootstrapper will upload the bootstrapper from the specified path.`,
|
2022-03-22 11:03:15 -04:00
|
|
|
RunE: runDeploy,
|
2022-06-29 09:26:29 -04:00
|
|
|
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",
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func runDeploy(cmd *cobra.Command, args []string) error {
|
2022-05-16 11:47:25 -04:00
|
|
|
debugConfigName, err := cmd.Flags().GetString("cdbg-config")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-05-16 11:47:25 -04:00
|
|
|
debugConfig, err := config.FromFile(fileHandler, debugConfigName)
|
2022-05-13 10:06:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-16 11:47:25 -04:00
|
|
|
constellationConfig, err := configc.FromFile(fileHandler, configName)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
return deploy(cmd, fileHandler, constellationConfig, debugConfig, bootstrapper.NewFileStreamer(afero.NewOsFs()))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 10:06:57 -04:00
|
|
|
func deploy(cmd *cobra.Command, fileHandler file.Handler, constellationConfig *configc.Config, debugConfig *config.CDBGConfig, reader fileToStreamReader) error {
|
2022-06-29 09:26:29 -04:00
|
|
|
overrideBootstrapperPath, err := cmd.Flags().GetString("bootstrapper")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-29 09:26:29 -04:00
|
|
|
if len(overrideBootstrapperPath) > 0 {
|
|
|
|
debugConfig.ConstellationDebugConfig.BootstrapperPath = overrideBootstrapperPath
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
overrideIPs, err := cmd.Flags().GetStringSlice("ips")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var ips []string
|
|
|
|
if len(overrideIPs) > 0 {
|
|
|
|
ips = overrideIPs
|
|
|
|
} else {
|
|
|
|
var stat statec.ConstellationState
|
2022-04-06 04:36:58 -04:00
|
|
|
err := fileHandler.ReadJSON(constants.StateFilename, &stat)
|
2022-03-22 11:03:15 -04:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
log.Println("Unable to load statefile. Maybe you forgot to run \"constellation create ...\" first?")
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("loading statefile: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
} else if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("loading statefile: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-13 10:06:57 -04:00
|
|
|
ips, err = getIPsFromConfig(stat, *constellationConfig)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
input := deployOnEndpointInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
debugdEndpoint: net.JoinHostPort(ip, debugd.DebugdPort),
|
|
|
|
bootstrapperPath: debugConfig.ConstellationDebugConfig.BootstrapperPath,
|
|
|
|
reader: reader,
|
|
|
|
authorizedKeys: debugConfig.ConstellationDebugConfig.AuthorizedKeys,
|
|
|
|
systemdUnits: debugConfig.ConstellationDebugConfig.SystemdUnits,
|
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
|
|
|
|
reader fileToStreamReader
|
|
|
|
authorizedKeys []configc.UserKey
|
|
|
|
systemdUnits []depl.SystemdUnit
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
// deployOnEndpoint deploys SSH public keys, systemd units and a locally 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)
|
|
|
|
dialCTX, cancel := context.WithTimeout(ctx, debugd.GRPCTimeout)
|
|
|
|
defer cancel()
|
|
|
|
conn, err := grpc.DialContext(dialCTX, in.debugdEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("connecting to other instance via gRPC: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
client := pb.NewDebugdClient(conn)
|
|
|
|
|
|
|
|
log.Println("Uploading authorized keys")
|
|
|
|
pbKeys := []*pb.AuthorizedKey{}
|
|
|
|
for _, key := range in.authorizedKeys {
|
|
|
|
pbKeys = append(pbKeys, &pb.AuthorizedKey{
|
|
|
|
Username: key.Username,
|
2022-05-16 11:32:00 -04:00
|
|
|
KeyValue: key.PublicKey,
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
authorizedKeysResponse, err := client.UploadAuthorizedKeys(ctx, &pb.UploadAuthorizedKeysRequest{Keys: pbKeys}, grpc.WaitForReady(true))
|
|
|
|
if err != nil || authorizedKeysResponse.Status != pb.UploadAuthorizedKeysStatus_UPLOAD_AUTHORIZED_KEYS_SUCCESS {
|
2022-06-29 09:26:29 -04:00
|
|
|
return fmt.Errorf("uploading bootstrapper to instance %v failed: %v / %w", in.debugdEndpoint, authorizedKeysResponse, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.systemdUnits) > 0 {
|
|
|
|
log.Println("Uploading systemd unit files")
|
|
|
|
|
|
|
|
pbUnits := []*pb.ServiceUnit{}
|
|
|
|
for _, unit := range in.systemdUnits {
|
|
|
|
pbUnits = append(pbUnits, &pb.ServiceUnit{
|
|
|
|
Name: unit.Name,
|
|
|
|
Contents: unit.Contents,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
uploadSystemdServiceUnitsResponse, err := client.UploadSystemServiceUnits(ctx, &pb.UploadSystemdServiceUnitsRequest{Units: pbUnits})
|
|
|
|
if err != nil || uploadSystemdServiceUnitsResponse.Status != pb.UploadSystemdServiceUnitsStatus_UPLOAD_SYSTEMD_SERVICE_UNITS_SUCCESS {
|
|
|
|
return fmt.Errorf("uploading systemd service unit to instance %v failed: %v / %w", in.debugdEndpoint, uploadSystemdServiceUnitsResponse, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
stream, err := client.UploadBootstrapper(ctx)
|
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-06-29 09:26:29 -04:00
|
|
|
log.Println("Uploaded bootstrapper")
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-13 10:06:57 -04:00
|
|
|
func getIPsFromConfig(stat statec.ConstellationState, config configc.Config) ([]string, error) {
|
2022-06-29 09:26:29 -04:00
|
|
|
controlPlanes, workers, err := state.GetScalingGroupsFromConfig(stat, &config)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
var ips []string
|
|
|
|
// only deploy to non empty public IPs
|
2022-06-29 09:26:29 -04:00
|
|
|
for _, ip := range append(controlPlanes.PublicIPs(), workers.PublicIPs()...) {
|
2022-05-24 04:04:42 -04:00
|
|
|
if ip != "" {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 04:59:59 -04:00
|
|
|
// add bootstrapper IP if it is not already in the list
|
|
|
|
var foundBootstrapperIP bool
|
|
|
|
for _, ip := range ips {
|
|
|
|
if ip == stat.BootstrapperHost {
|
|
|
|
foundBootstrapperIP = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundBootstrapperIP && stat.BootstrapperHost != "" {
|
|
|
|
ips = append(ips, stat.BootstrapperHost)
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("no public IPs found in statefile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(deployCmd)
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
deployCmd.Flags().StringSlice("ips", nil, "override the ips that the bootstrapper will be uploaded to (defaults to ips from constellation config)")
|
|
|
|
deployCmd.Flags().String("bootstrapper", "", "override the path to the bootstrapper binary uploaded to instances (defaults to path set in config)")
|
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
|
|
|
}
|