From 7524953b5ad3db443df2a82bfc4e97225bd7ec34 Mon Sep 17 00:00:00 2001 From: Bhupesh Varshney Date: Wed, 9 Sep 2026 14:03:05 +0530 Subject: [PATCH] feat: diff + user role --- common-lib/pubsub-lib/audit/AuditLogEvent.go | 21 ++++++++++++++++++++ common-lib/pubsub-lib/audit/constants.go | 12 +++++++++++ 2 files changed, 33 insertions(+) diff --git a/common-lib/pubsub-lib/audit/AuditLogEvent.go b/common-lib/pubsub-lib/audit/AuditLogEvent.go index 9295cddb1..ad8f8433a 100644 --- a/common-lib/pubsub-lib/audit/AuditLogEvent.go +++ b/common-lib/pubsub-lib/audit/AuditLogEvent.go @@ -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. @@ -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 @@ -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 { @@ -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) diff --git a/common-lib/pubsub-lib/audit/constants.go b/common-lib/pubsub-lib/audit/constants.go index 1b9b49a8d..268069966 100644 --- a/common-lib/pubsub-lib/audit/constants.go +++ b/common-lib/pubsub-lib/audit/constants.go @@ -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. @@ -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).