mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-08 00:57:24 +00:00
106 lines
3 KiB
Go
106 lines
3 KiB
Go
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(
|
|
ctx context.Context,
|
|
event *auditV1.AuditLogEntry,
|
|
visibility auditV1.Visibility,
|
|
routableIdentifier *RoutableIdentifier,
|
|
) error {
|
|
|
|
return a.LogWithTrace(ctx, event, visibility, routableIdentifier, nil, nil)
|
|
}
|
|
|
|
// LogWithTrace implements AuditApi.LogWithTrace.
|
|
// Validates and serializes the event but doesn't send it.
|
|
func (a *MockAuditApi) LogWithTrace(
|
|
_ context.Context,
|
|
event *auditV1.AuditLogEntry,
|
|
visibility auditV1.Visibility,
|
|
routableIdentifier *RoutableIdentifier,
|
|
traceParent *string,
|
|
traceState *string,
|
|
) error {
|
|
|
|
_, err := a.ValidateAndSerializeWithTrace(event, visibility, routableIdentifier, traceParent, traceState)
|
|
return err
|
|
}
|
|
|
|
// ValidateAndSerialize implements AuditApi.ValidateAndSerialize
|
|
func (a *MockAuditApi) ValidateAndSerialize(
|
|
event *auditV1.AuditLogEntry,
|
|
visibility auditV1.Visibility,
|
|
routableIdentifier *RoutableIdentifier,
|
|
) (*CloudEvent, error) {
|
|
|
|
return a.ValidateAndSerializeWithTrace(event, visibility, routableIdentifier, nil, nil)
|
|
}
|
|
|
|
// ValidateAndSerializeWithTrace implements AuditApi.ValidateAndSerializeWithTrace
|
|
func (a *MockAuditApi) ValidateAndSerializeWithTrace(
|
|
event *auditV1.AuditLogEntry,
|
|
visibility auditV1.Visibility,
|
|
routableIdentifier *RoutableIdentifier,
|
|
traceParent *string,
|
|
traceState *string,
|
|
) (*CloudEvent, error) {
|
|
|
|
routableEvent, err := validateAndSerializePartially(a.validator, event, visibility, routableIdentifier)
|
|
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,
|
|
traceParent: traceParent,
|
|
traceState: traceState,
|
|
}
|
|
|
|
return &message, nil
|
|
}
|
|
|
|
// Send implements AuditApi.Send
|
|
func (a *MockAuditApi) Send(context.Context, *RoutableIdentifier, *CloudEvent) error {
|
|
return nil
|
|
}
|