package api import ( "context" "fmt" "github.com/google/uuid" "google.golang.org/protobuf/proto" auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-core-platform/common-audit.git/gen/go/audit/v1" "github.com/bufbuild/protovalidate-go" ) // MockAuditApi is an implementation of AuditApi that does nothing and has no dependency to external systems. type MockAuditApi struct { validator *ProtobufValidator } func NewMockAuditApi() (*AuditApi, error) { validator, err := protovalidate.New() if err != nil { return nil, err } var protobufValidator ProtobufValidator = validator var auditApi AuditApi = &MockAuditApi{validator: &protobufValidator} return &auditApi, nil } // Log implements AuditApi.Log. // Validates and serializes the event but doesn't send it. func (a *MockAuditApi) Log( _ context.Context, event *auditV1.AuditLogEntry, visibility auditV1.Visibility, routingIdentifier *RoutingIdentifier, objectIdentifier *auditV1.ObjectIdentifier, ) error { _, err := a.ValidateAndSerialize(event, visibility, routingIdentifier, objectIdentifier) return err } // ValidateAndSerialize implements AuditApi.ValidateAndSerialize func (a *MockAuditApi) ValidateAndSerialize( event *auditV1.AuditLogEntry, visibility auditV1.Visibility, routingIdentifier *RoutingIdentifier, objectIdentifier *auditV1.ObjectIdentifier, ) (*CloudEvent, error) { routableEvent, err := validateAndSerializePartially(a.validator, event, visibility, routingIdentifier, objectIdentifier) if err != nil { return nil, err } routableEventBytes, err := proto.Marshal(routableEvent) if err != nil { return nil, err } message := CloudEvent{ specVersion: "1.0", source: event.ProtoPayload.ServiceName, // TODO what is the correct id? id: uuid.NewString(), time: event.ProtoPayload.RequestMetadata.RequestAttributes.Time.AsTime(), dataContentType: "application/cloudevents+protobuf", dataType: fmt.Sprintf("%v", routableEvent.ProtoReflect().Descriptor().FullName()), // TODO check if this is correct subject: event.ProtoPayload.ResourceName, data: routableEventBytes, } return &message, nil } // Send implements AuditApi.Send func (a *MockAuditApi) Send(context.Context, *RoutingIdentifier, *CloudEvent) error { return nil }