Skip to content

Commit

Permalink
Added assign_if transformation (#276)
Browse files Browse the repository at this point in the history
Fixes #275

Signed-off-by: Kuldeep Singh Pal <tkuldeep.allen@gmail.com>
  • Loading branch information
tkuldeep committed Aug 2, 2022
1 parent 0184936 commit d7656a2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ parameters:
output: value_smaller_than10
type: add_if
parameters: <10
- input: value
output: dir
type: add_if
parameters: ==1
assignee: in
- input: dstPort
output: service
type: add_service
Expand Down Expand Up @@ -418,7 +423,8 @@ The second `add_if` generates a new field named `value_smaller_than10` that cont
the contents of the `value` field for entries that satisfy the condition specified
in the `parameters` variable (smaller than 10 in the example above). In addition, the
field `value_smaller_than10_Evaluate` with value `true` is added to all satisfied
entries
entries. if `assignee` field is set, then on satified parmater i.e. if parameter evalutes true then
`output` value will get value of `assignee` key.

The third rule `add_service` generates a new field named `service` with the known network
service name of `dstPort` port and `protocol` protocol. Unrecognized ports are ignored
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Following is the supported API format for network transformations:
add_service: add output network service field from input port and parameters protocol field
add_kubernetes: add output kubernetes fields from input
parameters: parameters specific to type
assignee: value needs to assign to output field
kubeConfigPath: path to kubeconfig file (optional)
servicesFile: path to services file (optional, default: /etc/services)
protocolsFile: path to protocols file (optional, default: /etc/protocols)
Expand Down
1 change: 1 addition & 0 deletions pkg/api/transform_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type NetworkTransformRule struct {
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
Parameters string `yaml:"parameters,omitempty" json:"parameters,omitempty" doc:"parameters specific to type"`
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
}

type NetworkTransformRules []NetworkTransformRule
6 changes: 5 additions & 1 deletion pkg/pipeline/transform/transform_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ func (n *Network) TransformEntry(inputEntry config.GenericMap) config.GenericMap
}
result, evaluateErr := expression.Evaluate(map[string]interface{}{"val": outputEntry[rule.Input]})
if evaluateErr == nil && result.(bool) {
outputEntry[rule.Output] = outputEntry[rule.Input]
if rule.Assignee != "" {
outputEntry[rule.Output] = rule.Assignee
} else {
outputEntry[rule.Output] = outputEntry[rule.Input]
}
outputEntry[rule.Output+"_Evaluate"] = true
}
case api.TransformNetworkOperationName("AddSubnet"):
Expand Down
28 changes: 28 additions & 0 deletions pkg/pipeline/transform/transform_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) {
Type: "add_if",
Parameters: "<10",
},
api.NetworkTransformRule{
Input: "value",
Output: "dir",
Assignee: "in",
Type: "add_if",
Parameters: "==1",
},
api.NetworkTransformRule{
Input: "value",
Output: "dir",
Assignee: "out",
Type: "add_if",
Parameters: "==0",
},
},
},
}
Expand All @@ -320,6 +334,20 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) {
output = newNetworkTransform.Transform([]config.GenericMap{entry})
require.Equal(t, true, output[0]["smaller_than_10_Evaluate"])
require.Equal(t, 1.2345e-67, output[0]["smaller_than_10"])

entry = config.GenericMap{
"value": 1,
}
output = newNetworkTransform.Transform([]config.GenericMap{entry})
require.Equal(t, true, output[0]["dir_Evaluate"])
require.Equal(t, "in", output[0]["dir"])

entry = config.GenericMap{
"value": 0,
}
output = newNetworkTransform.Transform([]config.GenericMap{entry})
require.Equal(t, true, output[0]["dir_Evaluate"])
require.Equal(t, "out", output[0]["dir"])
}

func Test_TransformNetworkOperationNameCustomServices(t *testing.T) {
Expand Down

0 comments on commit d7656a2

Please sign in to comment.