2022-06-29 10:13:01 -04:00
|
|
|
package watcher
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/asn1"
|
2022-08-29 10:41:09 -04:00
|
|
|
"encoding/hex"
|
2022-05-23 05:36:54 -04:00
|
|
|
"fmt"
|
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-06-01 09:08:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/azure"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/gcp"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/qemu"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/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-05-23 05:36:54 -04:00
|
|
|
atls.Validator
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
// NewValidator initializes a new updatable validator.
|
|
|
|
func NewValidator(log *logger.Logger, csp string, fileHandler file.Handler) (*Updatable, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
var newValidator newValidatorFunc
|
|
|
|
switch cloudprovider.FromString(csp) {
|
|
|
|
case cloudprovider.Azure:
|
2022-08-29 10:41:09 -04:00
|
|
|
newValidator = func(m map[uint32][]byte, e []uint32, idkeydigest []byte, enforceIdKeyDigest bool, log *logger.Logger) atls.Validator {
|
|
|
|
return azure.NewValidator(m, e, idkeydigest, enforceIdKeyDigest, log)
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
case cloudprovider.GCP:
|
2022-08-29 10:41:09 -04:00
|
|
|
newValidator = func(m map[uint32][]byte, e []uint32, _ []byte, _ bool, log *logger.Logger) atls.Validator {
|
|
|
|
return gcp.NewValidator(m, e, log)
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
case cloudprovider.QEMU:
|
2022-08-29 10:41:09 -04:00
|
|
|
newValidator = func(m map[uint32][]byte, e []uint32, _ []byte, _ bool, log *logger.Logger) atls.Validator {
|
|
|
|
return qemu.NewValidator(m, e, log)
|
|
|
|
}
|
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-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 {
|
|
|
|
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
|
|
|
|
|
|
|
var measurements map[uint32][]byte
|
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-06-28 10:51:30 -04:00
|
|
|
u.log.Debugf("New measurements: %v", measurements)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
var enforced []uint32
|
|
|
|
if err := u.fileHandler.ReadJSON(filepath.Join(constants.ServiceBasePath, constants.EnforcedPCRsFilename), &enforced); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.log.Debugf("Enforced PCRs: %v", enforced)
|
|
|
|
|
2022-08-29 10:41:09 -04:00
|
|
|
var idkeydigest []byte
|
|
|
|
var enforceIdKeyDigest bool
|
|
|
|
if u.csp == cloudprovider.Azure {
|
|
|
|
u.log.Infof("Updating encforceIdKeyDigest value")
|
|
|
|
enforceRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.EnforceIdKeyDigestFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
enforceIdKeyDigest, err = strconv.ParseBool(string(enforceRaw))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing content of EnforceIdKeyDigestFilename: %s: %w", enforceRaw, err)
|
|
|
|
}
|
|
|
|
u.log.Debugf("New encforceIdKeyDigest value: %v", enforceIdKeyDigest)
|
|
|
|
|
|
|
|
u.log.Infof("Updating expected idkeydigest")
|
|
|
|
idkeydigestRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.IdKeyDigestFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
idkeydigest, err = hex.DecodeString(string(idkeydigestRaw))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing hexstring: %s: %w", idkeydigestRaw, err)
|
|
|
|
}
|
|
|
|
u.log.Debugf("New idkeydigest: %x", idkeydigest)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Validator = u.newValidator(measurements, enforced, idkeydigest, enforceIdKeyDigest, u.log)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:41:09 -04:00
|
|
|
type newValidatorFunc func(measurements map[uint32][]byte, enforcedPCRs []uint32, idkeydigest []byte, enforceIdKeyDigest bool, log *logger.Logger) atls.Validator
|