Skip to content

Commit ffcbaaa

Browse files
riccardotorneselloadamjensenbot
authored andcommitted
fix: update ChainType handling to use direct values instead of pointers
this update solves an issue with the FirewallConfiguration's mutating webhook that returns a nil pointer exception when the Type field is omitted
1 parent cfb3144 commit ffcbaaa

File tree

11 files changed

+24
-29
lines changed

11 files changed

+24
-29
lines changed

apis/networking/v1beta1/firewall/chain_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Chain struct {
8383
Rules RulesSet `json:"rules"`
8484
// Type defines what this chain will be used for.
8585
// +kubebuilder:validation:Enum="filter";"route";"nat"
86-
Type *ChainType `json:"type"`
86+
Type ChainType `json:"type"`
8787
// Policy defines what this chain default policy will be.
8888
// +kubebuilder:validation:Enum="drop";"accept"
8989
Policy *ChainPolicy `json:"policy"`

apis/networking/v1beta1/firewall/zz_generated.deepcopy.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/firewall/chain.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func addChain(nftconn *nftables.Conn, chain *firewallapi.Chain, table *nftables.
5555
if chain.Priority != nil {
5656
setPriority(nftChain, *chain.Priority)
5757
}
58-
if chain.Type != nil {
59-
setType(nftChain, *chain.Type)
58+
if chain.Type != "" {
59+
setType(nftChain, chain.Type)
6060
}
6161
if chain.Policy != nil {
6262
setPolicy(nftChain, *chain.Policy)
@@ -198,7 +198,7 @@ func isChainOutdated(nftChain *nftables.Chain, chains []firewallapi.Chain) (outd
198198
// isChainModified checks if the chain has been modified.
199199
// It does not consider policies since they can be modified without deleting the chain.
200200
func isChainModified(nftChain *nftables.Chain, chain *firewallapi.Chain) bool {
201-
if chain.Type != nil && *chain.Type != getType(nftChain.Type) {
201+
if chain.Type != "" && chain.Type != getType(nftChain.Type) {
202202
return true
203203
}
204204
if chain.Hook != nil && *chain.Hook != getHooknum(*nftChain.Hooknum) {
@@ -215,7 +215,7 @@ func isChainModified(nftChain *nftables.Chain, chain *firewallapi.Chain) bool {
215215

216216
// FromChainToRulesArray converts a chain to an array of rules.
217217
func FromChainToRulesArray(chain *firewallapi.Chain) (rules []firewallutils.Rule) {
218-
switch *chain.Type {
218+
switch chain.Type {
219219
case firewallapi.ChainTypeFilter:
220220
rules = make([]firewallutils.Rule, len(chain.Rules.FilterRules))
221221
for i := range chain.Rules.FilterRules {

pkg/liqo-controller-manager/networking/external-network/remapping/cidr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func forgeCIDRFirewallConfigurationDNATChain(cfg *networkingv1beta1.Configuratio
113113
return firewall.Chain{
114114
Name: &DNATChainName,
115115
Policy: ptr.To(firewall.ChainPolicyAccept),
116-
Type: ptr.To(firewall.ChainTypeNAT),
116+
Type: firewall.ChainTypeNAT,
117117
Hook: &firewall.ChainHookPrerouting,
118118
Priority: &firewall.ChainPriorityNATDest,
119119
Rules: firewall.RulesSet{
@@ -127,7 +127,7 @@ func forgeCIDRFirewallConfigurationSNATChain(cfg *networkingv1beta1.Configuratio
127127
return firewall.Chain{
128128
Name: &SNATChainName,
129129
Policy: ptr.To(firewall.ChainPolicyAccept),
130-
Type: ptr.To(firewall.ChainTypeNAT),
130+
Type: firewall.ChainTypeNAT,
131131
Hook: &firewall.ChainHookPostrouting,
132132
Priority: &firewall.ChainPriorityNATSource,
133133
Rules: firewall.RulesSet{

pkg/liqo-controller-manager/networking/external-network/remapping/ip.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ func enforceFirewallConfigurationChains(fwcfg *networkingv1beta1.FirewallConfigu
174174
chainPre := &fwcfg.Spec.Table.Chains[0]
175175
chainPre.Name = &PreroutingChainName
176176
chainPre.Policy = ptr.To(firewall.ChainPolicyAccept)
177-
chainPre.Type = ptr.To(firewall.ChainTypeNAT)
177+
chainPre.Type = firewall.ChainTypeNAT
178178
chainPre.Hook = &firewall.ChainHookPrerouting
179179
chainPre.Priority = ptr.To(firewall.ChainPriorityNATDest)
180180
ensureFirewallConfigurationDNATRules(&chainPre.Rules, ip)
181181

182182
chainPost := &fwcfg.Spec.Table.Chains[1]
183183
chainPost.Name = &PostroutingChainName
184184
chainPost.Policy = ptr.To(firewall.ChainPolicyAccept)
185-
chainPost.Type = ptr.To(firewall.ChainTypeNAT)
185+
chainPost.Type = firewall.ChainTypeNAT
186186
chainPost.Hook = &firewall.ChainHookPostrouting
187187
chainPost.Priority = ptr.To(firewall.ChainPriorityNATSource)
188188
ensureFirewallConfigurationSNATRules(&chainPost.Rules, ip)
@@ -195,15 +195,15 @@ func enforceFirewallConfigurationMasqChains(fwcfg *networkingv1beta1.FirewallCon
195195
chainPre := &fwcfg.Spec.Table.Chains[0]
196196
chainPre.Name = &PreroutingChainName
197197
chainPre.Policy = ptr.To(firewall.ChainPolicyAccept)
198-
chainPre.Type = ptr.To(firewall.ChainTypeNAT)
198+
chainPre.Type = firewall.ChainTypeNAT
199199
chainPre.Hook = &firewall.ChainHookPrerouting
200200
chainPre.Priority = ptr.To(firewall.ChainPriorityNATDest)
201201
ensureFirewallConfigurationDNATRules(&chainPre.Rules, ip)
202202

203203
chainPost := &fwcfg.Spec.Table.Chains[1]
204204
chainPost.Name = &PostroutingChainName
205205
chainPost.Policy = ptr.To(firewall.ChainPolicyAccept)
206-
chainPost.Type = ptr.To(firewall.ChainTypeNAT)
206+
chainPost.Type = firewall.ChainTypeNAT
207207
chainPost.Hook = &firewall.ChainHookPostrouting
208208
chainPost.Priority = ptr.To(firewall.ChainPriorityNATSource - 1)
209209
ensureFirewallConfigurationMasqSNATRules(&chainPost.Rules, ip)

pkg/liqo-controller-manager/networking/internal-network/configuration-controller/firewall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func forgeMutateFirewallConfiguration(fwcfg *networkingv1beta1.FirewallConfigura
8484
func forgeFirewallChain() *firewallapi.Chain {
8585
return &firewallapi.Chain{
8686
Name: ptr.To(PrePostroutingChainName),
87-
Type: ptr.To(firewallapi.ChainTypeNAT),
87+
Type: firewallapi.ChainTypeNAT,
8888
Policy: ptr.To(firewallapi.ChainPolicyAccept),
8989
Priority: ptr.To(firewallapi.ChainPriorityNATSource - 1),
9090
Hook: ptr.To(firewallapi.ChainHookPostrouting),

pkg/liqo-controller-manager/networking/internal-network/gw-masq-bypass/k8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func forgeFirewallPodUpdateFunction(internalnode *networkingv1beta1.InternalNode
129129

130130
func setFirewallPodChain(chain *firewall.Chain) {
131131
chain.Name = ptr.To(PrePostroutingChainName)
132-
chain.Type = ptr.To(firewall.ChainTypeNAT)
132+
chain.Type = firewall.ChainTypeNAT
133133
chain.Hook = ptr.To(firewall.ChainHookPostrouting)
134134
chain.Policy = ptr.To(firewall.ChainPolicyAccept)
135135
chain.Priority = ptr.To(firewall.ChainPriorityNATSource - 1)

pkg/liqo-controller-manager/networking/internal-network/gw-masq-bypass/leftovers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (r *PodReconciler) processFirewallConfiguration(ctx context.Context, fwcfgl
8888

8989
chain := fwcfglist.Items[i].Spec.Table.Chains[0]
9090

91-
if chain.Type == nil || *chain.Type != firewall.ChainTypeNAT {
92-
return fmt.Errorf("firewall configuration table chain should be of type NAT, not %s", *chain.Type)
91+
if chain.Type == "" || chain.Type != firewall.ChainTypeNAT {
92+
return fmt.Errorf("firewall configuration table chain should be of type NAT, not %s", chain.Type)
9393
}
9494

9595
if err := r.processRules(ctx, &chain, getNodeFromFirewallConfigurationName(fwcfglist.Items[i].Name)); err != nil {

pkg/liqo-controller-manager/networking/internal-network/route/internalnode_k8s.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func enforceFirewallConfigurationForwardChain(fwcfg *networkingv1beta1.FirewallC
126126
fwcfg.Spec.Table.Chains = append(fwcfg.Spec.Table.Chains, firewall.Chain{})
127127
}
128128
fwcfg.Spec.Table.Chains[0].Name = ptr.To("mark-to-conntrack")
129-
fwcfg.Spec.Table.Chains[0].Type = ptr.To(firewall.ChainTypeFilter)
129+
fwcfg.Spec.Table.Chains[0].Type = firewall.ChainTypeFilter
130130
fwcfg.Spec.Table.Chains[0].Policy = ptr.To(firewall.ChainPolicyAccept)
131131
fwcfg.Spec.Table.Chains[0].Hook = &firewall.ChainHookForward
132132
fwcfg.Spec.Table.Chains[0].Priority = &firewall.ChainPriorityFilter
@@ -171,7 +171,7 @@ func enforceFirewallConfigurationPreroutingChain(fwcfg *networkingv1beta1.Firewa
171171
fwcfg.Spec.Table.Chains = append(fwcfg.Spec.Table.Chains, firewall.Chain{})
172172
}
173173
fwcfg.Spec.Table.Chains[1].Name = ptr.To("conntrack-mark-to-meta-mark")
174-
fwcfg.Spec.Table.Chains[1].Type = ptr.To(firewall.ChainTypeFilter)
174+
fwcfg.Spec.Table.Chains[1].Type = firewall.ChainTypeFilter
175175
fwcfg.Spec.Table.Chains[1].Policy = ptr.To(firewall.ChainPolicyAccept)
176176
fwcfg.Spec.Table.Chains[1].Hook = ptr.To(firewall.ChainHookPrerouting)
177177
fwcfg.Spec.Table.Chains[1].Priority = ptr.To(firewall.ChainPriorityFilter)

pkg/webhooks/firewallconfiguration/chain.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func checkChain(tableFamily firewallapi.TableFamily, chain *firewallapi.Chain) e
3939
}
4040

4141
func checkAllowedTableFamilyChainTypeHook(tableFamily firewallapi.TableFamily, chain *firewallapi.Chain) error {
42-
if !allowedTableFamilyChainTypeHook(tableFamily, *chain.Type, *chain.Hook) {
42+
if !allowedTableFamilyChainTypeHook(tableFamily, chain.Type, *chain.Hook) {
4343
return fmt.Errorf(`in chain %s, the combination of family %s, chain type %s and hook %s is not allowed.
4444
Please refer to https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks`,
45-
*chain.Name, tableFamily, *chain.Type, *chain.Hook,
45+
*chain.Name, tableFamily, chain.Type, *chain.Hook,
4646
)
4747
}
4848
return nil
@@ -69,14 +69,14 @@ func totalDefinedRulesSets(rules firewallapi.RulesSet) int {
6969
return total
7070
}
7171

72-
func allowedChainType(chaintype *firewallapi.ChainType, rules firewallapi.RulesSet) error {
73-
if rules.NatRules != nil && *chaintype != firewallapi.ChainTypeNAT {
72+
func allowedChainType(chaintype firewallapi.ChainType, rules firewallapi.RulesSet) error {
73+
if rules.NatRules != nil && chaintype != firewallapi.ChainTypeNAT {
7474
return fmt.Errorf("NAT rules must be defined only when using NAT chain")
7575
}
76-
if rules.FilterRules != nil && *chaintype != firewallapi.ChainTypeFilter {
76+
if rules.FilterRules != nil && chaintype != firewallapi.ChainTypeFilter {
7777
return fmt.Errorf("filter rules must be defined only when using Filter chain")
7878
}
79-
if rules.RouteRules != nil && *chaintype != firewallapi.ChainTypeRoute {
79+
if rules.RouteRules != nil && chaintype != firewallapi.ChainTypeRoute {
8080
return fmt.Errorf("route rules must be defined only when using Route chain")
8181
}
8282

0 commit comments

Comments
 (0)