Skip to content

Commit

Permalink
validator: use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Dec 20, 2023
1 parent 1ea799c commit 03a9c5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cli/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"
"net"
"os"

Expand Down Expand Up @@ -92,7 +93,8 @@ func runSet(cmd *cobra.Command, args []string) error {
RequireIDBlock: true,
},
}
dialer := dialer.New(atls.NoIssuer, snp.NewValidator(validateOptsGen), &net.Dialer{})
// TODO(malt3): pass logger down.
dialer := dialer.New(atls.NoIssuer, snp.NewValidator(validateOptsGen, slog.Default()), &net.Dialer{})

conn, err := dialer.Dial(cmd.Context(), flags.coordinator)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion coordinator/intercom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net"
"time"

Expand All @@ -28,7 +29,8 @@ type certGetter interface {
}

func newIntercomServer(meshAuth *meshAuthority, caGetter certChainGetter) (*intercomServer, error) {
validator := snp.NewValidatorWithCallbacks(meshAuth, meshAuth)
// TODO(malt3): pass logger down.
validator := snp.NewValidatorWithCallbacks(meshAuth, slog.Default(), meshAuth)
credentials := atlscredentials.New(atls.NoIssuer, []atls.Validator{validator})
grpcServer := grpc.NewServer(
grpc.Creds(credentials),
Expand Down
21 changes: 12 additions & 9 deletions internal/attestation/snp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"log/slog"

"github.com/google/go-sev-guest/abi"
"github.com/google/go-sev-guest/proto/sevsnp"
Expand All @@ -23,6 +23,7 @@ import (
type Validator struct {
validateOptsGen validateOptsGenerator
callbackers []validateCallbacker
logger *slog.Logger
}

type validateCallbacker interface {
Expand All @@ -41,16 +42,18 @@ func (v *StaticValidateOptsGenerator) SNPValidateOpts(report *sevsnp.Report) (*v
return v.Opts, nil
}

func NewValidator(optsGen validateOptsGenerator) *Validator {
func NewValidator(optsGen validateOptsGenerator, log *slog.Logger) *Validator {
return &Validator{
validateOptsGen: optsGen,
logger: log.WithGroup("snp-validator"),
}
}

func NewValidatorWithCallbacks(optsGen validateOptsGenerator, callbacks ...validateCallbacker) *Validator {
func NewValidatorWithCallbacks(optsGen validateOptsGenerator, log *slog.Logger, callbacks ...validateCallbacker) *Validator {
return &Validator{
validateOptsGen: optsGen,
callbackers: callbacks,
logger: log.WithGroup("snp-validator"),
}
}

Expand All @@ -60,10 +63,10 @@ func (v *Validator) OID() asn1.ObjectIdentifier {

// Validate a TPM based attestation.
func (v *Validator) Validate(ctx context.Context, attDocRaw []byte, nonce []byte, peerPublicKey []byte) (err error) {
log.Printf("validator: validate called with nonce %s", hex.EncodeToString(nonce))
v.logger.Info("Validate called", "nonce", hex.EncodeToString(nonce))
defer func() {
if err != nil {
log.Printf("Failed to validate attestation document: %s", err)
v.logger.Error("Failed to validate attestation document: %s", err)
}
}()

Expand All @@ -73,7 +76,7 @@ func (v *Validator) Validate(ctx context.Context, attDocRaw []byte, nonce []byte
if _, err = base64.StdEncoding.Decode(reportRaw, attDocRaw); err != nil {
return err
}
log.Printf("validator: Report raw: %v", hex.EncodeToString(reportRaw))
v.logger.Info("Report decoded", "reportRaw", hex.EncodeToString(reportRaw))

report, err := abi.ReportToProto(reportRaw)
if err != nil {
Expand All @@ -90,7 +93,7 @@ func (v *Validator) Validate(ctx context.Context, attDocRaw []byte, nonce []byte
if err := verify.SnpAttestation(attestation, verifyOpts); err != nil {
return fmt.Errorf("verifying report: %w", err)
}
log.Println("validator: Successfully verified report signature")
v.logger.Info("Successfully verified report signature")

// Validate the report data.

Expand All @@ -103,7 +106,7 @@ func (v *Validator) Validate(ctx context.Context, attDocRaw []byte, nonce []byte
if err := validate.SnpAttestation(attestation, validateOpts); err != nil {
return fmt.Errorf("validating report claims: %w", err)
}
log.Println("validator: Successfully validated report data")
v.logger.Info("Successfully validated report data")

// Run callbacks.

Expand All @@ -113,6 +116,6 @@ func (v *Validator) Validate(ctx context.Context, attDocRaw []byte, nonce []byte
}
}

log.Println("validator: done")
v.logger.Info("Validate finished successfully")
return nil
}

0 comments on commit 03a9c5a

Please sign in to comment.