Commit e537bfa1 authored by Scarlett Perry's avatar Scarlett Perry
Browse files

check for nil

parent 872c638a
Showing with 26 additions and 4 deletions
+26 -4
......@@ -117,7 +117,7 @@ func parseFile(path string, pb proto.Message, template bool) error {
Solution is to use Clutch-specific templating tokens in the config that are then replaced
with the Go Template synax
1) $$ in lieu $
1) $$ in lieu of $
2) [[ ]] in lieu of {{ }}
*/
......
......@@ -50,6 +50,7 @@ type auditTemplateData struct {
Response map[string]interface{}
}
// TODO: (sperry) expand on helper funcs
// helper functions to format values in the Go template
var funcMap = template.FuncMap{
// for inputs that are type slice/map, returns a formatted slack list OR `N/A` if the slice/map is empty
......@@ -141,7 +142,11 @@ func FormatCustomText(message string, event *auditv1.RequestEvent) (string, erro
return "", err
}
return buf.String(), nil
// When a value is nil, the Go Template returns "<no value>" in its place. Replacing this with null instead.
// We hit this scenario with https://github.com/lyft/clutch/blob/main/api/k8s/v1/k8s.proto#L860
sanitized := strings.ReplaceAll(buf.String(), "<no value>", "null")
return sanitized, nil
}
// returns the API request/response details saved in an audit event
......@@ -176,6 +181,9 @@ func getAuditTemplateData(event *auditv1.RequestEvent) (*auditTemplateData, erro
// for inputs that are type slice/map, returns a formatted slack list OR "None" if the slice/map is empty
func slackList(data interface{}) string {
if data == nil {
return "None"
}
var b strings.Builder
const sliceItemFormat = "\n- %v"
const mapItemFormat = "\n- %v: %v"
......
......@@ -61,11 +61,12 @@ func TestFormatCustomText(t *testing.T) {
anyEc2Resp, _ := anypb.New(&ec2v1.ResizeAutoscalingGroupResponse{})
anyK8sDescribeReq, _ := anypb.New(&k8sapiv1.DescribePodRequest{Name: "foo"})
anyK8sDescribeResp, _ := anypb.New(&k8sapiv1.DescribePodResponse{Pod: &k8sapiv1.Pod{PodIp: "000"}})
anyK8sDescribeResp, _ := anypb.New(&k8sapiv1.DescribePodResponse{Pod: &k8sapiv1.Pod{PodIp: "000", Labels: map[string]string{}}})
k8sUpdateReq := &k8sapiv1.UpdatePodRequest{
ExpectedObjectMetaFields: &k8sapiv1.ExpectedObjectMetaFields{
Annotations: map[string]*k8sapiv1.NullableString{
"baz": &k8sapiv1.NullableString{Kind: &k8sapiv1.NullableString_Null{}},
"foo": &k8sapiv1.NullableString{Kind: &k8sapiv1.NullableString_Value{Value: "new-value"}},
},
},
......@@ -100,6 +101,15 @@ func TestFormatCustomText(t *testing.T) {
},
expectedOutput: "foo ip address is 000",
},
// metdata (labels) value is nil
{
text: "{{.Request.name}} labels: {{slackList .Response.pod.labels}}",
event: &auditv1.RequestEvent{
RequestMetadata: &auditv1.RequestMetadata{Body: anyK8sDescribeReq},
ResponseMetadata: &auditv1.ResponseMetadata{Body: anyK8sDescribeResp},
},
expectedOutput: "foo labels: None",
},
// metadata that is a map, uses helper slackList
{
text: "*Updated labels*:{{slackList .Request.objectMetaFields.labels}}",
......@@ -126,7 +136,7 @@ func TestFormatCustomText(t *testing.T) {
RequestMetadata: &auditv1.RequestMetadata{Body: anyK8sUpdateReq},
ResponseMetadata: &auditv1.ResponseMetadata{Body: anyK8UpdateResp},
},
expectedOutput: "*Expected Preconditions*:\n- foo: new-value",
expectedOutput: "*Expected Preconditions*:\n- baz: null\n- foo: new-value",
},
// invalid field name
{
......@@ -184,6 +194,10 @@ func TestSlackList(t *testing.T) {
input: []string{},
expectedOutput: "None",
},
{
input: nil,
expectedOutput: "None",
},
}
for _, test := range testCases {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment