Skip to content

Commit

Permalink
Merge pull request #418 from xmidt-org/denopink/wrp-validators/improv…
Browse files Browse the repository at this point in the history
…e-logging-and-setup

patch: improve setup and logging for wrp validators
  • Loading branch information
denopink authored Oct 9, 2024
2 parents d8f1c77 + 750c5ab commit d71aa9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/xmidt-org/webpa-common/v2 v2.3.2
github.com/xmidt-org/wrp-go/v3 v3.5.2
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.54.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
)

Expand Down Expand Up @@ -115,6 +114,7 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/fx v1.22.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/mod v0.17.0 // indirect
Expand Down
48 changes: 36 additions & 12 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/xmidt-org/clortho/clorthozap"
"github.com/xmidt-org/touchstone"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/xmidt-org/webpa-common/secure/handler"
Expand Down Expand Up @@ -69,16 +68,19 @@ const (
deviceID = "deviceID"

enforceCheck = "enforce"

zapWRPValidatorLabel = "wrp_validator_level"

Check failure on line 72 in primaryHandler.go

View workflow job for this annotation

GitHub Actions / ci / Go Lint

G101: Potential hardcoded credentials (gosec)
)

// Default values
const (
UnknownPartner = "unknown"
)

var errNoDeviceName = errors.New("no device name")

type validators []wrpvalidator.Validator
var (
errNoDeviceName = errors.New("no device name")
errWRPValidatorConfigError = errors.New("failed to configure wrp validators")
)

func authChain(v *viper.Viper, logger *zap.Logger, registry xmetrics.Registry, tf *touchstone.Factory) (alice.Chain, error) {
if registry == nil {
Expand Down Expand Up @@ -581,25 +583,31 @@ func validateWRP(v *viper.Viper, logger *zap.Logger, tf *touchstone.Factory) (fu

if valsConig := v.Get(wrpValidatorConfigKey); valsConig != nil {
if b, err := json.Marshal(valsConig); err != nil {
return nil, err
return nil, errors.Join(errWRPValidatorConfigError, err)
} else if err = json.Unmarshal(b, &vals); err != nil {
return nil, err
return nil, errors.Join(errWRPValidatorConfigError, err)
}

labelNames := []string{wrpvalidator.ClientIDLabel, wrpvalidator.PartnerIDLabel, wrpvalidator.MessageTypeLabel}
for _, v := range vals {
if err := v.AddMetric(tf, labelNames...); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
}
}

if errs != nil {
return nil, errors.Join(errWRPValidatorConfigError, errs)
}

return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if msg, ok := wrpcontext.GetMessage(r.Context()); ok {
var (
infoErrors error
warningErrors error
failureError error
unknownError error
satClientID = "N/A"
partnerID = device.UnknownPartner
)
Expand Down Expand Up @@ -628,19 +636,35 @@ func validateWRP(v *viper.Viper, logger *zap.Logger, tf *touchstone.Factory) (fu
)

switch v.Level() {
case wrpvalidator.ErrorLevel:
failureError = multierr.Append(failureError, err)
case wrpvalidator.InfoLevel:
infoErrors = errors.Join(infoErrors, err)
case wrpvalidator.WarningLevel:
warningErrors = multierr.Append(warningErrors, err)
warningErrors = errors.Join(warningErrors, err)
case wrpvalidator.ErrorLevel:
failureError = errors.Join(failureError, err)
default:
unknownError = errors.Join(unknownError, err)
}
}

if unknownError != nil {
logger.Warn("WRP message validation errors found",
zap.Error(unknownError), zap.String(zapWRPValidatorLabel, wrpvalidator.UnknownLevel.String()))
}

if infoErrors != nil {
logger.Warn("WRP message validation errors found",
zap.Error(infoErrors), zap.String(zapWRPValidatorLabel, wrpvalidator.InfoLevel.String()))
}

if warningErrors != nil {
logger.Warn("WRP message validation warnings found", zap.Error(warningErrors))
logger.Warn("WRP message validation errors found",
zap.Error(warningErrors), zap.String(zapWRPValidatorLabel, wrpvalidator.WarningLevel.String()))
}

if failureError != nil {
logger.Error("WRP message validation failures found", zap.Error(failureError))
logger.Error("WRP message validation (failure error level) found",
zap.Error(failureError), zap.String(zapWRPValidatorLabel, wrpvalidator.ErrorLevel.String()))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(
Expand Down

0 comments on commit d71aa9b

Please sign in to comment.