@@ -43,8 +43,6 @@ const (
43
43
44
44
// An injectable interface for running iptables commands. Implementations must be goroutine-safe.
45
45
type Interface interface {
46
- // GetVersion returns the "X.Y.Z" version string for iptables.
47
- GetVersion () (string , error )
48
46
// EnsureChain checks if the specified chain exists and, if not, creates it. If the chain existed, return true.
49
47
EnsureChain (table Table , chain Chain ) (bool , error )
50
48
// FlushChain clears the specified chain. If the chain did not exist, return error.
@@ -121,12 +119,13 @@ const NoFlushTables FlushFlag = false
121
119
122
120
// Versions of iptables less than this do not support the -C / --check flag
123
121
// (test whether a rule exists).
124
- const MinCheckVersion = "1.4.11"
122
+ var MinCheckVersion = utilversion . MustParseGeneric ( "1.4.11" )
125
123
126
124
// Minimum iptables versions supporting the -w and -w<seconds> flags
127
- const WaitMinVersion = "1.4.20"
128
- const WaitSecondsMinVersion = "1.4.22"
129
- const WaitRestoreMinVersion = "1.6.2"
125
+ var WaitMinVersion = utilversion .MustParseGeneric ("1.4.20" )
126
+ var WaitSecondsMinVersion = utilversion .MustParseGeneric ("1.4.22" )
127
+ var WaitRestoreMinVersion = utilversion .MustParseGeneric ("1.6.2" )
128
+
130
129
const WaitString = "-w"
131
130
const WaitSecondsValue = "5"
132
131
@@ -151,10 +150,10 @@ type runner struct {
151
150
// newInternal returns a new Interface which will exec iptables, and allows the
152
151
// caller to change the iptables-restore lockfile path
153
152
func newInternal (exec utilexec.Interface , dbus utildbus.Interface , protocol Protocol , lockfilePath string ) Interface {
154
- vstring , err := getIPTablesVersionString (exec , protocol )
153
+ version , err := getIPTablesVersion (exec , protocol )
155
154
if err != nil {
156
155
klog .Warningf ("Error checking iptables version, assuming version at least %s: %v" , MinCheckVersion , err )
157
- vstring = MinCheckVersion
156
+ version = MinCheckVersion
158
157
}
159
158
160
159
if lockfilePath == "" {
@@ -165,10 +164,10 @@ func newInternal(exec utilexec.Interface, dbus utildbus.Interface, protocol Prot
165
164
exec : exec ,
166
165
dbus : dbus ,
167
166
protocol : protocol ,
168
- hasCheck : getIPTablesHasCheckCommand ( vstring ),
167
+ hasCheck : version . AtLeast ( MinCheckVersion ),
169
168
hasListener : false ,
170
- waitFlag : getIPTablesWaitFlag (vstring ),
171
- restoreWaitFlag : getIPTablesRestoreWaitFlag (vstring ),
169
+ waitFlag : getIPTablesWaitFlag (version ),
170
+ restoreWaitFlag : getIPTablesRestoreWaitFlag (version ),
172
171
lockfilePath : lockfilePath ,
173
172
}
174
173
return runner
@@ -215,11 +214,6 @@ func (runner *runner) connectToFirewallD() {
215
214
go runner .dbusSignalHandler (bus )
216
215
}
217
216
218
- // GetVersion returns the version string.
219
- func (runner * runner ) GetVersion () (string , error ) {
220
- return getIPTablesVersionString (runner .exec , runner .protocol )
221
- }
222
-
223
217
// EnsureChain is part of Interface.
224
218
func (runner * runner ) EnsureChain (table Table , chain Chain ) (bool , error ) {
225
219
fullArgs := makeFullArgs (table , chain )
@@ -540,83 +534,46 @@ func makeFullArgs(table Table, chain Chain, args ...string) []string {
540
534
return append ([]string {string (chain ), "-t" , string (table )}, args ... )
541
535
}
542
536
543
- // Checks if iptables has the "-C" flag
544
- func getIPTablesHasCheckCommand (vstring string ) bool {
545
- minVersion , err := utilversion .ParseGeneric (MinCheckVersion )
546
- if err != nil {
547
- klog .Errorf ("MinCheckVersion (%s) is not a valid version string: %v" , MinCheckVersion , err )
548
- return true
549
- }
550
- version , err := utilversion .ParseGeneric (vstring )
551
- if err != nil {
552
- klog .Errorf ("vstring (%s) is not a valid version string: %v" , vstring , err )
553
- return true
554
- }
555
- return version .AtLeast (minVersion )
556
- }
557
-
558
- // Checks if iptables version has a "wait" flag
559
- func getIPTablesWaitFlag (vstring string ) []string {
560
- version , err := utilversion .ParseGeneric (vstring )
561
- if err != nil {
562
- klog .Errorf ("vstring (%s) is not a valid version string: %v" , vstring , err )
563
- return nil
564
- }
565
-
566
- minVersion , err := utilversion .ParseGeneric (WaitMinVersion )
567
- if err != nil {
568
- klog .Errorf ("WaitMinVersion (%s) is not a valid version string: %v" , WaitMinVersion , err )
569
- return nil
570
- }
571
- if version .LessThan (minVersion ) {
572
- return nil
573
- }
574
-
575
- minVersion , err = utilversion .ParseGeneric (WaitSecondsMinVersion )
576
- if err != nil {
577
- klog .Errorf ("WaitSecondsMinVersion (%s) is not a valid version string: %v" , WaitSecondsMinVersion , err )
578
- return nil
579
- }
580
- if version .LessThan (minVersion ) {
581
- return []string {WaitString }
582
- }
583
- return []string {WaitString , WaitSecondsValue }
584
- }
585
-
586
- // getIPTablesVersionString runs "iptables --version" to get the version string
587
- // in the form "X.X.X"
588
- func getIPTablesVersionString (exec utilexec.Interface , protocol Protocol ) (string , error ) {
537
+ // getIPTablesVersion runs "iptables --version" and parses the returned version
538
+ func getIPTablesVersion (exec utilexec.Interface , protocol Protocol ) (* utilversion.Version , error ) {
589
539
// this doesn't access mutable state so we don't need to use the interface / runner
590
540
iptablesCmd := iptablesCommand (protocol )
591
541
bytes , err := exec .Command (iptablesCmd , "--version" ).CombinedOutput ()
592
542
if err != nil {
593
- return "" , err
543
+ return nil , err
594
544
}
595
545
versionMatcher := regexp .MustCompile ("v([0-9]+(\\ .[0-9]+)+)" )
596
546
match := versionMatcher .FindStringSubmatch (string (bytes ))
597
547
if match == nil {
598
- return "" , fmt .Errorf ("no iptables version found in string: %s" , bytes )
548
+ return nil , fmt .Errorf ("no iptables version found in string: %s" , bytes )
599
549
}
600
- return match [1 ], nil
601
- }
602
-
603
- // Checks if iptables-restore has a "wait" flag
604
- func getIPTablesRestoreWaitFlag (vstring string ) []string {
605
- version , err := utilversion .ParseGeneric (vstring )
550
+ version , err := utilversion .ParseGeneric (match [1 ])
606
551
if err != nil {
607
- klog .Errorf ("vstring (%s) is not a valid version string: %v" , vstring , err )
608
- return nil
552
+ return nil , fmt .Errorf ("iptables version %q is not a valid version string: %v" , match [1 ], err )
609
553
}
610
554
611
- minVersion , err := utilversion .ParseGeneric (WaitRestoreMinVersion )
612
- if err != nil {
613
- klog .Errorf ("WaitRestoreMinVersion (%s) is not a valid version string: %v" , WaitRestoreMinVersion , err )
555
+ return version , nil
556
+ }
557
+
558
+ // Checks if iptables version has a "wait" flag
559
+ func getIPTablesWaitFlag (version * utilversion.Version ) []string {
560
+ switch {
561
+ case version .AtLeast (WaitSecondsMinVersion ):
562
+ return []string {WaitString , WaitSecondsValue }
563
+ case version .AtLeast (WaitMinVersion ):
564
+ return []string {WaitString }
565
+ default :
614
566
return nil
615
567
}
616
- if version .LessThan (minVersion ) {
568
+ }
569
+
570
+ // Checks if iptables-restore has a "wait" flag
571
+ func getIPTablesRestoreWaitFlag (version * utilversion.Version ) []string {
572
+ if version .AtLeast (WaitRestoreMinVersion ) {
573
+ return []string {WaitString , WaitSecondsValue }
574
+ } else {
617
575
return nil
618
576
}
619
- return []string {WaitString , WaitSecondsValue }
620
577
}
621
578
622
579
// goroutine to listen for D-Bus signals
0 commit comments