Skip to content

Commit 35fd018

Browse files
authored
fix(misconf): fix for Azure Storage Account network acls adaptation (#7602)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
1 parent cd44bb4 commit 35fd018

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

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

+11-13
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ func adaptAccounts(deployment azure.Deployment) []storage.Account {
1818
var accounts []storage.Account
1919
for _, resource := range deployment.GetResourcesByType("Microsoft.Storage/storageAccounts") {
2020

21-
var networkRules []storage.NetworkRule
22-
for _, acl := range resource.Properties.GetMapValue("networkAcls").AsList() {
21+
acl := resource.Properties.GetMapValue("networkAcls")
2322

24-
var bypasses []types.StringValue
25-
bypassProp := acl.GetMapValue("bypass")
26-
for _, bypass := range strings.Split(bypassProp.AsString(), ",") {
27-
bypasses = append(bypasses, types.String(bypass, bypassProp.GetMetadata()))
28-
}
23+
var bypasses []types.StringValue
24+
bypassProp := acl.GetMapValue("bypass")
25+
for _, bypass := range strings.Split(bypassProp.AsString(), ",") {
26+
bypasses = append(bypasses, types.String(strings.TrimSpace(bypass), bypassProp.GetMetadata()))
27+
}
2928

30-
networkRules = append(networkRules, storage.NetworkRule{
31-
Metadata: acl.GetMetadata(),
32-
Bypass: bypasses,
33-
AllowByDefault: types.Bool(acl.GetMapValue("defaultAction").EqualTo("Allow"), acl.GetMetadata()),
34-
})
29+
networkRule := storage.NetworkRule{
30+
Metadata: acl.GetMetadata(),
31+
Bypass: bypasses,
32+
AllowByDefault: types.Bool(acl.GetMapValue("defaultAction").EqualTo("Allow"), acl.GetMetadata()),
3533
}
3634

3735
var queues []storage.Queue
@@ -52,7 +50,7 @@ func adaptAccounts(deployment azure.Deployment) []storage.Account {
5250

5351
account := storage.Account{
5452
Metadata: resource.Metadata,
55-
NetworkRules: networkRules,
53+
NetworkRules: []storage.NetworkRule{networkRule},
5654
EnforceHTTPS: resource.Properties.GetMapValue("supportsHttpsTrafficOnly").AsBoolValue(false, resource.Properties.GetMetadata()),
5755
Containers: containers,
5856
QueueProperties: storage.QueueProperties{

pkg/iac/adapters/arm/storage/adapt_test.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
88

9+
"github.com/aquasecurity/trivy/internal/testutil"
10+
"github.com/aquasecurity/trivy/pkg/iac/providers/azure/storage"
911
"github.com/aquasecurity/trivy/pkg/iac/scanners/azure"
1012
"github.com/aquasecurity/trivy/pkg/iac/types"
1113
)
@@ -43,6 +45,10 @@ func Test_AdaptStorage(t *testing.T) {
4345
"minimumTlsVersion": azure.NewValue("TLS1_2", types.NewTestMetadata()),
4446
"supportsHttpsTrafficOnly": azure.NewValue(true, types.NewTestMetadata()),
4547
"publicNetworkAccess": azure.NewValue("Disabled", types.NewTestMetadata()),
48+
"networkAcls": azure.NewValue(map[string]azure.Value{
49+
"bypass": azure.NewValue("Logging, Metrics", types.NewTestMetadata()),
50+
"defaultAction": azure.NewValue("Allow", types.NewTestMetadata()),
51+
}, types.NewTestMetadata()),
4652
}, types.NewTestMetadata()),
4753
},
4854
},
@@ -52,9 +58,20 @@ func Test_AdaptStorage(t *testing.T) {
5258

5359
require.Len(t, output.Accounts, 1)
5460

55-
account := output.Accounts[0]
56-
assert.Equal(t, "TLS1_2", account.MinimumTLSVersion.Value())
57-
assert.True(t, account.EnforceHTTPS.Value())
58-
assert.False(t, account.PublicNetworkAccess.Value())
61+
expected := storage.Storage{
62+
Accounts: []storage.Account{{
63+
MinimumTLSVersion: types.StringTest("TLS1_2"),
64+
EnforceHTTPS: types.BoolTest(true),
65+
PublicNetworkAccess: types.BoolTest(false),
66+
NetworkRules: []storage.NetworkRule{{
67+
Bypass: []types.StringValue{
68+
types.StringTest("Logging"),
69+
types.StringTest("Metrics"),
70+
},
71+
AllowByDefault: types.BoolTest(true),
72+
}},
73+
}},
74+
}
5975

76+
testutil.AssertDefsecEqual(t, expected, output)
6077
}

0 commit comments

Comments
 (0)