Skip to content

Commit 186984c

Browse files
authored
chore(nats): Send audit logs events to specific subjects (#2300)
Signed-off-by: Javier Rodriguez <[email protected]>
1 parent aa02aeb commit 186984c

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

app/controlplane/pkg/auditor/events/organization.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (p *OrgBase) TargetID() *uuid.UUID {
5757

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

6363
return json.Marshal(&p)
@@ -79,6 +79,10 @@ func (p *OrgCreated) Description() string {
7979
// user joined the organization
8080
type OrgUserJoined struct {
8181
*OrgBase
82+
// UserID of the user that joined the organization
83+
UserID uuid.UUID `json:"user_id,omitempty"`
84+
// UserEmail of the user that joined the organization
85+
UserEmail string `json:"user_email,omitempty"`
8286
}
8387

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

96+
func (p *OrgUserJoined) ActionInfo() (json.RawMessage, error) {
97+
if p.OrgName == "" || p.OrgID == nil || p.UserID == uuid.Nil || p.UserEmail == "" {
98+
return nil, errors.New("org name, org id, user id and user email are required")
99+
}
100+
101+
return json.Marshal(&p)
102+
}
103+
92104
// user left the organization
93105
type OrgUserLeft struct {
94106
*OrgBase

app/controlplane/pkg/auditor/nats.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22+
"strings"
2223
"time"
2324

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

2930
const (
30-
streamName = "chainloop-audit"
31+
streamName = "chainloop-audit"
32+
// subjectName is the base subject for the stream to listen to.
3133
subjectName = "audit.>"
34+
// baseSubjectName is the base subject for audit logs for the publisher to publish to.
35+
// The pattern for the specific subjects is "audit.<target_type>.<action_type>"
36+
baseSubjectName = "audit"
3237
)
3338

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

77-
return n.conn.Publish(subjectName, jsonPayload)
82+
// Send the event to the specific subject based on the event type "audit.<target_type>.<action_type>"
83+
specificSubject := fmt.Sprintf("%s.%s.%s", baseSubjectName, strings.ToLower(string(data.Data.TargetType)), strings.ToLower(data.Data.ActionType))
84+
return n.conn.Publish(specificSubject, jsonPayload)
7885
}

app/controlplane/pkg/biz/orginvitation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ func (uc *OrgInvitationUseCase) AcceptPendingInvitations(ctx context.Context, re
290290
OrgID: &orgUUID,
291291
OrgName: invitation.Org.Name,
292292
},
293+
UserID: userUUID,
294+
UserEmail: user.Email,
293295
}, &orgUUID)
294296
}
295297

0 commit comments

Comments
 (0)