Skip to content

Commit

Permalink
feat(agent): implement Skipper middleware with logging and configuration
Browse files Browse the repository at this point in the history
- Add logging statements to track Skipper middleware creation and execution
- Implement configuration handling for the Skipper
- Add option settings application and default values
- Improve code structure for better readability and maintainability
  • Loading branch information
godcong committed Dec 20, 2024
1 parent 49fba10 commit 37cef51
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions agent/middleware/security/skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,49 @@ import (

"github.com/origadmin/runtime/context"
configv1 "github.com/origadmin/runtime/gen/go/config/v1"
"github.com/origadmin/runtime/log"
)

// Skipper returns a middleware that skips certain operations based on the provided configuration.
// It takes a Security configuration and a variable number of OptionSettings.
// If the Skipper is not configured, it returns nil and false.
func Skipper(cfg *configv1.Security, ss ...OptionSetting) (middleware.Middleware, bool) {
log.Debugf("Skipper: creating middleware with config: %+v", cfg)
// Apply default settings to the options
option := settings.ApplyDefaultsOrZero(ss...)
log.Debugf("Skipper: applied default settings to options: %+v", option)

// If the Skipper is not configured, return immediately
if option.Skipper == nil {
log.Debugf("Skipper: skipper is not configured, returning nil and false")
return nil, false
}

// Return a middleware that wraps the provided handler
log.Debugf("Skipper: returning middleware with skipper: %+v", option.Skipper)
return func(handler middleware.Handler) middleware.Handler {
// Return a new handler that checks if the operation should be skipped
return func(ctx context.Context, req interface{}) (interface{}, error) {
log.Debugf("Skipper: handling request: %+v", req)
// If the Skipper is configured, check if the operation should be skipped
if option.Skipper != nil {
// Get the transport from the client context
if tr, ok := transport.FromClientContext(ctx); ok {
log.Debugf("Skipper: got transport from client context: %+v", tr)
// todo: check the request method
// If the operation should be skipped, create a new skip context and call the next handler
if option.Skipper(tr.Operation()) {
log.Debugf("Skipper: skipping request, creating new skip context")
return handler(NewSkipContext(ctx), req)
}
} else {
log.Debugf("Skipper: unable to get transport from client context")
}
} else {
log.Debugf("Skipper: skipper is nil, not skipping request")
}
// If the operation should not be skipped, call the next handler with the original context
log.Debugf("Skipper: not skipping request, calling next handler with original context")
return handler(ctx, req)
}
}, true
Expand Down

0 comments on commit 37cef51

Please sign in to comment.