Skip to content

Commit 513e5ac

Browse files
committed
Merge pull request #38 from gatewayd-io/non-strict-verification-policy
Non-strict (permissive) verification policy
2 parents 7d371c6 + c46c93b commit 513e5ac

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

cmd/config_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func getPath(path string) string {
3333

3434
func verificationPolicy() network.Policy {
3535
vPolicy := globalConfig.String("plugins.verificationPolicy")
36-
verificationPolicy := network.Ignore // default
36+
verificationPolicy := network.PassDown // default
3737
switch vPolicy {
3838
case "ignore":
3939
verificationPolicy = network.Ignore

network/hooks.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ type (
2020
)
2121

2222
const (
23-
Ignore Policy = iota // Ignore errors and continue
24-
Abort // Abort on first error and return results
25-
Remove // Remove the hook from the list on error and continue
23+
// Non-strict (permissive) mode.
24+
PassDown Policy = iota // Pass down the extra keys/values in result to the next plugins
25+
// Strict mode.
26+
Ignore // Ignore errors and continue
27+
Abort // Abort on first error and return results
28+
Remove // Remove the hook from the list on error and continue
2629
)
2730

2831
const (
@@ -113,7 +116,9 @@ func (h *HookConfig) Run(
113116

114117
// This is done to ensure that the return value of the hook is always valid,
115118
// and that the hook does not return any unexpected values.
116-
if verify(args, result) {
119+
// If the verification mode is non-strict (permissive), let the plugin pass
120+
// extra keys/values to the next plugin in chain.
121+
if verify(args, result) || verification == PassDown {
117122
// Update the last return value with the current result
118123
returnVal = result
119124
continue
@@ -122,6 +127,7 @@ func (h *HookConfig) Run(
122127
// At this point, the hook returned an invalid value, so we need to handle it.
123128
// The result of the current hook will be ignored, regardless of the policy.
124129
switch verification {
130+
// Ignore the result of this plugin, log an error and execute the next hook.
125131
case Ignore:
126132
errMsg := fmt.Sprintf(
127133
"Hook %s (Prio %d) returned invalid value, ignoring", hookType, prio)
@@ -134,7 +140,7 @@ func (h *HookConfig) Run(
134140
if idx == 0 {
135141
returnVal = args
136142
}
137-
continue
143+
// Abort execution of the plugins, log the error and return the result of the last hook.
138144
case Abort:
139145
errMsg := fmt.Sprintf(
140146
"Hook %s (Prio %d) returned invalid value, aborting", hookType, prio)
@@ -147,6 +153,7 @@ func (h *HookConfig) Run(
147153
return args
148154
}
149155
return returnVal
156+
// Remove the hook from the registry, log the error and execute the next hook.
150157
case Remove:
151158
errMsg := fmt.Sprintf(
152159
"Hook %s (Prio %d) returned invalid value, removing", hookType, prio)
@@ -159,7 +166,9 @@ func (h *HookConfig) Run(
159166
if idx == 0 {
160167
returnVal = args
161168
}
162-
continue
169+
case PassDown:
170+
default:
171+
returnVal = result
163172
}
164173
}
165174

network/hooks_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,45 @@ func Test_verify_fail(t *testing.T) {
9999
}
100100
}
101101

102+
func Test_HookConfig_Run_PassDown(t *testing.T) {
103+
hooks := NewHookConfig()
104+
// The result of the hook will be nil and will be passed down to the next hook.
105+
hooks.Add(OnNewLogger, 0, func(s Signature) Signature {
106+
return nil
107+
})
108+
// The consolidated result should be Signature{"test": "test"}.
109+
hooks.Add(OnNewLogger, 1, func(s Signature) Signature {
110+
return Signature{
111+
"test": "test",
112+
}
113+
})
114+
// Although the first hook returns nil, and its Signature doesn't match the params,
115+
// so its result (nil) is passed down to the next hook in chain (prio 2).
116+
// Then the second hook runs and returns a Signature with a "test" key and value.
117+
assert.NotNil(t, hooks.Run(OnNewLogger, Signature{"test": "test"}, PassDown))
118+
}
119+
120+
func Test_HookConfig_Run_PassDown_2(t *testing.T) {
121+
hooks := NewHookConfig()
122+
// The result of the hook will be nil and will be passed down to the next hook.
123+
hooks.Add(OnNewLogger, 0, func(s Signature) Signature {
124+
return Signature{
125+
"test1": "test1",
126+
}
127+
})
128+
// The consolidated result should be Signature{"test1": "test1", "test2": "test2"}.
129+
hooks.Add(OnNewLogger, 1, func(s Signature) Signature {
130+
return Signature{
131+
"test2": "test2",
132+
}
133+
})
134+
// Although the first hook returns nil, and its Signature doesn't match the params,
135+
// so its result (nil) is passed down to the next hook in chain (prio 2).
136+
// Then the second hook runs and returns a Signature with a "test1" and "test2" key and value.
137+
assert.NotNil(t, hooks.Run(
138+
OnNewLogger, Signature{"test1": "test1", "test2": "test2"}, PassDown))
139+
}
140+
102141
func Test_HookConfig_Run_Ignore(t *testing.T) {
103142
hooks := NewHookConfig()
104143
// This should not run, because the return value is not the same as the params

network/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (pr *ProxyImpl) PassThrough(gconn gnet.Conn) error {
131131
return ErrClientNotFound
132132
}
133133

134-
// buf contains the data from the client (query)
134+
// buf contains the data from the client (<type>, length, query)
135135
buf, err := gconn.Next(-1)
136136
if err != nil {
137137
pr.logger.Error().Err(err).Msgf("Error reading from client: %v", err)

0 commit comments

Comments
 (0)