Skip to content

Commit 93c7e1c

Browse files
committed
Cross off some minor todos before full testing
1 parent b04c087 commit 93c7e1c

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

internal/data/events.go

-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ func RegisterClusterHealthWatcher(fnc ClusterHealthFunc) {
9494
func watchEvents() {
9595
wc := etcd.Watch(context.Background(), "", clientv3.WithPrefix(), clientv3.WithPrevKV())
9696
for watchEvent := range wc {
97-
log.Println("got event: ", watchEvent)
98-
99-
// TODO make sure that we account for compaction events
10097
for _, event := range watchEvent.Events {
10198

10299
var (

internal/router/init.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
var lock sync.RWMutex
1919

20-
func Setup(error chan<- error, iptables bool) (err error) {
20+
func Setup(errorChan chan<- error, iptables bool) (err error) {
2121

2222
initialUsers, knownDevices, err := data.GetInitialData()
2323
if err != nil {
@@ -47,14 +47,14 @@ func Setup(error chan<- error, iptables bool) (err error) {
4747
return err
4848
}
4949

50-
handleEvents()
50+
handleEvents(errorChan)
5151

5252
go func() {
5353
startup := true
5454
cache := map[string]string{}
5555
d, err := data.GetAllDevices()
5656
if err != nil {
57-
error <- err
57+
errorChan <- err
5858
return
5959
}
6060

@@ -66,7 +66,7 @@ func Setup(error chan<- error, iptables bool) (err error) {
6666

6767
dev, err := ctrl.Device(config.Values().Wireguard.DevName)
6868
if err != nil {
69-
error <- fmt.Errorf("endpoint watcher: %s", err)
69+
errorChan <- fmt.Errorf("endpoint watcher: %s", err)
7070
return
7171
}
7272

internal/router/statemachine.go

+27-16
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"log"
55

66
"github.com/NHAS/wag/internal/acls"
7+
"github.com/NHAS/wag/internal/config"
78
"github.com/NHAS/wag/internal/data"
89
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
910
)
1011

11-
func handleEvents() {
12+
func handleEvents(erroChan chan<- error) {
1213
data.RegisterAclsWatcher(aclsChanges)
13-
data.RegisterClusterHealthWatcher(clusterState)
14+
data.RegisterClusterHealthWatcher(clusterState(erroChan))
1415
data.RegisterDeviceWatcher(deviceChanges)
1516
data.RegisterGroupsWatcher(groupChanges)
1617
data.RegisterUserWatcher(userChanges)
@@ -41,14 +42,23 @@ func deviceChanges(device data.BasicEvent[data.Device], state int) {
4142
}
4243
}
4344

44-
if (device.CurrentValue.Attempts != device.Previous.Attempts && device.CurrentValue.Attempts > 5) ||
45+
if (device.CurrentValue.Attempts != device.Previous.Attempts && device.CurrentValue.Attempts > config.Values().Lockout) ||
4546
device.CurrentValue.Endpoint.String() != device.Previous.Endpoint.String() {
4647
err := Deauthenticate(device.CurrentValue.Address)
4748
if err != nil {
4849
log.Println(err)
4950
}
5051
}
5152

53+
if device.CurrentValue.Authorised != device.Previous.Authorised {
54+
if device.CurrentValue.Attempts <= config.Values().Lockout {
55+
err := SetAuthorized(device.CurrentValue.Address, device.CurrentValue.Username)
56+
if err != nil {
57+
log.Println(err)
58+
}
59+
}
60+
}
61+
5262
default:
5363
panic("unknown state")
5464
}
@@ -117,19 +127,20 @@ func groupChanges(groupChange data.TargettedEvent[[]string], state int) {
117127
}
118128
}
119129

120-
func clusterState(stateText string, state int) {
121-
switch stateText {
122-
case "dead":
123-
TearDown()
124-
case "healthy":
125-
errors := make(chan error)
126-
go func() {
127-
<-errors
128-
// TODO fix this
129-
}()
130-
err := Setup(errors, true)
131-
if err != nil {
132-
log.Fatal(err)
130+
func clusterState(errorsChan chan<- error) data.ClusterHealthFunc {
131+
132+
return func(stateText string, state int) {
133+
switch stateText {
134+
case "dead":
135+
log.Println("Cluster has entered dead state, tearing down")
136+
TearDown()
137+
case "healthy":
138+
err := Setup(errorsChan, true)
139+
if err != nil {
140+
errorsChan <- err
141+
log.Println("was unable to return wag member to healthy state, dying: ", err)
142+
return
143+
}
133144
}
134145
}
135146
}

internal/users/user.go

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ func (u *user) Authenticate(device, mfaType string, authenticator authenticators
150150
return fmt.Errorf("%s %s unable to reset number of mfa attempts: %s", u.Username, device, err)
151151
}
152152

153-
// TODO gonna have to do an additional something here in order to send the statemachine a message we need to update the ebpf
154-
155153
return nil
156154
}
157155

0 commit comments

Comments
 (0)