fix(firewall): iptables support detection

- Add dummy rule to `INPUT` to test for iptables support
- This may resolve #896
This commit is contained in:
Quentin McGaw
2022-03-30 08:39:32 +00:00
parent 179274ade0
commit 20f20f051b
4 changed files with 77 additions and 48 deletions

View File

@@ -15,41 +15,11 @@ import (
)
var (
ErrIPTablesNotSupported = errors.New("no iptables supported found")
ErrNetAdminMissing = errors.New("NET_ADMIN capability is missing")
ErrIPTablesVersionTooShort = errors.New("iptables version string is too short")
ErrPolicyUnknown = errors.New("unknown policy")
ErrNeedIP6Tables = errors.New("ip6tables is required, please upgrade your kernel to support it")
)
func findIptablesSupported(ctx context.Context, runner command.Runner) (iptablesPath string, err error) {
binsToTry := []string{"iptables", "iptables-nft"}
var errMessage string
for _, iptablesPath = range binsToTry {
cmd := exec.CommandContext(ctx, iptablesPath, "-L")
errMessage, err = runner.Run(cmd)
if err == nil {
break
}
const permissionDeniedString = "Permission denied (you must be root)"
if strings.Contains(errMessage, permissionDeniedString) {
return "", fmt.Errorf("%w: %s (%s)", ErrNetAdminMissing, errMessage, err)
}
errMessage = fmt.Sprintf("%s (%s)", errMessage, err)
}
if err != nil {
return "", fmt.Errorf("%w: from %s: last error is: %s",
ErrIPTablesNotSupported, strings.Join(binsToTry, ", "),
errMessage)
}
return iptablesPath, nil
}
func appendOrDelete(remove bool) string {
if remove {
return "--delete"