Skip to content

Commit

Permalink
Merge pull request #137 from telekom-mms/feature/move-setting-splitrt…
Browse files Browse the repository at this point in the history
…-excludes-to-vpnsetup

Move commands for setting split routing excludes to VPNSetup
  • Loading branch information
hwipl authored Jan 23, 2025
2 parents 09303ab + c6bad7f commit 1b43c55
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 306 deletions.
56 changes: 20 additions & 36 deletions internal/cmdtmpl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,6 @@ func (cl *CommandList) executeTemplate(tmpl string, data any) (string, error) {
return s, nil
}

// getCommandListSplitRouting returns the command list identified by name for SplitRouting.
func getCommandListSplitRouting(name string) *CommandList {
var cl *CommandList
switch name {
case "SplitRoutingSetExcludes":
// Set Excludes
cl = &CommandList{
Name: name,
Commands: []*Command{
// flush existing entries
// add entries
{Line: "{{.Executables.Nft}} -f -",
Stdin: `flush set inet oc-daemon-routing excludes4
flush set inet oc-daemon-routing excludes6
{{range .Addresses -}}
{{if .Addr.Is6 -}}
add element inet oc-daemon-routing excludes6 { {{.}} }
{{else -}}
add element inet oc-daemon-routing excludes4 { {{.}} }
{{end -}}
{{end}}`},
},
defaultTemplate: VPNSetupDefaultTemplate,
}
default:
return nil

}

cl.template = template.Must(template.New("Template").Parse(cl.defaultTemplate))
return cl
}

