Skip to content

Commit

Permalink
pass cached certificates to constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Sanft <[email protected]>
  • Loading branch information
msanft committed Sep 22, 2023
1 parent 82b3857 commit 57fe823
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
4 changes: 1 addition & 3 deletions joinservice/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ func main() {
log.With(zap.Error(err)).Fatalf("Failed to create certificate chain cache")
}

validator := watcher.
NewValidator(log.Named("validator"), attVariant, handler).
WithCachedCerts(cachedCerts)
validator := watcher.NewValidator(log.Named("validator"), attVariant, handler, cachedCerts)
if err := validator.Update(); err != nil {
flag.Usage()
log.With(zap.Error(err)).Fatalf("Failed to create validator")
Expand Down
15 changes: 5 additions & 10 deletions joinservice/internal/watcher/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,15 @@ type Updatable struct {
}

// NewValidator initializes a new updatable validator.
func NewValidator(log *logger.Logger, variant variant.Variant, fileHandler file.Handler) *Updatable {
func NewValidator(log *logger.Logger, variant variant.Variant, fileHandler file.Handler, cachedCerts *certcache.CachedCerts) *Updatable {
return &Updatable{
log: log,
fileHandler: fileHandler,
variant: variant,
cachedCerts: cachedCerts,
}
}

// WithCachedCerts sets the available cached certificates in the updatable validator.
func (u *Updatable) WithCachedCerts(cachedCerts *certcache.CachedCerts) *Updatable {
u.cachedCerts = cachedCerts
return u
}

// Validate calls the validators Validate method, and prevents any updates during the call.
func (u *Updatable) Validate(ctx context.Context, attDoc []byte, nonce []byte) ([]byte, error) {
u.mux.Lock()
Expand Down Expand Up @@ -80,7 +75,7 @@ func (u *Updatable) Update() error {
}
u.log.Debugf("New expected measurements: %+v", cfg.GetMeasurements())

cfgWithCerts, err := u.addCachedCerts(cfg)
cfgWithCerts, err := u.configWithCerts(cfg)
if err != nil {
return fmt.Errorf("adding cached certificates: %w", err)
}
Expand All @@ -94,8 +89,8 @@ func (u *Updatable) Update() error {
return nil
}

// addCachedCerts adds the certificates cached by the validator to the config, if applicable.
func (u *Updatable) addCachedCerts(cfg config.AttestationCfg) (config.AttestationCfg, error) {
// addCachedCerts adds the certificates cached by the validator to the attestation config, if applicable.
func (u *Updatable) configWithCerts(cfg config.AttestationCfg) (config.AttestationCfg, error) {
switch c := cfg.(type) {
case *config.AzureSEVSNP:
ask, err := u.getCachedAskCert()
Expand Down
1 change: 1 addition & 0 deletions joinservice/internal/watcher/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestNewUpdateableValidator(t *testing.T) {
logger.NewTest(t),
tc.variant,
handler,
nil, // TODO(msanft): add certcache test
).Update()
if tc.wantErr {
assert.Error(err)
Expand Down

0 comments on commit 57fe823

Please sign in to comment.