Skip to content

Commit defdd1c

Browse files
committed
configure nft to block outgoing traffic and allow to certain ips
1 parent 146e178 commit defdd1c

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

cmds/modules/netlightd/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ func action(cli *cli.Context) error {
107107
return fmt.Errorf("failed to apply host nft rules: %w", err)
108108
}
109109
rules.Close()
110+
111+
if err := nft.UpdateNFTWhitelist(); err != nil {
112+
return fmt.Errorf("failed to allow whitelist outgoing traffic")
113+
}
114+
110115
bridge, err := netlight.CreateNDMZBridge()
111116
if err != nil {
112117
return fmt.Errorf("failed to create ndmz bridge: %w", err)

pkg/environment/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type Config struct {
2727
Users struct {
2828
Authorized []string `json:"authorized"`
2929
} `json:"users"`
30+
Whitelist struct {
31+
Ips []string `json:"ips"`
32+
} `json:"whitelist"`
3033
}
3134

3235
// Merge, updates current config with cfg merging and override config

pkg/netlight/nft/nft.go

+73
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package nft
22

33
import (
4+
"fmt"
45
"io"
56
"os/exec"
7+
"time"
68

9+
"github.com/go-co-op/gocron"
710
"github.com/rs/zerolog/log"
11+
"github.com/threefoldtech/zos/pkg/environment"
812

913
"github.com/pkg/errors"
1014
)
@@ -32,3 +36,72 @@ func Apply(r io.Reader, ns string) error {
3236
}
3337
return nil
3438
}
39+
40+
// UpdateNFTWhitelist periodically pull list of ips from config repo and
41+
// update the nft white list
42+
func UpdateNFTWhitelist() error {
43+
scheduler := gocron.NewScheduler(time.UTC)
44+
cron := "0 * * * *"
45+
46+
updateWhitelist := func() error {
47+
ips, err := whiteList()
48+
if err != nil {
49+
return err
50+
}
51+
52+
cmds := []string{
53+
"nft flush chain inet filter output",
54+
"nft add rule inet filter output ct state established,related accept",
55+
"nft add rule inet filter output tcp dport 22 accept",
56+
}
57+
58+
ipCmdTemplate := "nft add rule inet filter output ip daddr %s accept"
59+
blockCmd := "nft add rule inet filter output drop"
60+
61+
for _, cmd := range cmds {
62+
if err := runCommand(cmd); err != nil {
63+
return nil
64+
}
65+
}
66+
67+
for _, ip := range ips {
68+
if err := runCommand(fmt.Sprintf(ipCmdTemplate, ip)); err != nil {
69+
return nil
70+
}
71+
}
72+
73+
if err := runCommand(blockCmd); err != nil {
74+
return nil
75+
}
76+
77+
return nil
78+
}
79+
80+
if err := updateWhitelist(); err != nil {
81+
return err
82+
}
83+
84+
if _, err := scheduler.Cron(cron).Do(updateWhitelist); err != nil {
85+
return err
86+
}
87+
scheduler.StartAsync()
88+
89+
return nil
90+
}
91+
92+
func runCommand(cmdStr string) error {
93+
cmd := exec.Command("sh", "-c", cmdStr)
94+
if output, err := cmd.CombinedOutput(); err != nil {
95+
return fmt.Errorf("command failed: %s, output: %s", err, output)
96+
}
97+
return nil
98+
}
99+
100+
func whiteList() ([]string, error) {
101+
cfg, err := environment.GetConfig()
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
return cfg.Whitelist.Ips, nil
107+
}

0 commit comments

Comments
 (0)