// TrafPolDefaultTemplate is the default template for Traffic Policing.
const TrafPolDefaultTemplate = `
{{- define "TrafPolRules"}}
Expand Down Expand Up @@ -502,6 +469,26 @@ func getCommandListVPNSetup(name string) *CommandList {
},
defaultTemplate: VPNSetupDefaultTemplate,
}
case "VPNSetupSetExcludes":
// Set Excludes
cl = &CommandList{
Name: name,
Commands: []*Command{
// flush existing entries
// add entries
{Line: "{{.Executables.Nft}} -f -",
Stdin: `flush set inet oc-daemon-routing excludes4
flush set inet oc-daemon-routing excludes6
{{range .Addresses -}}
{{if .Addr.Is6 -}}
add element inet oc-daemon-routing excludes6 { {{.}} }
{{else -}}
add element inet oc-daemon-routing excludes4 { {{.}} }
{{end -}}
{{end}}`},
},
defaultTemplate: VPNSetupDefaultTemplate,
}
case "VPNSetupSetupDNSServer":
// Setup DNS server
cl = &CommandList{
Expand Down Expand Up @@ -568,9 +555,6 @@ func getCommandListVPNSetup(name string) *CommandList {

// getCommandList returns the command list identified by name.
func getCommandList(name string) *CommandList {
if strings.HasPrefix(name, "SplitRouting") {
return getCommandListSplitRouting(name)
}
if strings.HasPrefix(name, "TrafPol") {
return getCommandListTrafPol(name)
}
Expand Down
15 changes: 5 additions & 10 deletions internal/cmdtmpl/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestExecuteTemplateParseError(t *testing.T) {
func TestGetCommandList(t *testing.T) {
// not existing
for _, name := range []string{
"SplitRoutingDoesNotExist",
"TrafPolDoesNotExist",
"VPNSetupDoesNotExist",
"DoesNotExist",
Expand All @@ -35,9 +34,6 @@ func TestGetCommandList(t *testing.T) {

// existing
for _, name := range []string{
// Split Routing
"SplitRoutingSetExcludes",

// Traffic Policing
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
Expand All @@ -52,6 +48,7 @@ func TestGetCommandList(t *testing.T) {
// VPN Setup
"VPNSetupSetup",
"VPNSetupTeardown",
"VPNSetupSetExcludes",
"VPNSetupSetupDNSServer",
"VPNSetupSetupDNSDomains",
"VPNSetupSetupDNSDefaultRoute",
Expand Down Expand Up @@ -89,9 +86,6 @@ func TestGetCmds(t *testing.T) {

// existing, that only need daemon config as input data
for _, name := range []string{
// Split Routing
// "SplitRoutingSetExcludes", // skip, requires excludes

// Traffic Policing
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
Expand All @@ -106,6 +100,7 @@ func TestGetCmds(t *testing.T) {
// VPN Setup
"VPNSetupSetup",
"VPNSetupTeardown",
// "VPNSetupSetExcludes", // skip, requires excludes
"VPNSetupSetupDNSServer",
"VPNSetupSetupDNSDomains",
"VPNSetupSetupDNSDefaultRoute",
Expand All @@ -121,13 +116,13 @@ func TestGetCmds(t *testing.T) {

// existing, with insufficient input data
for _, name := range []string{
// Split Routing
"SplitRoutingSetExcludes",

// Traffic Policing
"TrafPolAddAllowedDevice",
"TrafPolRemoveAllowedDevice",
"TrafPolAddAllowedHost",

// VPN Setup
"VPNSetupSetExcludes",
} {
if _, err := GetCmds(name, daemoncfg.NewConfig()); err == nil {
t.Errorf("insufficient data should return error for list %s", name)
Expand Down
5 changes: 1 addition & 4 deletions internal/splitrt/excludes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sync"

log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
)

const (
Expand All @@ -22,7 +21,6 @@ type dynExclude struct {
// Excludes contains split Excludes.
type Excludes struct {
sync.Mutex
conf *daemoncfg.Config
s map[string]netip.Prefix
d map[netip.Addr]*dynExclude
done chan struct{}
Expand Down Expand Up @@ -175,9 +173,8 @@ func (e *Excludes) List() (static, dynamic []string) {
}

// NewExcludes returns new split excludes.
func NewExcludes(conf *daemoncfg.Config) *Excludes {
func NewExcludes() *Excludes {
return &Excludes{
conf: conf,
s: make(map[string]netip.Prefix),
d: make(map[netip.Addr]*dynExclude),
done: make(chan struct{}),
Expand Down
20 changes: 8 additions & 12 deletions internal/splitrt/excludes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package splitrt
import (
"net/netip"
"testing"

"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
)

// getTestExcludes returns excludes for testing.
Expand Down Expand Up @@ -59,7 +57,7 @@ func getTestDynamicExcludes(t *testing.T) []netip.Prefix {

// TestExcludesAddStatic tests AddStatic of Excludes.
func TestExcludesAddStatic(t *testing.T) {
e := NewExcludes(daemoncfg.NewConfig())
e := NewExcludes()
excludes := getTestStaticExcludes(t)

// test adding excludes
Expand All @@ -77,7 +75,7 @@ func TestExcludesAddStatic(t *testing.T) {
}

// test adding overlapping excludes
e = NewExcludes(daemoncfg.NewConfig())
e = NewExcludes()
for _, exclude := range getTestStaticExcludesOverlap(t) {
e.AddStatic(exclude)
}
Expand All @@ -90,7 +88,7 @@ func TestExcludesAddStatic(t *testing.T) {

// TestExcludesAddDynamic tests AddDynamic of Excludes.
func TestExcludesAddDynamic(t *testing.T) {
e := NewExcludes(daemoncfg.NewConfig())
e := NewExcludes()
excludes := getTestDynamicExcludes(t)

// test adding excludes
Expand All @@ -110,7 +108,7 @@ func TestExcludesAddDynamic(t *testing.T) {
// test adding excludes with existing static excludes,
// should only add new excludes
statics := getTestStaticExcludes(t)
e = NewExcludes(daemoncfg.NewConfig())
e = NewExcludes()
for _, exclude := range statics {
if !e.AddStatic(exclude) {
t.Errorf("should add exclude %s", exclude)
Expand All @@ -132,7 +130,7 @@ func TestExcludesAddDynamic(t *testing.T) {
}

// test adding invalid excludes (static as dynamic)
e = NewExcludes(daemoncfg.NewConfig())
e = NewExcludes()
for _, exclude := range getTestStaticExcludes(t) {
if e.AddDynamic(exclude, 300) {
t.Errorf("should not add exclude %s", exclude)
Expand All @@ -142,7 +140,7 @@ func TestExcludesAddDynamic(t *testing.T) {

// TestExcludesRemoveStatic tests RemoveStatic of Excludes.
func TestExcludesRemove(t *testing.T) {
e := NewExcludes(daemoncfg.NewConfig())
e := NewExcludes()
excludes := getTestStaticExcludes(t)

// test removing not existing excludes
Expand Down Expand Up @@ -182,7 +180,7 @@ func TestExcludesRemove(t *testing.T) {

// TestExcludesCleanup tests cleanup of Excludes.
func TestExcludesCleanup(t *testing.T) {
e := NewExcludes(daemoncfg.NewConfig())
e := NewExcludes()

// test without excludes
if e.cleanup() {
Expand Down Expand Up @@ -219,10 +217,8 @@ func TestExcludesCleanup(t *testing.T) {

// TestNewExcludes tests NewExcludes.
func TestNewExcludes(t *testing.T) {
conf := daemoncfg.NewConfig()
e := NewExcludes(conf)
e := NewExcludes()
if e == nil ||
e.conf != conf ||
e.s == nil ||
e.d == nil ||
e.done == nil ||
Expand Down
38 changes: 0 additions & 38 deletions internal/splitrt/filter.go

This file was deleted.

Loading

0 comments on commit 1b43c55

Please sign in to comment.