2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
package initserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2022-08-01 10:51:34 -04:00
|
|
|
"time"
|
2022-06-21 11:59:12 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/initproto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/diskencryption"
|
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/k8sapi/resources"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/vmtype"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/atlscredentials"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/grpclog"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/nodestate"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
2022-06-21 11:59:12 -04:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2022-08-01 10:51:34 -04:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-06-21 11:59:12 -04:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
// Server is the initialization server, which is started on each node.
|
|
|
|
// The server handles initialization calls from the CLI and initializes the
|
|
|
|
// Kubernetes cluster.
|
2022-06-21 11:59:12 -04:00
|
|
|
type Server struct {
|
2022-08-31 14:10:49 -04:00
|
|
|
nodeLock locker
|
|
|
|
initializer ClusterInitializer
|
|
|
|
disk encryptedDisk
|
|
|
|
fileHandler file.Handler
|
|
|
|
grpcServer serveStopper
|
|
|
|
cleaner cleaner
|
|
|
|
issuerWrapper IssuerWrapper
|
2022-06-21 11:59:12 -04:00
|
|
|
|
2022-07-14 07:30:44 -04:00
|
|
|
log *logger.Logger
|
2022-06-21 11:59:12 -04:00
|
|
|
|
|
|
|
initproto.UnimplementedAPIServer
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
// New creates a new initialization server.
|
2022-08-31 14:10:49 -04:00
|
|
|
func New(lock locker, kube ClusterInitializer, issuerWrapper IssuerWrapper, fh file.Handler, log *logger.Logger) *Server {
|
2022-07-14 07:30:44 -04:00
|
|
|
log = log.Named("initServer")
|
2022-08-31 14:10:49 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
server := &Server{
|
2022-08-31 14:10:49 -04:00
|
|
|
nodeLock: lock,
|
|
|
|
disk: diskencryption.New(),
|
|
|
|
initializer: kube,
|
|
|
|
fileHandler: fh,
|
|
|
|
issuerWrapper: issuerWrapper,
|
|
|
|
log: log,
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(
|
2022-08-31 14:10:49 -04:00
|
|
|
grpc.Creds(atlscredentials.New(issuerWrapper, nil)),
|
2022-08-01 10:51:34 -04:00
|
|
|
grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}),
|
2022-07-14 07:30:44 -04:00
|
|
|
log.Named("gRPC").GetServerUnaryInterceptor(),
|
2022-06-21 11:59:12 -04:00
|
|
|
)
|
|
|
|
initproto.RegisterAPIServer(grpcServer, server)
|
|
|
|
|
|
|
|
server.grpcServer = grpcServer
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// Serve starts the initialization server.
|
|
|
|
func (s *Server) Serve(ip, port string, cleaner cleaner) error {
|
2022-07-14 09:45:04 -04:00
|
|
|
s.cleaner = cleaner
|
2022-06-21 11:59:12 -04:00
|
|
|
lis, err := net.Listen("tcp", net.JoinHostPort(ip, port))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to listen: %w", err)
|
|
|
|
}
|
2022-08-01 10:51:34 -04:00
|
|
|
|
|
|
|
s.log.Infof("Starting")
|
2022-07-14 09:45:04 -04:00
|
|
|
return s.grpcServer.Serve(lis)
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
// Init initializes the cluster.
|
2022-06-21 11:59:12 -04:00
|
|
|
func (s *Server) Init(ctx context.Context, req *initproto.InitRequest) (*initproto.InitResponse, error) {
|
2022-07-14 09:45:04 -04:00
|
|
|
defer s.cleaner.Clean()
|
2022-07-14 07:30:44 -04:00
|
|
|
log := s.log.With(zap.String("peer", grpclog.PeerAddrFromContext(ctx)))
|
|
|
|
log.Infof("Init called")
|
2022-07-05 08:14:11 -04:00
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
// generate values for cluster attestation
|
2022-07-29 03:52:47 -04:00
|
|
|
measurementSalt, clusterID, err := deriveMeasurementValues(req.MasterSecret, req.Salt)
|
2022-07-08 04:59:59 -04:00
|
|
|
if err != nil {
|
2022-07-26 04:58:39 -04:00
|
|
|
return nil, status.Errorf(codes.Internal, "deriving measurement values: %s", err)
|
2022-07-08 04:59:59 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
nodeLockAcquired, err := s.nodeLock.TryLockOnce(clusterID)
|
2022-07-08 04:59:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "locking node: %s", err)
|
|
|
|
}
|
|
|
|
if !nodeLockAcquired {
|
2022-06-28 12:33:27 -04:00
|
|
|
// The join client seems to already have a connection to an
|
|
|
|
// existing join service. At this point, any further call to
|
|
|
|
// init does not make sense, so we just stop.
|
|
|
|
//
|
|
|
|
// The server stops itself after the current call is done.
|
2022-07-14 07:30:44 -04:00
|
|
|
log.Warnf("Node is already in a join process")
|
2022-06-21 11:59:12 -04:00
|
|
|
return nil, status.Error(codes.FailedPrecondition, "node is already being activated")
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
if err := s.setupDisk(req.MasterSecret, req.Salt); err != nil {
|
2022-06-21 11:59:12 -04:00
|
|
|
return nil, status.Errorf(codes.Internal, "setting up disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := nodestate.NodeState{
|
2022-07-26 04:58:39 -04:00
|
|
|
Role: role.ControlPlane,
|
|
|
|
MeasurementSalt: measurementSalt,
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|
|
|
|
if err := state.ToFile(s.fileHandler); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "persisting node state: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
kubeconfig, err := s.initializer.InitCluster(ctx,
|
2022-06-21 11:59:12 -04:00
|
|
|
req.CloudServiceAccountUri,
|
|
|
|
req.KubernetesVersion,
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt,
|
2022-08-12 09:59:45 -04:00
|
|
|
req.EnforcedPcrs,
|
2022-08-29 10:41:09 -04:00
|
|
|
req.EnforceIdkeydigest,
|
2022-10-05 09:02:46 -04:00
|
|
|
s.issuerWrapper.IDKeyDigest(),
|
2022-08-31 14:10:49 -04:00
|
|
|
s.issuerWrapper.VMType() == vmtype.AzureCVM,
|
2022-07-29 03:52:47 -04:00
|
|
|
resources.KMSConfig{
|
2022-06-21 11:59:12 -04:00
|
|
|
MasterSecret: req.MasterSecret,
|
2022-07-29 03:52:47 -04:00
|
|
|
Salt: req.Salt,
|
2022-06-21 11:59:12 -04:00
|
|
|
KMSURI: req.KmsUri,
|
|
|
|
StorageURI: req.StorageUri,
|
|
|
|
KeyEncryptionKeyID: req.KeyEncryptionKeyId,
|
|
|
|
UseExistingKEK: req.UseExistingKek,
|
|
|
|
},
|
|
|
|
sshProtoKeysToMap(req.SshUserKeys),
|
2022-08-12 04:20:19 -04:00
|
|
|
req.HelmDeployments,
|
2022-09-20 04:07:55 -04:00
|
|
|
req.ConformanceMode,
|
2022-07-14 07:30:44 -04:00
|
|
|
s.log,
|
2022-06-21 11:59:12 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "initializing cluster: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:30:44 -04:00
|
|
|
log.Infof("Init succeeded")
|
2022-06-21 11:59:12 -04:00
|
|
|
return &initproto.InitResponse{
|
|
|
|
Kubeconfig: kubeconfig,
|
2022-07-26 04:58:39 -04:00
|
|
|
ClusterId: clusterID,
|
2022-06-21 11:59:12 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// Stop stops the initialization server gracefully.
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
func (s *Server) setupDisk(masterSecret, salt []byte) error {
|
2022-06-21 11:59:12 -04:00
|
|
|
if err := s.disk.Open(); err != nil {
|
|
|
|
return fmt.Errorf("opening encrypted disk: %w", err)
|
|
|
|
}
|
|
|
|
defer s.disk.Close()
|
|
|
|
|
|
|
|
uuid, err := s.disk.UUID()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving uuid of disk: %w", err)
|
|
|
|
}
|
|
|
|
uuid = strings.ToLower(uuid)
|
|
|
|
|
2022-08-01 03:11:13 -04:00
|
|
|
diskKey, err := crypto.DeriveKey(masterSecret, salt, []byte(crypto.HKDFInfoPrefix+uuid), crypto.DerivedKeyLengthDefault)
|
2022-06-21 11:59:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.disk.UpdatePassphrase(string(diskKey))
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:10:49 -04:00
|
|
|
type IssuerWrapper struct {
|
|
|
|
atls.Issuer
|
|
|
|
vmType vmtype.VMType
|
|
|
|
idkeydigest []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIssuerWrapper(issuer atls.Issuer, vmType vmtype.VMType, idkeydigest []byte) IssuerWrapper {
|
|
|
|
return IssuerWrapper{
|
|
|
|
Issuer: issuer,
|
|
|
|
vmType: vmType,
|
|
|
|
idkeydigest: idkeydigest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IssuerWrapper) VMType() vmtype.VMType {
|
|
|
|
return i.vmType
|
|
|
|
}
|
|
|
|
|
2022-10-05 09:02:46 -04:00
|
|
|
func (i *IssuerWrapper) IDKeyDigest() []byte {
|
2022-08-31 14:10:49 -04:00
|
|
|
return i.idkeydigest
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
func sshProtoKeysToMap(keys []*initproto.SSHUserKey) map[string]string {
|
|
|
|
keyMap := make(map[string]string)
|
|
|
|
for _, key := range keys {
|
|
|
|
keyMap[key.Username] = key.PublicKey
|
|
|
|
}
|
|
|
|
return keyMap
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
func deriveMeasurementValues(masterSecret, hkdfSalt []byte) (salt, clusterID []byte, err error) {
|
2022-07-26 04:58:39 -04:00
|
|
|
salt, err = crypto.GenerateRandomBytes(crypto.RNGLengthDefault)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-07-29 03:52:47 -04:00
|
|
|
secret, err := attestation.DeriveMeasurementSecret(masterSecret, hkdfSalt)
|
2022-07-26 04:58:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-07-29 03:52:47 -04:00
|
|
|
clusterID, err = attestation.DeriveClusterID(secret, salt)
|
2022-07-26 04:58:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return salt, clusterID, nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
// ClusterInitializer has the ability to initialize a cluster.
|
2022-06-21 11:59:12 -04:00
|
|
|
type ClusterInitializer interface {
|
2022-06-28 12:33:27 -04:00
|
|
|
// InitCluster initializes a new Kubernetes cluster.
|
2022-06-21 11:59:12 -04:00
|
|
|
InitCluster(
|
|
|
|
ctx context.Context,
|
|
|
|
cloudServiceAccountURI string,
|
2022-06-28 12:33:27 -04:00
|
|
|
k8sVersion string,
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt []byte,
|
2022-08-12 09:59:45 -04:00
|
|
|
enforcedPcrs []uint32,
|
2022-10-05 09:02:46 -04:00
|
|
|
enforceIDKeyDigest bool,
|
2022-08-31 14:10:49 -04:00
|
|
|
idKeyDigest []byte,
|
|
|
|
azureCVM bool,
|
2022-07-29 03:52:47 -04:00
|
|
|
kmsConfig resources.KMSConfig,
|
2022-06-21 11:59:12 -04:00
|
|
|
sshUserKeys map[string]string,
|
2022-08-12 04:20:19 -04:00
|
|
|
helmDeployments []byte,
|
2022-09-20 04:07:55 -04:00
|
|
|
conformanceMode bool,
|
2022-07-14 07:30:44 -04:00
|
|
|
log *logger.Logger,
|
2022-06-21 11:59:12 -04:00
|
|
|
) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
type encryptedDisk interface {
|
2022-06-21 11:59:12 -04:00
|
|
|
// Open prepares the underlying device for disk operations.
|
|
|
|
Open() error
|
|
|
|
// Close closes the underlying device.
|
|
|
|
Close() error
|
|
|
|
// UUID gets the device's UUID.
|
|
|
|
UUID() (string, error)
|
|
|
|
// UpdatePassphrase switches the initial random passphrase of the encrypted disk to a permanent passphrase.
|
|
|
|
UpdatePassphrase(passphrase string) error
|
|
|
|
}
|
2022-06-28 12:33:27 -04:00
|
|
|
|
|
|
|
type serveStopper interface {
|
|
|
|
// Serve starts the server.
|
|
|
|
Serve(lis net.Listener) error
|
|
|
|
// GracefulStop stops the server and blocks until all requests are done.
|
|
|
|
GracefulStop()
|
|
|
|
}
|
2022-07-08 04:59:59 -04:00
|
|
|
|
|
|
|
type locker interface {
|
|
|
|
// TryLockOnce tries to lock the node. If the node is already locked, it
|
|
|
|
// returns false. If the node is unlocked, it locks it and returns true.
|
2022-07-26 04:58:39 -04:00
|
|
|
TryLockOnce(clusterID []byte) (bool, error)
|
2022-07-08 04:59:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type cleaner interface {
|
|
|
|
Clean()
|
|
|
|
}
|