2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
package watcher
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/asn1"
|
2023-01-18 10:49:55 -05:00
|
|
|
"encoding/json"
|
2022-11-24 04:57:58 -05:00
|
|
|
"errors"
|
2022-05-23 05:36:54 -04:00
|
|
|
"fmt"
|
2022-11-24 04:57:58 -05:00
|
|
|
"os"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
2022-08-29 10:41:09 -04:00
|
|
|
"strconv"
|
2022-05-23 05:36:54 -04:00
|
|
|
"sync"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-10-29 22:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/aws"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/azure/snp"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/azure/trustedlaunch"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/gcp"
|
2023-01-18 10:49:55 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/idkeydigest"
|
2022-11-24 04:57:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/qemu"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Updatable implements an updatable atls.Validator.
|
|
|
|
type Updatable struct {
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-05-23 05:36:54 -04:00
|
|
|
mux sync.Mutex
|
|
|
|
newValidator newValidatorFunc
|
|
|
|
fileHandler file.Handler
|
2022-08-29 10:41:09 -04:00
|
|
|
csp cloudprovider.Provider
|
2022-08-31 14:10:49 -04:00
|
|
|
azureCVM bool
|
2022-05-23 05:36:54 -04:00
|
|
|
atls.Validator
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
// NewValidator initializes a new updatable validator.
|
2022-08-31 14:10:49 -04:00
|
|
|
func NewValidator(log *logger.Logger, csp string, fileHandler file.Handler, azureCVM bool) (*Updatable, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
var newValidator newValidatorFunc
|
|
|
|
switch cloudprovider.FromString(csp) {
|
2022-10-29 22:36:58 -04:00
|
|
|
case cloudprovider.AWS:
|
2023-01-18 10:49:55 -05:00
|
|
|
newValidator = func(m measurements.M, _ idkeydigest.IDKeyDigests, _ bool, log *logger.Logger) atls.Validator {
|
2022-11-24 04:57:58 -05:00
|
|
|
return aws.NewValidator(m, log)
|
2022-10-29 22:36:58 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
case cloudprovider.Azure:
|
2022-08-31 14:10:49 -04:00
|
|
|
if azureCVM {
|
2023-01-18 10:49:55 -05:00
|
|
|
newValidator = func(m measurements.M, idkeydigest idkeydigest.IDKeyDigests, enforceIdKeyDigest bool, log *logger.Logger) atls.Validator {
|
2022-11-24 04:57:58 -05:00
|
|
|
return snp.NewValidator(m, idkeydigest, enforceIdKeyDigest, log)
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-18 10:49:55 -05:00
|
|
|
newValidator = func(m measurements.M, idkeydigest idkeydigest.IDKeyDigests, enforceIdKeyDigest bool, log *logger.Logger) atls.Validator {
|
2022-11-24 04:57:58 -05:00
|
|
|
return trustedlaunch.NewValidator(m, log)
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
case cloudprovider.GCP:
|
2023-01-18 10:49:55 -05:00
|
|
|
newValidator = func(m measurements.M, _ idkeydigest.IDKeyDigests, _ bool, log *logger.Logger) atls.Validator {
|
2022-11-24 04:57:58 -05:00
|
|
|
return gcp.NewValidator(m, log)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
case cloudprovider.QEMU:
|
2023-01-18 10:49:55 -05:00
|
|
|
newValidator = func(m measurements.M, _ idkeydigest.IDKeyDigests, _ bool, log *logger.Logger) atls.Validator {
|
2022-11-24 04:57:58 -05:00
|
|
|
return qemu.NewValidator(m, log)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown cloud service provider: %q", csp)
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &Updatable{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-05-23 05:36:54 -04:00
|
|
|
newValidator: newValidator,
|
|
|
|
fileHandler: fileHandler,
|
2022-08-29 10:41:09 -04:00
|
|
|
csp: cloudprovider.FromString(csp),
|
2022-08-31 14:10:49 -04:00
|
|
|
azureCVM: azureCVM,
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := u.Update(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate calls the validators Validate method, and prevents any updates during the call.
|
|
|
|
func (u *Updatable) Validate(attDoc []byte, nonce []byte) ([]byte, error) {
|
|
|
|
u.mux.Lock()
|
|
|
|
defer u.mux.Unlock()
|
|
|
|
return u.Validator.Validate(attDoc, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OID returns the validators Object Identifier.
|
|
|
|
func (u *Updatable) OID() asn1.ObjectIdentifier {
|
2022-11-24 09:50:59 -05:00
|
|
|
u.mux.Lock()
|
|
|
|
defer u.mux.Unlock()
|
2022-05-23 05:36:54 -04:00
|
|
|
return u.Validator.OID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update switches out the underlying validator.
|
|
|
|
func (u *Updatable) Update() error {
|
|
|
|
u.mux.Lock()
|
|
|
|
defer u.mux.Unlock()
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
u.log.Infof("Updating expected measurements")
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
var measurements measurements.M
|
2022-06-29 10:13:01 -04:00
|
|
|
if err := u.fileHandler.ReadJSON(filepath.Join(constants.ServiceBasePath, constants.MeasurementsFilename), &measurements); err != nil {
|
2022-05-23 05:36:54 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-24 04:57:58 -05:00
|
|
|
u.log.Debugf("New measurements: %+v", measurements)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
// handle legacy measurement format, where expected measurements and enforced measurements were stored in separate data structures
|
|
|
|
// TODO: remove with v2.4.0
|
2022-08-12 09:59:45 -04:00
|
|
|
var enforced []uint32
|
2022-11-24 04:57:58 -05:00
|
|
|
if err := u.fileHandler.ReadJSON(filepath.Join(constants.ServiceBasePath, constants.EnforcedPCRsFilename), &enforced); err == nil {
|
|
|
|
u.log.Debugf("Detected legacy format. Loading enforced PCRs...")
|
|
|
|
if err := measurements.SetEnforced(enforced); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.log.Debugf("Merged measurements with enforced values: %+v", measurements)
|
|
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
2022-08-12 09:59:45 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:49:55 -05:00
|
|
|
var digest idkeydigest.IDKeyDigests
|
2022-10-05 09:02:46 -04:00
|
|
|
var enforceIDKeyDigest bool
|
2022-08-31 14:10:49 -04:00
|
|
|
if u.csp == cloudprovider.Azure && u.azureCVM {
|
2022-08-29 10:41:09 -04:00
|
|
|
u.log.Infof("Updating encforceIdKeyDigest value")
|
2022-10-05 09:02:46 -04:00
|
|
|
enforceRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.EnforceIDKeyDigestFilename))
|
2022-08-29 10:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-05 09:02:46 -04:00
|
|
|
enforceIDKeyDigest, err = strconv.ParseBool(string(enforceRaw))
|
2022-08-29 10:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing content of EnforceIdKeyDigestFilename: %s: %w", enforceRaw, err)
|
|
|
|
}
|
2022-10-05 09:02:46 -04:00
|
|
|
u.log.Debugf("New encforceIdKeyDigest value: %v", enforceIDKeyDigest)
|
2022-08-29 10:41:09 -04:00
|
|
|
|
|
|
|
u.log.Infof("Updating expected idkeydigest")
|
2022-10-05 09:02:46 -04:00
|
|
|
idkeydigestRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.IDKeyDigestFilename))
|
2022-08-29 10:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 10:49:55 -05:00
|
|
|
if err = json.Unmarshal(idkeydigestRaw, &digest); err != nil {
|
|
|
|
return fmt.Errorf("unmarshaling content of IDKeyDigestFilename: %s: %w", idkeydigestRaw, err)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2023-01-18 10:49:55 -05:00
|
|
|
u.log.Debugf("New idkeydigest: %v", digest)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:49:55 -05:00
|
|
|
u.Validator = u.newValidator(measurements, digest, enforceIDKeyDigest, u.log)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:49:55 -05:00
|
|
|
type newValidatorFunc func(measurements measurements.M, idkeydigest idkeydigest.IDKeyDigests, enforceIdKeyDigest bool, log *logger.Logger) atls.Validator
|