2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-05 08:14:11 -04:00
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-23 05:36:54 -04:00
|
|
|
"flag"
|
2022-07-05 08:14:11 -04:00
|
|
|
"net"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2022-07-14 09:45:04 -04:00
|
|
|
"time"
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-10-29 22:36:58 -04:00
|
|
|
awscloud "github.com/edgelesssys/constellation/v2/internal/cloud/aws"
|
2022-09-21 07:47:57 -04:00
|
|
|
azurecloud "github.com/edgelesssys/constellation/v2/internal/cloud/azure"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
gcpcloud "github.com/edgelesssys/constellation/v2/internal/cloud/gcp"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
2023-03-07 05:58:33 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/openstack"
|
2022-09-21 07:47:57 -04:00
|
|
|
qemucloud "github.com/edgelesssys/constellation/v2/internal/cloud/qemu"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/atlscredentials"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-03-29 03:30:13 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/watcher"
|
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/internal/kms"
|
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/internal/kubeadm"
|
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/internal/kubernetesca"
|
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/internal/server"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
// vpcIPTimeout is the maximum amount of time to wait for retrieval of the VPC ip.
|
|
|
|
const vpcIPTimeout = 30 * time.Second
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
func main() {
|
|
|
|
provider := flag.String("cloud-provider", "", "cloud service provider this binary is running on")
|
2023-01-20 12:51:06 -05:00
|
|
|
keyServiceEndpoint := flag.String("key-service-endpoint", "", "endpoint of Constellations key management service")
|
2023-03-14 06:46:27 -04:00
|
|
|
attestationVariant := flag.String("attestation-variant", "", "attestation variant to use for aTLS connections")
|
2022-11-14 03:02:56 -05:00
|
|
|
verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription)
|
2022-05-23 05:36:54 -04:00
|
|
|
flag.Parse()
|
2022-07-05 08:14:11 -04:00
|
|
|
|
2022-11-14 03:02:56 -05:00
|
|
|
log := logger.New(logger.JSONLog, logger.VerbosityFromInt(*verbosity))
|
2023-03-14 06:46:27 -04:00
|
|
|
log.With(
|
|
|
|
zap.String("version", constants.VersionInfo()),
|
|
|
|
zap.String("cloudProvider", *provider),
|
|
|
|
zap.String("attestationVariant", *attestationVariant),
|
|
|
|
).Infof("Constellation Node Join Service")
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewOsFs())
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
variant, err := variant.FromString(*attestationVariant)
|
2022-08-31 14:10:49 -04:00
|
|
|
if err != nil {
|
2023-03-14 06:46:27 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to parse attestation variant")
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
2023-03-14 06:46:27 -04:00
|
|
|
validator, err := watcher.NewValidator(log.Named("validator"), variant, handler)
|
2022-05-23 05:36:54 -04:00
|
|
|
if err != nil {
|
|
|
|
flag.Usage()
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to create validator")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 05:40:27 -04:00
|
|
|
creds := atlscredentials.New(nil, []atls.Validator{validator})
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), vpcIPTimeout)
|
|
|
|
defer cancel()
|
2022-07-08 04:59:59 -04:00
|
|
|
vpcIP, err := getVPCIP(ctx, *provider)
|
2022-07-05 08:14:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to get IP in VPC")
|
|
|
|
}
|
|
|
|
apiServerEndpoint := net.JoinHostPort(vpcIP, strconv.Itoa(constants.KubernetesPort))
|
|
|
|
kubeadm, err := kubeadm.New(apiServerEndpoint, log.Named("kubeadm"))
|
2022-05-23 05:36:54 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to create kubeadm")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
2023-01-20 12:51:06 -05:00
|
|
|
keyServiceClient := kms.New(log.Named("keyServiceClient"), *keyServiceEndpoint)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt, err := handler.Read(filepath.Join(constants.ServiceBasePath, constants.MeasurementSaltFilename))
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to read measurement salt")
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
server, err := server.New(
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt,
|
2022-06-28 10:51:30 -04:00
|
|
|
kubernetesca.New(log.Named("certificateAuthority"), handler),
|
|
|
|
kubeadm,
|
2023-01-20 12:51:06 -05:00
|
|
|
keyServiceClient,
|
2022-07-26 04:58:39 -04:00
|
|
|
log.Named("server"),
|
2022-06-28 10:51:30 -04:00
|
|
|
)
|
2022-11-23 04:29:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to create server")
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
watcher, err := watcher.New(log.Named("fileWatcher"), validator)
|
2022-05-23 05:36:54 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to create watcher for measurements updates")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
go func() {
|
2022-06-29 10:13:01 -04:00
|
|
|
log.Infof("starting file watcher for measurements file %s", filepath.Join(constants.ServiceBasePath, constants.MeasurementsFilename))
|
|
|
|
if err := watcher.Watch(filepath.Join(constants.ServiceBasePath, constants.MeasurementsFilename)); err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to watch measurements file")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
if err := server.Run(creds, strconv.Itoa(constants.JoinServicePort)); err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to run server")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 08:14:11 -04:00
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
func getVPCIP(ctx context.Context, provider string) (string, error) {
|
2023-03-07 05:58:33 -05:00
|
|
|
var metadataClient metadataAPI
|
2022-08-04 05:08:20 -04:00
|
|
|
var err error
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
switch cloudprovider.FromString(provider) {
|
2022-10-29 22:36:58 -04:00
|
|
|
case cloudprovider.AWS:
|
2023-03-07 05:58:33 -05:00
|
|
|
metadataClient, err = awscloud.New(ctx)
|
2022-10-29 22:36:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-05 08:14:11 -04:00
|
|
|
case cloudprovider.Azure:
|
2023-03-07 05:58:33 -05:00
|
|
|
metadataClient, err = azurecloud.New(ctx)
|
2022-07-05 08:14:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
case cloudprovider.GCP:
|
2022-11-09 08:43:48 -05:00
|
|
|
gcpMeta, err := gcpcloud.New(ctx)
|
2022-07-05 08:14:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-11-09 08:43:48 -05:00
|
|
|
defer gcpMeta.Close()
|
2023-03-07 05:58:33 -05:00
|
|
|
metadataClient = gcpMeta
|
|
|
|
case cloudprovider.OpenStack:
|
|
|
|
metadataClient, err = openstack.New(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-05 08:14:11 -04:00
|
|
|
case cloudprovider.QEMU:
|
2023-03-07 05:58:33 -05:00
|
|
|
metadataClient = qemucloud.New()
|
2022-07-05 08:14:11 -04:00
|
|
|
default:
|
|
|
|
return "", errors.New("unsupported cloud provider")
|
|
|
|
}
|
2022-08-04 05:08:20 -04:00
|
|
|
|
2023-03-07 05:58:33 -05:00
|
|
|
self, err := metadataClient.Self(ctx)
|
2022-08-04 05:08:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return self.VPCIP, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type metadataAPI interface {
|
|
|
|
Self(ctx context.Context) (metadata.InstanceMetadata, error)
|
2022-07-05 08:14:11 -04:00
|
|
|
}
|