2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2022-09-08 08:45:27 -04:00
|
|
|
"context"
|
2022-04-11 08:25:19 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
2022-09-08 08:45:27 -04:00
|
|
|
"fmt"
|
2022-04-11 08:25:19 -04:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-08-01 10:51:34 -04:00
|
|
|
"strconv"
|
2022-09-08 08:45:27 -04:00
|
|
|
"sync"
|
2022-04-11 08:25:19 -04:00
|
|
|
"syscall"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/systemd"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
"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/logger"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/nodestate"
|
2022-04-11 08:25:19 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyPath = "/run/cryptsetup-keys.d"
|
|
|
|
keyFile = "state.key"
|
|
|
|
stateDiskMappedName = "state"
|
|
|
|
stateDiskMountPath = "/var/run/state"
|
2022-08-15 08:50:03 -04:00
|
|
|
cryptsetupOptions = "cipher=aes-xts-plain64,integrity=hmac-sha256"
|
2022-04-11 08:25:19 -04:00
|
|
|
stateInfoPath = stateDiskMountPath + "/constellation/node_state.json"
|
|
|
|
)
|
|
|
|
|
2022-08-15 08:50:03 -04:00
|
|
|
// SetupManager handles formatting, mapping, mounting and unmounting of state disks.
|
2022-04-11 08:25:19 -04:00
|
|
|
type SetupManager struct {
|
2022-09-08 08:45:27 -04:00
|
|
|
log *logger.Logger
|
|
|
|
csp string
|
|
|
|
diskPath string
|
|
|
|
fs afero.Afero
|
|
|
|
mapper DeviceMapper
|
|
|
|
mounter Mounter
|
|
|
|
config ConfigurationGenerator
|
|
|
|
openTPM vtpm.TPMOpenFunc
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a SetupManager with the given parameters.
|
2022-09-08 08:45:27 -04:00
|
|
|
func New(log *logger.Logger, csp string, diskPath string, fs afero.Afero,
|
|
|
|
mapper DeviceMapper, mounter Mounter, openTPM vtpm.TPMOpenFunc,
|
|
|
|
) *SetupManager {
|
2022-04-11 08:25:19 -04:00
|
|
|
return &SetupManager{
|
2022-09-08 08:45:27 -04:00
|
|
|
log: log,
|
|
|
|
csp: csp,
|
|
|
|
diskPath: diskPath,
|
|
|
|
fs: fs,
|
|
|
|
mapper: mapper,
|
|
|
|
mounter: mounter,
|
|
|
|
config: systemd.New(fs),
|
|
|
|
openTPM: openTPM,
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareExistingDisk requests and waits for a decryption key to remap the encrypted state disk.
|
|
|
|
// Once the disk is mapped, the function taints the node as initialized by updating it's PCRs.
|
2022-09-08 08:45:27 -04:00
|
|
|
func (s *SetupManager) PrepareExistingDisk(recover RecoveryDoer) error {
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.Infof("Preparing existing state disk")
|
2022-04-11 08:25:19 -04:00
|
|
|
uuid := s.mapper.DiskUUID()
|
|
|
|
|
2022-08-01 10:51:34 -04:00
|
|
|
endpoint := net.JoinHostPort("0.0.0.0", strconv.Itoa(constants.RecoveryPort))
|
2022-09-08 08:45:27 -04:00
|
|
|
|
|
|
|
passphrase, measurementSecret, err := recover.Do(uuid, endpoint)
|
2022-04-11 08:25:19 -04:00
|
|
|
if err != nil {
|
2022-09-08 08:45:27 -04:00
|
|
|
return fmt.Errorf("failed to perform recovery: %w", err)
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mapper.MapDisk(stateDiskMappedName, string(passphrase)); err != nil {
|
2022-09-08 08:45:27 -04:00
|
|
|
return err
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mounter.MkdirAll(stateDiskMountPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
// we do not care about cleaning up the mount point on error, since any errors returned here should cause a boot failure
|
2022-04-11 08:25:19 -04:00
|
|
|
if err := s.mounter.Mount(filepath.Join("/dev/mapper/", stateDiskMappedName), stateDiskMountPath, "ext4", syscall.MS_RDONLY, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSalt, err := s.readMeasurementSalt(stateInfoPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-29 03:52:47 -04:00
|
|
|
clusterID, err := attestation.DeriveClusterID(measurementSecret, measurementSalt)
|
2022-04-11 08:25:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// taint the node as initialized
|
2022-07-26 04:58:39 -04:00
|
|
|
if err := vtpm.MarkNodeAsBootstrapped(s.openTPM, clusterID); err != nil {
|
2022-04-11 08:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:50:03 -04:00
|
|
|
if err := s.saveConfiguration(passphrase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
return s.mounter.Unmount(stateDiskMountPath, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareNewDisk prepares an instances state disk by formatting the disk as a LUKS device using a random passphrase.
|
|
|
|
func (s *SetupManager) PrepareNewDisk() error {
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.Infof("Preparing new state disk")
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
// generate and save temporary passphrase
|
2022-07-26 04:58:39 -04:00
|
|
|
passphrase := make([]byte, crypto.RNGLengthDefault)
|
2022-04-11 08:25:19 -04:00
|
|
|
if _, err := rand.Read(passphrase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-15 08:50:03 -04:00
|
|
|
if err := s.saveConfiguration(passphrase); err != nil {
|
2022-04-11 08:25:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.mapper.FormatDisk(string(passphrase)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mapper.MapDisk(stateDiskMappedName, string(passphrase))
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
func (s *SetupManager) readMeasurementSalt(path string) ([]byte, error) {
|
2022-04-11 08:25:19 -04:00
|
|
|
handler := file.NewHandler(s.fs)
|
|
|
|
var state nodestate.NodeState
|
|
|
|
if err := handler.ReadJSON(path, &state); err != nil {
|
2022-07-26 04:58:39 -04:00
|
|
|
return nil, err
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
if len(state.MeasurementSalt) != crypto.RNGLengthDefault {
|
|
|
|
return nil, errors.New("missing state information to retaint node")
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
return state.MeasurementSalt, nil
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
2022-08-15 08:50:03 -04:00
|
|
|
|
|
|
|
// saveConfiguration saves the given passphrase and cryptsetup mapping configuration to disk.
|
|
|
|
func (s *SetupManager) saveConfiguration(passphrase []byte) error {
|
|
|
|
// passphrase
|
|
|
|
if err := s.fs.MkdirAll(keyPath, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.fs.WriteFile(filepath.Join(keyPath, keyFile), passphrase, 0o400); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// systemd cryptsetup unit
|
|
|
|
return s.config.Generate(stateDiskMappedName, s.diskPath, filepath.Join(keyPath, keyFile), cryptsetupOptions)
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
type RecoveryServer interface {
|
2022-09-08 08:45:27 -04:00
|
|
|
Serve(context.Context, net.Listener, string) (key, secret []byte, err error)
|
|
|
|
}
|
|
|
|
|
2022-09-14 07:25:42 -04:00
|
|
|
type RejoinClient interface {
|
2022-09-08 08:45:27 -04:00
|
|
|
Start(context.Context, string) (key, secret []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeRecoverer struct {
|
2022-09-14 07:25:42 -04:00
|
|
|
recoveryServer RecoveryServer
|
|
|
|
rejoinClient RejoinClient
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeRecoverer initializes a new nodeRecoverer.
|
2022-09-14 07:25:42 -04:00
|
|
|
func NewNodeRecoverer(recoveryServer RecoveryServer, rejoinClient RejoinClient) *nodeRecoverer {
|
2022-09-08 08:45:27 -04:00
|
|
|
return &nodeRecoverer{
|
|
|
|
recoveryServer: recoveryServer,
|
|
|
|
rejoinClient: rejoinClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do performs a recovery procedure on the given state disk.
|
|
|
|
// The method starts a gRPC server to allow manual recovery by a user.
|
|
|
|
// At the same time it tries to request a decryption key from all available Constellation control-plane nodes.
|
|
|
|
func (r *nodeRecoverer) Do(uuid, endpoint string) (passphrase, measurementSecret []byte, err error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
lis, err := net.Listen("tcp", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer lis.Close()
|
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
key, secret, serveErr := r.recoveryServer.Serve(ctx, lis, uuid)
|
|
|
|
once.Do(func() {
|
|
|
|
cancel()
|
|
|
|
passphrase = key
|
|
|
|
measurementSecret = secret
|
|
|
|
})
|
|
|
|
if serveErr != nil && !errors.Is(serveErr, context.Canceled) {
|
|
|
|
err = serveErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
key, secret := r.rejoinClient.Start(ctx, uuid)
|
|
|
|
once.Do(func() {
|
|
|
|
cancel()
|
|
|
|
passphrase = key
|
|
|
|
measurementSecret = secret
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return passphrase, measurementSecret, err
|
|
|
|
}
|