Skip to content

chore(nats): Send audit logs events to specific subjects #2300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2025
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
14 changes: 13 additions & 1 deletion app/controlplane/pkg/auditor/events/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *OrgBase) TargetID() *uuid.UUID {

func (p *OrgBase) ActionInfo() (json.RawMessage, error) {
if p.OrgName == "" || p.OrgID == nil {
return nil, errors.New("user id and org name are required")
return nil, errors.New("org name and org id are required")
}

return json.Marshal(&p)
Expand All @@ -79,6 +79,10 @@ func (p *OrgCreated) Description() string {
// user joined the organization
type OrgUserJoined struct {
*OrgBase
// UserID of the user that joined the organization
UserID uuid.UUID `json:"user_id,omitempty"`
// UserEmail of the user that joined the organization
UserEmail string `json:"user_email,omitempty"`
}

func (p *OrgUserJoined) ActionType() string {
Expand All @@ -89,6 +93,14 @@ func (p *OrgUserJoined) Description() string {
return fmt.Sprintf("{{ .ActorEmail }} has joined the organization %s", p.OrgName)
}

func (p *OrgUserJoined) ActionInfo() (json.RawMessage, error) {
if p.OrgName == "" || p.OrgID == nil || p.UserID == uuid.Nil || p.UserEmail == "" {
return nil, errors.New("org name, org id, user id and user email are required")
}

return json.Marshal(&p)
}

// user left the organization
type OrgUserLeft struct {
*OrgBase
Expand Down
11 changes: 9 additions & 2 deletions app/controlplane/pkg/auditor/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/go-kratos/kratos/v2/log"
Expand All @@ -27,8 +28,12 @@ import (
)

const (
streamName = "chainloop-audit"
streamName = "chainloop-audit"
// subjectName is the base subject for the stream to listen to.
subjectName = "audit.>"
// baseSubjectName is the base subject for audit logs for the publisher to publish to.
// The pattern for the specific subjects is "audit.<target_type>.<action_type>"
baseSubjectName = "audit"
)

type AuditLogPublisher struct {
Expand Down Expand Up @@ -74,5 +79,7 @@ func (n *AuditLogPublisher) Publish(data *EventPayload) error {
return fmt.Errorf("failed to marshal event payload: %w", err)
}

return n.conn.Publish(subjectName, jsonPayload)
// Send the event to the specific subject based on the event type "audit.<target_type>.<action_type>"
specificSubject := fmt.Sprintf("%s.%s.%s", baseSubjectName, strings.ToLower(string(data.Data.TargetType)), strings.ToLower(data.Data.ActionType))
return n.conn.Publish(specificSubject, jsonPayload)
}
2 changes: 2 additions & 0 deletions app/controlplane/pkg/biz/orginvitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func (uc *OrgInvitationUseCase) AcceptPendingInvitations(ctx context.Context, re
OrgID: &orgUUID,
OrgName: invitation.Org.Name,
},
UserID: userUUID,
UserEmail: user.Email,
}, &orgUUID)
}

Expand Down
Loading