@@ -32,18 +32,16 @@ var existingRules = []byte(`# iptables-save
32
32
:POSTROUTING ACCEPT [0:0]
33
33
:PROXY_INIT_OUTPUT - [0:0]
34
34
:PROXY_INIT_REDIRECT - [0:0]
35
- -A PREROUTING -m comment --comment "proxy-init/install-proxy-init-prerouting/testExecutionTraceID " -j PROXY_INIT_REDIRECT
36
- -A OUTPUT -m comment --comment "proxy-init/install-proxy-init-output/testExecutionTraceID " -j PROXY_INIT_OUTPUT
37
- -A PROXY_INIT_OUTPUT -o lo -m comment --comment "proxy-init/ignore-loopback/testExecutionTraceID " -j RETURN
38
- -A PROXY_INIT_OUTPUT -p tcp -m comment --comment "proxy-init/redirect-all-outgoing-to-proxy-port/testExecutionTraceID " -j REDIRECT --to-ports 1234
39
- -A PROXY_INIT_REDIRECT -p tcp -m multiport --dports 1234 -m comment --comment "proxy-init/ignore-port-1234/testExecutionTraceID " -j RETURN
35
+ -A PREROUTING -m comment --comment "proxy-init/install-proxy-init-prerouting" -j PROXY_INIT_REDIRECT
36
+ -A OUTPUT -m comment --comment "proxy-init/install-proxy-init-output" -j PROXY_INIT_OUTPUT
37
+ -A PROXY_INIT_OUTPUT -o lo -m comment --comment "proxy-init/ignore-loopback" -j RETURN
38
+ -A PROXY_INIT_OUTPUT -p tcp -m comment --comment "proxy-init/redirect-all-outgoing-to-proxy-port" -j REDIRECT --to-ports 1234
39
+ -A PROXY_INIT_REDIRECT -p tcp -m multiport --dports 1234 -m comment --comment "proxy-init/ignore-port-1234" -j RETURN
40
40
COMMIT
41
41
# Completed on Fri Jan 6 23:00:00 2023
42
42
` )
43
43
44
44
func TestAddIncomingTrafficRules (t * testing.T ) {
45
- ExecutionTraceID = "testExecutionTraceID"
46
-
47
45
for _ , tt := range []struct {
48
46
name string
49
47
existingRules []byte
@@ -53,16 +51,16 @@ func TestAddIncomingTrafficRules(t *testing.T) {
53
51
name : "no existing rules, create new chain and PREROUTING rule" ,
54
52
wantCommands : []* exec.Cmd {
55
53
exec .Command ("<iptables>" , "-t" , "nat" , "-N" , "PROXY_INIT_REDIRECT" ),
56
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_REDIRECT" , "-p" , "tcp" , "--match" , "multiport" , "--dports" , "1234" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-port-1234/testExecutionTraceID " ),
57
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PREROUTING" , "-j" , "PROXY_INIT_REDIRECT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-prerouting/testExecutionTraceID " ),
54
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_REDIRECT" , "-p" , "tcp" , "--match" , "multiport" , "--dports" , "1234" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-port-1234" ),
55
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PREROUTING" , "-j" , "PROXY_INIT_REDIRECT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-prerouting" ),
58
56
},
59
57
},
60
58
{
61
59
name : "existing rules, flush existing chain and reuse PREROUTING rule" ,
62
60
existingRules : existingRules ,
63
61
wantCommands : []* exec.Cmd {
64
62
exec .Command ("<iptables>" , "-t" , "nat" , "-F" , "PROXY_INIT_REDIRECT" ),
65
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_REDIRECT" , "-p" , "tcp" , "--match" , "multiport" , "--dports" , "1234" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-port-1234/testExecutionTraceID " ),
63
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_REDIRECT" , "-p" , "tcp" , "--match" , "multiport" , "--dports" , "1234" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-port-1234" ),
66
64
},
67
65
},
68
66
} {
@@ -78,8 +76,6 @@ func TestAddIncomingTrafficRules(t *testing.T) {
78
76
}
79
77
80
78
func TestAddOutgoingTrafficRules (t * testing.T ) {
81
- ExecutionTraceID = "testExecutionTraceID"
82
-
83
79
for _ , tt := range []struct {
84
80
name string
85
81
existingRules []byte
@@ -89,18 +85,18 @@ func TestAddOutgoingTrafficRules(t *testing.T) {
89
85
name : "no existing rules, create new chain and OUTPUT rule" ,
90
86
wantCommands : []* exec.Cmd {
91
87
exec .Command ("<iptables>" , "-t" , "nat" , "-N" , "PROXY_INIT_OUTPUT" ),
92
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-o" , "lo" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-loopback/testExecutionTraceID " ),
93
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-port" , "1234" , "-m" , "comment" , "--comment" , "proxy-init/redirect-all-outgoing-to-proxy-port/testExecutionTraceID " ),
94
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "OUTPUT" , "-j" , "PROXY_INIT_OUTPUT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-output/testExecutionTraceID " ),
88
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-o" , "lo" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-loopback" ),
89
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-port" , "1234" , "-m" , "comment" , "--comment" , "proxy-init/redirect-all-outgoing-to-proxy-port" ),
90
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "OUTPUT" , "-j" , "PROXY_INIT_OUTPUT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-output" ),
95
91
},
96
92
},
97
93
{
98
94
name : "existing rules, flush existing chain and reuse OUTPUT rule" ,
99
95
existingRules : existingRules ,
100
96
wantCommands : []* exec.Cmd {
101
97
exec .Command ("<iptables>" , "-t" , "nat" , "-F" , "PROXY_INIT_OUTPUT" ),
102
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-o" , "lo" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-loopback/testExecutionTraceID " ),
103
- exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-port" , "1234" , "-m" , "comment" , "--comment" , "proxy-init/redirect-all-outgoing-to-proxy-port/testExecutionTraceID " ),
98
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-o" , "lo" , "-j" , "RETURN" , "-m" , "comment" , "--comment" , "proxy-init/ignore-loopback" ),
99
+ exec .Command ("<iptables>" , "-t" , "nat" , "-A" , "PROXY_INIT_OUTPUT" , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-port" , "1234" , "-m" , "comment" , "--comment" , "proxy-init/redirect-all-outgoing-to-proxy-port" ),
104
100
},
105
101
},
106
102
} {
@@ -113,6 +109,26 @@ func TestAddOutgoingTrafficRules(t *testing.T) {
113
109
assertEqual (t , cmds , tt .wantCommands )
114
110
})
115
111
}
112
+
113
+ }
114
+
115
+ func TestCleanupFirewallConfig (t * testing.T ) {
116
+ wantCommands := []* exec.Cmd {
117
+ exec .Command ("<iptables>" , "-t" , "nat" , "-D" , "PREROUTING" , "-j" , "PROXY_INIT_REDIRECT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-prerouting" ),
118
+ exec .Command ("<iptables>" , "-t" , "nat" , "-D" , "OUTPUT" , "-j" , "PROXY_INIT_OUTPUT" , "-m" , "comment" , "--comment" , "proxy-init/install-proxy-init-output" ),
119
+ exec .Command ("<iptables>" , "-t" , "nat" , "-F" , "PROXY_INIT_OUTPUT" ),
120
+ exec .Command ("<iptables>" , "-t" , "nat" , "-F" , "PROXY_INIT_REDIRECT" ),
121
+ exec .Command ("<iptables>" , "-t" , "nat" , "-X" , "PROXY_INIT_OUTPUT" ),
122
+ exec .Command ("<iptables>" , "-t" , "nat" , "-X" , "PROXY_INIT_REDIRECT" ),
123
+ }
124
+
125
+ fc := & FirewallConfiguration {
126
+ BinPath : "<iptables>" ,
127
+ InboundPortsToIgnore : []string {"1234" },
128
+ }
129
+ cmds := fc .cleanupRules (nil )
130
+ assertEqual (t , cmds , wantCommands )
131
+
116
132
}
117
133
118
134
func assertEqual (t * testing.T , check , expected interface {}) {
0 commit comments