Skip to content

Commit

Permalink
Merge pull request #114 from telekom-mms/feature/add-tnd-state-to-dbu…
Browse files Browse the repository at this point in the history
…s-properties

Add TND State to D-Bus Properties
  • Loading branch information
hwipl authored Aug 7, 2024
2 parents 71b0381 + 557faf7 commit f4c16db
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func printStatus(status *vpnstatus.Status) error {

fmt.Printf("TrafPol State: %s\n", status.TrafPolState)
fmt.Printf("Allowed Hosts: %s\n", status.AllowedHosts)
fmt.Printf("TND State: %s\n", status.TNDState)

if status.VPNConfig == nil {
fmt.Printf("VPN Config:\n")
Expand Down
21 changes: 21 additions & 0 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ func (d *Daemon) setStatusAllowedHosts(hosts []string) {
d.dbus.SetProperty(dbusapi.PropertyAllowedHosts, hosts)
}

// setStatusTNDState sets the TND state in status.
func (d *Daemon) setStatusTNDState(state vpnstatus.TNDState) {
if d.status.TNDState == state {
// TND state not changed
return
}

// TND state changed
log.WithField("TNDState", state).Info("Daemon changed TNDState status")
d.status.TNDState = state
d.dbus.SetProperty(dbusapi.PropertyTNDState, state)
}

// setStatusVPNConfig sets the VPN config in status.
func (d *Daemon) setStatusVPNConfig(config *vpnconfig.Config) {
if d.status.VPNConfig.Equal(config) {
Expand Down Expand Up @@ -644,6 +657,10 @@ func (d *Daemon) startTND() error {
if err := d.tnd.Start(); err != nil {
return fmt.Errorf("Daemon could not start TND: %w", err)
}

// update tnd status
d.setStatusTNDState(vpnstatus.TNDStateActive)

return nil
}

Expand All @@ -655,6 +672,9 @@ func (d *Daemon) stopTND() {
log.Info("Daemon stopping TND")
d.tnd.Stop()
d.tnd = nil

// update tnd status
d.setStatusTNDState(vpnstatus.TNDStateInactive)
}

// checkTND checks if TND should be running and starts or stops it.
Expand Down Expand Up @@ -846,6 +866,7 @@ func (d *Daemon) Start() error {
d.setStatusConnectedAt(0)
d.setStatusOCRunning(false)
d.setStatusTrafPolState(vpnstatus.TrafPolStateInactive)
d.setStatusTNDState(vpnstatus.TNDStateInactive)

// start traffic policing
err = d.checkTrafPol()
Expand Down
15 changes: 15 additions & 0 deletions internal/dbusapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
PropertyOCRunning = "OCRunning"
PropertyTrafPolState = "TrafPolState"
PropertyAllowedHosts = "AllowedHosts"
PropertyTNDState = "TNDState"
PropertyVPNConfig = "VPNConfig"
)

Expand Down Expand Up @@ -101,6 +102,13 @@ var (
AllowedHostsInvalid []string
)

// Property "TND State" states.
const (
TNDStateUnknown uint32 = iota
TNDStateInactive
TNDStateActive
)

// Property "VPNConfig" values.
const (
VPNConfigInvalid = ""
Expand Down Expand Up @@ -250,6 +258,7 @@ func (s *Service) start() {
s.props.SetMust(Interface, PropertyOCRunning, OCRunningUnknown)
s.props.SetMust(Interface, PropertyTrafPolState, TrafPolStateUnknown)
s.props.SetMust(Interface, PropertyAllowedHosts, AllowedHostsInvalid)
s.props.SetMust(Interface, PropertyTNDState, TNDStateUnknown)
s.props.SetMust(Interface, PropertyVPNConfig, VPNConfigInvalid)
}

Expand Down Expand Up @@ -372,6 +381,12 @@ func (s *Service) Start() error {
Emit: prop.EmitTrue,
Callback: nil,
},
PropertyTNDState: {
Value: TNDStateUnknown,
Writable: false,
Emit: prop.EmitTrue,
Callback: nil,
},
PropertyVPNConfig: {
Value: VPNConfigInvalid,
Writable: false,
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func updateStatusFromProperties(status *vpnstatus.Status, props map[string]dbus.
err = v.Store(&dest.TrafPolState)
case dbusapi.PropertyAllowedHosts:
err = v.Store(&dest.AllowedHosts)
case dbusapi.PropertyTNDState:
err = v.Store(&dest.TNDState)
case dbusapi.PropertyVPNConfig:
s := dbusapi.VPNConfigInvalid
if err := v.Store(&s); err != nil {
Expand Down Expand Up @@ -278,6 +280,8 @@ func handlePropertiesChanged(s *dbus.Signal, status *vpnstatus.Status) *vpnstatu
status.TrafPolState = vpnstatus.TrafPolStateUnknown
case dbusapi.PropertyAllowedHosts:
status.AllowedHosts = dbusapi.AllowedHostsInvalid
case dbusapi.PropertyTNDState:
status.TNDState = vpnstatus.TNDStateUnknown
case dbusapi.PropertyVPNConfig:
status.VPNConfig = nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestDBusClientQuery(t *testing.T) {
dbusapi.PropertyOCRunning: dbus.MakeVariant(dbusapi.OCRunningUnknown),
dbusapi.PropertyTrafPolState: dbus.MakeVariant(dbusapi.TrafPolStateUnknown),
dbusapi.PropertyAllowedHosts: dbus.MakeVariant(dbusapi.AllowedHostsInvalid),
dbusapi.PropertyTNDState: dbus.MakeVariant(dbusapi.TNDStateUnknown),
dbusapi.PropertyVPNConfig: dbus.MakeVariant(dbusapi.VPNConfigInvalid),
},
{
Expand Down Expand Up @@ -214,6 +215,7 @@ func TestDBusClientSubscribe(t *testing.T) {
dbusapi.PropertyOCRunning,
dbusapi.PropertyTrafPolState,
dbusapi.PropertyAllowedHosts,
dbusapi.PropertyTNDState,
dbusapi.PropertyVPNConfig,
}},
},
Expand Down
25 changes: 25 additions & 0 deletions pkg/vpnstatus/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ func (t TrafPolState) String() string {
return ""
}

// TNDState is the current TND state.
type TNDState uint32

// TNDState states.
const (
TNDStateUnknown = iota
TNDStateInactive
TNDStateActive
)

// String resturns TNDState as string.
func (t TNDState) String() string {
switch t {
case TNDStateUnknown:
return "unknown"
case TNDStateInactive:
return "inactive"
case TNDStateActive:
return "active"
}
return ""
}

// Status is a VPN status.
type Status struct {
TrustedNetwork TrustedNetwork
Expand All @@ -133,6 +156,7 @@ type Status struct {
OCRunning OCRunning
TrafPolState TrafPolState
AllowedHosts []string
TNDState TNDState
VPNConfig *vpnconfig.Config
}

Expand All @@ -153,6 +177,7 @@ func (s *Status) Copy() *Status {
OCRunning: s.OCRunning,
TrafPolState: s.TrafPolState,
AllowedHosts: append(s.AllowedHosts[:0:0], s.AllowedHosts...),
TNDState: s.TNDState,
VPNConfig: s.VPNConfig.Copy(),
}
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/vpnstatus/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ func TestTrafPolStateString(t *testing.T) {
}
}

// TestTNDStateString tests String of TNDState.
func TestTNDStateString(t *testing.T) {
for v, s := range map[TNDState]string{
// valid
TNDStateUnknown: "unknown",
TNDStateInactive: "inactive",
TNDStateActive: "active",

// invalid
123456: "",
} {
if v.String() != s {
t.Errorf("got %s, want %s", v.String(), s)
}
}
}

// TestStatusCopy tests Copy of Status.
func TestStatusCopy(t *testing.T) {
// test nil
Expand All @@ -155,6 +172,7 @@ func TestStatusCopy(t *testing.T) {
OCRunning: OCRunningRunning,
TrafPolState: TrafPolStateActive,
AllowedHosts: []string{"test.example.com"},
TNDState: TNDStateActive,
VPNConfig: vpnconfig.New(),
},
} {
Expand Down
10 changes: 10 additions & 0 deletions tools/dbusclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
ocRunning := dbusapi.OCRunningUnknown
trafPolState := dbusapi.TrafPolStateUnknown
allowedHosts := dbusapi.AllowedHostsInvalid
tndState := dbusapi.TNDStateUnknown
vpnConfig := dbusapi.VPNConfigInvalid

getProperty := func(name string, val any) {
Expand All @@ -61,6 +62,7 @@ func main() {
getProperty(dbusapi.PropertyOCRunning, &ocRunning)
getProperty(dbusapi.PropertyTrafPolState, &trafPolState)
getProperty(dbusapi.PropertyAllowedHosts, &allowedHosts)
getProperty(dbusapi.PropertyTNDState, &tndState)
getProperty(dbusapi.PropertyVPNConfig, &vpnConfig)

log.Println("TrustedNetwork:", trustedNetwork)
Expand All @@ -74,6 +76,7 @@ func main() {
log.Println("OCRunning:", ocRunning)
log.Println("TrafPolState:", trafPolState)
log.Println("AllowedHosts:", allowedHosts)
log.Println("TNDState:", tndState)
log.Println("VPNConfig:", vpnConfig)

// handle signals
Expand Down Expand Up @@ -157,6 +160,11 @@ func main() {
log.Fatal(err)
}
fmt.Println(allowedHosts)
case dbusapi.PropertyTNDState:
if err := value.Store(&tndState); err != nil {
log.Fatal(err)
}
fmt.Println(tndState)
case dbusapi.PropertyVPNConfig:
if err := value.Store(&vpnConfig); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -196,6 +204,8 @@ func main() {
trafPolState = dbusapi.TrafPolStateUnknown
case dbusapi.PropertyAllowedHosts:
allowedHosts = dbusapi.AllowedHostsInvalid
case dbusapi.PropertyTNDState:
tndState = dbusapi.TNDStateUnknown
case dbusapi.PropertyVPNConfig:
vpnConfig = dbusapi.VPNConfigInvalid
}
Expand Down

0 comments on commit f4c16db

Please sign in to comment.