mirror of
https://dev.azure.com/schwarzit/schwarzit.stackit-public/_git/audit-go
synced 2026-02-08 00:57:24 +00:00
98 lines
3 KiB
Go
98 lines
3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"buf.build/go/protovalidate"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/gen/go/audit/v1"
|
|
internalApi "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/internal/audit/api"
|
|
pkgAuditCommon "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/pkg/audit/common"
|
|
)
|
|
|
|
// MockAuditApi is an implementation of AuditApi that does nothing and has no dependency to external systems.
|
|
type MockAuditApi struct {
|
|
tracer trace.Tracer
|
|
validator pkgAuditCommon.ProtobufValidator
|
|
}
|
|
|
|
func NewMockAuditApi() (pkgAuditCommon.AuditApi, error) {
|
|
validator, err := protovalidate.New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var protobufValidator pkgAuditCommon.ProtobufValidator = validator
|
|
var auditApi pkgAuditCommon.AuditApi = &MockAuditApi{
|
|
tracer: otel.Tracer("mock-audit-api"),
|
|
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 *pkgAuditCommon.RoutableIdentifier,
|
|
) error {
|
|
|
|
_, err := a.ValidateAndSerialize(ctx, event, visibility, routableIdentifier)
|
|
return err
|
|
}
|
|
|
|
// ValidateAndSerialize implements AuditApi.ValidateAndSerialize
|
|
func (a *MockAuditApi) ValidateAndSerialize(
|
|
ctx context.Context,
|
|
event *auditV1.AuditLogEntry,
|
|
visibility auditV1.Visibility,
|
|
routableIdentifier *pkgAuditCommon.RoutableIdentifier,
|
|
) (*pkgAuditCommon.CloudEvent, error) {
|
|
|
|
ctx, span := a.tracer.Start(ctx, "validate-and-serialize")
|
|
defer span.End()
|
|
|
|
routableEvent, err := internalApi.ValidateAndSerializePartially(a.validator, event, visibility, routableIdentifier)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Reject event type data-access as the downstream services
|
|
// cannot handle it at the moment
|
|
if strings.HasSuffix(event.LogName, string(pkgAuditCommon.EventTypeDataAccess)) {
|
|
return nil, pkgAuditCommon.ErrUnsupportedEventTypeDataAccess
|
|
}
|
|
|
|
routableEventBytes, err := proto.Marshal(routableEvent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
traceParent, traceState := internalApi.TraceParentAndStateFromContext(ctx)
|
|
|
|
message := pkgAuditCommon.CloudEvent{
|
|
SpecVersion: "1.0",
|
|
Source: event.ProtoPayload.ServiceName,
|
|
Id: event.InsertId,
|
|
Time: event.ProtoPayload.RequestMetadata.RequestAttributes.Time.AsTime(),
|
|
DataContentType: "application/cloudevents+protobuf",
|
|
DataType: fmt.Sprintf("%v", routableEvent.ProtoReflect().Descriptor().FullName()),
|
|
Subject: event.ProtoPayload.ResourceName,
|
|
Data: routableEventBytes,
|
|
TraceParent: &traceParent,
|
|
TraceState: &traceState,
|
|
}
|
|
|
|
return &message, nil
|
|
}
|
|
|
|
// Send implements AuditApi.Send
|
|
func (a *MockAuditApi) Send(context.Context, *pkgAuditCommon.RoutableIdentifier, *pkgAuditCommon.CloudEvent) error {
|
|
return nil
|
|
}
|