From 37cef51e048ac19e7c513e58bbd6f9fca0f73dbf Mon Sep 17 00:00:00 2001 From: godcong Date: Fri, 20 Dec 2024 23:08:48 +0800 Subject: [PATCH] feat(agent): implement Skipper middleware with logging and configuration - 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 --- agent/middleware/security/skipper.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/agent/middleware/security/skipper.go b/agent/middleware/security/skipper.go index 43c5f20..1ebdadd 100644 --- a/agent/middleware/security/skipper.go +++ b/agent/middleware/security/skipper.go @@ -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