Skip to content

Commit 7205a44

Browse files
committed
Revert "update detection logic"
This reverts commit 13138e5.
1 parent 451c21f commit 7205a44

File tree

3 files changed

+101
-165
lines changed

3 files changed

+101
-165
lines changed

pkg/iptables/detect.go

Lines changed: 0 additions & 145 deletions
This file was deleted.

pkg/iptables/iptables.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ type FirewallConfiguration struct {
6868
func ConfigureFirewall(firewallConfiguration FirewallConfiguration) error {
6969
log.Debugf("tracing script execution as [%s]", executionTraceID)
7070

71-
// Using configured iptables binaries as-is.
71+
// Before executing, ensure the configured iptables binaries exist; if not, attempt a fallback.
72+
resolveBinFallback(&firewallConfiguration)
7273

7374
existingRules, err := executeCommand(firewallConfiguration, firewallConfiguration.makeShowAllRules())
7475
if err != nil {
@@ -112,7 +113,8 @@ func CleanupFirewallConfig(firewallConfiguration FirewallConfiguration) error {
112113
log.Debugf("using '%s' to clean-up firewall rules", firewallConfiguration.BinPath)
113114
log.Debugf("using '%s' to list all available rules", firewallConfiguration.SaveBinPath)
114115

115-
// Using configured iptables binaries as-is for cleanup as well.
116+
// Ensure binaries exist before attempting cleanup as well
117+
resolveBinFallback(&firewallConfiguration)
116118

117119
commands := make([]*exec.Cmd, 0)
118120
commands = firewallConfiguration.cleanupRules(commands)
@@ -451,4 +453,61 @@ func asDestination(portRange util.PortRange) string {
451453
return fmt.Sprintf("%d:%d", portRange.LowerBound, portRange.UpperBound)
452454
}
453455

454-
// resolveBinFallback removed: binaries are assumed to be correctly set by callers.
456+
// resolveBinFallback ensures the configured BinPath and SaveBinPath exist on PATH; if not, it
457+
// tries reasonable alternatives of the same family (ip6tables vs iptables). Returns true if a
458+
// fallback was applied.
459+
func resolveBinFallback(fc *FirewallConfiguration) {
460+
// helper to check presence
461+
has := func(name string) bool {
462+
_, err := exec.LookPath(name)
463+
return err == nil
464+
}
465+
466+
// Both present? nothing to do
467+
if has(fc.BinPath) && has(fc.SaveBinPath) {
468+
log.WithFields(log.Fields{
469+
"requestedBin": fc.BinPath,
470+
"requestedSaveBin": fc.SaveBinPath,
471+
}).Debug("iptables: using configured binaries")
472+
return
473+
}
474+
475+
// Decide family based on current name
476+
ipv6 := strings.Contains(fc.BinPath, "ip6tables") || strings.Contains(fc.SaveBinPath, "ip6tables")
477+
478+
// Candidate orders: prefer nft, then plain, then legacy
479+
var candidates [][2]string
480+
if ipv6 {
481+
candidates = [][2]string{
482+
{"ip6tables-nft", "ip6tables-nft-save"},
483+
{"ip6tables", "ip6tables-save"},
484+
{"ip6tables-legacy", "ip6tables-legacy-save"},
485+
}
486+
} else {
487+
candidates = [][2]string{
488+
{"iptables-nft", "iptables-nft-save"},
489+
{"iptables", "iptables-save"},
490+
{"iptables-legacy", "iptables-legacy-save"},
491+
}
492+
}
493+
494+
// Use first candidate where both exist
495+
for _, pair := range candidates {
496+
if has(pair[0]) && has(pair[1]) {
497+
if pair[0] != fc.BinPath || pair[1] != fc.SaveBinPath {
498+
log.WithFields(log.Fields{
499+
"requestedBin": fc.BinPath,
500+
"requestedSaveBin": fc.SaveBinPath,
501+
"fallbackBin": pair[0],
502+
"fallbackSaveBin": pair[1],
503+
}).Warn("iptables: configured binaries not found; applying fallback to available binaries")
504+
}
505+
fc.BinPath = pair[0]
506+
fc.SaveBinPath = pair[1]
507+
return
508+
}
509+
}
510+
511+
// No candidates found; keep as-is and let execution fail with a clear error later
512+
log.WithFields(log.Fields{"binPath": fc.BinPath, "saveBinPath": fc.SaveBinPath}).Error("iptables: no suitable binaries found on PATH; commands may fail")
513+
}

proxy-init/cmd/root.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const (
2121
// IPTablesModePlain signals the usage of the iptables commands, which
2222
// can be either legacy or nft
2323
IPTablesModePlain = "plain"
24-
// IPTablesModeAuto signals automatic detection of the iptables backend
25-
IPTablesModeAuto = "auto"
2624

2725
cmdLegacy = "iptables-legacy"
2826
cmdLegacySave = "iptables-legacy-save"
@@ -167,11 +165,21 @@ func NewRootCmd() *cobra.Command {
167165

168166
// BuildFirewallConfiguration returns an iptables FirewallConfiguration suitable to use to configure iptables.
169167
func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfiguration, error) {
170-
if options.IPTablesMode != "" &&
171-
options.IPTablesMode != IPTablesModeLegacy &&
172-
options.IPTablesMode != IPTablesModeNFT &&
173-
options.IPTablesMode != IPTablesModePlain {
174-
return nil, fmt.Errorf("--iptables-mode valid values are only \"%s\", \"%s\", \"%s\", and \"%s\"", IPTablesModeLegacy, IPTablesModeNFT, IPTablesModeAuto, IPTablesModePlain)
168+
if options.IPTablesMode != "" && options.IPTablesMode != IPTablesModeLegacy && options.IPTablesMode != IPTablesModeNFT && options.IPTablesMode != IPTablesModePlain {
169+
return nil, fmt.Errorf("--iptables-mode valid values are only \"%s\", \"%s\" and \"%s\"", IPTablesModeLegacy, IPTablesModeNFT, IPTablesModePlain)
170+
}
171+
172+
if options.IPTablesMode == "" {
173+
switch options.FirewallBinPath {
174+
case "", cmdLegacy:
175+
options.IPTablesMode = IPTablesModeLegacy
176+
case cmdNFT:
177+
options.IPTablesMode = IPTablesModeNFT
178+
case cmdPlain:
179+
options.IPTablesMode = IPTablesModePlain
180+
default:
181+
return nil, fmt.Errorf("--firewall-bin-path valid values are only \"%s\", \"%s\" and \"%s\"", cmdLegacy, cmdNFT, cmdPlain)
182+
}
175183
}
176184

177185
if !util.IsValidPort(options.IncomingProxyPort) {
@@ -182,6 +190,8 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
182190
return nil, fmt.Errorf("--outgoing-proxy-port must be a valid TCP port number")
183191
}
184192

193+
cmd, cmdSave := getCommands(options)
194+
185195
sanitizedSubnets := []string{}
186196
for _, subnet := range options.SubnetsToIgnore {
187197
subnet := strings.TrimSpace(subnet)
@@ -205,6 +215,8 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
205215
SimulateOnly: options.SimulateOnly,
206216
NetNs: options.NetNs,
207217
UseWaitFlag: options.UseWaitFlag,
218+
BinPath: cmd,
219+
SaveBinPath: cmdSave,
208220
}
209221

210222
if len(options.PortsToRedirect) > 0 {
@@ -213,16 +225,6 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
213225
firewallConfiguration.Mode = iptables.RedirectAllMode
214226
}
215227

216-
// For backwards-compatibility, if IPTablesMode is not set, use the FirewallBinPath
217-
// explicitly set by the user.
218-
if options.IPTablesMode == "" {
219-
firewallConfiguration.BinPath = options.FirewallBinPath
220-
firewallConfiguration.SaveBinPath = options.FirewallSaveBinPath
221-
} else {
222-
// Otherwise, detect and set the appropriate backend.
223-
iptables.DetectBackend(firewallConfiguration, exec.LookPath, options.IPv6, options.IPTablesMode)
224-
}
225-
226228
return firewallConfiguration, nil
227229
}
228230

@@ -235,6 +237,26 @@ func getFormatter(format string) log.Formatter {
235237
}
236238
}
237239

240+
func getCommands(options *RootOptions) (string, string) {
241+
switch options.IPTablesMode {
242+
case IPTablesModeLegacy:
243+
if options.IPv6 {
244+
return cmdLegacyIPv6, cmdLegacyIPv6Save
245+
}
246+
return cmdLegacy, cmdLegacySave
247+
case IPTablesModeNFT:
248+
if options.IPv6 {
249+
return cmdNFTIPv6, cmdNFTIPv6Save
250+
}
251+
return cmdNFT, cmdNFTSave
252+
default:
253+
if options.IPv6 {
254+
return cmdPlainIPv6, cmdPlainIPv6Save
255+
}
256+
return cmdPlain, cmdPlainSave
257+
}
258+
}
259+
238260
func setLogLevel(logLevel string) error {
239261
level, err := log.ParseLevel(logLevel)
240262
if err != nil {

0 commit comments

Comments
 (0)