Skip to content

Commit acfe904

Browse files
committed
format audit events on create, not on print
1 parent d41d5cd commit acfe904

File tree

2 files changed

+70
-100
lines changed

2 files changed

+70
-100
lines changed

internal/audit/audit_event.go

Lines changed: 44 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ import (
1414

1515
var EventsDestination string
1616

17-
type Event struct { //flat event struct for everything
18-
ID string
19-
IdempotencyKey string
20-
Action Action
21-
Component string
22-
MethodName string
23-
ContainerID string
24-
Subject string
25-
SanitizedToken string
26-
Resource Resource
27-
GRPCRequest proto.Message
28-
Status string
29-
Reason string
30-
Timestamp time.Time
17+
type GenericAuditFields struct {
18+
ID string `json:"request_id"`
19+
IdempotencyKey string `json:"idempotency_key"`
20+
Service string `json:"service"`
21+
SpecVersion string `json:"specversion"`
22+
Action Action `json:"action"`
23+
Resource Resource `json:"resource"`
24+
Component string `json:"component"`
25+
FolderID string `json:"folder_id"`
26+
Subject string `json:"subject"`
27+
SanitizedToken string `json:"sanitized_token"`
28+
Status string `json:"status"`
29+
Reason string `json:"reason,omitempty"`
30+
Timestamp string `json:"@timestamp"`
31+
IsBackground bool `json:"is_background"`
3132
}
3233

3334
type EventEnvelope struct {
@@ -36,8 +37,8 @@ type EventEnvelope struct {
3637
}
3738

3839
type EventJson struct {
39-
Destination string
40-
Event *EventEnvelope
40+
Destination string `json:"destination,omitempty"`
41+
Event *EventEnvelope `json:"event"`
4142
}
4243

4344
func marshalProtoMessage(msg proto.Message) json.RawMessage {
@@ -54,59 +55,15 @@ func marshalProtoMessage(msg proto.Message) json.RawMessage {
5455
return b
5556
}
5657

57-
func (e *Event) MarshalJSON() ([]byte, error) {
58-
return json.Marshal(
59-
&struct {
60-
ID string `json:"request_id"`
61-
IdempotencyKey string `json:"idempotency_key"`
62-
Service string `json:"service"`
63-
SpecVersion string `json:"specversion"`
64-
Action string `json:"action"`
65-
Resource Resource `json:"resource"`
66-
Component string `json:"component"`
67-
MethodName string `json:"operation,omitempty"`
68-
ContainerID string `json:"folder_id"`
69-
Subject string `json:"subject"`
70-
SanitizedToken string `json:"sanitized_token"`
71-
GRPCRequest json.RawMessage `json:"grpc_request,omitempty"`
72-
Status string `json:"status"`
73-
Reason string `json:"reason,omitempty"`
74-
Timestamp string `json:"@timestamp"`
75-
IsBackground bool `json:"is_background"`
76-
}{
77-
ID: e.ID,
78-
IdempotencyKey: e.IdempotencyKey,
79-
Service: "ydbcp",
80-
SpecVersion: "1.0",
81-
Action: string(e.Action),
82-
Resource: e.Resource,
83-
Component: e.Component,
84-
MethodName: e.MethodName,
85-
ContainerID: e.ContainerID,
86-
Subject: formatSubject(e.Subject),
87-
SanitizedToken: e.SanitizedToken,
88-
GRPCRequest: marshalProtoMessage(e.GRPCRequest),
89-
Status: e.Status,
90-
Reason: e.Reason,
91-
Timestamp: e.Timestamp.Format(time.RFC3339Nano),
92-
},
93-
)
94-
}
58+
type GRPCCallEvent struct {
59+
GenericAuditFields
9560

96-
func (ej *EventJson) MarshalJSON() ([]byte, error) {
97-
return json.Marshal(
98-
&struct {
99-
Destination string `json:"destination,omitempty"`
100-
Event *EventEnvelope `json:"event"`
101-
}{
102-
Destination: ej.Destination,
103-
Event: ej.Event,
104-
},
105-
)
61+
MethodName string `json:"operation"`
62+
GRPCRequest json.RawMessage `json:"grpc_request"`
10663
}
10764

108-
func makeEnvelope(event *Event) (*EventEnvelope, error) {
109-
data, err := event.MarshalJSON()
65+
func makeEnvelope(event any) (*EventEnvelope, error) {
66+
data, err := json.Marshal(event)
11067
if err != nil {
11168
return nil, err
11269
}
@@ -147,22 +104,27 @@ func GRPCCallAuditEvent(
147104
containerID string,
148105
inProgress bool,
149106
err error,
150-
) *Event {
107+
) *GRPCCallEvent {
151108
s, r := getStatus(inProgress, err)
152-
return &Event{
153-
ID: uuid.New().String(),
154-
IdempotencyKey: grpcinfo.GetRequestID(ctx),
155-
Component: "grpc_api",
156-
MethodName: methodName,
157-
GRPCRequest: req,
158-
ContainerID: containerID,
159-
Subject: subject,
160-
SanitizedToken: token,
161-
Action: ActionFromMethodName(ctx, methodName),
162-
Resource: ResourceFromMethodName(ctx, methodName),
163-
Status: s,
164-
Reason: r,
165-
Timestamp: time.Now(),
109+
return &GRPCCallEvent{
110+
GenericAuditFields: GenericAuditFields{
111+
ID: uuid.New().String(),
112+
IdempotencyKey: grpcinfo.GetRequestID(ctx),
113+
Service: "ydbcp",
114+
SpecVersion: "1.0",
115+
Action: ActionFromMethodName(ctx, methodName),
116+
Resource: ResourceFromMethodName(ctx, methodName),
117+
Component: "grpc_api",
118+
FolderID: containerID,
119+
Subject: formatSubject(subject),
120+
SanitizedToken: token,
121+
Status: s,
122+
Reason: r,
123+
Timestamp: time.Now().Format(time.RFC3339Nano),
124+
IsBackground: false,
125+
},
126+
MethodName: methodName,
127+
GRPCRequest: marshalProtoMessage(req),
166128
}
167129
}
168130

@@ -186,7 +148,7 @@ func ReportGRPCCallEnd(
186148
ReportAuditEvent(ctx, event)
187149
}
188150

189-
func ReportAuditEvent(ctx context.Context, event *Event) {
151+
func ReportAuditEvent(ctx context.Context, event *GRPCCallEvent) {
190152
env, err := makeEnvelope(event)
191153
if err != nil {
192154
xlog.Error(ctx, "error reporting audit event", zap.Error(err))

internal/audit/audit_event_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,46 @@ func TestGRPCCallAuditEvent(t *testing.T) {
4343
)
4444

4545
assert.Equal(t, "grpc_api", event.Component)
46-
assert.Equal(t, "subj", event.Subject)
47-
assert.Equal(t, "cid1", event.ContainerID)
46+
assert.Equal(t, "subj@as", event.Subject)
47+
assert.Equal(t, "cid1", event.FolderID)
4848
assert.Equal(t, pb.BackupScheduleService_GetBackupSchedule_FullMethodName, event.MethodName)
4949
assert.Equal(t, "ERROR", event.Status)
50-
assert.Equal(t, msg, event.GRPCRequest)
50+
assert.Equal(t, marshalProtoMessage(msg), event.GRPCRequest)
5151
}
5252

5353
func TestEventMarshalJSON(t *testing.T) {
54-
event := &Event{
55-
Resource: "resource",
56-
Action: ActionGet,
57-
Component: "grpc_api",
58-
MethodName: "Method",
59-
Subject: "sub",
60-
SanitizedToken: "tok",
61-
GRPCRequest: &pb.ListBackupsRequest{
62-
ContainerId: "id1",
54+
event := &GRPCCallEvent{
55+
GenericAuditFields: GenericAuditFields{
56+
Resource: "resource",
57+
Action: ActionGet,
58+
Component: "grpc_api",
59+
Subject: "sub@as",
60+
FolderID: "id1",
61+
SanitizedToken: "tok",
62+
Status: "SUCCESS",
63+
Timestamp: time.Now().Format(time.RFC3339Nano),
6364
},
64-
Status: "SUCCESS",
65-
Timestamp: time.Now(),
65+
GRPCRequest: marshalProtoMessage(
66+
&pb.ListBackupsRequest{
67+
ContainerId: "id1",
68+
},
69+
),
70+
MethodName: "Method",
6671
}
6772

6873
data, err := json.Marshal(event)
6974
assert.NoError(t, err)
7075
assert.Contains(t, string(data), `"resource":"resource"`)
7176
assert.Contains(t, string(data), `"component":"grpc_api"`)
7277
assert.Contains(t, string(data), `"subject":"sub@as`)
73-
assert.Contains(t, string(data), `"container_id":"id1"`)
78+
assert.Contains(t, string(data), `"folder_id":"id1"`)
79+
assert.Contains(t, string(data), `"operation":"Method"`)
7480
assert.Contains(t, string(data), `"status":`)
7581
assert.Contains(t, string(data), `"@timestamp":`)
7682
}
7783

7884
func TestEventJsonMarshal(t *testing.T) {
79-
event, err := makeEnvelope(&Event{Component: "test"})
85+
event, err := makeEnvelope(&GRPCCallEvent{GenericAuditFields: GenericAuditFields{Component: "test"}})
8086
require.NoError(t, err)
8187
ej := &EventJson{
8288
Destination: "stdout",
@@ -94,9 +100,11 @@ func TestEventJsonMarshal(t *testing.T) {
94100

95101
func TestReportAuditEvent(t *testing.T) {
96102
ctx := context.Background()
97-
event := &Event{
103+
event := &GRPCCallEvent{
98104
MethodName: "reportTest",
99-
Status: "SUCCESS",
105+
GenericAuditFields: GenericAuditFields{
106+
Status: "SUCCESS",
107+
},
100108
}
101109

102110
oldStream := os.Stdout

0 commit comments

Comments
 (0)