mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-08 00:57:24 +00:00
97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-core-platform/audit-go.git/gen/go/audit/v1"
|
|
"encoding/json"
|
|
"errors"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
// LogEvent logs an event to the terminal
|
|
func LogEvent(event *CloudEvent) error {
|
|
|
|
if event.DataType != "audit.v1.RoutableAuditEvent" {
|
|
return errors.New("Unsupported data type " + event.DataType)
|
|
}
|
|
|
|
var routableAuditEvent *auditV1.RoutableAuditEvent
|
|
err := proto.Unmarshal(event.Data, routableAuditEvent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var auditEvent *auditV1.AuditLogEntry
|
|
err = proto.Unmarshal(routableAuditEvent.GetUnencryptedData().Data, auditEvent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Convert to json
|
|
auditEventJson, err := protojson.Marshal(auditEvent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
auditEventMap := make(map[string]interface{})
|
|
err = json.Unmarshal(auditEventJson, &auditEventMap)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
objectIdentifierJson, err := protojson.Marshal(routableAuditEvent.ObjectIdentifier)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
objectIdentifierMap := make(map[string]interface{})
|
|
err = json.Unmarshal(objectIdentifierJson, &objectIdentifierMap)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cloudEvent := cloudEvent{
|
|
SpecVersion: event.SpecVersion,
|
|
Source: event.Source,
|
|
Id: event.Id,
|
|
Time: event.Time,
|
|
DataContentType: event.DataContentType,
|
|
DataType: event.DataType,
|
|
Subject: event.Subject,
|
|
Data: routableEvent{
|
|
OperationName: auditEvent.ProtoPayload.OperationName,
|
|
Visibility: routableAuditEvent.Visibility.String(),
|
|
ResourceReference: objectIdentifierMap,
|
|
Data: auditEventMap,
|
|
},
|
|
TraceParent: event.TraceParent,
|
|
TraceState: event.TraceState,
|
|
}
|
|
cloudEventJson, err := json.Marshal(cloudEvent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
slog.Info(string(cloudEventJson))
|
|
return nil
|
|
}
|
|
|
|
type cloudEvent struct {
|
|
SpecVersion string
|
|
Source string
|
|
Id string
|
|
Time time.Time
|
|
DataContentType string
|
|
DataType string
|
|
Subject string
|
|
Data routableEvent
|
|
TraceParent *string
|
|
TraceState *string
|
|
}
|
|
|
|
type routableEvent struct {
|
|
OperationName string
|
|
Visibility string
|
|
ResourceReference map[string]interface{}
|
|
Data map[string]interface{}
|
|
}
|