Debian Codegraph @

plugins/meta/firewall/ingresspolicy.go

raw symbols SCIP
1 // Copyright 2022 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This is a sample chained plugin that supports multiple CNI versions. It
16 // parses prevResult according to the cniVersion
17 package mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/
18
19 import (
20 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
21
22 "github.com/coreos/go-iptables/iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/"
23
24 types100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/"
25 "github.com/containernetworking/plugins/pkg/utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/"
26 )
27
28 func setupIngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIngressPolicy().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, prevResult *types100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#) error {
29 switch conf.IngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IngressPolicy. {
30 case "", IngressPolicyOpenscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicyOpen.:
31 // NOP
32 return nil
33 case IngressPolicySameBridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicySameBridge.:
34 return setupIngressPolicyBridgeIsolationscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIngressPolicyBridgeIsolation().(conf, prevResult, false)
35 case IngressPolicyIsolatedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicyIsolated.:
36 return setupIngressPolicyBridgeIsolationscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIngressPolicyBridgeIsolation().(conf, prevResult, true)
37 default:
38 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("unknown ingress policy: %q", conf.IngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IngressPolicy.)
39 }
40 }
41
42 func setupIngressPolicyBridgeIsolationscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIngressPolicyBridgeIsolation().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#, prevResult *types100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#, isolated bool) error {
43 if len(prevResult.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.) == 0 {
44 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("interface needs to be set for ingress policy %q, make sure to chain \"firewall\" plugin with \"bridge\"",
45 conf.IngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IngressPolicy.)
46 }
47 intf := prevResult.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.[0]
48 if intf == nil {
49 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("got nil interface")
50 }
51 bridgeName := intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.
52 if bridgeName == "" {
53 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("got empty bridge name")
54 }
55 for _, iptProto := range findProtosscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/findProtos().(conf) {
56 ipt, err := iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.NewWithProtocolscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/NewWithProtocol().(iptProto)
57 if err != nil {
58 return err
59 }
60 if err := setupIsolationChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIsolationChains().(ipt, bridgeName, isolated); err != nil {
61 return err
62 }
63 }
64 return nil
65 }
66
67 func teardownIngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/teardownIngressPolicy().(conf *FirewallNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#) error {
68 switch conf.IngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IngressPolicy. {
69 case "", IngressPolicyOpenscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicyOpen.:
70 // NOP
71 return nil
72 case IngressPolicySameBridgescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicySameBridge., IngressPolicyIsolatedscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/IngressPolicyIsolated.:
73 // NOP
74 //
75 // We can't be sure whether conf.bridgeName is still in use by other containers.
76 // So we do not remove the iptable rules that are created per bridge.
77 return nil
78 default:
79 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("unknown ingress policy: %q", conf.IngressPolicyscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/FirewallNetConf#IngressPolicy.)
80 }
81 }
82
83 const (
84 filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName. = "filter" // built-in
85 forwardChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/forwardChainName. = "FORWARD" // built-in
86 )
87
88 // setupIsolationChains executes the following iptables commands for isolating networks:
89 // ```
90 // iptables -N CNI-ISOLATION-STAGE-1
91 // iptables -N CNI-ISOLATION-STAGE-2
92 // # NOTE: "-j CNI-ISOLATION-STAGE-1" needs to be before "CNI-FORWARD" chain. So we use -I here.
93 // iptables -I FORWARD -j CNI-ISOLATION-STAGE-1
94 // iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} ! -o ${bridgeName} -j CNI-ISOLATION-STAGE-2
95 // [isolated=true] iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} -o ${bridgeName} -j DROP
96 // iptables -A CNI-ISOLATION-STAGE-1 -j RETURN
97 // iptables -A CNI-ISOLATION-STAGE-2 -o ${bridgeName} -j DROP
98 // iptables -A CNI-ISOLATION-STAGE-2 -j RETURN
99 // ```
100 func setupIsolationChainsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/setupIsolationChains().(ipt *iptablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/.IPTablesscip-go gomod github.com/coreos/go-iptables v0.8.0 `github.com/coreos/go-iptables/iptables`/IPTables#, bridgeName string, isolated bool) error {
101 const (
102 // Future version may support custom chain names
103 stage1Chain = "CNI-ISOLATION-STAGE-1"
104 stage2Chain = "CNI-ISOLATION-STAGE-2"
105 )
106
107 ingressPolicyName := "same-bridge"
108 if isolated {
109 ingressPolicyName = "isolated"
110 }
111
112 withPolicyComment := withPolicyCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withPolicyComment().(ingressPolicyName)
113 // Commands:
114 // ```
115 // iptables -N CNI-ISOLATION-STAGE-1
116 // iptables -N CNI-ISOLATION-STAGE-2
117 // ```
118 for _, chain := range []string{stage1Chain, stage2Chain} {
119 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.EnsureChainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/EnsureChain().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., chain); err != nil {
120 return err
121 }
122 }
123
124 // Commands:
125 // ```
126 // iptables -I FORWARD -j CNI-ISOLATION-STAGE-1
127 // ```
128 jumpToStage1 := withDefaultCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withDefaultComment().([]string{"-j", stage1Chain})
129 // NOTE: "-j CNI-ISOLATION-STAGE-1" needs to be before "CNI-FORWARD" created by CNI firewall plugin.
130 // So we specify prepend = true .
131 const jumpToStage1Prepend = true
132 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., forwardChainNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/forwardChainName., jumpToStage1Prepend, jumpToStage1); err != nil {
133 return err
134 }
135
136 // Commands:
137 // ```
138 // iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} ! -o ${bridgeName} -j CNI-ISOLATION-STAGE-2
139 // [isolate=true] iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} -o ${bridgeName} -j DROP
140 // iptables -A CNI-ISOLATION-STAGE-1 -j RETURN
141 // ```
142 stage1BridgeRule := withPolicyComment(isolationStage1BridgeRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage1BridgeRule().(bridgeName, stage2Chain))
143 stage1BridgeDropRule := withPolicyComment(isolationStage1BridgeDropRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage1BridgeDropRule().(bridgeName))
144 // prepend = true because this needs to be before "-j RETURN"
145 const stage1BridgePrepend = true
146 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., stage1Chain, stage1BridgePrepend, stage1BridgeRule); err != nil {
147 return err
148 }
149 if isolated {
150 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., stage1Chain, stage1BridgePrepend, stage1BridgeDropRule); err != nil {
151 return err
152 }
153 }
154
155 stage1Return := withDefaultCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withDefaultComment().([]string{"-j", "RETURN"})
156 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., stage1Chain, false, stage1Return); err != nil {
157 return err
158 }
159
160 // Commands:
161 // ```
162 // iptables -A CNI-ISOLATION-STAGE-2 -o ${bridgeName} -j DROP
163 // iptables -A CNI-ISOLATION-STAGE-2 -j RETURN
164 // ```
165 stage2Bridge := withDefaultCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withDefaultComment().(isolationStage2BridgeRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage2BridgeRule().(bridgeName))
166 // prepend = true because this needs to be before "-j RETURN"
167 const stage2BridgePrepend = true
168 if err := utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., stage2Chain, stage2BridgePrepend, stage2Bridge); err != nil {
169 return err
170 }
171 stage2Return := withDefaultCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withDefaultComment().([]string{"-j", "RETURN"})
172 return utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.InsertUniquescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/InsertUnique().(ipt, filterTableNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/filterTableName., stage2Chain, false, stage2Return)
173 }
174
175 func isolationStage1BridgeRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage1BridgeRule().(bridgeName, stage2Chain string) []string {
176 return []string{"-i", bridgeName, "!", "-o", bridgeName, "-j", stage2Chain}
177 }
178
179 func isolationStage1BridgeDropRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage1BridgeDropRule().(bridgeName string) []string {
180 return []string{"-i", bridgeName, "-o", bridgeName, "-j", "DROP"}
181 }
182
183 func isolationStage2BridgeRulescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/isolationStage2BridgeRule().(bridgeName string) []string {
184 return []string{"-o", bridgeName, "-j", "DROP"}
185 }
186
187 func withPolicyCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withPolicyComment().(policy string) func([]string) []string {
188 return func(rule []string) []string {
189 comment := fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("CNI firewall plugin rules (ingressPolicy: %s)", policy)
190
191 return withCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withComment().(rule, comment)
192 }
193 }
194
195 func withDefaultCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withDefaultComment().(rule []string) []string {
196 defaultComment := "CNI firewall plugin rules (ingressPolicy: same-bridge|isolated)"
197 return withCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withComment().(rule, defaultComment)
198 }
199
200 func withCommentscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/firewall`/withComment().(rule []string, comment string) []string {
201 return append(rule, []string{"-m", "comment", "--comment", comment}...)
202 }