Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Clutch
Commits
e537bfa1
Commit
e537bfa1
authored
4 years ago
by
Scarlett Perry
Browse files
Options
Download
Email Patches
Plain Diff
check for nil
parent
872c638a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/gateway/config.go
+1
-1
backend/gateway/config.go
backend/service/auditsink/slack/slack.go
+9
-1
backend/service/auditsink/slack/slack.go
backend/service/auditsink/slack/slack_test.go
+16
-2
backend/service/auditsink/slack/slack_test.go
with
26 additions
and
4 deletions
+26
-4
backend/gateway/config.go
+
1
-
1
View file @
e537bfa1
...
...
@@ -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 {{ }}
*/
...
...
This diff is collapsed.
Click to expand it.
backend/service/auditsink/slack/slack.go
+
9
-
1
View file @
e537bfa1
...
...
@@ -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"
...
...
This diff is collapsed.
Click to expand it.
backend/service/auditsink/slack/slack_test.go
+
16
-
2
View file @
e537bfa1
...
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment