Skip to content

Commit

Permalink
Add comments and slightly refactor action functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Mar 1, 2024
1 parent 4e2698f commit bbda501
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
91 changes: 49 additions & 42 deletions act/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,78 +65,85 @@ func BuiltinActions() map[string]*sdkAct.Action {
}
}

// Passthrough is a built-in action that always returns true and no error.
func Passthrough(map[string]any, ...sdkAct.Parameter) (any, error) {
return true, nil
}

// Terminate is a built-in action that terminates the connection if the
// terminate signal is true and the policy is set to "stop". The action
// can optionally receive a result parameter.
func Terminate(_ map[string]any, params ...sdkAct.Parameter) (any, error) {
if len(params) == 0 || params[0].Key != "logger" {
if len(params) == 0 || params[0].Key != LoggerKey {
// No logger parameter or the first parameter is not a logger.
return nil, gerr.ErrLoggerRequired
}

logger, ok := params[0].Value.(zerolog.Logger)

Check failure on line 82 in act/builtins.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

variable name 'ok' is too short for the scope of its usage (varnamelen)
if !ok {
// The first parameter is not a logger.
return nil, gerr.ErrLoggerRequired
}

if len(params) >= TerminateDefaultFieldCount {
if params[1].Key != "result" {
logger.Debug().Msg(
"terminate action can optionally receive a result parameter")
return true, nil
}

result, ok := params[1].Value.(map[string]any)
if !ok {
logger.Debug().Msg("terminate action can receive a result parameter")
return true, nil
}
if len(params) < TerminateDefaultFieldCount || params[1].Key != ResultKey {
logger.Debug().Msg(
"terminate action can optionally receive a result parameter")
return true, nil
}

// If the result from the plugin does not contain a response,
// yet it is a terminal action (hence running this action),
// add an error response to the result and terminate the connection.
if _, exists := result["response"]; !exists {
logger.Trace().Fields(result).Msg(
"Terminating without response, returning an error response")
result["response"] = (&pgproto3.Terminate{}).Encode(
postgres.ErrorResponse(
"Request terminated",
"ERROR",
"42000",
"Policy terminated the request",
),
)
}
result, ok := params[1].Value.(map[string]any)
if !ok {
logger.Debug().Msg("terminate action received a non-map result parameter")
return true, nil
}

return result, nil
// If the result from the plugin does not contain a response,
// yet it is a terminal action (hence running this action),
// add an error response to the result and terminate the connection.
if _, exists := result["response"]; !exists {
logger.Trace().Fields(result).Msg(
"Terminating without response, returning an error response")
result["response"] = (&pgproto3.Terminate{}).Encode(
postgres.ErrorResponse(
"Request terminated",
"ERROR",
"42000",
"Policy terminated the request",
),
)
}

return true, nil
return result, nil
}

// Log is a built-in action that logs the data received from the plugin.
func Log(data map[string]any, params ...sdkAct.Parameter) (any, error) {
if len(params) == 0 || params[0].Key != LoggerKey {
// No logger parameter or the first parameter is not a logger.
return nil, gerr.ErrLoggerRequired
}

logger, ok := params[0].Value.(zerolog.Logger)
if !ok {
// The first parameter is not a logger.
return nil, gerr.ErrLoggerRequired
}

fields := map[string]any{}
// Only log the fields that are not level, message, or log.
if len(data) > LogDefaultFieldCount {
for k, v := range data {

Check failure on line 134 in act/builtins.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

variable name 'v' is too short for the scope of its usage (varnamelen)
// Skip these necessary fields, as they are already used by the logger.
// level: The log level.
// message: The log message.
// log: The log signal.
if k == "level" || k == "message" || k == "log" {
continue
}
// Add the rest of the fields to the logger as extra fields.
fields[k] = v
}
}

if len(params) == 0 || params[0].Key != "logger" {
// No logger parameter or the first parameter is not a logger.
return false, nil
}

logger, ok := params[0].Value.(zerolog.Logger)
if !ok {
// The first parameter is not a logger.
return false, nil
}

logger.WithLevel(
logging.GetZeroLogLevel(cast.ToString(data["level"])),
).Fields(fields).Msg(cast.ToString(data["message"]))
Expand Down
9 changes: 7 additions & 2 deletions act/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/rs/zerolog"
)

const (
LoggerKey = "__logger__"
ResultKey = "__result__"
)

type IRegistry interface {
Add(policy *sdkAct.Policy)
Apply(signals []sdkAct.Signal) []*sdkAct.Output
Expand Down Expand Up @@ -248,7 +253,7 @@ func (r *Registry) Run(
// This is automatically prepended to the parameters when running an action.
func WithLogger(logger zerolog.Logger) sdkAct.Parameter {
return sdkAct.Parameter{
Key: "logger",
Key: LoggerKey,
Value: logger,
}
}
Expand All @@ -257,7 +262,7 @@ func WithLogger(logger zerolog.Logger) sdkAct.Parameter {
// to be used by the action.
func WithResult(result map[string]any) sdkAct.Parameter {
return sdkAct.Parameter{
Key: "result",
Key: ResultKey,
Value: result,
}
}

0 comments on commit bbda501

Please sign in to comment.