Skip to content

Commit bc379e4

Browse files
committed
chore(pkg/wireguard): replace createLinkUsingUserspaceImpl bash script with native code
There is no reason to use bash, or even to call out to the shell for most of what was happening. Replacing it allows for better error handling, clarity and reliability. Fixes: #79
1 parent 8565ade commit bc379e4

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ require (
470470
golang.org/x/net v0.24.0 // indirect
471471
golang.org/x/oauth2 v0.10.0 // indirect
472472
golang.org/x/sync v0.7.0 // indirect
473-
golang.org/x/sys v0.19.0 // indirect
473+
golang.org/x/sys v0.20.0 // indirect
474474
golang.org/x/term v0.19.0 // indirect
475475
golang.org/x/text v0.14.0 // indirect
476476
golang.org/x/time v0.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@ golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
13301330
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13311331
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
13321332
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1333+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
1334+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13331335
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
13341336
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
13351337
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

pkg/wireguard/wireguard.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package wireguard
22

33
import (
44
"fmt"
5-
"github.com/go-logr/logr"
65
"net"
6+
"os"
77
"os/exec"
88
"syscall"
99

10+
"github.com/go-logr/logr"
1011
"github.com/jodevsa/wireguard-operator/pkg/agent"
1112
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
1213
"github.com/vishvananda/netlink"
14+
"golang.org/x/sys/unix"
1315
"golang.zx2c4.com/wireguard/wgctrl"
1416
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
1517
)
@@ -71,17 +73,18 @@ func syncAddress(_ agent.State, iface string) error {
7173
}
7274

7375
func createLinkUsingUserspaceImpl(iface string, wgUserspaceImplementationFallback string) error {
74-
75-
bashCommand := fmt.Sprintf("mkdir -p /dev/net && if [ ! -c /dev/net/tun ]; then\n mknod /dev/net/tun c 10 200\nfi && %s %s", wgUserspaceImplementationFallback, iface)
76-
cmd := exec.Command("bash", "-c", bashCommand)
77-
78-
err := cmd.Run()
79-
if err != nil {
80-
return err
76+
if err := os.MkdirAll("/dev/net", 0o600); err != nil {
77+
return fmt.Errorf("mkdir all: %w", err)
8178
}
8279

83-
return nil
84-
80+
if _, err := os.Stat("/dev/net/tun"); os.IsNotExist(err) {
81+
if err := unix.Mknod("/dev/net/tun", unix.S_IFIFO|0o600, int(unix.Mkdev(10, 100))); err != nil {
82+
return fmt.Errorf("mknod: %w", err)
83+
}
84+
} else if err != nil {
85+
return fmt.Errorf("stat: %w", err)
86+
}
87+
return exec.Command(wgUserspaceImplementationFallback, iface).Run()
8588
}
8689

8790
func createLinkUsingKernalModule(iface string) error {

0 commit comments

Comments
 (0)