audit-go/api.go
2024-07-05 10:38:18 +02:00

83 lines
3.6 KiB
Go

package main
import (
auditV1 "audit-schema/gen/go/audit/v1"
"context"
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
)
// AuditApi is the interface to log audit events.
//
// It provides a Log method that can be used to validate and directly send events.
// If the transactional outbox patter should be used, the ValidateAndSerialize and Send methods
// can be called manually to decouple operations.
type AuditApi interface {
// Log is a convenience method that validates, serializes and sends data over the wire.
// If the transactional outbox patter should be used, the ValidateAndSerialize method
// and Send method can be called manually.
//
// Parameters:
// * ctx - the context object
// * event - the auditV1.AuditEvent
// * visibility - route the event only internally or to the customer (not evaluated in the legacy solution)
// * routingIdentifier - the identifier for the AMQP-Topic selection (optional)
// * auditV1.ObjectType_OBJECT_TYPE_ORGANIZATION
// * auditV1.ObjectType_OBJECT_TYPE_PROJECT
// * objectIdentifier - the identifier of the object (optional - if not folder must be identical to routingIdentifier)
// * auditV1.ObjectType_OBJECT_TYPE_ORGANIZATION
// * auditV1.ObjectType_OBJECT_TYPE_FOLDER
// * auditV1.ObjectType_OBJECT_TYPE_PROJECT
//
// It may return one of the following errors:
/*
- ErrEventNil - if event is nil
- ErrObjectIdentifierVisibilityMismatch - if object identifier and visibility are not in a valid state
- ErrRoutableIdentifierMissing - if routing identifier type and object identifier type do not match
- ErrRoutableIdentifierTypeMismatch - if routing identifier type and object identifier types are not compatible
- ErrUnsupportedObjectIdentifierType - if an unsupported object identifier type was provided
- ErrUnsupportedResourceReferenceType - if an unsupported resource reference type was provided
- protovalidate.ValidationError - if schema validation errors have been detected
- protobuf serialization errors - if the event couldn't be serialized
*/
Log(ctx context.Context, event *auditV1.AuditEvent, visibility auditV1.Visibility, routingIdentifier *RoutingIdentifier, objectIdentifier *auditV1.ObjectIdentifier) error
// ValidateAndSerialize validates and serializes the event into a byte representation.
// The result has to be sent explicitly by calling the Send method.
ValidateAndSerialize(event *auditV1.AuditEvent, visibility auditV1.Visibility, routingIdentifier *RoutingIdentifier, objectIdentifier *auditV1.ObjectIdentifier) (SerializedPayload, error)
// Send the serialized content as byte array to the audit log system.
Send(ctx context.Context, routingIdentifier *RoutingIdentifier, serializedPayload *SerializedPayload) error
}
// ProtobufValidator is an abstraction for validators.
// Concrete implementations are e.g. protovalidate.Validator
type ProtobufValidator interface {
Validate(msg proto.Message) error
}
// SerializedPayload is an abstraction for serialized content
type SerializedPayload interface {
// GetPayload returns the actual payload as byte array
GetPayload() []byte
// GetContentType returns the content type of the payload
GetContentType() string
}
// RoutingIdentifierType is an enumeration of allowed identifier types.
type RoutingIdentifierType int
// RoutingIdentifierType enumeration values.
const (
RoutingIdentifierTypeOrganization RoutingIdentifierType = 0
RoutingIdentifierTypeProject RoutingIdentifierType = 1
)
// RoutingIdentifier is a representation for identifiers of allowed types.
type RoutingIdentifier struct {
Identifier uuid.UUID
Type RoutingIdentifierType
}