Skip to content

Commit 8859707

Browse files
committed
Changed format to json and added custom marshalling
1 parent 270948b commit 8859707

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

authz/audit/stdout_logger.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ import (
2828

2929
var grpcLogger = grpclog.Component("authz-audit")
3030

31+
type StdoutEvent struct {
32+
FullMethodName string `json:"fullMethodName"`
33+
Principal string `json:"principal"`
34+
PolicyName string `json:"policyName"`
35+
MatchedRule string `json:"matchedRule"`
36+
Authorized bool `json:"authorized"`
37+
//Timestamp is populated using time.Now() during StdoutLogger.Log call
38+
Timestamp string `json:"timestamp"`
39+
}
40+
3141
type StdOutLogger struct {
3242
}
3343

3444
func (logger *StdOutLogger) Log(event *Event) {
35-
jsonBytes, err := json.Marshal(event)
45+
jsonBytes, err := json.Marshal(convertEvent(event)) //internal structure mimicking event with annotations how to marshall to json
3646
if err != nil {
3747
grpcLogger.Errorf("failed to marshal AuditEvent data to JSON: %v", err)
3848
}
39-
message := fmt.Sprintf("[AuthZ Audit StdOutLogger] %s %v",
40-
time.Now().Format(time.RFC3339), string(jsonBytes))
41-
fmt.Println(message)
49+
//message := fmt.Sprintf("[AuthZ Audit StdOutLogger] %s %v",
50+
// time.Now().Format(time.RFC3339), string(jsonBytes))
51+
fmt.Println(string(jsonBytes)) // built in log.go
4252
}
4353

4454
const (
@@ -64,3 +74,14 @@ func (StdOutLoggerBuilder) ParseLoggerConfig(config json.RawMessage) (LoggerConf
6474
"StdOutLogger doesn't support custom configs", string(config))
6575
return &StdoutLoggerConfig{}, nil
6676
}
77+
78+
func convertEvent(event *Event) StdoutEvent {
79+
return StdoutEvent{
80+
FullMethodName: event.FullMethodName,
81+
Principal: event.Principal,
82+
PolicyName: event.PolicyName,
83+
MatchedRule: event.MatchedRule,
84+
Authorized: event.Authorized,
85+
Timestamp: time.Now().Format(time.RFC3339),
86+
}
87+
}

authz/audit/stdout_logger_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,31 @@ func TestStdOutLogger_Log(t *testing.T) {
5050
log.Fatal(err)
5151
}
5252

53-
expected := ` {"FullMethodName":"","Principal":"test principal","PolicyName":"test policy","MatchedRule":"","Authorized":false}`
54-
if buf.String() != ("[AuthZ Audit StdOutLogger] " + time.Now().Format(time.RFC3339) + expected + "\n") {
53+
expected := `{"fullMethodName":"","principal":"test principal","policyName":"test policy","matchedRule":"","authorized":false`
54+
if buf.String() != (expected + ",\"timestamp\":\"" + time.Now().Format(time.RFC3339) + "\"}\n") {
5555
t.Fatalf("unexpected error\nwant:%v\n got:%v", expected, buf.String())
5656
}
5757
os.Stdout = orig
5858
}
59+
60+
//func TestStdOutLogger_LogFullMethodName(t *testing.T) {
61+
// orig := os.Stdout
62+
// r, w, _ := os.Pipe()
63+
// os.Stdout = w
64+
//
65+
// event := &Event{PolicyName: "test policy", Principal: "test principal", FullMethodName: "/helloworld.Greeter/SayHello"}
66+
// auditLogger.Log(event)
67+
//
68+
// w.Close()
69+
// var buf bytes.Buffer
70+
// _, err := io.Copy(&buf, r)
71+
// if err != nil {
72+
// log.Fatal(err)
73+
// }
74+
//
75+
// expected := `{"fullMethodName":"","principal":"test principal","policyName":"test policy","matchedRule":"","authorized":false`
76+
// if buf.String() != (expected + ",\"timestamp\":\"" + time.Now().Format(time.RFC3339) + "\"}\n") {
77+
// t.Fatalf("unexpected error\nwant:%v\n got:%v", expected, buf.String())
78+
// }
79+
// os.Stdout = orig
80+
//}

0 commit comments

Comments
 (0)