Skip to content

Commit

Permalink
Merge pull request #18 from telekom-mms/feature/make-tnd-start-fallible
Browse files Browse the repository at this point in the history
Feature/make tnd start fallible
  • Loading branch information
hwipl authored Dec 13, 2023
2 parents 2dd3aa0 + 0873884 commit a69e6f4
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 75 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func main() {
t.SetServers(servers)

// start tnd
t.Start()
if err := t.Start(); err != nil {
log.Fatal(err)
}
for r := range t.Results() {
log.Println("Trusted Network:", r)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/tnd/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// package main contains a TND example.
package main

import (
Expand Down Expand Up @@ -50,7 +51,9 @@ func main() {
t.SetServers(httpsServers)

// start tnd
t.Start()
if err := t.Start(); err != nil {
log.Fatal(err)
}
for r := range t.Results() {
log.WithField("trusted", r).Info("TND result")
}
Expand Down
67 changes: 45 additions & 22 deletions internal/files/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
log "github.com/sirupsen/logrus"
)

const (
var (
// resolv.conf files in /etc and /run/systemd/resolve.
etc = "/etc"
etcResolvConf = etc + "/resolv.conf"
Expand All @@ -15,11 +15,18 @@ const (
stubResolvConf = systemdResolveDir + "/stub-resolv.conf"
)

// Watcher is the file watcher interface.
type Watcher interface {
Start() error
Stop()
}

// Watch watches resolv.conf files and then probes the trusted https servers.
type Watch struct {
probes chan struct{}
done chan struct{}
closed chan struct{}
watcher *fsnotify.Watcher
probes chan struct{}
done chan struct{}
closed chan struct{}
}

// sendProbe sends a probe request over the probe channel.
Expand All @@ -46,33 +53,19 @@ func isResolvConfEvent(event fsnotify.Event) bool {
// start starts the Watch.
func (w *Watch) start() {
defer close(w.closed)

// create watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.WithError(err).Fatal("TND could not create file watcher")
}
defer func() {
if err := watcher.Close(); err != nil {
if err := w.watcher.Close(); err != nil {
log.WithError(err).Error("TND could not stop file watcher")
}
}()

// add resolv.conf folders to watcher
if err := watcher.Add(etc); err != nil {
log.WithError(err).Debug("TND could not add etc to file watcher")
}
if err := watcher.Add(systemdResolveDir); err != nil {
log.WithError(err).Debug("TND could not add systemd to file watcher")
}

// run initial probe
w.sendProbe()

// watch the files
for {
select {
case event, ok := <-watcher.Events:
case event, ok := <-w.watcher.Events:
if !ok {
return
}
Expand All @@ -83,7 +76,7 @@ func (w *Watch) start() {
}).Debug("TND got resolv.conf file event")
w.sendProbe()
}
case err, ok := <-watcher.Errors:
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
Expand All @@ -94,9 +87,39 @@ func (w *Watch) start() {
}
}

// fsnotifyNewWatcher is fsnotify.NewWatcher for testing.
var fsnotifyNewWatcher = fsnotify.NewWatcher

// watcherAdd is watcher.Add for testing.
var watcherAdd = func(watcher *fsnotify.Watcher, name string) error {
return watcher.Add(name)
}

// Start starts the Watch.
func (w *Watch) Start() {
func (w *Watch) Start() error {
// create watcher
watcher, err := fsnotifyNewWatcher()
if err != nil {
log.WithError(err).Error("TND could not create file watcher")
return err
}

// add resolv.conf folders to watcher
if err := watcherAdd(watcher, etc); err != nil {
log.WithError(err).Error("TND could not add etc to file watcher")
_ = watcher.Close()
return err
}
if err := watcherAdd(watcher, systemdResolveDir); err != nil {
log.WithError(err).Error("TND could not add systemd to file watcher")
_ = watcher.Close()
return err
}

// start watcher
w.watcher = watcher
go w.start()
return nil
}

// Stop stops the Watch.
Expand Down
76 changes: 71 additions & 5 deletions internal/files/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,79 @@
package files

import "testing"
import (
"errors"
"testing"

"github.com/fsnotify/fsnotify"
)

// TestWatchStartStop tests Start() and Stop() of Watch.
func TestWatchStartStop(_ *testing.T) {
func TestWatchStartStop(t *testing.T) {
probes := make(chan struct{})
fw := NewWatch(probes)
fw.Start()
fw.Stop()

// error creating fsnotify.Watcher
t.Run("watcher error", func(t *testing.T) {
// fail when creating watcher
defer func() { fsnotifyNewWatcher = fsnotify.NewWatcher }()
fsnotifyNewWatcher = func() (*fsnotify.Watcher, error) {
return nil, errors.New("test error")
}

// test error
fw := NewWatch(probes)
if err := fw.Start(); err == nil {
t.Errorf("start should fail")
}
})

// error adding dir to watcher
t.Run("add dir error", func(t *testing.T) {
// cleanups after test
oldAdd := watcherAdd
defer func() { watcherAdd = oldAdd }()

// test dirs
for _, dir := range []string{
etc,
systemdResolveDir,
} {
// fail when adding dir
watcherAdd = func(_ *fsnotify.Watcher, name string) error {
if name == dir {
return errors.New("test error")
}
return nil
}

// test error
fw := NewWatch(probes)
if err := fw.Start(); err == nil {
t.Errorf("start should fail")
}
}
})

// no errors
t.Run("no errors", func(t *testing.T) {
// cleanups after test
oldEtc := etc
oldResolve := systemdResolveDir
defer func() {
etc = oldEtc
systemdResolveDir = oldResolve
}()

// create test dirs
etc = t.TempDir()
systemdResolveDir = t.TempDir()

// test without errors
fw := NewWatch(probes)
if err := fw.Start(); err != nil {
t.Errorf("start should not fail: %v", err)
}
fw.Stop()
})
}

// TestNewWatch tests NewWatch.
Expand Down
30 changes: 22 additions & 8 deletions internal/routes/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import (
"golang.org/x/sys/unix"
)

// Watcher is the watcher interface.
type Watcher interface {
Start() error
Stop()
Probes() chan struct{}
}

// Watch waits for routing update events and then probes the
// trusted https servers.
type Watch struct {
events chan netlink.RouteUpdate
probes chan struct{}
done chan struct{}
}
Expand All @@ -24,17 +32,11 @@ func (w *Watch) sendProbe() {

// start starts the Watch.
func (w *Watch) start() {
// register for route update events
events := make(chan netlink.RouteUpdate)
if err := netlink.RouteSubscribe(events, w.done); err != nil {
log.WithError(err).Fatal("TND route subscribe error")
}

// run initial probe
w.sendProbe()

// handle route update events
for e := range events {
for e := range w.events {
switch e.Type {
case unix.RTM_NEWROUTE:
log.WithField("dst", e.Dst).Debug("TND got route NEW event")
Expand All @@ -45,9 +47,20 @@ func (w *Watch) start() {
}
}

// netlinkRouteSubscribe is netlink.RouteSubscribe for testing.
var netlinkRouteSubscribe = netlink.RouteSubscribe

// Start starts the Watch.
func (w *Watch) Start() {
func (w *Watch) Start() error {
// register for route update events
if err := netlinkRouteSubscribe(w.events, w.done); err != nil {
log.WithError(err).Error("TND route subscribe error")
return err
}

// start watcher
go w.start()
return nil
}

// Stop stops the Watch.
Expand All @@ -67,6 +80,7 @@ func (w *Watch) Probes() chan struct{} {
// NewWatch returns a new Watch.
func NewWatch(probes chan struct{}) *Watch {
return &Watch{
events: make(chan netlink.RouteUpdate),
probes: probes,
done: make(chan struct{}),
}
Expand Down
35 changes: 30 additions & 5 deletions internal/routes/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package routes

import "testing"
import (
"errors"
"testing"

"github.com/vishvananda/netlink"
)

// TestWatchStartStop tests Start and Stop of Watch.
func TestWatchStartStop(_ *testing.T) {
func TestWatchStartStop(t *testing.T) {
probes := make(chan struct{})
rw := NewWatch(probes)
rw.Start()
rw.Stop()

t.Run("subscribe error", func(t *testing.T) {
defer func() { netlinkRouteSubscribe = netlink.RouteSubscribe }()
netlinkRouteSubscribe = func(chan<- netlink.RouteUpdate, <-chan struct{}) error {
return errors.New("test error")
}

rw := NewWatch(probes)
if err := rw.Start(); err == nil {
t.Error("start should fail")
}
})

t.Run("no errors", func(t *testing.T) {
rw := NewWatch(probes)
if err := rw.Start(); err != nil {
t.Errorf("start should not fail: %v", err)
}
rw.Stop()
})
}

// TestWatchProbes tests Probes of Watch.
Expand All @@ -23,6 +45,9 @@ func TestWatchProbes(t *testing.T) {
func TestNewWatch(t *testing.T) {
probes := make(chan struct{})
rw := NewWatch(probes)
if rw.events == nil {
t.Errorf("got nil, want != nil")
}
if rw.probes != probes {
t.Errorf("got %p, want %p", rw.probes, probes)
}
Expand Down
Loading

0 comments on commit a69e6f4

Please sign in to comment.