@@ -14,20 +14,21 @@ import (
1414
1515var 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
3334type EventEnvelope struct {
@@ -36,8 +37,8 @@ type EventEnvelope struct {
3637}
3738
3839type EventJson struct {
39- Destination string
40- Event * EventEnvelope
40+ Destination string `json:"destination,omitempty"`
41+ Event * EventEnvelope `json:"event"`
4142}
4243
4344func 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 ))
0 commit comments