mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-09 01:27:26 +00:00
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-core-platform/common-audit.git/gen/go/audit/v1"
|
|
|
|
"github.com/bufbuild/protovalidate-go"
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
func main() {
|
|
agent := "aaa"
|
|
parameters, _ := structpb.NewValue(map[string]any{
|
|
"parameter": "b",
|
|
})
|
|
body, _ := structpb.NewValue(map[string]any{
|
|
"body": "b",
|
|
})
|
|
auditEvent := auditV1.AuditEvent{
|
|
EventName: "XXX",
|
|
EventTrigger: auditV1.EventTrigger_EVENT_TRIGGER_REQUEST,
|
|
Request: &auditV1.RequestDetails{
|
|
Endpoint: "XXX",
|
|
SourceIpAddress: "127.0.0.1",
|
|
UserAgent: &agent,
|
|
Parameters: parameters.GetStructValue(),
|
|
Body: body.GetStructValue(),
|
|
Headers: []*auditV1.RequestHeader{
|
|
{
|
|
Key: "abc",
|
|
Value: "def",
|
|
},
|
|
},
|
|
},
|
|
EventTimeStamp: nil,
|
|
Initiator: nil,
|
|
Principals: nil,
|
|
ResourceId: nil,
|
|
ResourceName: nil,
|
|
CorrelationId: nil,
|
|
Details: nil,
|
|
}
|
|
|
|
auditEventBytes, err := proto.Marshal(&auditEvent)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return
|
|
}
|
|
payload := auditV1.UnencryptedData{
|
|
Data: auditEventBytes,
|
|
ProtobufType: fmt.Sprintf("%v", auditEvent.ProtoReflect().Descriptor().FullName()),
|
|
}
|
|
|
|
//var version int32 = 0
|
|
routableEvent := auditV1.RoutableAuditEvent{
|
|
EventName: "A_V1",
|
|
Visibility: auditV1.Visibility_VISIBILITY_PRIVATE,
|
|
ResourceReference: &auditV1.RoutableAuditEvent_ObjectName{ObjectName: auditV1.ObjectName_OBJECT_NAME_SYSTEM},
|
|
Data: &auditV1.RoutableAuditEvent_UnencryptedData{UnencryptedData: &payload},
|
|
}
|
|
|
|
validator, err := protovalidate.New()
|
|
if err != nil {
|
|
fmt.Println("failed to initialize validator:", err)
|
|
}
|
|
err = validator.Validate(&auditEvent)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
|
|
err = validator.Validate(&routableEvent)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
|
|
routableEventBytes, err := proto.Marshal(&routableEvent)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return
|
|
}
|
|
|
|
var deserializedRoutableEvent auditV1.RoutableAuditEvent
|
|
err = proto.Unmarshal(routableEventBytes, &deserializedRoutableEvent)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return
|
|
}
|
|
if proto.Equal(&routableEvent, &deserializedRoutableEvent) {
|
|
fmt.Println("Event Matched")
|
|
} else {
|
|
fmt.Println("Event Not Matched")
|
|
}
|
|
//fmt.Println(deserializedRoutableEvent.String())
|
|
|
|
protoMessage := getProto(deserializedRoutableEvent.GetUnencryptedData().ProtobufType)
|
|
err = proto.Unmarshal(deserializedRoutableEvent.GetUnencryptedData().Data, protoMessage)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return
|
|
}
|
|
deserializedAuditEvent, isAuditEvent := protoMessage.(*auditV1.AuditEvent)
|
|
if isAuditEvent {
|
|
fmt.Println(deserializedAuditEvent.String())
|
|
}
|
|
|
|
fmt.Println(string(auditEventBytes))
|
|
}
|
|
|
|
func getProto(dataType string) protoreflect.ProtoMessage {
|
|
t, _ := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(dataType))
|
|
m := t.New().Interface()
|
|
return m
|
|
}
|