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 server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-23 04:29:36 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-05-23 05:36:54 -04:00
|
|
|
"fmt"
|
2022-11-23 04:29:36 -05:00
|
|
|
"io/fs"
|
2022-05-23 05:36:54 -04:00
|
|
|
"net"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
2022-11-23 04:29:36 -05:00
|
|
|
"strings"
|
2022-05-23 05:36:54 -04:00
|
|
|
"time"
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/grpclog"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-11-23 04:29:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/internal/kubernetes"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/joinproto"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-05-23 05:36:54 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-11-23 04:29:36 -05:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
kubeadmv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
// Server implements the core logic of Constellation's node join service.
|
2022-05-23 05:36:54 -04:00
|
|
|
type Server struct {
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt []byte
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-05-23 05:36:54 -04:00
|
|
|
file file.Handler
|
|
|
|
joinTokenGetter joinTokenGetter
|
|
|
|
dataKeyGetter dataKeyGetter
|
2022-06-02 09:58:19 -04:00
|
|
|
ca certificateAuthority
|
2022-11-23 04:29:36 -05:00
|
|
|
kubeClient kubeClient
|
2022-07-05 05:41:31 -04:00
|
|
|
joinproto.UnimplementedAPIServer
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a new Server.
|
2022-07-26 04:58:39 -04:00
|
|
|
func New(
|
|
|
|
measurementSalt []byte, fileHandler file.Handler, ca certificateAuthority,
|
|
|
|
joinTokenGetter joinTokenGetter, dataKeyGetter dataKeyGetter, log *logger.Logger,
|
2022-11-23 04:29:36 -05:00
|
|
|
) (*Server, error) {
|
|
|
|
kubeClient, err := kubernetes.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create kubernetes client: %w", err)
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
return &Server{
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt: measurementSalt,
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-05-23 05:36:54 -04:00
|
|
|
file: fileHandler,
|
|
|
|
joinTokenGetter: joinTokenGetter,
|
|
|
|
dataKeyGetter: dataKeyGetter,
|
2022-06-02 09:58:19 -04:00
|
|
|
ca: ca,
|
2022-11-23 04:29:36 -05:00
|
|
|
kubeClient: kubeClient,
|
|
|
|
}, nil
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the gRPC server on the given port, using the provided tlsConfig.
|
2022-06-13 05:40:27 -04:00
|
|
|
func (s *Server) Run(creds credentials.TransportCredentials, port string) error {
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.WithIncreasedLevel(zap.WarnLevel).Named("gRPC").ReplaceGRPCLogger()
|
2022-05-23 05:36:54 -04:00
|
|
|
grpcServer := grpc.NewServer(
|
2022-06-13 05:40:27 -04:00
|
|
|
grpc.Creds(creds),
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.Named("gRPC").GetServerUnaryInterceptor(),
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
2022-07-05 05:41:31 -04:00
|
|
|
joinproto.RegisterAPIServer(grpcServer, s)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", net.JoinHostPort("", port))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to listen: %s", err)
|
|
|
|
}
|
2022-07-05 08:13:19 -04:00
|
|
|
s.log.Infof("Starting join service on %s", lis.Addr().String())
|
2022-05-23 05:36:54 -04:00
|
|
|
return grpcServer.Serve(lis)
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
// IssueJoinTicket handles join requests of Constellation nodes.
|
2022-07-05 05:41:31 -04:00
|
|
|
// A node will receive:
|
2022-06-21 05:10:32 -04:00
|
|
|
// - stateful disk encryption key.
|
|
|
|
// - Kubernetes join token.
|
2022-07-26 04:58:39 -04:00
|
|
|
// - measurement salt and secret, to mark the node as initialized.
|
2022-07-05 05:41:31 -04:00
|
|
|
// In addition, control plane nodes receive:
|
2022-06-21 05:10:32 -04:00
|
|
|
// - a decryption key for CA certificates uploaded to the Kubernetes cluster.
|
2022-07-26 04:58:39 -04:00
|
|
|
func (s *Server) IssueJoinTicket(ctx context.Context, req *joinproto.IssueJoinTicketRequest) (*joinproto.IssueJoinTicketResponse, error) {
|
2022-06-28 10:51:30 -04:00
|
|
|
log := s.log.With(zap.String("peerAddress", grpclog.PeerAddrFromContext(ctx)))
|
2022-07-26 04:58:39 -04:00
|
|
|
log.Infof("IssueJoinTicket called")
|
|
|
|
|
|
|
|
log.Infof("Requesting measurement secret")
|
|
|
|
measurementSecret, err := s.dataKeyGetter.GetDataKey(ctx, attestation.MeasurementSecretContext, crypto.DerivedKeyLengthDefault)
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Errorf("Unable to get measurement secret")
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get measurement secret: %s", err)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Requesting disk encryption key")
|
2022-07-26 04:58:39 -04:00
|
|
|
stateDiskKey, err := s.dataKeyGetter.GetDataKey(ctx, req.DiskUuid, crypto.StateDiskKeyLength)
|
2022-05-23 05:36:54 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Unable to get key for stateful disk")
|
2022-07-05 05:41:31 -04:00
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get key for stateful disk: %s", err)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Creating Kubernetes join token")
|
2022-05-23 05:36:54 -04:00
|
|
|
kubeArgs, err := s.joinTokenGetter.GetJoinToken(constants.KubernetesJoinTokenTTL)
|
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Unable to generate Kubernetes join arguments")
|
2022-07-05 05:41:31 -04:00
|
|
|
return nil, status.Errorf(codes.Internal, "unable to generate Kubernetes join arguments: %s", err)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
log.Infof("Querying K8sVersion ConfigMap for Kubernetes version")
|
2022-07-26 04:58:39 -04:00
|
|
|
k8sVersion, err := s.getK8sVersion()
|
2022-07-18 06:28:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get k8s version: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
log.Infof("Querying K8sVersion ConfigMap for components ConfigMap name")
|
|
|
|
componentsConfigMapName, err := s.getK8sComponentsConfigMapName()
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// If the file does not exist, the Constellation was initialized with a version before 2.3.0
|
|
|
|
// As a migration step, the join service will create the ConfigMap with the K8s components which
|
|
|
|
// match the K8s minor version of the cluster.
|
|
|
|
log.Warnf("Reference to K8sVersion ConfigMap does not exist, creating fallback Components ConfigMap and referencing it in K8sVersion ConfigMap")
|
|
|
|
log.Warnf("This is expected if the Constellation was initialized with a CLI before version 2.3.0")
|
|
|
|
log.Warnf("DEPRECATION WARNING: This is a migration step and will be removed in a future release")
|
|
|
|
componentsConfigMapName, err = s.createFallbackComponentsConfigMap(ctx, k8sVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to create fallback k8s components configmap: %s", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get components ConfigMap name: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Querying %s ConfigMap for components", componentsConfigMapName)
|
|
|
|
components, err := s.kubeClient.GetComponents(ctx, componentsConfigMapName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get components: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Creating signed kubelet certificate")
|
2022-07-15 03:33:11 -04:00
|
|
|
kubeletCert, err := s.ca.GetCertificate(req.CertificateRequest)
|
2022-06-02 09:58:19 -04:00
|
|
|
if err != nil {
|
2022-07-05 05:41:31 -04:00
|
|
|
return nil, status.Errorf(codes.Internal, "unable to generate kubelet certificate: %s", err)
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-11 07:29:22 -04:00
|
|
|
var controlPlaneFiles []*joinproto.ControlPlaneCertOrKey
|
2022-07-05 05:41:31 -04:00
|
|
|
if req.IsControlPlane {
|
2022-07-14 09:45:04 -04:00
|
|
|
log.Infof("Loading control plane certificates and keys")
|
2022-07-11 07:29:22 -04:00
|
|
|
filesMap, err := s.joinTokenGetter.GetControlPlaneCertificatesAndKeys()
|
2022-07-05 05:41:31 -04:00
|
|
|
if err != nil {
|
2022-07-14 09:45:04 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Failed to load control plane certificates and keys")
|
|
|
|
return nil, status.Errorf(codes.Internal, "ActivateControlPlane failed: %s", err)
|
2022-07-05 05:41:31 -04:00
|
|
|
}
|
2022-07-11 07:29:22 -04:00
|
|
|
|
|
|
|
for k, v := range filesMap {
|
|
|
|
controlPlaneFiles = append(controlPlaneFiles, &joinproto.ControlPlaneCertOrKey{
|
|
|
|
Name: k,
|
|
|
|
Data: v,
|
|
|
|
})
|
|
|
|
}
|
2022-07-05 05:41:31 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-12-06 12:48:01 -05:00
|
|
|
nodeName, err := s.ca.GetNodeNameFromCSR(req.CertificateRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get node name from CSR: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.kubeClient.AddNodeToJoiningNodes(ctx, nodeName, components.GetHash()); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to add node to joining nodes: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
log.Infof("IssueJoinTicket successful")
|
2022-07-05 05:41:31 -04:00
|
|
|
return &joinproto.IssueJoinTicketResponse{
|
|
|
|
StateDiskKey: stateDiskKey,
|
2022-07-26 04:58:39 -04:00
|
|
|
MeasurementSalt: s.measurementSalt,
|
|
|
|
MeasurementSecret: measurementSecret,
|
2022-07-05 05:41:31 -04:00
|
|
|
ApiServerEndpoint: kubeArgs.APIServerEndpoint,
|
|
|
|
Token: kubeArgs.Token,
|
|
|
|
DiscoveryTokenCaCertHash: kubeArgs.CACertHashes[0],
|
|
|
|
KubeletCert: kubeletCert,
|
2022-07-11 07:29:22 -04:00
|
|
|
ControlPlaneFiles: controlPlaneFiles,
|
2022-07-18 06:28:02 -04:00
|
|
|
KubernetesVersion: k8sVersion,
|
2022-11-23 04:29:36 -05:00
|
|
|
KubernetesComponents: components.ToJoinProto(),
|
2022-07-05 05:41:31 -04:00
|
|
|
}, nil
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// IssueRejoinTicket issues a ticket for nodes to rejoin cluster.
|
2022-07-26 04:58:39 -04:00
|
|
|
func (s *Server) IssueRejoinTicket(ctx context.Context, req *joinproto.IssueRejoinTicketRequest) (*joinproto.IssueRejoinTicketResponse, error) {
|
|
|
|
log := s.log.With(zap.String("peerAddress", grpclog.PeerAddrFromContext(ctx)))
|
|
|
|
log.Infof("IssueRejoinTicket called")
|
|
|
|
|
|
|
|
log.Infof("Requesting measurement secret")
|
|
|
|
measurementSecret, err := s.dataKeyGetter.GetDataKey(ctx, attestation.MeasurementSecretContext, crypto.DerivedKeyLengthDefault)
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Errorf("Unable to get measurement secret")
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get measurement secret: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Requesting disk encryption key")
|
|
|
|
stateDiskKey, err := s.dataKeyGetter.GetDataKey(ctx, req.DiskUuid, crypto.StateDiskKeyLength)
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Errorf("Unable to get key for stateful disk")
|
|
|
|
return nil, status.Errorf(codes.Internal, "unable to get key for stateful disk: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &joinproto.IssueRejoinTicketResponse{
|
|
|
|
StateDiskKey: stateDiskKey,
|
|
|
|
MeasurementSecret: measurementSecret,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
// getK8sVersion reads the k8s version from a VolumeMount that is backed by the k8s-version ConfigMap.
|
2022-07-26 04:58:39 -04:00
|
|
|
func (s *Server) getK8sVersion() (string, error) {
|
2022-11-23 04:29:36 -05:00
|
|
|
fileContent, err := s.file.Read(filepath.Join(constants.ServiceBasePath, constants.K8sVersionConfigMapName))
|
2022-07-18 06:28:02 -04:00
|
|
|
if err != nil {
|
2022-11-23 04:29:36 -05:00
|
|
|
return "", fmt.Errorf("could not read k8s version file: %w", err)
|
2022-07-18 06:28:02 -04:00
|
|
|
}
|
|
|
|
k8sVersion := string(fileContent)
|
|
|
|
|
|
|
|
return k8sVersion, nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
// getK8sComponentsConfigMapName reads the k8s components config map name from a VolumeMount that is backed by the k8s-version ConfigMap.
|
|
|
|
func (s *Server) getK8sComponentsConfigMapName() (string, error) {
|
|
|
|
fileContent, err := s.file.Read(filepath.Join(constants.ServiceBasePath, constants.K8sComponentsFieldName))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read k8s version file: %w", err)
|
|
|
|
}
|
|
|
|
componentsConfigMapName := string(fileContent)
|
|
|
|
|
|
|
|
return componentsConfigMapName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function mimics the creation of the components ConfigMap which is now done in the bootstrapper
|
|
|
|
// during the first initialization of the Constellation .
|
|
|
|
// For more information see setupK8sVersionConfigMap() in bootstrapper/internal/kubernetes/kubernetes.go.
|
|
|
|
// This is a migration step and will be removed in a future release.
|
|
|
|
func (s *Server) createFallbackComponentsConfigMap(ctx context.Context, k8sVersion string) (string, error) {
|
|
|
|
validK8sVersion, err := versions.NewValidK8sVersion(k8sVersion)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not create fallback components config map: %w", err)
|
|
|
|
}
|
|
|
|
components := versions.VersionConfigs[validK8sVersion].KubernetesComponents
|
|
|
|
componentsMarshalled, err := json.Marshal(components)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("marshalling component versions: %w", err)
|
|
|
|
}
|
|
|
|
componentsHash := components.GetHash()
|
|
|
|
componentConfigMapName := fmt.Sprintf("k8s-component-%s", strings.ReplaceAll(componentsHash, ":", "-"))
|
|
|
|
|
|
|
|
componentsConfig := corev1.ConfigMap{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "ConfigMap",
|
|
|
|
},
|
|
|
|
Immutable: to.Ptr(true),
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: componentConfigMapName,
|
|
|
|
Namespace: "kube-system",
|
|
|
|
},
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.K8sComponentsFieldName: string(componentsMarshalled),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.kubeClient.CreateConfigMap(ctx, componentsConfig); err != nil {
|
|
|
|
return "", fmt.Errorf("creating fallback components config map: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.kubeClient.AddReferenceToK8sVersionConfigMap(ctx, "k8s-version", componentConfigMapName); err != nil {
|
|
|
|
return "", fmt.Errorf("adding reference to fallback components config map: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return componentConfigMapName, nil
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
// joinTokenGetter returns Kubernetes bootstrap (join) tokens.
|
|
|
|
type joinTokenGetter interface {
|
|
|
|
// GetJoinToken returns a bootstrap (join) token.
|
|
|
|
GetJoinToken(ttl time.Duration) (*kubeadmv1.BootstrapTokenDiscovery, error)
|
2022-07-11 07:29:22 -04:00
|
|
|
GetControlPlaneCertificatesAndKeys() (map[string][]byte, error)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// dataKeyGetter interacts with Constellation's key management system to retrieve keys.
|
|
|
|
type dataKeyGetter interface {
|
|
|
|
// GetDataKey returns a key derived from Constellation's KMS.
|
|
|
|
GetDataKey(ctx context.Context, uuid string, length int) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2022-06-02 09:58:19 -04:00
|
|
|
type certificateAuthority interface {
|
|
|
|
// GetCertificate returns a certificate and private key, signed by the issuer.
|
2022-07-15 03:33:11 -04:00
|
|
|
GetCertificate(certificateRequest []byte) (kubeletCert []byte, err error)
|
2022-12-06 12:48:01 -05:00
|
|
|
// GetNodeNameFromCSR returns the node name from the CSR.
|
|
|
|
GetNodeNameFromCSR(csr []byte) (string, error)
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
2022-11-23 04:29:36 -05:00
|
|
|
|
|
|
|
type kubeClient interface {
|
|
|
|
GetComponents(ctx context.Context, configMapName string) (versions.ComponentVersions, error)
|
|
|
|
CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error
|
2022-12-06 12:48:01 -05:00
|
|
|
AddNodeToJoiningNodes(ctx context.Context, nodeName string, componentsHash string) error
|
2022-11-23 04:29:36 -05:00
|
|
|
AddReferenceToK8sVersionConfigMap(ctx context.Context, k8sVersionsConfigMapName string, componentsConfigMapName string) error
|
|
|
|
}
|