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 (
|
2023-03-29 03:06:10 -04:00
|
|
|
"context"
|
2022-05-23 05:36:54 -04:00
|
|
|
"encoding/asn1"
|
2023-01-18 10:49:55 -05:00
|
|
|
"encoding/json"
|
2023-03-21 07:46:49 -04:00
|
|
|
"errors"
|
2022-05-23 05:36:54 -04:00
|
|
|
"fmt"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
2022-05-23 05:36:54 -04:00
|
|
|
"sync"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2023-03-14 06:46:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/choose"
|
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/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-03-29 03:30:13 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
2023-03-21 07:46:49 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Updatable implements an updatable atls.Validator.
|
|
|
|
type Updatable struct {
|
2023-03-14 06:46:27 -04:00
|
|
|
log *logger.Logger
|
|
|
|
mux sync.Mutex
|
|
|
|
fileHandler file.Handler
|
2023-03-29 03:30:13 -04:00
|
|
|
variant variant.Variant
|
2022-05-23 05:36:54 -04:00
|
|
|
atls.Validator
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
// NewValidator initializes a new updatable validator.
|
2023-03-29 03:30:13 -04:00
|
|
|
func NewValidator(log *logger.Logger, variant variant.Variant, fileHandler file.Handler) (*Updatable, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
u := &Updatable{
|
2023-03-14 06:46:27 -04:00
|
|
|
log: log,
|
|
|
|
fileHandler: fileHandler,
|
|
|
|
variant: variant,
|
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.
|
2023-03-29 03:06:10 -04:00
|
|
|
func (u *Updatable) Validate(ctx context.Context, attDoc []byte, nonce []byte) ([]byte, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
u.mux.Lock()
|
|
|
|
defer u.mux.Unlock()
|
2023-03-29 03:06:10 -04:00
|
|
|
return u.Validator.Validate(ctx, attDoc, nonce)
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
// Read ID Key config
|
|
|
|
var idKeyCfg idkeydigest.Config
|
2023-03-29 03:30:13 -04:00
|
|
|
if u.variant.Equal(variant.AzureSEVSNP{}) {
|
2023-03-21 07:46:49 -04:00
|
|
|
u.log.Infof("Updating SEV-SNP ID Key config")
|
2022-08-29 10:41:09 -04:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
err := u.fileHandler.ReadJSON(filepath.Join(constants.ServiceBasePath, constants.IDKeyConfigFilename), &idKeyCfg)
|
2022-08-29 10:41:09 -04:00
|
|
|
if err != nil {
|
2023-03-21 07:46:49 -04:00
|
|
|
if !errors.Is(err, afero.ErrFileNotFound) {
|
|
|
|
return fmt.Errorf("reading ID Key config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.log.Warnf("ID Key config file not found, falling back to old format (v2.6 or earlier)")
|
|
|
|
|
|
|
|
// v2.6 fallback
|
|
|
|
// TODO: Remove after v2.7 release
|
|
|
|
var digest idkeydigest.IDKeyDigests
|
|
|
|
var enforceIDKeyDigest idkeydigest.EnforceIDKeyDigest
|
|
|
|
enforceRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.EnforceIDKeyDigestFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
enforceIDKeyDigest = idkeydigest.EnforcePolicyFromString(string(enforceRaw))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing content of EnforceIdKeyDigestFilename: %s: %w", enforceRaw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idkeydigestRaw, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.IDKeyDigestFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(idkeydigestRaw, &digest); err != nil {
|
|
|
|
return fmt.Errorf("unmarshaling content of IDKeyDigestFilename: %s: %w", idkeydigestRaw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idKeyCfg.IDKeyDigests = digest
|
|
|
|
idKeyCfg.EnforcementPolicy = enforceIDKeyDigest
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2023-03-21 07:46:49 -04:00
|
|
|
|
|
|
|
u.log.Debugf("New ID Key config: %+v", idKeyCfg)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
validator, err := choose.Validator(u.variant, measurements, idKeyCfg, u.log)
|
2023-03-14 06:46:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("updating validator: %w", err)
|
|
|
|
}
|
|
|
|
u.Validator = validator
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|