Refactor Policy Enforcer to Policy Checker
- Renamed the `PolicyEnforcer` interface and related implementations to `PolicyChecker` for clarity and consistency. - Updated configuration keys in YAML files to reflect the new `checkPolicy` terminology. - Adjusted related code, tests, and documentation to support the new naming convention and ensure compatibility. - Enhanced comments and examples for the `checkPolicy` configuration to improve usability.
This commit is contained in:
@@ -19,7 +19,7 @@ type PluginManager interface {
|
||||
Publisher(ctx context.Context, cfg *plugin.Config) (definition.Publisher, error)
|
||||
Signer(ctx context.Context, cfg *plugin.Config) (definition.Signer, error)
|
||||
Step(ctx context.Context, cfg *plugin.Config) (definition.Step, error)
|
||||
PolicyEnforcer(ctx context.Context, cfg *plugin.Config) (definition.PolicyEnforcer, error)
|
||||
PolicyChecker(ctx context.Context, cfg *plugin.Config) (definition.PolicyChecker, error)
|
||||
Cache(ctx context.Context, cfg *plugin.Config) (definition.Cache, error)
|
||||
Registry(ctx context.Context, cfg *plugin.Config) (definition.RegistryLookup, error)
|
||||
KeyManager(ctx context.Context, cache definition.Cache, rLookup definition.RegistryLookup, cfg *plugin.Config) (definition.KeyManager, error)
|
||||
@@ -38,7 +38,7 @@ const (
|
||||
// PluginCfg holds the configuration for various plugins.
|
||||
type PluginCfg struct {
|
||||
SchemaValidator *plugin.Config `yaml:"schemaValidator,omitempty"`
|
||||
PolicyEnforcer *plugin.Config `yaml:"policyEnforcer,omitempty"`
|
||||
PolicyChecker *plugin.Config `yaml:"checkPolicy,omitempty"`
|
||||
SignValidator *plugin.Config `yaml:"signValidator,omitempty"`
|
||||
Publisher *plugin.Config `yaml:"publisher,omitempty"`
|
||||
Signer *plugin.Config `yaml:"signer,omitempty"`
|
||||
|
||||
@@ -35,7 +35,7 @@ type stdHandler struct {
|
||||
registry definition.RegistryLookup
|
||||
km definition.KeyManager
|
||||
schemaValidator definition.SchemaValidator
|
||||
policyEnforcer definition.PolicyEnforcer
|
||||
policyChecker definition.PolicyChecker
|
||||
router definition.Router
|
||||
publisher definition.Publisher
|
||||
transportWrapper definition.TransportWrapper
|
||||
@@ -319,7 +319,7 @@ func (h *stdHandler) initPlugins(ctx context.Context, mgr PluginManager, cfg *Pl
|
||||
if h.transportWrapper, err = loadPlugin(ctx, "TransportWrapper", cfg.TransportWrapper, mgr.TransportWrapper); err != nil {
|
||||
return err
|
||||
}
|
||||
if h.policyEnforcer, err = loadPlugin(ctx, "PolicyEnforcer", cfg.PolicyEnforcer, mgr.PolicyEnforcer); err != nil {
|
||||
if h.policyChecker, err = loadPlugin(ctx, "PolicyChecker", cfg.PolicyChecker, mgr.PolicyChecker); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -354,8 +354,8 @@ func (h *stdHandler) initSteps(ctx context.Context, mgr PluginManager, cfg *Conf
|
||||
s, err = newValidateSchemaStep(h.schemaValidator)
|
||||
case "addRoute":
|
||||
s, err = newAddRouteStep(h.router)
|
||||
case "policyEnforcer":
|
||||
s, err = newEnforcePolicyStep(h.policyEnforcer)
|
||||
case "checkPolicy":
|
||||
s, err = newCheckPolicyStep(h.policyChecker)
|
||||
default:
|
||||
if customStep, exists := steps[step]; exists {
|
||||
s = customStep
|
||||
|
||||
@@ -1,11 +1,69 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/beckn-one/beckn-onix/pkg/plugin"
|
||||
"github.com/beckn-one/beckn-onix/pkg/plugin/definition"
|
||||
)
|
||||
|
||||
// noopPluginManager satisfies PluginManager with nil plugins (unused loaders are never invoked when config is omitted).
|
||||
type noopPluginManager struct{}
|
||||
|
||||
func (noopPluginManager) Middleware(context.Context, *plugin.Config) (func(http.Handler) http.Handler, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) SignValidator(context.Context, *plugin.Config) (definition.SignValidator, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) Validator(context.Context, *plugin.Config) (definition.SchemaValidator, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) Router(context.Context, *plugin.Config) (definition.Router, error) { return nil, nil }
|
||||
func (noopPluginManager) Publisher(context.Context, *plugin.Config) (definition.Publisher, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) Signer(context.Context, *plugin.Config) (definition.Signer, error) { return nil, nil }
|
||||
func (noopPluginManager) Step(context.Context, *plugin.Config) (definition.Step, error) { return nil, nil }
|
||||
func (noopPluginManager) PolicyChecker(context.Context, *plugin.Config) (definition.PolicyChecker, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) Cache(context.Context, *plugin.Config) (definition.Cache, error) { return nil, nil }
|
||||
func (noopPluginManager) Registry(context.Context, *plugin.Config) (definition.RegistryLookup, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) KeyManager(context.Context, definition.Cache, definition.RegistryLookup, *plugin.Config) (definition.KeyManager, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) TransportWrapper(context.Context, *plugin.Config) (definition.TransportWrapper, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (noopPluginManager) SchemaValidator(context.Context, *plugin.Config) (definition.SchemaValidator, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestNewStdHandler_CheckPolicyStepWithoutPluginFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cfg := &Config{
|
||||
Plugins: PluginCfg{},
|
||||
Steps: []string{"checkPolicy"},
|
||||
}
|
||||
_, err := NewStdHandler(ctx, noopPluginManager{}, cfg, "testModule")
|
||||
if err == nil {
|
||||
t.Fatal("expected error when steps list checkPolicy but checkPolicy plugin is omitted")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "failed to initialize steps") {
|
||||
t.Fatalf("expected steps init failure, got: %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "PolicyChecker plugin not configured") {
|
||||
t.Fatalf("expected explicit PolicyChecker config error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHTTPClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -316,10 +316,18 @@ func extractSchemaVersion(body []byte) string {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// newEnforcePolicyStep creates and returns the enforcePolicy step after validation.
|
||||
func newEnforcePolicyStep(policyEnforcer definition.PolicyEnforcer) (definition.Step, error) {
|
||||
if policyEnforcer == nil {
|
||||
return nil, fmt.Errorf("invalid config: PolicyEnforcer plugin not configured")
|
||||
}
|
||||
return policyEnforcer, nil
|
||||
// checkPolicyStep adapts PolicyChecker into the Step interface.
|
||||
type checkPolicyStep struct {
|
||||
checker definition.PolicyChecker
|
||||
}
|
||||
|
||||
func newCheckPolicyStep(policyChecker definition.PolicyChecker) (definition.Step, error) {
|
||||
if policyChecker == nil {
|
||||
return nil, fmt.Errorf("invalid config: PolicyChecker plugin not configured")
|
||||
}
|
||||
return &checkPolicyStep{checker: policyChecker}, nil
|
||||
}
|
||||
|
||||
func (s *checkPolicyStep) Run(ctx *model.StepContext) error {
|
||||
return s.checker.CheckPolicy(ctx)
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ func (m *mockPluginManager) SchemaValidator(ctx context.Context, cfg *plugin.Con
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// PolicyEnforcer returns a mock policy enforcer implementation.
|
||||
func (m *mockPluginManager) PolicyEnforcer(ctx context.Context, cfg *plugin.Config) (definition.PolicyEnforcer, error) {
|
||||
// PolicyChecker returns a mock policy checker implementation.
|
||||
func (m *mockPluginManager) PolicyChecker(ctx context.Context, cfg *plugin.Config) (definition.PolicyChecker, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user