constellation/cli/internal/cmd/init.go

426 lines
15 KiB
Go
Raw Normal View History

package cmd
import (
2022-06-21 15:59:12 +00:00
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"net"
"strconv"
2022-04-05 07:13:09 +00:00
"text/tabwriter"
2022-06-21 15:59:12 +00:00
"time"
"github.com/edgelesssys/constellation/bootstrapper/initproto"
2022-06-07 14:30:41 +00:00
"github.com/edgelesssys/constellation/cli/internal/azure"
"github.com/edgelesssys/constellation/cli/internal/cloudcmd"
2022-06-07 12:52:47 +00:00
"github.com/edgelesssys/constellation/cli/internal/gcp"
2022-08-12 08:20:19 +00:00
"github.com/edgelesssys/constellation/cli/internal/helm"
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
"github.com/edgelesssys/constellation/internal/config"
2022-04-06 08:36:58 +00:00
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/crypto"
"github.com/edgelesssys/constellation/internal/deploy/ssh"
"github.com/edgelesssys/constellation/internal/file"
2022-06-21 15:59:12 +00:00
"github.com/edgelesssys/constellation/internal/grpc/dialer"
grpcRetry "github.com/edgelesssys/constellation/internal/grpc/retry"
"github.com/edgelesssys/constellation/internal/license"
"github.com/edgelesssys/constellation/internal/retry"
"github.com/edgelesssys/constellation/internal/state"
2022-06-29 14:17:23 +00:00
kms "github.com/edgelesssys/constellation/kms/setup"
"github.com/spf13/afero"
"github.com/spf13/cobra"
2022-06-21 15:59:12 +00:00
"google.golang.org/grpc"
)
2022-06-08 06:14:28 +00:00
// NewInitCmd returns a new cobra.Command for the init command.
func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize the Constellation cluster",
2022-05-04 07:13:46 +00:00
Long: "Initialize the Constellation cluster. Start your confidential Kubernetes.",
ValidArgsFunction: initCompletion,
Args: cobra.ExactArgs(0),
RunE: runInitialize,
}
2022-05-09 15:02:47 +00:00
cmd.Flags().String("master-secret", "", "path to base64-encoded master secret")
cmd.Flags().Bool("autoscale", false, "enable Kubernetes cluster-autoscaler")
return cmd
}
// runInitialize runs the initialize command.
func runInitialize(cmd *cobra.Command, args []string) error {
fileHandler := file.NewHandler(afero.NewOsFs())
2022-04-13 11:01:38 +00:00
serviceAccountCreator := cloudcmd.NewServiceAccountCreator()
newDialer := func(validator *cloudcmd.Validator) *dialer.Dialer {
return dialer.New(nil, validator.V(cmd), &net.Dialer{})
}
2022-08-12 08:20:19 +00:00
helmLoader := &helm.ChartLoader{}
return initialize(cmd, newDialer, serviceAccountCreator, fileHandler, helmLoader, license.NewClient())
}
// initialize initializes a Constellation.
2022-08-12 08:20:19 +00:00
func initialize(cmd *cobra.Command, newDialer func(validator *cloudcmd.Validator) *dialer.Dialer,
serviceAccCreator serviceAccountCreator, fileHandler file.Handler, helmLoader helmLoader, licenseClient licenseClient,
) error {
2022-04-13 13:01:02 +00:00
flags, err := evalFlagArgs(cmd, fileHandler)
if err != nil {
return err
}
var stat state.ConstellationState
err = fileHandler.ReadJSON(constants.StateFilename, &stat)
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("missing Constellation state file: %w. Please do 'constellation create ...' before 'constellation init'", err)
} else if err != nil {
return fmt.Errorf("loading Constellation state file: %w", err)
}
provider := cloudprovider.FromString(stat.CloudProvider)
config, err := readConfig(cmd.OutOrStdout(), fileHandler, flags.configPath, provider)
if err != nil {
return fmt.Errorf("reading and validating config: %w", err)
}
licenseID, err := license.FromFile(fileHandler, constants.LicenseFilename)
if err != nil {
cmd.Println("Unable to find license file. Assuming community license.")
licenseID = license.CommunityLicense
}
quotaResp, err := licenseClient.CheckQuota(cmd.Context(), license.CheckQuotaRequest{
License: licenseID,
Action: license.Init,
})
if err != nil {
cmd.Println("Unable to contact license server.")
cmd.Println("Please keep your vCPU quota in mind.")
cmd.Printf("For community installation the vCPU quota is: %d.\n", license.CommunityQuota)
}
cmd.Printf("Constellation license found: %s\n", licenseID)
cmd.Printf("Please keep your vCPU quota (%d) in mind.\n", quotaResp.Quota)
var sshUsers []*ssh.UserKey
for _, user := range config.SSHUsers {
sshUsers = append(sshUsers, &ssh.UserKey{
Username: user.Username,
PublicKey: user.PublicKey,
})
}
validator, err := cloudcmd.NewValidator(provider, config)
2022-04-19 15:02:02 +00:00
if err != nil {
return err
}
cmd.Println("Creating service account ...")
serviceAccount, stat, err := serviceAccCreator.Create(cmd.Context(), stat, config)
if err != nil {
return err
}
2022-04-06 08:36:58 +00:00
if err := fileHandler.WriteJSON(constants.StateFilename, stat, file.OptOverwrite); err != nil {
return err
}
_, workers, err := getScalingGroupsFromState(stat, config)
if err != nil {
return err
}
var autoscalingNodeGroups []string
2022-04-13 13:01:02 +00:00
if flags.autoscale {
autoscalingNodeGroups = append(autoscalingNodeGroups, workers.GroupID)
}
2022-08-12 08:20:19 +00:00
cmd.Println("Loading Helm charts ...")
helmDeployments, err := helmLoader.Load(stat.CloudProvider)
if err != nil {
return fmt.Errorf("loading Helm charts: %w", err)
}
2022-06-21 15:59:12 +00:00
req := &initproto.InitRequest{
AutoscalingNodeGroups: autoscalingNodeGroups,
MasterSecret: flags.masterSecret.Key,
Salt: flags.masterSecret.Salt,
2022-06-21 15:59:12 +00:00
KmsUri: kms.ClusterKMSURI,
StorageUri: kms.NoStoreURI,
KeyEncryptionKeyId: "",
UseExistingKek: false,
CloudServiceAccountUri: serviceAccount,
KubernetesVersion: config.KubernetesVersion,
2022-06-21 15:59:12 +00:00
SshUserKeys: ssh.ToProtoSlice(sshUsers),
2022-08-12 08:20:19 +00:00
HelmDeployments: helmDeployments,
EnforcedPcrs: getEnforcedMeasurements(provider, config),
}
resp, err := initCall(cmd.Context(), newDialer(validator), stat.BootstrapperHost, req)
if err != nil {
return err
}
return writeOutput(resp, stat.BootstrapperHost, cmd.OutOrStdout(), fileHandler)
}
2022-06-21 15:59:12 +00:00
func initCall(ctx context.Context, dialer grpcDialer, ip string, req *initproto.InitRequest) (*initproto.InitResponse, error) {
doer := &initDoer{
dialer: dialer,
endpoint: net.JoinHostPort(ip, strconv.Itoa(constants.BootstrapperPort)),
2022-06-21 15:59:12 +00:00
req: req,
}
retrier := retry.NewIntervalRetrier(doer, 30*time.Second, grpcRetry.ServiceIsUnavailable)
2022-06-29 12:28:37 +00:00
if err := retrier.Do(ctx); err != nil {
2022-06-21 15:59:12 +00:00
return nil, err
}
2022-06-21 15:59:12 +00:00
return doer.resp, nil
}
2022-06-21 15:59:12 +00:00
type initDoer struct {
dialer grpcDialer
endpoint string
req *initproto.InitRequest
resp *initproto.InitResponse
}
2022-06-21 15:59:12 +00:00
func (d *initDoer) Do(ctx context.Context) error {
conn, err := d.dialer.Dial(ctx, d.endpoint)
if err != nil {
return fmt.Errorf("dialing init server: %w", err)
}
2022-07-05 12:14:11 +00:00
defer conn.Close()
2022-06-21 15:59:12 +00:00
protoClient := initproto.NewAPIClient(conn)
resp, err := protoClient.Init(ctx, d.req)
2022-03-29 09:38:14 +00:00
if err != nil {
2022-07-05 12:14:11 +00:00
return fmt.Errorf("init call: %w", err)
2022-03-29 09:38:14 +00:00
}
2022-06-21 15:59:12 +00:00
d.resp = resp
return nil
2022-03-29 09:38:14 +00:00
}
2022-07-05 12:14:11 +00:00
func writeOutput(resp *initproto.InitResponse, ip string, wr io.Writer, fileHandler file.Handler) error {
2022-04-27 12:21:36 +00:00
fmt.Fprint(wr, "Your Constellation cluster was successfully initialized.\n\n")
2022-04-05 07:13:09 +00:00
2022-07-05 12:14:11 +00:00
ownerID := base64.StdEncoding.EncodeToString(resp.OwnerId)
clusterID := base64.StdEncoding.EncodeToString(resp.ClusterId)
2022-04-05 07:13:09 +00:00
tw := tabwriter.NewWriter(wr, 0, 0, 2, ' ', 0)
// writeRow(tw, "Constellation cluster's owner identifier", ownerID)
writeRow(tw, "Constellation cluster identifier", clusterID)
2022-04-06 08:36:58 +00:00
writeRow(tw, "Kubernetes configuration", constants.AdminConfFilename)
2022-04-05 07:13:09 +00:00
tw.Flush()
fmt.Fprintln(wr)
2022-06-21 15:59:12 +00:00
if err := fileHandler.Write(constants.AdminConfFilename, resp.Kubeconfig, file.OptNone); err != nil {
return fmt.Errorf("writing kubeconfig: %w", err)
}
2022-04-05 07:13:09 +00:00
2022-07-05 12:14:11 +00:00
idFile := clusterIDsFile{
ClusterID: clusterID,
OwnerID: ownerID,
IP: ip,
2022-07-05 12:14:11 +00:00
}
2022-07-05 11:52:36 +00:00
if err := fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone); err != nil {
return fmt.Errorf("writing Constellation id file: %w", err)
}
2022-05-04 07:13:46 +00:00
fmt.Fprintln(wr, "You can now connect to your cluster by executing:")
2022-04-06 08:36:58 +00:00
fmt.Fprintf(wr, "\texport KUBECONFIG=\"$PWD/%s\"\n", constants.AdminConfFilename)
return nil
}
2022-04-05 07:13:09 +00:00
func writeRow(wr io.Writer, col1 string, col2 string) {
fmt.Fprint(wr, col1, "\t", col2, "\n")
}
func getEnforcedMeasurements(provider cloudprovider.Provider, config *config.Config) []uint32 {
switch provider {
case cloudprovider.Azure:
return config.Provider.Azure.EnforcedMeasurements
case cloudprovider.GCP:
return config.Provider.GCP.EnforcedMeasurements
case cloudprovider.QEMU:
return config.Provider.QEMU.EnforcedMeasurements
default:
return nil
}
}
// evalFlagArgs gets the flag values and does preprocessing of these values like
// reading the content from file path flags and deriving other values from flag combinations.
2022-04-13 13:01:02 +00:00
func evalFlagArgs(cmd *cobra.Command, fileHandler file.Handler) (initFlags, error) {
masterSecretPath, err := cmd.Flags().GetString("master-secret")
if err != nil {
return initFlags{}, fmt.Errorf("parsing master-secret path argument: %w", err)
}
masterSecret, err := readOrGenerateMasterSecret(cmd.OutOrStdout(), fileHandler, masterSecretPath)
if err != nil {
return initFlags{}, fmt.Errorf("parsing or generating master mastersecret from file %s: %w", masterSecretPath, err)
}
autoscale, err := cmd.Flags().GetBool("autoscale")
if err != nil {
return initFlags{}, fmt.Errorf("parsing autoscale argument: %w", err)
2022-04-13 13:01:02 +00:00
}
configPath, err := cmd.Flags().GetString("config")
2022-04-13 13:01:02 +00:00
if err != nil {
return initFlags{}, fmt.Errorf("parsing config path argument: %w", err)
}
2022-04-13 13:01:02 +00:00
return initFlags{
2022-06-21 15:59:12 +00:00
configPath: configPath,
autoscale: autoscale,
masterSecret: masterSecret,
}, nil
}
2022-04-13 13:01:02 +00:00
// initFlags are the resulting values of flag preprocessing.
type initFlags struct {
2022-06-21 15:59:12 +00:00
configPath string
masterSecret masterSecret
2022-06-21 15:59:12 +00:00
autoscale bool
}
// masterSecret holds the master key and salt for deriving keys.
type masterSecret struct {
Key []byte `json:"key"`
Salt []byte `json:"salt"`
}
// readOrGenerateMasterSecret reads a base64 encoded master secret from file or generates a new 32 byte secret.
func readOrGenerateMasterSecret(writer io.Writer, fileHandler file.Handler, filename string) (masterSecret, error) {
if filename != "" {
var secret masterSecret
if err := fileHandler.ReadJSON(filename, &secret); err != nil {
return masterSecret{}, err
}
if len(secret.Key) < crypto.MasterSecretLengthMin {
return masterSecret{}, fmt.Errorf("provided master secret is smaller than the required minimum of %d Bytes", crypto.MasterSecretLengthMin)
}
if len(secret.Salt) < crypto.RNGLengthDefault {
return masterSecret{}, fmt.Errorf("provided salt is smaller than the required minimum of %d Bytes", crypto.RNGLengthDefault)
}
return secret, nil
}
// No file given, generate a new secret, and save it to disk
key, err := crypto.GenerateRandomBytes(crypto.MasterSecretLengthDefault)
if err != nil {
return masterSecret{}, err
}
salt, err := crypto.GenerateRandomBytes(crypto.RNGLengthDefault)
if err != nil {
return masterSecret{}, err
}
secret := masterSecret{
Key: key,
Salt: salt,
}
if err := fileHandler.WriteJSON(constants.MasterSecretFilename, secret, file.OptNone); err != nil {
return masterSecret{}, err
}
fmt.Fprintf(writer, "Your Constellation master secret was successfully written to ./%s\n", constants.MasterSecretFilename)
return secret, nil
}
func getScalingGroupsFromState(stat state.ConstellationState, config *config.Config) (controlPlanes, workers cloudtypes.ScalingGroup, err error) {
switch {
2022-07-29 06:10:51 +00:00
case len(stat.GCPControlPlaneInstances) != 0:
return getGCPInstances(stat, config)
2022-07-29 06:10:51 +00:00
case len(stat.AzureControlPlaneInstances) != 0:
return getAzureInstances(stat, config)
2022-07-29 06:10:51 +00:00
case len(stat.QEMUControlPlaneInstances) != 0:
return getQEMUInstances(stat, config)
default:
2022-06-07 15:15:23 +00:00
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no instances to initialize")
}
}
func getGCPInstances(stat state.ConstellationState, config *config.Config) (controlPlanes, workers cloudtypes.ScalingGroup, err error) {
2022-07-29 06:10:51 +00:00
if len(stat.GCPControlPlaneInstances) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane workers available, can't create Constellation without any instance")
}
2022-06-07 15:15:23 +00:00
// GroupID of controlPlanes is empty, since they currently do not scale.
controlPlanes = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.GCPControlPlaneInstances,
GroupID: "",
}
2022-07-29 06:10:51 +00:00
if len(stat.GCPWorkerInstances) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker workers available, can't create Constellation with one instance")
}
// TODO: make min / max configurable and abstract autoscaling for different cloud providers
workers = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.GCPWorkerInstances,
GroupID: gcp.AutoscalingNodeGroup(stat.GCPProject, stat.GCPZone, stat.GCPWorkerInstanceGroup, config.AutoscalingNodeGroupMin, config.AutoscalingNodeGroupMax),
}
return
}
func getAzureInstances(stat state.ConstellationState, config *config.Config) (controlPlanes, workers cloudtypes.ScalingGroup, err error) {
2022-07-29 06:10:51 +00:00
if len(stat.AzureControlPlaneInstances) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane workers available, can't create Constellation cluster without any instance")
}
2022-06-07 15:15:23 +00:00
// GroupID of controlPlanes is empty, since they currently do not scale.
controlPlanes = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.AzureControlPlaneInstances,
GroupID: "",
}
2022-07-29 06:10:51 +00:00
if len(stat.AzureWorkerInstances) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker workers available, can't create Constellation cluster with one instance")
}
// TODO: make min / max configurable and abstract autoscaling for different cloud providers
workers = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.AzureWorkerInstances,
GroupID: azure.AutoscalingNodeGroup(stat.AzureWorkerScaleSet, config.AutoscalingNodeGroupMin, config.AutoscalingNodeGroupMax),
}
return
}
func getQEMUInstances(stat state.ConstellationState, _ *config.Config) (controlPlanes, workers cloudtypes.ScalingGroup, err error) {
2022-07-29 06:10:51 +00:00
controlPlanesMap := stat.QEMUControlPlaneInstances
if len(controlPlanesMap) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no controlPlanes available, can't create Constellation without any instance")
}
2022-06-07 15:15:23 +00:00
// QEMU does not support autoscaling
controlPlanes = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.QEMUControlPlaneInstances,
GroupID: "",
}
2022-07-29 06:10:51 +00:00
if len(stat.QEMUWorkerInstances) == 0 {
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no workers available, can't create Constellation with one instance")
}
// QEMU does not support autoscaling
workers = cloudtypes.ScalingGroup{
2022-07-29 06:10:51 +00:00
Instances: stat.QEMUWorkerInstances,
GroupID: "",
}
return
}
// initCompletion handels the completion of CLI arguments. It is frequently called
// while the user types arguments of the command to suggest completion.
func initCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return []string{}, cobra.ShellCompDirectiveError
}
return []string{}, cobra.ShellCompDirectiveDefault
}
2022-06-21 15:59:12 +00:00
type grpcDialer interface {
Dial(ctx context.Context, target string) (*grpc.ClientConn, error)
}
type licenseClient interface {
CheckQuota(ctx context.Context, checkRequest license.CheckQuotaRequest) (license.CheckQuotaResponse, error)
}