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"
|
|
|
|
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"
|
|
|
|
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"
|
|
|
|
"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")
|
|
|
|
kmsEndpoint := flag.String("kms-endpoint", "", "endpoint of Constellations key management service")
|
2022-07-01 10:17:06 -04: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-07-01 10:17:06 -04:00
|
|
|
log := logger.New(logger.JSONLog, logger.VerbosityFromInt(*verbosity))
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.String("version", constants.VersionInfo), zap.String("cloudProvider", *provider)).
|
2022-07-05 08:13:19 -04:00
|
|
|
Infof("Constellation Node Join Service")
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewOsFs())
|
|
|
|
|
2022-08-31 14:10:49 -04:00
|
|
|
cvmRaw, err := handler.Read(filepath.Join(constants.ServiceBasePath, constants.AzureCVM))
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to get azureCVM from config map")
|
|
|
|
}
|
|
|
|
azureCVM, err := strconv.ParseBool(string(cvmRaw))
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to parse content of AzureCVM: %s", cvmRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator, err := watcher.NewValidator(log.Named("validator"), *provider, handler, azureCVM)
|
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
|
|
|
}
|
2022-06-28 10:51:30 -04:00
|
|
|
kms := kms.New(log.Named("kms"), *kmsEndpoint)
|
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-06-28 10:51:30 -04:00
|
|
|
server := server.New(
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt,
|
2022-06-28 10:51:30 -04:00
|
|
|
handler,
|
|
|
|
kubernetesca.New(log.Named("certificateAuthority"), handler),
|
|
|
|
kubeadm,
|
|
|
|
kms,
|
2022-07-26 04:58:39 -04:00
|
|
|
log.Named("server"),
|
2022-06-28 10:51:30 -04:00
|
|
|
)
|
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) {
|
2022-08-04 05:08:20 -04:00
|
|
|
var metadata metadataAPI
|
|
|
|
var err error
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
switch cloudprovider.FromString(provider) {
|
|
|
|
case cloudprovider.Azure:
|
2022-08-04 05:08:20 -04:00
|
|
|
metadata, err = azurecloud.NewMetadata(ctx)
|
2022-07-05 08:14:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
gcpClient, err := gcpcloud.NewClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-04 05:08:20 -04:00
|
|
|
metadata = gcpcloud.New(gcpClient)
|
2022-07-05 08:14:11 -04:00
|
|
|
case cloudprovider.QEMU:
|
2022-08-04 05:08:20 -04:00
|
|
|
metadata = &qemucloud.Metadata{}
|
2022-07-05 08:14:11 -04:00
|
|
|
default:
|
|
|
|
return "", errors.New("unsupported cloud provider")
|
|
|
|
}
|
2022-08-04 05:08:20 -04:00
|
|
|
|
|
|
|
self, err := metadata.Self(ctx)
|
|
|
|
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
|
|
|
}
|