Skip to content

Commit 4fb8541

Browse files
authored
chore(internal/iptables): exec iptables-restore directly (#159)
iptables-restore was being executed via bash, which was unnecessary. Executing directly is simpler and more reliable. Fixes: #79
1 parent 8565ade commit 4fb8541

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

internal/iptables/iptables.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,18 @@ package iptables
22

33
import (
44
"fmt"
5+
"os/exec"
6+
"strings"
7+
58
"github.com/go-logr/logr"
69
"github.com/jodevsa/wireguard-operator/pkg/agent"
710
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
8-
"os"
9-
"os/exec"
10-
"strings"
1111
)
1212

1313
func ApplyRules(rules string) error {
14-
file, err := os.CreateTemp("/tmp", "iptables-")
15-
if err != nil {
16-
return err
17-
}
18-
defer os.RemoveAll(file.Name())
19-
20-
err = os.WriteFile(file.Name(), []byte(rules), 0640)
21-
22-
if err != nil {
23-
return err
24-
}
25-
26-
bashCommand := fmt.Sprintf("iptables-restore < %s", file.Name())
27-
cmd := exec.Command("bash", "-c", bashCommand)
28-
29-
err = cmd.Run()
30-
if err != nil {
31-
return err
32-
}
33-
34-
return nil
14+
cmd := exec.Command("iptables-restore")
15+
cmd.Stdin = strings.NewReader(rules)
16+
return cmd.Run()
3517
}
3618

3719
type Iptables struct {

0 commit comments

Comments
 (0)