Skip to content

Commit

Permalink
Improve log output in router state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Apr 27, 2024
1 parent c7769ea commit 9613410
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 93 deletions.
2 changes: 1 addition & 1 deletion commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (g *start) Check() error {
}
}

err = data.Load(config.Values.DatabaseLocation, g.clusterJoinToken)
err = data.Load(config.Values.DatabaseLocation, g.clusterJoinToken, false)
if err != nil {
return fmt.Errorf("cannot load database: %v", err)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_disabled_max_lifetime.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"Port": "8080"
}
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Authenticators": {
"Issuer": "192.168.121.61"
},
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_disabled_sliding_window.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg0",
"ListenPort": 53230,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_fail_with_multiple.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_in_memory_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_port_based_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_roaming_all_routes_mfa_priority.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/test_route_restriction_preference.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"Authenticators": {
"Issuer": "192.168.121.61"
},
"Clustering": {
"ClusterState": "new",
"ETCDLogLevel": "error",
"Witness": false,
"TLSManagerListenURL": "https://localhost:3434"
},
"Wireguard": {
"DevName": "wg45",
"ListenPort": 53230,
Expand Down
15 changes: 11 additions & 4 deletions internal/data/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ type Device struct {
Authorised time.Time
}

func (d Device) String() string {

authorised := "no"
if !d.Authorised.Equal(time.Time{}) {
authorised = d.Authorised.Format(time.DateTime)
}

return fmt.Sprintf("device[%s:%s][active: %t, attempts: %d, authorised: %s]", d.Username, d.Address, d.Active, d.Attempts, authorised)
}

func UpdateDeviceEndpoint(address string, endpoint *net.UDPAddr) error {

realKey, err := etcd.Get(context.Background(), "deviceref-"+address)
Expand All @@ -36,10 +46,7 @@ func UpdateDeviceEndpoint(address string, endpoint *net.UDPAddr) error {
return errors.New("device was not found")
}

var realDeviceAddr string
json.Unmarshal(realKey.Kvs[0].Value, &realDeviceAddr)

return doSafeUpdate(context.Background(), realDeviceAddr, func(gr *clientv3.GetResponse) (string, error) {
return doSafeUpdate(context.Background(), string(realKey.Kvs[0].Value), func(gr *clientv3.GetResponse) (string, error) {
if len(gr.Kvs) != 1 {
return "", errors.New("user device has multiple keys")
}
Expand Down
6 changes: 5 additions & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func RegisterEventListener[T any](path string, isPrefix bool, f func(key string,

go func(key []byte) {
if err := f(string(key), currentValue, previousValue, state); err != nil {
log.Println("applying event failed: ", currentValue, "err:", err)
log.Println("applying event failed: ", state, currentValue, "err:", err)
err = RaiseError(GetServerID(), err, value)
if err != nil {
log.Println("failed to raise error with cluster: ", err)
Expand Down Expand Up @@ -180,6 +180,10 @@ func checkClusterHealth() {
notifyHealthy()

case <-time.After(1 * time.Second):
if etcdServer == nil {
return
}

leader := etcdServer.Server.Leader()
if leader == 0 {
notifyClusterHealthListeners("electing")
Expand Down
66 changes: 37 additions & 29 deletions internal/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func parseUrls(values ...string) []url.URL {
return urls
}

func Load(path, joinToken string) error {
func Load(path, joinToken string, testing bool) error {

doMigration := true
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -84,33 +84,35 @@ func Load(path, joinToken string) error {

var err error

if joinToken == "" {
TLSManager, err = manager.New(config.Values.Clustering.TLSManagerStorage, config.Values.Clustering.TLSManagerListenURL)
if err != nil {
return fmt.Errorf("tls manager: %s", err)
}
} else {

if config.Values.Clustering.TLSManagerStorage == "" {
config.Values.Clustering.TLSManagerStorage = "certificates"
}
if TLSManager == nil {
if joinToken == "" {
TLSManager, err = manager.New(config.Values.Clustering.TLSManagerStorage, config.Values.Clustering.TLSManagerListenURL)
if err != nil {
return fmt.Errorf("tls manager: %s", err)
}
} else {

TLSManager, err = manager.Join(joinToken, config.Values.Clustering.TLSManagerStorage, map[string]func(name string, data string){
"config.json": func(name, data string) {
err := os.WriteFile("config.json", []byte(data), 0600)
if err != nil {
log.Fatal("failed to create config.json from other cluster members config: ", err)
}
if config.Values.Clustering.TLSManagerStorage == "" {
config.Values.Clustering.TLSManagerStorage = "certificates"
}

log.Println("got additional, loading config file")
err = config.Load("config.json")
if err != nil {
log.Fatal("config supplied by other cluster member was invalid (potential version issues?): ", err)
}
},
})
if err != nil {
return err
TLSManager, err = manager.Join(joinToken, config.Values.Clustering.TLSManagerStorage, map[string]func(name string, data string){
"config.json": func(name, data string) {
err := os.WriteFile("config.json", []byte(data), 0600)
if err != nil {
log.Fatal("failed to create config.json from other cluster members config: ", err)
}

log.Println("got additional, loading config file")
err = config.Load("config.json")
if err != nil {
log.Fatal("config supplied by other cluster member was invalid (potential version issues?): ", err)
}
},
})
if err != nil {
return err
}
}
}
part, err := generateRandomBytes(10)
Expand All @@ -121,8 +123,11 @@ func Load(path, joinToken string) error {

cfg := embed.NewConfig()
cfg.Name = config.Values.Clustering.Name
if testing {
cfg.Name += part
}
cfg.ClusterState = config.Values.Clustering.ClusterState
cfg.InitialClusterToken = "wag-test"
cfg.InitialClusterToken = "wag"
cfg.LogLevel = config.Values.Clustering.ETCDLogLevel
cfg.ListenPeerUrls = parseUrls(config.Values.Clustering.ListenAddresses...)
cfg.ListenClientUrls = parseUrls(etcdUnixSocket)
Expand All @@ -149,7 +154,7 @@ func Load(path, joinToken string) error {

cfg.InitialCluster = cfg.InitialCluster[:len(cfg.InitialCluster)-1]

cfg.Dir = filepath.Join(config.Values.Clustering.DatabaseLocation, config.Values.Clustering.Name+".wag-node.etcd")
cfg.Dir = filepath.Join(config.Values.Clustering.DatabaseLocation, cfg.Name+".wag-node.etcd")
etcdServer, err = embed.StartEtcd(cfg)
if err != nil {
return fmt.Errorf("error starting etcd: %s", err)
Expand Down Expand Up @@ -454,8 +459,11 @@ func migrateFromSql(database *sql.DB) error {

func TearDown() {
if etcdServer != nil {
log.Println("Tearing down server")
etcd.Close()
etcdServer.Close()

etcd = nil
etcdServer = nil
}
}

Expand Down
7 changes: 1 addition & 6 deletions internal/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,7 @@ func DeleteUser(username string) error {
return err
}

_, err = etcd.Delete(context.Background(), "devices-"+username+"-", clientv3.WithPrefix())
if err != nil {
return err
}

return err
return DeleteDevices(username)
}

func GetUserData(username string) (u UserModel, err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/router/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func RemoveUser(username string) error {
for address, publicKey := range usersToAddresses[username] {
err = _removePeer(publicKey, address)
if err != nil {
log.Println("unable to remove peer: ", err)
log.Println("unable to remove peer: ", address, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/router/fwentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d fwentry) Bytes() []byte {

func (d *fwentry) Unpack(b []byte) error {
if len(b) != 40 {
return errors.New("too short")
return errors.New("firewall entry is too short")
}

d.sessionExpiry = binary.LittleEndian.Uint64(b[:8])
Expand Down
Loading

0 comments on commit 9613410

Please sign in to comment.