Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/observe: improve policy verdict output in compact mode #745

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,7 @@ func GetFlowType(f *pb.Flow) string {
case api.MessageTypeDrop:
return api.DropReason(uint8(f.GetEventType().GetSubType()))
case api.MessageTypePolicyVerdict:
switch f.GetVerdict() {
case pb.Verdict_FORWARDED, pb.Verdict_AUDIT, pb.Verdict_REDIRECTED:
return api.PolicyMatchType(f.GetPolicyMatchType()).String()
case pb.Verdict_DROPPED:
return api.DropReason(uint8(f.GetDropReason()))
case pb.Verdict_ERROR:
// ERROR should only happen for L7 events.
}
return api.MessageTypeNamePolicyVerdict + ":" + api.PolicyMatchType(f.GetPolicyMatchType()).String()
case api.MessageTypeCapture:
return f.GetDebugCapturePoint().String()
}
Expand All @@ -242,15 +235,25 @@ func GetFlowType(f *pb.Flow) string {

func (p Printer) getVerdict(f *pb.Flow) string {
verdict := f.GetVerdict()
msg := verdict.String()
switch verdict {
case pb.Verdict_FORWARDED, pb.Verdict_REDIRECTED:
return p.color.verdictForwarded(verdict.String())
if f.GetEventType().GetType() == api.MessageTypePolicyVerdict {
msg = "ALLOWED"
}
return p.color.verdictForwarded(msg)
case pb.Verdict_DROPPED, pb.Verdict_ERROR:
return p.color.verdictDropped(verdict.String())
if f.GetEventType().GetType() == api.MessageTypePolicyVerdict {
msg = "DENIED"
}
return p.color.verdictDropped(msg)
case pb.Verdict_AUDIT:
return p.color.verdictAudit(verdict.String())
if f.GetEventType().GetType() == api.MessageTypePolicyVerdict {
msg = "AUDITED"
}
return p.color.verdictAudit(msg)
default:
return verdict.String()
return msg
}
}

Expand Down
25 changes: 23 additions & 2 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func TestPrinter_WriteProtoFlow(t *testing.T) {
reply.IsReply = &wrapperspb.BoolValue{Value: true}
unknown := proto.Clone(&f).(*pb.Flow)
unknown.IsReply = nil
policyDenied := proto.Clone(&f).(*pb.Flow)
policyDenied.EventType = &pb.CiliumEventType{
Type: monitorAPI.MessageTypePolicyVerdict,
}
policyDenied.IsReply = nil
type args struct {
f *pb.Flow
}
Expand Down Expand Up @@ -153,6 +158,22 @@ Jan 1 00:20:34.567 k8s1 1.1.1.1:31793 2.2.2.2:8080 Policy denied DROP
"2.2.2.2:8080 (ID:12345) <- 1.1.1.1:31793 (health) " +
"Policy denied DROPPED (TCP Flags: SYN)\n",
},
{
name: "compact-policy-verdict-denied",
options: []Option{
Compact(),
WithColor("never"),
WithNodeName(),
Writer(&buf),
},
args: args{
f: policyDenied,
},
wantErr: false,
expected: "Jan 1 00:20:34.567 [k8s1]: " +
"1.1.1.1:31793 (health) <> 2.2.2.2:8080 (ID:12345) " +
"policy-verdict:none DENIED (TCP Flags: SYN)\n",
},
{
name: "compact-direction-unknown",
options: []Option{
Expand Down Expand Up @@ -631,7 +652,7 @@ func Test_getFlowType(t *testing.T) {
PolicyMatchType: monitorAPI.PolicyMatchL3L4,
},
},
want: "L3-L4",
want: "policy-verdict:L3-L4",
},
{
name: "L4",
Expand All @@ -644,7 +665,7 @@ func Test_getFlowType(t *testing.T) {
DropReason: 153,
},
},
want: "Error while correcting L3 checksum",
want: "policy-verdict:none",
},
{
name: "Debug Capture",
Expand Down