@@ -196,9 +196,17 @@ func Test_Apply(t *testing.T) {
196196 })
197197 assert .NotNil (t , actRegistry )
198198
199- outputs := actRegistry .Apply ([]sdkAct.Signal {
200- * sdkAct .Passthrough (),
201- })
199+ outputs := actRegistry .Apply (
200+ []sdkAct.Signal {
201+ * sdkAct .Passthrough (),
202+ },
203+ sdkAct.Hook {
204+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
205+ Priority : 1000 ,
206+ Params : map [string ]any {},
207+ Result : map [string ]any {},
208+ },
209+ )
202210 assert .NotNil (t , outputs )
203211 assert .Len (t , outputs , 1 )
204212 assert .Equal (t , "passthrough" , outputs [0 ].MatchedPolicy )
@@ -225,7 +233,15 @@ func Test_Apply_NoSignals(t *testing.T) {
225233 })
226234 assert .NotNil (t , actRegistry )
227235
228- outputs := actRegistry .Apply ([]sdkAct.Signal {})
236+ outputs := actRegistry .Apply (
237+ []sdkAct.Signal {},
238+ sdkAct.Hook {
239+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
240+ Priority : 1000 ,
241+ Params : map [string ]any {},
242+ Result : map [string ]any {},
243+ },
244+ )
229245 assert .NotNil (t , outputs )
230246 assert .Len (t , outputs , 1 )
231247 assert .Equal (t , "passthrough" , outputs [0 ].MatchedPolicy )
@@ -272,7 +288,12 @@ func Test_Apply_ContradictorySignals(t *testing.T) {
272288 assert .NotNil (t , actRegistry )
273289
274290 for _ , s := range signals {
275- outputs := actRegistry .Apply (s )
291+ outputs := actRegistry .Apply (s , sdkAct.Hook {
292+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
293+ Priority : 1000 ,
294+ Params : map [string ]any {},
295+ Result : map [string ]any {},
296+ })
276297 assert .NotNil (t , outputs )
277298 assert .Len (t , outputs , 2 )
278299 assert .Equal (t , "terminate" , outputs [0 ].MatchedPolicy )
@@ -318,6 +339,11 @@ func Test_Apply_ActionNotMatched(t *testing.T) {
318339
319340 outputs := actRegistry .Apply ([]sdkAct.Signal {
320341 {Name : "non-existent" },
342+ }, sdkAct.Hook {
343+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
344+ Priority : 1000 ,
345+ Params : map [string ]any {},
346+ Result : map [string ]any {},
321347 })
322348 assert .NotNil (t , outputs )
323349 assert .Len (t , outputs , 1 )
@@ -351,6 +377,11 @@ func Test_Apply_PolicyNotMatched(t *testing.T) {
351377
352378 outputs := actRegistry .Apply ([]sdkAct.Signal {
353379 * sdkAct .Terminate (),
380+ }, sdkAct.Hook {
381+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
382+ Priority : 1000 ,
383+ Params : map [string ]any {},
384+ Result : map [string ]any {},
354385 })
355386 assert .NotNil (t , outputs )
356387 assert .Len (t , outputs , 1 )
@@ -399,6 +430,11 @@ func Test_Apply_NonBoolPolicy(t *testing.T) {
399430
400431 outputs := actRegistry .Apply ([]sdkAct.Signal {
401432 * sdkAct .Passthrough (),
433+ }, sdkAct.Hook {
434+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
435+ Priority : 1000 ,
436+ Params : map [string ]any {},
437+ Result : map [string ]any {},
402438 })
403439 assert .NotNil (t , outputs )
404440 assert .Len (t , outputs , 1 )
@@ -447,6 +483,110 @@ func Test_Apply_BadPolicy(t *testing.T) {
447483 }
448484}
449485
486+ // Test_Apply_Hook tests the Apply function of the act registry with a policy that
487+ // has the hook info and makes use of it.
488+ func Test_Apply_Hook (t * testing.T ) {
489+ buf := bytes.Buffer {}
490+ logger := zerolog .New (& buf )
491+
492+ // Custom policy leveraging the hook info.
493+ policies := map [string ]* sdkAct.Policy {
494+ "passthrough" : sdkAct .MustNewPolicy (
495+ "passthrough" ,
496+ "true" ,
497+ nil ,
498+ ),
499+ "log" : sdkAct .MustNewPolicy (
500+ "log" ,
501+ `Signal.log == true && Policy.log == "enabled" &&
502+ split(Hook.Params.client.remote, ":")[0] == "192.168.0.1"` ,
503+ map [string ]any {
504+ "log" : "enabled" ,
505+ },
506+ ),
507+ }
508+
509+ actRegistry := NewActRegistry (
510+ Registry {
511+ Signals : BuiltinSignals (),
512+ Policies : policies ,
513+ Actions : BuiltinActions (),
514+ DefaultPolicyName : config .DefaultPolicy ,
515+ PolicyTimeout : config .DefaultPolicyTimeout ,
516+ DefaultActionTimeout : config .DefaultActionTimeout ,
517+ Logger : logger ,
518+ })
519+ assert .NotNil (t , actRegistry )
520+
521+ hook := sdkAct.Hook {
522+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
523+ Priority : 1000 ,
524+ // Input parameters for the hook.
525+ Params : map [string ]any {
526+ "field" : "value" ,
527+ "server" : map [string ]any {
528+ "local" : "value" ,
529+ "remote" : "value" ,
530+ },
531+ "client" : map [string ]any {
532+ "local" : "value" ,
533+ "remote" : "192.168.0.1:15432" ,
534+ },
535+ "request" : "Base64EncodedRequest" ,
536+ "error" : "" ,
537+ },
538+ // Output parameters for the hook.
539+ Result : map [string ]any {
540+ "field" : "value" ,
541+ "server" : map [string ]any {
542+ "local" : "value" ,
543+ "remote" : "value" ,
544+ },
545+ "client" : map [string ]any {
546+ "local" : "value" ,
547+ "remote" : "value" ,
548+ },
549+ "request" : "Base64EncodedRequest" ,
550+ "error" : "" ,
551+ sdkAct .Signals : []any {
552+ sdkAct .Log ("error" , "error message" , map [string ]any {"key" : "value" }).ToMap (),
553+ },
554+ "response" : "Base64EncodedResponse" ,
555+ },
556+ }
557+
558+ outputs := actRegistry .Apply (
559+ []sdkAct.Signal {
560+ * sdkAct .Log (
561+ "error" ,
562+ "policy matched from incoming address 192.168.0.1, so we are seeing this error message" ,
563+ map [string ]any {"key" : "value" },
564+ ),
565+ },
566+ hook ,
567+ )
568+ assert .NotNil (t , outputs )
569+ assert .Len (t , outputs , 1 )
570+ assert .Equal (t , "log" , outputs [0 ].MatchedPolicy )
571+ assert .Equal (t , outputs [0 ].Metadata , map [string ]any {
572+ "key" : "value" ,
573+ "level" : "error" ,
574+ "log" : true ,
575+ "message" : "policy matched from incoming address 192.168.0.1, so we are seeing this error message" ,
576+ })
577+ assert .False (t , outputs [0 ].Sync ) // Asynchronous action.
578+ assert .True (t , cast .ToBool (outputs [0 ].Verdict ))
579+ assert .False (t , outputs [0 ].Terminal )
580+
581+ result , err := actRegistry .Run (outputs [0 ], WithResult (hook .Result ))
582+ assert .Equal (t , err , gerr .ErrAsyncAction , "expected async action sentinel error" )
583+ assert .Nil (t , result , "expected nil result" )
584+
585+ time .Sleep (time .Millisecond ) // wait for async action to complete
586+
587+ assert .Contains (t , buf .String (), `{"level":"error","key":"value","message":"policy matched from incoming address 192.168.0.1, so we are seeing this error message"}` ) //nolint:lll
588+ }
589+
450590// Test_Run tests the Run function of the act registry with a non-terminal action.
451591func Test_Run (t * testing.T ) {
452592 logger := zerolog.Logger {}
@@ -464,6 +604,11 @@ func Test_Run(t *testing.T) {
464604
465605 outputs := actRegistry .Apply ([]sdkAct.Signal {
466606 * sdkAct .Passthrough (),
607+ }, sdkAct.Hook {
608+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
609+ Priority : 1000 ,
610+ Params : map [string ]any {},
611+ Result : map [string ]any {},
467612 })
468613 assert .NotNil (t , outputs )
469614
@@ -489,6 +634,11 @@ func Test_Run_Terminate(t *testing.T) {
489634
490635 outputs := actRegistry .Apply ([]sdkAct.Signal {
491636 * sdkAct .Terminate (),
637+ }, sdkAct.Hook {
638+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
639+ Priority : 1000 ,
640+ Params : map [string ]any {},
641+ Result : map [string ]any {},
492642 })
493643 assert .NotNil (t , outputs )
494644 assert .Equal (t , "terminate" , outputs [0 ].MatchedPolicy )
@@ -522,6 +672,11 @@ func Test_Run_Async(t *testing.T) {
522672
523673 outputs := actRegistry .Apply ([]sdkAct.Signal {
524674 * sdkAct .Log ("info" , "test" , map [string ]any {"async" : true }),
675+ }, sdkAct.Hook {
676+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
677+ Priority : 1000 ,
678+ Params : map [string ]any {},
679+ Result : map [string ]any {},
525680 })
526681 assert .NotNil (t , outputs )
527682 assert .Equal (t , "log" , outputs [0 ].MatchedPolicy )
@@ -647,7 +802,15 @@ func Test_Run_Timeout(t *testing.T) {
647802 })
648803 assert .NotNil (t , actRegistry )
649804
650- outputs := actRegistry .Apply ([]sdkAct.Signal {* signals [name ]})
805+ outputs := actRegistry .Apply (
806+ []sdkAct.Signal {* signals [name ]},
807+ sdkAct.Hook {
808+ Name : "HOOK_NAME_ON_TRAFFIC_FROM_CLIENT" ,
809+ Priority : 1000 ,
810+ Params : map [string ]any {},
811+ Result : map [string ]any {},
812+ },
813+ )
651814 assert .NotNil (t , outputs )
652815 assert .Equal (t , name , outputs [0 ].MatchedPolicy )
653816 assert .Equal (t ,
0 commit comments