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

Act system #451

Merged
merged 50 commits into from
Mar 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ba68601
Update dependencies
mostafa Feb 24, 2024
527f7cf
Add Act registry
mostafa Feb 24, 2024
bbd97de
Fix issue after updating dependency (prometheus_client for Go)
mostafa Feb 24, 2024
dc7816f
Implement Act system in the plugin registry
mostafa Feb 24, 2024
fcbf380
Add config options for policies
mostafa Feb 24, 2024
6617bf1
Refactor proxy to use the new Act system
mostafa Feb 24, 2024
28a369a
Initialize Act system on run
mostafa Feb 24, 2024
dae7d6c
Allow expr
mostafa Feb 24, 2024
10c6aea
Update tests to reflect changes implemented as part of the Act system
mostafa Feb 24, 2024
e3c3b53
Update actions
mostafa Feb 24, 2024
42766a7
Move Signal helper functions to SDK
mostafa Feb 25, 2024
e2143cf
Update SDK
mostafa Feb 25, 2024
20322c0
Use optional params for action run function
mostafa Feb 25, 2024
ccb361b
Upgrade to Go v1.22
mostafa Feb 25, 2024
cf9bfa0
Pin golangci-lint version and use go install
mostafa Feb 25, 2024
d0f834b
Update workflows and Docker image to use Go v0.22
mostafa Feb 25, 2024
12609d2
Use pointer to struct to access fields
mostafa Feb 25, 2024
dced52e
Marshal/unmarshal to JSON to deal with time.Duration field
mostafa Feb 25, 2024
e564ace
Use the latest main branch to fix issues before release
mostafa Feb 25, 2024
217a735
Fix linter errors and improve code
mostafa Feb 25, 2024
c973282
Error sentinel is safe to be bypassed
mostafa Feb 26, 2024
6dc84d7
Run default signal, action and policy
mostafa Feb 26, 2024
122e12a
Actions now receive the result map from the traffic hooks (onTrafficF…
mostafa Feb 26, 2024
d6fa8fd
Lower log level and add clarifying comment
mostafa Feb 27, 2024
6ee9b82
Terminate connection with an error response if none is provided by th…
mostafa Feb 27, 2024
7537d38
Update signature of act.NewRegistry to remove global variables
mostafa Feb 27, 2024
a36b4fc
Fix linter issues
mostafa Feb 27, 2024
a328900
Ignore contradictory signals
mostafa Feb 27, 2024
e08fbe0
Use the latest (tagged) version of prometheus client for Go
mostafa Feb 28, 2024
e51c89d
Remove unused code
mostafa Feb 28, 2024
6d02b36
Add tests for Act utils functions
mostafa Feb 28, 2024
33ea274
Add tests for policy registry
mostafa Feb 28, 2024
9e6ccf6
Add cast to allow list of tests
mostafa Feb 28, 2024
670bda4
Remove comparison of pointers
mostafa Feb 28, 2024
f70efac
Add test for act/registry.go
mostafa Feb 28, 2024
0629368
Rename variable for clarification
mostafa Feb 29, 2024
fbf1ca8
Add policies and policyTimeout config params to sample config file
mostafa Feb 29, 2024
00db220
Remove termination policy (#458)
mostafa Feb 29, 2024
2b45ee7
Fix bug in handling contradictory signals
mostafa Feb 29, 2024
4a39c8e
Fix stack overflow when policy evaluation error occurs
mostafa Feb 29, 2024
bd2d65b
Add more tests for policy registry
mostafa Feb 29, 2024
86e5104
Add coverage report to test target
mostafa Feb 29, 2024
4e2698f
Rename policy registry to act registry
mostafa Feb 29, 2024
bbda501
Add comments and slightly refactor action functions
mostafa Mar 1, 2024
a5126a0
Refactor and rename variables and add comments
mostafa Mar 1, 2024
a2a13e7
Add more tests for nils
mostafa Mar 1, 2024
710a2da
Add comments
mostafa Mar 1, 2024
c4109b5
Fix linter issues
mostafa Mar 1, 2024
930b3ec
Rename policiesRegistry to actRegistry
mostafa Mar 1, 2024
f65832d
Merge branch 'main' into act-system
mostafa Mar 1, 2024
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
Prev Previous commit
Next Next commit
Run default signal, action and policy
mostafa committed Feb 26, 2024

Verified

This commit was signed with the committer’s verified signature.
mostafa Mostafa Moradian
commit 6dc84d7aa9e9abaf791c6a8a7250787c1169a6a4
15 changes: 5 additions & 10 deletions act/builtins.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,11 @@ import (
const LogDefaultFieldCount = 3

var (
builtinSignals = []*sdkAct.Signal{
sdkAct.Passthrough(),
sdkAct.Terminate(),
}

builtinsPolicies = []*sdkAct.Policy{
sdkAct.MustNewPolicy("passthrough", "true", nil),
sdkAct.MustNewPolicy(
@@ -75,14 +80,4 @@ var (
},
},
}

// TODO: figure out if the default output should match the default policy.
DefaultOutput = func() *sdkAct.Output {
return &sdkAct.Output{
MatchedPolicy: "passthrough",
Verdict: true,
Terminal: false,
Sync: true,
}
}
)
24 changes: 17 additions & 7 deletions act/registry.go
Original file line number Diff line number Diff line change
@@ -20,15 +20,23 @@ type Registry struct {
logger zerolog.Logger
timeout time.Duration // Timeout for policy evaluation

Signals map[string]*sdkAct.Signal
Policies map[string]*sdkAct.Policy
Actions map[string]*sdkAct.Action
DefaultPolicy *sdkAct.Policy
DefaultSignal *sdkAct.Signal
}

var _ IRegistry = (*Registry)(nil)

// NewRegistry creates a new registry with the specified default policy and timeout.
func NewRegistry(defaultPolicy string, timeout time.Duration, logger zerolog.Logger) *Registry {
signals := make(map[string]*sdkAct.Signal)
for _, signal := range builtinSignals {
signals[signal.Name] = signal
logger.Debug().Str("name", signal.Name).Msg("Registered builtin signal")
}

policies := make(map[string]*sdkAct.Policy)
for _, policy := range builtinsPolicies {
policies[policy.Name] = policy
@@ -48,12 +56,16 @@ func NewRegistry(defaultPolicy string, timeout time.Duration, logger zerolog.Log
defaultPolicy = "passthrough"
}

logger.Debug().Str("name", defaultPolicy).Msg("Using default policy")

return &Registry{
logger: logger,
timeout: timeout,
Signals: signals,
Actions: actions,
Policies: policies,
DefaultPolicy: policies[defaultPolicy],
DefaultSignal: signals[defaultPolicy],
}
}

@@ -74,12 +86,9 @@ func (r *Registry) Add(policy *sdkAct.Policy) {

// Apply applies the signals to the registry and returns the outputs.
func (r *Registry) Apply(signals []sdkAct.Signal) []*sdkAct.Output {
DefaultOutputs := []*sdkAct.Output{
DefaultOutput(),
}

// If there are no signals, apply the default policy.
if len(signals) == 0 {
return DefaultOutputs
return r.Apply([]sdkAct.Signal{*r.DefaultSignal})
}

// TODO: Check for non-contradictory actions (forward vs. drop)
@@ -95,7 +104,7 @@ func (r *Registry) Apply(signals []sdkAct.Signal) []*sdkAct.Output {
}

if len(outputs) == 0 {
return DefaultOutputs
return r.Apply([]sdkAct.Signal{*r.DefaultSignal})
}

return outputs
@@ -161,8 +170,9 @@ func (r *Registry) Run(output *sdkAct.Output) (any, *gerr.GatewayDError) {
output, err := action.Run(output.Metadata, WithLogger(r.logger))
if err != nil {
r.logger.Error().Err(err).Str("action", action.Name).Msg("Error running action")
return output, gerr.ErrRunningAction.Wrap(err)
}
return output, gerr.ErrRunningAction.Wrap(err)
return output, nil
}

r.logger.Debug().Fields(map[string]interface{}{
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ require (
github.com/NYTimes/gziphandler v1.1.1
github.com/codingsince1985/checksum v1.3.0
github.com/envoyproxy/protoc-gen-validate v1.0.4
github.com/gatewayd-io/gatewayd-plugin-sdk v0.2.2
github.com/gatewayd-io/gatewayd-plugin-sdk v0.2.3
github.com/getsentry/sentry-go v0.27.0
github.com/go-co-op/gocron v1.37.0
github.com/google/go-github/v53 v53.2.0
@@ -75,7 +75,7 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.17.0 // indirect
8 changes: 4 additions & 4 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugin/plugin_registry.go
Original file line number Diff line number Diff line change
@@ -371,6 +371,7 @@ func (reg *Registry) Apply(hookName string, result *v1.Struct) ([]*sdkAct.Output
outputs := ApplyPolicies(hookName, signals, reg.Logger, reg.PolicyRegistry())

// If no policies are found, return a default output.
// Note: this should never happen, as the default policy is always loaded.
if len(outputs) == 0 {
reg.Logger.Debug().Msg("No policies found for the given signals")
return nil, false
4 changes: 0 additions & 4 deletions plugin/utils.go
Original file line number Diff line number Diff line change
@@ -79,10 +79,6 @@ func GetSignals(result map[string]any) []sdkAct.Signal {
func ApplyPolicies(
hookName string, signals []sdkAct.Signal, logger zerolog.Logger, reg act.IRegistry,
) []*sdkAct.Output {
if len(signals) == 0 {
return nil
}

signalNames := []string{}
for _, signal := range signals {
signalNames = append(signalNames, signal.Name)