Skip to content

Commit c5c62d5

Browse files
authored
fix(misconf): wrap Azure PortRange in iac types (#7357)
Signed-off-by: nikpivkin <[email protected]>
1 parent 0c6687d commit c5c62d5

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

pkg/iac/adapters/arm/network/adapt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func adaptSecurityGroups(deployment azure.Deployment) (sgs []network.SecurityGro
2727
func adaptSecurityGroup(resource azure.Resource, deployment azure.Deployment) network.SecurityGroup {
2828
return network.SecurityGroup{
2929
Metadata: resource.Metadata,
30-
Rules: adaptSecurityGroupRules(resource, deployment),
30+
Rules: adaptSecurityGroupRules(deployment),
3131
}
3232
}
3333

34-
func adaptSecurityGroupRules(resource azure.Resource, deployment azure.Deployment) (rules []network.SecurityGroupRule) {
34+
func adaptSecurityGroupRules(deployment azure.Deployment) (rules []network.SecurityGroupRule) {
3535
for _, resource := range deployment.GetResourcesByType("Microsoft.Network/networkSecurityGroups/securityRules") {
3636
rules = append(rules, adaptSecurityGroupRule(resource))
3737
}
@@ -120,7 +120,7 @@ func expandRange(r string, m iacTypes.Metadata) network.PortRange {
120120

121121
return network.PortRange{
122122
Metadata: m,
123-
Start: start,
124-
End: end,
123+
Start: iacTypes.Int(start, m),
124+
End: iacTypes.Int(end, m),
125125
}
126126
}

pkg/iac/adapters/terraform/azure/network/adapt.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func (a *adapter) adaptSource(ruleBlock *terraform.Block, rule *network.Security
136136
f := sourcePortRangeAttr.AsNumber()
137137
rule.SourcePorts = append(rule.SourcePorts, network.PortRange{
138138
Metadata: sourcePortRangeAttr.GetMetadata(),
139-
Start: int(f),
140-
End: int(f),
139+
Start: iacTypes.Int(int(f), sourcePortRangeAttr.GetMetadata()),
140+
End: iacTypes.Int(int(f), sourcePortRangeAttr.GetMetadata()),
141141
})
142142
}
143143
}
@@ -160,8 +160,8 @@ func (a *adapter) adaptDestination(ruleBlock *terraform.Block, rule *network.Sec
160160
f := destPortRangeAttr.AsNumber()
161161
rule.DestinationPorts = append(rule.DestinationPorts, network.PortRange{
162162
Metadata: destPortRangeAttr.GetMetadata(),
163-
Start: int(f),
164-
End: int(f),
163+
Start: iacTypes.Int(int(f), destPortRangeAttr.GetMetadata()),
164+
End: iacTypes.Int(int(f), destPortRangeAttr.GetMetadata()),
165165
})
166166
}
167167
}
@@ -189,8 +189,8 @@ func expandRange(r string, m iacTypes.Metadata) network.PortRange {
189189

190190
return network.PortRange{
191191
Metadata: m,
192-
Start: start,
193-
End: end,
192+
Start: iacTypes.Int(start, m),
193+
End: iacTypes.Int(end, m),
194194
}
195195
}
196196

pkg/iac/adapters/terraform/azure/network/adapt_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ func Test_Adapt(t *testing.T) {
6565
SourcePorts: []network.PortRange{
6666
{
6767
Metadata: iacTypes.NewTestMetadata(),
68-
Start: 0,
69-
End: 65535,
68+
Start: iacTypes.IntTest(0),
69+
End: iacTypes.IntTest(65535),
7070
},
7171
},
7272
DestinationPorts: []network.PortRange{
7373
{
7474
Metadata: iacTypes.NewTestMetadata(),
75-
Start: 3389,
76-
End: 3389,
75+
Start: iacTypes.IntTest(3389),
76+
End: iacTypes.IntTest(3389),
7777
},
7878
},
7979
Protocol: iacTypes.String("TCP", iacTypes.NewTestMetadata()),

pkg/iac/providers/azure/network/network.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ type SecurityGroupRule struct {
2727

2828
type PortRange struct {
2929
Metadata iacTypes.Metadata
30-
Start int
31-
End int
30+
Start iacTypes.IntValue
31+
End iacTypes.IntValue
3232
}
3333

3434
func (r PortRange) Includes(port int) bool {
35-
return port >= r.Start && port <= r.End
35+
return port >= r.Start.Value() && port <= r.End.Value()
3636
}
3737

3838
type NetworkWatcherFlowLog struct {

pkg/iac/rego/schemas/cloud.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -5207,10 +5207,12 @@
52075207
"$ref": "#/definitions/github.com.aquasecurity.trivy.pkg.iac.types.Metadata"
52085208
},
52095209
"end": {
5210-
"type": "integer"
5210+
"type": "object",
5211+
"$ref": "#/definitions/github.com.aquasecurity.trivy.pkg.iac.types.IntValue"
52115212
},
52125213
"start": {
5213-
"type": "integer"
5214+
"type": "object",
5215+
"$ref": "#/definitions/github.com.aquasecurity.trivy.pkg.iac.types.IntValue"
52145216
}
52155217
}
52165218
},

0 commit comments

Comments
 (0)