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"
|
|
|
|
"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-04-06 11:00:56 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
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"
|
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
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
data, err := u.fileHandler.Read(filepath.Join(constants.ServiceBasePath, constants.AttestationConfigFilename))
|
|
|
|
if err != nil {
|
2022-05-23 05:36:54 -04:00
|
|
|
return err
|
|
|
|
}
|
2023-05-03 05:11:53 -04:00
|
|
|
cfg, err := config.UnmarshalAttestationConfig(data, u.variant)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshaling config: %w", err)
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
2023-05-03 05:11:53 -04:00
|
|
|
u.log.Debugf("New expected measurements: %+v", cfg.GetMeasurements())
|
2022-08-29 10:41:09 -04:00
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
validator, err := choose.Validator(cfg, 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
|
|
|
|
}
|