Skip to content

Commit cfc2e76

Browse files
authored
feat: storage account - public access enabled (#9)
Co-authored-by: Preben Huybrechts <[email protected]>
1 parent 4cdf70c commit cfc2e76

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|azurerm_linux_web_app_https_only|Force all traffic over https |WARNING|||
77
|azurerm_linux_web_app_minimum_tls_version|Enforce TLS 1.2 on linux web apps |WARNING|||
88
|azurerm_mssql_database_transparent_data_encryption_enabled|Enforce transparant data encryption|WARNING|||
9+
|azurerm_storage_account_public_network_access_enabled|Consider disabling public network access on storage accounts. |NOTICE|||
910
|azurerm_storage_account_tls_version|Enforce TLS 1.2 on storage accounts |WARNING|||
1011
|azurerm_windows_web_app_ftps_state|Disable sftp to a windows web app |WARNING|||
1112
|azurerm_windows_web_app_https_only|Force all traffic over https |WARNING|||

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func CreateRuleSet() *tflint.BuiltinRuleSet {
1616
rules.NewAzurermLinuxWebAppHttpsOnly(),
1717
rules.NewAzurermLinuxWebAppMinimumTlsVersion(),
1818
rules.NewAzurermMssqlDatabaseEncryption(),
19+
rules.NewAzurermStorageAccountPublicNetworkAccessEnabled(),
1920
rules.NewAzurermStorageAccountUnsecureTls(),
2021
rules.NewAzurermWindowsWebAppFtpsState(),
2122
rules.NewAzurermWindowsWebAppHttpsOnly(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package rules
2+
3+
import (
4+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
5+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
6+
)
7+
8+
// AzurermStorageAccountPublicNetworkAccessEnabled checks that transparent data encryption is enabled
9+
type AzurermStorageAccountPublicNetworkAccessEnabled struct {
10+
tflint.DefaultRule
11+
12+
resourceType string
13+
attributeName string
14+
}
15+
16+
// NewAzurermStorageAccountPublicNetworkAccessEnabled returns a new rule instance
17+
func NewAzurermStorageAccountPublicNetworkAccessEnabled() *AzurermStorageAccountPublicNetworkAccessEnabled {
18+
return &AzurermStorageAccountPublicNetworkAccessEnabled{
19+
resourceType: "azurerm_storage_account",
20+
attributeName: "public_network_access_enabled",
21+
}
22+
}
23+
24+
// Name returns the rule name
25+
func (r *AzurermStorageAccountPublicNetworkAccessEnabled) Name() string {
26+
return "azurerm_storage_account_public_network_access_enabled"
27+
}
28+
29+
// Enabled returns whether the rule is enabled by default
30+
func (r *AzurermStorageAccountPublicNetworkAccessEnabled) Enabled() bool {
31+
return true
32+
}
33+
34+
// Severity returns the rule severity
35+
func (r *AzurermStorageAccountPublicNetworkAccessEnabled) Severity() tflint.Severity {
36+
return tflint.NOTICE
37+
}
38+
39+
// Link returns the rule reference link
40+
func (r *AzurermStorageAccountPublicNetworkAccessEnabled) Link() string {
41+
return ""
42+
}
43+
44+
// Check checks if transparent data encryption is enabled
45+
func (r *AzurermStorageAccountPublicNetworkAccessEnabled) Check(runner tflint.Runner) error {
46+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
47+
Attributes: []hclext.AttributeSchema{
48+
{Name: r.attributeName},
49+
},
50+
}, nil)
51+
if err != nil {
52+
return err
53+
}
54+
55+
for _, resource := range resources.Blocks {
56+
attribute, exists := resource.Body.Attributes[r.attributeName]
57+
if !exists {
58+
// Emit an issue if the attribute does not exist
59+
runner.EmitIssue(
60+
r,
61+
"public_network_access_enabled is not defined and defaults to true, consider disabling it",
62+
resource.DefRange,
63+
)
64+
continue
65+
}
66+
67+
err := runner.EvaluateExpr(attribute.Expr, func(val bool) error {
68+
if val {
69+
runner.EmitIssue(
70+
r,
71+
"Consider changing public_network_access_enabled to false",
72+
attribute.Expr.Range(),
73+
)
74+
}
75+
return nil
76+
}, nil)
77+
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
83+
return nil
84+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_AzurermStorageAccountPublicNetworkAccessEnabled(t *testing.T) {
11+
tests := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "public network access disabled",
18+
Content: `
19+
resource "azurerm_storage_account" "example" {
20+
public_network_access_enabled = true
21+
}`,
22+
Expected: helper.Issues{
23+
{
24+
Rule: NewAzurermStorageAccountPublicNetworkAccessEnabled(),
25+
Message: "Consider changing public_network_access_enabled to false",
26+
Range: hcl.Range{
27+
Filename: "resource.tf",
28+
Start: hcl.Pos{Line: 3, Column: 37},
29+
End: hcl.Pos{Line: 3, Column: 41},
30+
},
31+
},
32+
},
33+
},
34+
{
35+
Name: "public network access missing",
36+
Content: `
37+
resource "azurerm_storage_account" "example" {
38+
}`,
39+
Expected: helper.Issues{
40+
{
41+
Rule: NewAzurermStorageAccountPublicNetworkAccessEnabled(),
42+
Message: "public_network_access_enabled is not defined and defaults to true, consider disabling it",
43+
Range: hcl.Range{
44+
Filename: "resource.tf",
45+
Start: hcl.Pos{Line: 2, Column: 1},
46+
End: hcl.Pos{Line: 2, Column: 45},
47+
},
48+
},
49+
},
50+
},
51+
{
52+
Name: "public network access disabled",
53+
Content: `
54+
resource "azurerm_storage_account" "example" {
55+
public_network_access_enabled = false
56+
}`,
57+
Expected: helper.Issues{},
58+
},
59+
}
60+
61+
rule := NewAzurermStorageAccountPublicNetworkAccessEnabled()
62+
63+
for _, test := range tests {
64+
t.Run(test.Name, func(t *testing.T) {
65+
runner := helper.TestRunner(t, map[string]string{"resource.tf": test.Content})
66+
67+
if err := rule.Check(runner); err != nil {
68+
t.Fatalf("Unexpected error occurred: %s", err)
69+
}
70+
71+
helper.AssertIssues(t, test.Expected, runner.Issues)
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)