Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions pkg/iac/adapters/arm/network/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types"
)

const (
securityGroupType = "Microsoft.Network/networkSecurityGroups"
securityRuleType = "securityRules"
)

func parsePortRange(input string, meta iacTypes.Metadata) common.PortRange {
return common.ParsePortRange(input, meta, common.WithWildcard())
}
Expand All @@ -20,79 +25,89 @@ func Adapt(deployment azure.Deployment) network.Network {
}

func adaptSecurityGroups(deployment azure.Deployment) (sgs []network.SecurityGroup) {
for _, resource := range deployment.GetResourcesByType("Microsoft.Network/networkSecurityGroups") {
for _, resource := range deployment.GetResourcesByType(securityGroupType) {
sgs = append(sgs, adaptSecurityGroup(resource, deployment))
}
if orphans := deployment.GetOrphanResources(securityGroupType, securityRuleType); len(orphans) > 0 {
sgs = append(sgs, network.SecurityGroup{
Metadata: iacTypes.NewUnmanagedMetadata(),
Rules: adaptSecurityRuleResources(orphans),
})
}
return sgs

}

func adaptSecurityGroup(resource azure.Resource, deployment azure.Deployment) network.SecurityGroup {
var rules []network.SecurityGroupRule
for _, rule := range resource.Properties.GetMapValue("securityRules").AsList() {
rules = append(rules, adaptSecurityGroupRule(rule.GetMapValue("properties"), rule.GetMetadata()))
}
return network.SecurityGroup{
Metadata: resource.Metadata,
Rules: adaptSecurityGroupRules(deployment),
Rules: append(rules, adaptSecurityRuleResources(deployment.GetChildResources(resource, securityRuleType))...),
}
}

func adaptSecurityGroupRules(deployment azure.Deployment) (rules []network.SecurityGroupRule) {
for _, resource := range deployment.GetResourcesByType("Microsoft.Network/networkSecurityGroups/securityRules") {
rules = append(rules, adaptSecurityGroupRule(resource))
func adaptSecurityRuleResources(resources []azure.Resource) []network.SecurityGroupRule {
var rules []network.SecurityGroupRule
for _, resource := range resources {
rules = append(rules, adaptSecurityGroupRule(resource.Properties, resource.Metadata))
}
return rules
}

func adaptSecurityGroupRule(resource azure.Resource) network.SecurityGroupRule {
sourceAddressPrefixes := resource.Properties.GetMapValue("sourceAddressPrefixes").AsStringValuesList("")
if prefix := resource.Properties.GetMapValue("sourceAddressPrefix").AsStringValue("", resource.Metadata); prefix.IsNotEmpty() {
func adaptSecurityGroupRule(properties azure.Value, metadata iacTypes.Metadata) network.SecurityGroupRule {
sourceAddressPrefixes := properties.GetMapValue("sourceAddressPrefixes").AsStringValuesList("")
if prefix := properties.GetMapValue("sourceAddressPrefix").AsStringValue("", metadata); prefix.IsNotEmpty() {
sourceAddressPrefixes = append(sourceAddressPrefixes, prefix)
}

var sourcePortRanges []common.PortRange
for _, portRange := range resource.Properties.GetMapValue("sourcePortRanges").AsList() {
rng := parsePortRange(portRange.AsString(), resource.Metadata)
for _, portRange := range properties.GetMapValue("sourcePortRanges").AsList() {
rng := parsePortRange(portRange.AsString(), metadata)
if rng.Valid() {
sourcePortRanges = append(sourcePortRanges, rng)
}
}
if rng := parsePortRange(resource.Properties.GetMapValue("sourcePortRange").AsString(), resource.Metadata); rng.Valid() {
if rng := parsePortRange(properties.GetMapValue("sourcePortRange").AsString(), metadata); rng.Valid() {
sourcePortRanges = append(sourcePortRanges, rng)
}

destinationAddressPrefixes := resource.Properties.GetMapValue("destinationAddressPrefixes").AsStringValuesList("")
if prefix := resource.Properties.GetMapValue("destinationAddressPrefix").AsStringValue("", resource.Metadata); prefix.IsNotEmpty() {
destinationAddressPrefixes := properties.GetMapValue("destinationAddressPrefixes").AsStringValuesList("")
if prefix := properties.GetMapValue("destinationAddressPrefix").AsStringValue("", metadata); prefix.IsNotEmpty() {
destinationAddressPrefixes = append(destinationAddressPrefixes, prefix)
}

var destinationPortRanges []common.PortRange
for _, portRange := range resource.Properties.GetMapValue("destinationPortRanges").AsList() {
rng := parsePortRange(portRange.AsString(), resource.Metadata)
for _, portRange := range properties.GetMapValue("destinationPortRanges").AsList() {
rng := parsePortRange(portRange.AsString(), metadata)
if rng.Valid() {
destinationPortRanges = append(destinationPortRanges, rng)
}
}
if rng := parsePortRange(resource.Properties.GetMapValue("destinationPortRange").AsString(), resource.Metadata); rng.Valid() {
if rng := parsePortRange(properties.GetMapValue("destinationPortRange").AsString(), metadata); rng.Valid() {
destinationPortRanges = append(destinationPortRanges, rng)
}

allow := iacTypes.BoolDefault(false, resource.Metadata)
if resource.Properties.GetMapValue("access").AsString() == "Allow" {
allow = iacTypes.Bool(true, resource.Metadata)
allow := iacTypes.BoolDefault(false, metadata)
if properties.GetMapValue("access").AsString() == "Allow" {
allow = iacTypes.Bool(true, metadata)
}

outbound := iacTypes.BoolDefault(false, resource.Metadata)
if resource.Properties.GetMapValue("direction").AsString() == "Outbound" {
outbound = iacTypes.Bool(true, resource.Metadata)
outbound := iacTypes.BoolDefault(false, metadata)
if properties.GetMapValue("direction").AsString() == "Outbound" {
outbound = iacTypes.Bool(true, metadata)
}

return network.SecurityGroupRule{
Metadata: resource.Metadata,
Metadata: metadata,
Outbound: outbound,
Allow: allow,
SourceAddresses: sourceAddressPrefixes,
SourcePorts: sourcePortRanges,
DestinationAddresses: destinationAddressPrefixes,
DestinationPorts: destinationPortRanges,
Protocol: resource.Properties.GetMapValue("protocol").AsStringValue("", resource.Metadata),
Protocol: properties.GetMapValue("protocol").AsStringValue("", metadata),
}
}

Expand Down
184 changes: 183 additions & 1 deletion pkg/iac/adapters/arm/network/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package network
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/trivy/internal/testutil"
"github.com/aquasecurity/trivy/pkg/iac/adapters/arm/adaptertest"
"github.com/aquasecurity/trivy/pkg/iac/adapters/common"
"github.com/aquasecurity/trivy/pkg/iac/providers/azure/network"
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure/arm/parser"
"github.com/aquasecurity/trivy/pkg/iac/types"
)

Expand All @@ -21,10 +26,12 @@ func TestAdapt(t *testing.T) {
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "nsg",
"properties": {}
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "nsg/rule",
"properties": {}
},
{
Expand Down Expand Up @@ -56,10 +63,12 @@ func TestAdapt(t *testing.T) {
}
},
{
"type": "Microsoft.Network/networkSecurityGroups"
"type": "Microsoft.Network/networkSecurityGroups",
"name": "nsg"
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "nsg/rule",
"properties": {
"access": "Allow",
"direction": "Inbound",
Expand Down Expand Up @@ -141,6 +150,156 @@ func TestAdapt(t *testing.T) {
}},
},
},
{
name: "rules in the securityRules property",
source: `{
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "nsg",
"properties": {
"securityRules": [
{"name": "ssh", "properties": {"access": "Allow", "protocol": "Tcp"}},
{"name": "dns", "properties": {"access": "Deny", "protocol": "Udp"}}
]
}
}
]
}`,
expected: network.Network{
SecurityGroups: []network.SecurityGroup{{
Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Tcp")},
{Protocol: types.StringTest("Udp")},
},
}},
},
},
{
name: "rules as child resources",
source: `{
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "nsg",
"resources": [
{
"type": "securityRules",
"name": "ssh",
"properties": {"access": "Allow", "protocol": "Tcp"}
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "dns",
"properties": {"access": "Allow", "protocol": "Udp"}
}
]
}
]
}`,
expected: network.Network{
SecurityGroups: []network.SecurityGroup{{
Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Tcp")},
{Allow: types.BoolTest(true), Protocol: types.StringTest("Udp")},
},
}},
},
},
{
name: "top-level rules go to the group they name",
source: `{
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "web"
},
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "db"
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "web/https",
"properties": {"access": "Allow", "protocol": "Tcp"}
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "DB/sql",
"properties": {"access": "Allow", "protocol": "Udp"}
}
]
}`,
expected: network.Network{
SecurityGroups: []network.SecurityGroup{
{Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Tcp")},
}},
{Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Udp")},
}},
},
},
},
{
name: "top-level rule with an expression name goes to the orphan group",
source: `{
"parameters": {
"nsgName": {"type": "string", "defaultValue": "web"}
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "web"
},
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "db"
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "[concat(parameters('nsgName'), '/https')]",
"properties": {"access": "Allow", "protocol": "Tcp"}
}
]
}`,
expected: network.Network{
SecurityGroups: []network.SecurityGroup{
{},
{},
{Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Tcp")},
}},
},
},
},
{
name: "top-level rule goes to the orphan group when the group name is an expression",
source: `{
"parameters": {
"nsgName": {"type": "string", "defaultValue": "web"}
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"name": "[parameters('nsgName')]"
},
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "web/https",
"properties": {"access": "Allow", "protocol": "Tcp"}
}
]
}`,
expected: network.Network{
SecurityGroups: []network.SecurityGroup{
{},
{Rules: []network.SecurityGroupRule{
{Allow: types.BoolTest(true), Protocol: types.StringTest("Tcp")},
}},
},
},
},
{
name: "network interface with ip configurations",
source: `{
Expand Down Expand Up @@ -214,3 +373,26 @@ func TestAdapt(t *testing.T) {
})
}
}

func TestAdaptOrphanSecurityGroupIsUnmanaged(t *testing.T) {
fsys := testutil.CreateFS(map[string]string{
"test.json": `{
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"name": "[format('{0}/ssh', parameters('nsgName'))]",
"properties": {"access": "Allow", "protocol": "Tcp"}
}
]
}`,
})
deployments, err := parser.New(fsys).ParseFS(t.Context(), ".")
require.NoError(t, err)
require.Len(t, deployments, 1)

groups := Adapt(deployments[0]).SecurityGroups
require.Len(t, groups, 1)
assert.False(t, groups[0].Metadata.IsManaged())
require.Len(t, groups[0].Rules, 1)
assert.True(t, groups[0].Rules[0].Metadata.IsManaged())
}
Loading