Skip to content

Commit a3b6f3b

Browse files
committed
temp: additional events
1 parent 7034ff9 commit a3b6f3b

File tree

8 files changed

+138
-37
lines changed

8 files changed

+138
-37
lines changed

internal/eventlog/events.go

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,86 @@ import (
77
)
88

99
const (
10-
tailnetCreated = "ionscale.tailnet.created"
11-
tailnetDeleted = "ionscale.tailnet.deleted"
12-
nodeCreated = "ionscale.node.created"
10+
tailnetCreated = "ionscale.tailnet.create"
11+
tailnetIamUpdated = "ionscale.tailnet.iam.update"
12+
tailnetAclUpdated = "ionscale.tailnet.acl.update"
13+
tailnetDNSConfigUpdated = "ionscale.tailnet.dns_config.update"
14+
nodeCreated = "ionscale.node.create"
1315
)
1416

15-
func TailnetCreated(tailnet *domain.Tailnet, actor *domain.User) cloudevents.Event {
16-
data := &EventData{
17+
func TailnetCreated(tailnet *domain.Tailnet, actor ActorOpts) cloudevents.Event {
18+
data := &EventData[any]{
1719
Tailnet: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
1820
Target: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
19-
Actor: system,
21+
Actor: actor(),
2022
}
2123

22-
if actor != nil {
23-
data.Actor = Actor{ID: idToStr(actor.ID), Name: actor.Name}
24+
event := cloudevents.NewEvent()
25+
event.SetType(tailnetCreated)
26+
_ = event.SetData(cloudevents.ApplicationJSON, data)
27+
28+
return event
29+
}
30+
31+
func TailnetIAMUpdated(tailnet *domain.Tailnet, old *domain.IAMPolicy, actor ActorOpts) cloudevents.Event {
32+
data := &EventData[*domain.IAMPolicy]{
33+
Tailnet: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
34+
Target: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
35+
Actor: actor(),
36+
Attr: &Attr[*domain.IAMPolicy]{
37+
New: &tailnet.IAMPolicy,
38+
Old: old,
39+
},
2440
}
2541

2642
event := cloudevents.NewEvent()
27-
event.SetType(tailnetCreated)
43+
event.SetType(tailnetIamUpdated)
44+
_ = event.SetData(cloudevents.ApplicationJSON, data)
45+
46+
return event
47+
}
48+
49+
func TailnetACLUpdated(tailnet *domain.Tailnet, old *domain.ACLPolicy, actor ActorOpts) cloudevents.Event {
50+
data := &EventData[*domain.ACLPolicy]{
51+
Tailnet: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
52+
Target: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
53+
Actor: actor(),
54+
Attr: &Attr[*domain.ACLPolicy]{
55+
New: &tailnet.ACLPolicy,
56+
Old: old,
57+
},
58+
}
59+
60+
event := cloudevents.NewEvent()
61+
event.SetType(tailnetAclUpdated)
2862
_ = event.SetData(cloudevents.ApplicationJSON, data)
2963

3064
return event
3165
}
3266

33-
func MachineCreated(machine *domain.Machine, actor *domain.User) cloudevents.Event {
34-
data := &EventData{
67+
func TailnetDNSConfigUpdated(tailnet *domain.Tailnet, old *domain.DNSConfig, actor ActorOpts) cloudevents.Event {
68+
data := &EventData[*domain.DNSConfig]{
69+
Tailnet: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
70+
Target: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name},
71+
Actor: actor(),
72+
Attr: &Attr[*domain.DNSConfig]{
73+
New: &tailnet.DNSConfig,
74+
Old: old,
75+
},
76+
}
77+
78+
event := cloudevents.NewEvent()
79+
event.SetType(tailnetDNSConfigUpdated)
80+
_ = event.SetData(cloudevents.ApplicationJSON, data)
81+
82+
return event
83+
}
84+
85+
func MachineCreated(machine *domain.Machine, actor ActorOpts) cloudevents.Event {
86+
data := &EventData[any]{
3587
Tailnet: &Target{ID: idToStr(machine.Tailnet.ID), Name: machine.Tailnet.Name},
36-
Target: &Target{ID: idToStr(machine.ID), Name: machine.CompleteName(), Addresses: machine.IPs()},
37-
Actor: UserToActor(actor),
88+
Target: &Target{ID: idToStr(machine.ID), Name: machine.CompleteName()},
89+
Actor: actor(),
3890
}
3991

4092
event := cloudevents.NewEvent()
@@ -44,38 +96,51 @@ func MachineCreated(machine *domain.Machine, actor *domain.User) cloudevents.Eve
4496
return event
4597
}
4698

47-
func UserToActor(actor *domain.User) Actor {
48-
if actor == nil {
49-
return system
99+
type ActorOpts func() Actor
100+
101+
func User(u *domain.User) ActorOpts {
102+
if u == nil {
103+
return SystemAdmin()
50104
}
51105

52-
switch actor.UserType {
106+
switch u.UserType {
53107
case domain.UserTypePerson:
54-
return Actor{ID: idToStr(actor.ID), Name: actor.Name}
108+
return func() Actor {
109+
return Actor{ID: idToStr(u.ID), Name: u.Name}
110+
}
55111
default:
56-
return system
112+
return SystemAdmin()
113+
}
114+
}
115+
116+
func SystemAdmin() ActorOpts {
117+
return func() Actor {
118+
return Actor{ID: "", Name: "system admin"}
57119
}
58120
}
59121

60-
type EventData struct {
61-
Tailnet *Target `json:"tailnet,omitempty"`
62-
Target *Target `json:"target,omitempty"`
63-
Actor Actor `json:"actor"`
122+
type EventData[T any] struct {
123+
Tailnet *Target `json:"tailnet,omitempty"`
124+
Target *Target `json:"target,omitempty"`
125+
Attr *Attr[T] `json:"attr,omitempty"`
126+
Actor Actor `json:"actor"`
64127
}
65128

66129
type Target struct {
67-
ID string `json:"id"`
68-
Name string `json:"name"`
69-
Addresses []string `json:"addresses,omitempty"`
130+
ID string `json:"id"`
131+
Name string `json:"name"`
70132
}
71133

72134
type Actor struct {
73135
ID string `json:"id,omitempty"`
74136
Name string `json:"name"`
75137
}
76138

139+
type Attr[T any] struct {
140+
New T `json:"new"`
141+
Old T `json:"old,omitempty"`
142+
}
143+
77144
func idToStr(id uint64) string {
78145
return big.NewInt(int64(id)).Text(10)
79146
}
80-
81-
var system = Actor{ID: "", Name: "ionscale system"}

internal/eventlog/global.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func Configure(c *config.Config) error {
9999
_globalMu.Lock()
100100
defer _globalMu.Unlock()
101101
_globalE = &eventer{
102-
source: c.ServerUrl,
102+
source: c.WebPublicUrl.String(),
103103
sinks: sinks,
104104
}
105105

internal/handlers/authentication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (h *AuthenticationHandlers) endMachineRegistrationFlow(c echo.Context, form
509509
m.IPv4 = domain.IP{Addr: ipv4}
510510
m.IPv6 = domain.IP{Addr: ipv6}
511511

512-
events = append(events, eventlog.MachineCreated(m, user))
512+
events = append(events, eventlog.MachineCreated(m, eventlog.User(user)))
513513
} else {
514514
registeredTags := tags
515515
advertisedTags := domain.SanitizeTags(req.Hostinfo.RequestTags)

internal/handlers/registration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (h *RegistrationHandlers) authenticateMachineWithAuthKey(c echo.Context, ma
222222
m.IPv4 = domain.IP{Addr: ipv4}
223223
m.IPv6 = domain.IP{Addr: ipv6}
224224

225-
events = append(events, eventlog.MachineCreated(m, &user))
225+
events = append(events, eventlog.MachineCreated(m, eventlog.User(&user)))
226226
} else {
227227
sanitizeHostname := dnsname.SanitizeHostname(req.Hostinfo.Hostname)
228228
if m.Name != sanitizeHostname {

internal/service/acl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/bufbuild/connect-go"
77
"github.com/jsiebens/ionscale/internal/domain"
8+
"github.com/jsiebens/ionscale/internal/eventlog"
89
"github.com/jsiebens/ionscale/internal/mapping"
910
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
1011
)
@@ -60,6 +61,7 @@ func (s *Service) SetACLPolicy(ctx context.Context, req *connect.Request[api.Set
6061
return nil, logError(err)
6162
}
6263

64+
eventlog.Send(ctx, eventlog.TailnetACLUpdated(tailnet, &oldPolicy, eventlog.User(principal.User)))
6365
s.sessionManager.NotifyAll(tailnet.ID)
6466

6567
return connect.NewResponse(&api.SetACLPolicyResponse{}), nil

internal/service/dns.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/bufbuild/connect-go"
77
"github.com/jsiebens/ionscale/internal/config"
88
"github.com/jsiebens/ionscale/internal/domain"
9+
"github.com/jsiebens/ionscale/internal/eventlog"
910
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
1011
)
1112

@@ -66,6 +67,7 @@ func (s *Service) SetDNSConfig(ctx context.Context, req *connect.Request[api.Set
6667
return nil, logError(err)
6768
}
6869

70+
eventlog.Send(ctx, eventlog.TailnetDNSConfigUpdated(tailnet, &oldConfig, eventlog.User(principal.User)))
6971
s.sessionManager.NotifyAll(tailnet.ID)
7072

7173
return connect.NewResponse(&api.SetDNSConfigResponse{Config: domainDNSConfigToApiDNSConfig(tailnet)}), nil

internal/service/iam.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/bufbuild/connect-go"
77
"github.com/jsiebens/ionscale/internal/domain"
8+
"github.com/jsiebens/ionscale/internal/eventlog"
89
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
910
)
1011

@@ -68,6 +69,8 @@ func (s *Service) SetIAMPolicy(ctx context.Context, req *connect.Request[api.Set
6869
return nil, logError(err)
6970
}
7071

72+
eventlog.Send(ctx, eventlog.TailnetIAMUpdated(tailnet, &oldPolicy, eventlog.User(principal.User)))
73+
7174
return connect.NewResponse(&api.SetIAMPolicyResponse{}), nil
7275
}
7376

internal/service/tailnet.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"github.com/bufbuild/connect-go"
8+
cloudevents "github.com/cloudevents/sdk-go/v2"
89
"github.com/jsiebens/ionscale/internal/domain"
910
"github.com/jsiebens/ionscale/internal/eventlog"
1011
"github.com/jsiebens/ionscale/internal/mapping"
@@ -97,7 +98,12 @@ func (s *Service) CreateTailnet(ctx context.Context, req *connect.Request[api.Cr
9798
return nil, logError(err)
9899
}
99100

100-
eventlog.Send(ctx, eventlog.TailnetCreated(tailnet, principal.User))
101+
eventlog.Send(ctx,
102+
eventlog.TailnetCreated(tailnet, eventlog.User(principal.User)),
103+
eventlog.TailnetIAMUpdated(tailnet, nil, eventlog.User(principal.User)),
104+
eventlog.TailnetACLUpdated(tailnet, nil, eventlog.User(principal.User)),
105+
eventlog.TailnetDNSConfigUpdated(tailnet, nil, eventlog.User(principal.User)),
106+
)
101107

102108
resp := &api.CreateTailnetResponse{Tailnet: t}
103109

@@ -119,26 +125,48 @@ func (s *Service) UpdateTailnet(ctx context.Context, req *connect.Request[api.Up
119125
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("tailnet not found"))
120126
}
121127

128+
events := make([]cloudevents.Event, 0)
129+
122130
if req.Msg.IamPolicy != nil {
123131
if err := validateIamPolicy(req.Msg.IamPolicy); err != nil {
124132
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid iam policy: %w", err))
125133
}
126134

127-
tailnet.IAMPolicy = domain.IAMPolicy{}
128-
if err := mapping.CopyViaJson(req.Msg.IamPolicy, &tailnet.IAMPolicy); err != nil {
135+
oldPolicy := tailnet.IAMPolicy
136+
var newPolicy domain.IAMPolicy
137+
138+
if err := mapping.CopyViaJson(req.Msg.IamPolicy, &newPolicy); err != nil {
129139
return nil, logError(err)
130140
}
141+
142+
if !oldPolicy.Equal(&newPolicy) {
143+
tailnet.IAMPolicy = newPolicy
144+
events = append(events, eventlog.TailnetIAMUpdated(tailnet, &oldPolicy, eventlog.User(principal.User)))
145+
}
131146
}
132147

133148
if req.Msg.AclPolicy != nil {
134-
tailnet.ACLPolicy = domain.ACLPolicy{}
135-
if err := mapping.CopyViaJson(req.Msg.AclPolicy, &tailnet.ACLPolicy); err != nil {
149+
oldPolicy := tailnet.ACLPolicy
150+
var newPolicy domain.ACLPolicy
151+
152+
if err := mapping.CopyViaJson(req.Msg.AclPolicy, &newPolicy); err != nil {
136153
return nil, logError(err)
137154
}
155+
156+
if !oldPolicy.Equal(&newPolicy) {
157+
tailnet.ACLPolicy = newPolicy
158+
events = append(events, eventlog.TailnetACLUpdated(tailnet, &oldPolicy, eventlog.User(principal.User)))
159+
}
138160
}
139161

140162
if req.Msg.DnsConfig != nil {
141-
tailnet.DNSConfig = apiDNSConfigToDomainDNSConfig(req.Msg.DnsConfig)
163+
oldConfig := tailnet.DNSConfig
164+
newConfig := apiDNSConfigToDomainDNSConfig(req.Msg.DnsConfig)
165+
166+
if !oldConfig.Equal(&newConfig) {
167+
tailnet.DNSConfig = newConfig
168+
events = append(events, eventlog.TailnetDNSConfigUpdated(tailnet, &oldConfig, eventlog.User(principal.User)))
169+
}
142170
}
143171

144172
tailnet.ServiceCollectionEnabled = req.Msg.ServiceCollectionEnabled
@@ -150,6 +178,7 @@ func (s *Service) UpdateTailnet(ctx context.Context, req *connect.Request[api.Up
150178
return nil, logError(err)
151179
}
152180

181+
eventlog.Send(ctx, events...)
153182
s.sessionManager.NotifyAll(tailnet.ID)
154183

155184
t, err := domainTailnetToApiTailnet(tailnet)

0 commit comments

Comments
 (0)