Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions common-lib/pubsub-lib/audit/AuditLogEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type EnrichmentEntity struct {
Identifier string `json:"identifier"`
}

// AdditionalInfoEntry is a typed extra-info bag; publish may emit an empty marker that enrichment fills.
type AdditionalInfoEntry struct {
Type AuditInfoType `json:"type"`
Data map[string]interface{} `json:"data,omitempty"`
}

// AuditLogEvent is the canonical NATS payload published on AUDIT_LOG_TOPIC.
// It is shared between the publisher (orchestrator) and the consumer
// (audit-log service) so both agree on the contract.
Expand All @@ -61,6 +67,8 @@ type AuditLogEvent struct {
ResponseTime time.Duration `json:"responseTime"` // request handling duration (nanoseconds)
Payload map[string]interface{} `json:"payload"` // the actual request body of the audited call, always present
EnrichmentContext map[string]EnrichmentEntity `json:"enrichmentContext,omitempty"`
Role string `json:"role,omitempty"` // display name of the acting user's role scoped to the resource acted upon (e.g. "Super Admin", "Manager")
AdditionalInfo []AdditionalInfoEntry `json:"additionalInfo,omitempty"` // typed extra info (e.g. config diff coordinates), filled at enrich time
}

// NewAuditLogEvent constructs an event with the structural metadata parsed
Expand Down Expand Up @@ -114,6 +122,13 @@ func (e *AuditLogEvent) WithAction(action string) *AuditLogEvent {
return e
}

// WithRole sets the display name of the acting user's role scoped to the resource
// acted upon (e.g. "Super Admin", "Manager").
func (e *AuditLogEvent) WithRole(role string) *AuditLogEvent {
e.Role = role
return e
}

// WithPayloadField adds a single key/value to the payload bag.
func (e *AuditLogEvent) WithPayloadField(key string, value interface{}) *AuditLogEvent {
if e.Payload == nil {
Expand All @@ -137,6 +152,12 @@ func (e *AuditLogEvent) WithEnrichment(entity, identifier string) *AuditLogEvent
return e
}

// WithAdditionalInfo appends a typed extra-info entry (publish stamps an empty marker; enrichment fills it).
func (e *AuditLogEvent) WithAdditionalInfo(infoType AuditInfoType, data map[string]interface{}) *AuditLogEvent {
e.AdditionalInfo = append(e.AdditionalInfo, AdditionalInfoEntry{Type: infoType, Data: data})
return e
}

// Marshal serializes the event to a JSON string for PubSubClientService.Publish.
func (e *AuditLogEvent) Marshal() (string, error) {
data, err := json.Marshal(e)
Expand Down
12 changes: 12 additions & 0 deletions common-lib/pubsub-lib/audit/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//go:generate go run github.com/devtron-labs/common-lib/tools/strenum -type=AuditModule
//go:generate go run github.com/devtron-labs/common-lib/tools/strenum -type=AuditResource
//go:generate go run github.com/devtron-labs/common-lib/tools/strenum -type=AuditAction
//go:generate go run github.com/devtron-labs/common-lib/tools/strenum -type=AuditInfoType
package audit

// AuditModule is the top-level functional area an audited action belongs to.
Expand Down Expand Up @@ -134,6 +135,17 @@ const (
ActionApply AuditAction = "Apply"
)

// AuditInfoType names a kind of typed extra info carried in
// AuditLogEvent.AdditionalInfo. Values are human-readable.
type AuditInfoType string

func (t AuditInfoType) ToString() string { return string(t) }

const (
// AuditInfoConfigDiff carries config before/after history-version coordinates.
AuditInfoConfigDiff AuditInfoType = "Config Diff"
)

// Enrichment entity keys used in AuditLogEvent.EnrichmentContext. These name
// the entity whose identifier the audit-log service resolves into a full
// record (e.g. "app" -> app_id -> application name/team/etc).
Expand Down
Loading