Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: improve setup and logging for wrp validators #418

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
deviceID = "deviceID"

enforceCheck = "enforce"

zapWRPValidatorLabel = "wrp_validator_level"
)

// 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 @@

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

Check warning on line 586 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L586

Added line #L586 was not covered by tests
} else if err = json.Unmarshal(b, &vals); err != nil {
return nil, err
return nil, errors.Join(errWRPValidatorConfigError, err)

Check warning on line 588 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L588

Added line #L588 was not covered by tests
}

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)

Check warning on line 594 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L594

Added line #L594 was not covered by tests
}
}
}

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

Check warning on line 600 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L599-L600

Added lines #L599 - L600 were not covered by tests
}

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

Check warning on line 607 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L607

Added line #L607 was not covered by tests
warningErrors error
failureError error
unknownError error

Check warning on line 610 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L610

Added line #L610 was not covered by tests
satClientID = "N/A"
partnerID = device.UnknownPartner
)
Expand Down Expand Up @@ -628,19 +636,35 @@
)

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

Check warning on line 640 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L639-L640

Added lines #L639 - L640 were not covered by tests
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)

Check warning on line 646 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L642-L646

Added lines #L642 - L646 were not covered by tests
}
}

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

Check warning on line 652 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L650-L652

Added lines #L650 - L652 were not covered by tests
}

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

Check warning on line 657 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L655-L657

Added lines #L655 - L657 were not covered by tests
}

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()))

Check warning on line 662 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L661-L662

Added lines #L661 - L662 were not covered by tests
}

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()))

Check warning on line 667 in primaryHandler.go

View check run for this annotation

Codecov / codecov/patch

primaryHandler.go#L666-L667

Added lines #L666 - L667 were not covered by tests
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(
Expand Down