Skip to content

Commit

Permalink
Merge pull request #144 from telekom-mms/feature/add-set-allowed-hosts
Browse files Browse the repository at this point in the history
Add commands for setting allowed hosts to TrafPol
  • Loading branch information
hwipl authored Jan 29, 2025
2 parents a677aee + f56fc37 commit 611c956
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
30 changes: 11 additions & 19 deletions internal/cmdtmpl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,29 +225,21 @@ add element inet oc-daemon-filter allowdevs { {{.}} }
},
defaultTemplate: TrafPolDefaultTemplate,
}
case "TrafPolFlushAllowedHosts":
// Flush Allowed Hosts
cl = &CommandList{
Name: name,
Commands: []*Command{
{Line: "{{.Executables.Nft}} -f - flush set inet oc-daemon-filter allowhosts4"},
{Line: "{{.Executables.Nft}} -f - flush set inet oc-daemon-filter allowhosts6"},
},
defaultTemplate: TrafPolDefaultTemplate,
}
case "TrafPolAddAllowedHost":
// Add Allowed Host
case "TrafPolSetAllowedHosts":
// Set Allowed Hosts
cl = &CommandList{
Name: name,
Commands: []*Command{
{Line: "{{.Executables.Nft}} -f -",
Stdin: `
{{if .AllowedIP.Addr.Is4}}
add element inet oc-daemon-filter allowhosts4 { {{.AllowedIP}} }
{{else}}
add element inet oc-daemon-filter allowhosts6 { {{.AllowedIP}} }
{{end}}
`,
Stdin: `flush set inet oc-daemon-filter allowhosts4
flush set inet oc-daemon-filter allowhosts6
{{range .AllowedIPs -}}
{{if .Addr.Is4 -}}
add element inet oc-daemon-filter allowhosts4 { {{.}} }
{{else -}}
add element inet oc-daemon-filter allowhosts6 { {{.}} }
{{end -}}
{{end}}`,
},
},
defaultTemplate: TrafPolDefaultTemplate,
Expand Down
8 changes: 3 additions & 5 deletions internal/cmdtmpl/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func TestGetCommandList(t *testing.T) {
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
"TrafPolSetAllowedDevices",
"TrafPolFlushAllowedHosts",
"TrafPolAddAllowedHost",
"TrafPolSetAllowedHosts",
"TrafPolSetAllowedPorts",
"TrafPolCleanup",

Expand Down Expand Up @@ -86,8 +85,7 @@ func TestGetCmds(t *testing.T) {
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
// TrafPolSetAllowedDevices", // skip, requires devices
"TrafPolFlushAllowedHosts",
// "TrafPolAddAllowedHost", // skip, requires host
// "TrafPolSetAllowedHosts", // skip, requires hosts
//"TrafPolSetAllowedPorts", // skip, requires ports
"TrafPolCleanup",

Expand All @@ -110,7 +108,7 @@ func TestGetCmds(t *testing.T) {
for _, name := range []string{
// Traffic Policing
"TrafPolSetAllowedDevices",
"TrafPolAddAllowedHost",
"TrafPolSetAllowedHosts",
"TrafPolSetAllowedPorts",

// VPN Setup
Expand Down
49 changes: 12 additions & 37 deletions internal/trafpol/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,29 @@ func setAllowedDevices(ctx context.Context, conf *daemoncfg.Config, devices []st

// setAllowedIPs set the allowed hosts.
func setAllowedIPs(ctx context.Context, conf *daemoncfg.Config, ips []netip.Prefix) {
// we perform all nft commands separately here and not as one atomic
// operation to avoid issues where the whole update fails because nft
// runs into "file exists" errors even though we remove duplicates from
// ips before calling this function and we flush the existing entries

// flush allowed hosts
cmds, err := cmdtmpl.GetCmds("TrafPolFlushAllowedHosts", conf)
// set allowed hosts
data := &struct {
daemoncfg.Config
AllowedIPs []netip.Prefix
}{
Config: *conf,
AllowedIPs: ips,
}
cmds, err := cmdtmpl.GetCmds("TrafPolSetAllowedHosts", data)
if err != nil {
log.WithError(err).Error("TrafPol could not get flush allowed hosts commands")
log.WithError(err).Error("TrafPol could not get set allowed hosts commands")
}
for _, c := range cmds {
if stdout, stderr, err := c.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
log.WithFields(log.Fields{
"hosts": ips,
"command": c.Cmd,
"args": c.Args,
"stdin": c.Stdin,
"stdout": string(stdout),
"stderr": string(stderr),
"error": err,
}).Error("TrafPol could not run flush allowed hosts command")
}
}

// add allowed hosts
for _, ip := range ips {
data := &struct {
daemoncfg.Config
AllowedIP netip.Prefix
}{
Config: *conf,
AllowedIP: ip,
}
cmds, err := cmdtmpl.GetCmds("TrafPolAddAllowedHost", data)
if err != nil {
log.WithError(err).Error("TrafPol could not get add allowed host commands")
}
for _, c := range cmds {
if stdout, stderr, err := c.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
log.WithFields(log.Fields{
"host": ip,
"command": c.Cmd,
"args": c.Args,
"stdin": c.Stdin,
"stdout": string(stdout),
"stderr": string(stderr),
"error": err,
}).Error("TrafPol could not run add allowed host command")
}
}).Error("TrafPol could not run set allowed hosts command")
}
}
}
Expand Down

0 comments on commit 611c956

Please sign in to comment.