Skip to content

Commit 47f8916

Browse files
committed
rules nested under site_config
1 parent acb42a5 commit 47f8916

15 files changed

+474
-119
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# don't track built binary
22
/tflint-ruleset-azurerm-security
3+
4+
tools/

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ plugin "azurerm-security" {
2323

2424
## Rules
2525

26-
|Name|Description|Severity|Enabled|Link|
27-
| --- | --- | --- | --- | --- |
28-
|azurerm_linux_web_app_ftps_state|Disable sftp to a linux web app |ERROR|||
29-
|azurerm_linux_web_app_minimum_tls_version|Enforce TLS 1.2 on linux web apps |ERROR|||
30-
|azurerm_mssql_database_transparent_data_encryption_enabled|Enforce transparant data encryption|ERROR|||
31-
|azurerm_storage_account_tls_version|Enforce TLS 1.2 on storage accounts |ERROR|||
32-
|azurerm_windows_web_app_ftps_state|Disable sftp to a windows web app |ERROR|||
33-
|azurerm_windows_web_app_minimum_tls_version|Enforce TLS 1.2 on windows web apps |ERROR|||
26+
See the [documentation](docs/README.md).
3427

3528
## Building the plugin
3629

@@ -48,6 +41,8 @@ $ make install
4841

4942
Note that if you install the plugin with make install, you must omit the version and source attributes in .tflint.hcl:
5043

44+
```
5145
plugin "azurerm-security" {
5246
enabled = true
53-
}
47+
}
48+
```

docs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Rules
2+
3+
|Name|Description|Severity|Enabled|Link|
4+
| --- | --- | --- | --- | --- |
5+
|azurerm_linux_web_app_ftps_state|Disable sftp to a linux web app |WARNING|||
6+
|azurerm_linux_web_app_minimum_tls_version|Enforce TLS 1.2 on linux web apps |WARNING|||
7+
|azurerm_mssql_database_transparent_data_encryption_enabled|Enforce transparant data encryption|WARNING|||
8+
|azurerm_storage_account_tls_version|Enforce TLS 1.2 on storage accounts |WARNING|||
9+
|azurerm_windows_web_app_ftps_state|Disable sftp to a windows web app |WARNING|||
10+
|azurerm_windows_web_app_minimum_tls_version|Enforce TLS 1.2 on windows web apps |WARNING|||

main.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// main.go
12
package main
23

34
import (
@@ -6,19 +7,23 @@ import (
67
"github.com/terraform-linters/tflint-ruleset-azurerm-security/rules"
78
)
89

10+
func CreateRuleSet() *tflint.BuiltinRuleSet {
11+
return &tflint.BuiltinRuleSet{
12+
Name: "azurerm-security",
13+
Version: "0.1.2",
14+
Rules: []tflint.Rule{
15+
rules.NewAzurermLinuxWebAppFtpsState(),
16+
rules.NewAzurermLinuxWebAppMinimumTlsVersion(),
17+
rules.NewAzurermMssqlDatabaseEncryption(),
18+
rules.NewAzurermStorageAccountUnsecureTls(),
19+
rules.NewAzurermWindowsWebAppFtpsState(),
20+
rules.NewAzurermWindowsWebAppMinimumTlsVersion(),
21+
},
22+
}
23+
}
24+
925
func main() {
1026
plugin.Serve(&plugin.ServeOpts{
11-
RuleSet: &tflint.BuiltinRuleSet{
12-
Name: "azurerm-security",
13-
Version: "0.1.2",
14-
Rules: []tflint.Rule{
15-
rules.NewAzurermLinuxWebAppFtpsState(),
16-
rules.NewAzurermLinuxWebAppMinimumTlsVersion(),
17-
rules.NewAzurermMssqlDatabaseEncryption(),
18-
rules.NewAzurermStorageAccountUnsecureTls(),
19-
rules.NewAzurermWindowsWebAppFtpsState(),
20-
rules.NewAzurermWindowsWebAppMinimumTlsVersion(),
21-
},
22-
},
27+
RuleSet: CreateRuleSet(),
2328
})
2429
}

main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// main_test.go
2+
package main
3+
4+
import (
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestRulesLength(t *testing.T) {
12+
ruleSet := CreateRuleSet()
13+
actualRules := len(ruleSet.Rules)
14+
15+
// Count .go files in rules directory that don't end with _test.go
16+
rulesPath := "./rules"
17+
expectedRules := 0
18+
19+
err := filepath.Walk(rulesPath, func(path string, info os.FileInfo, err error) error {
20+
if err != nil {
21+
return err
22+
}
23+
if !info.IsDir() &&
24+
strings.HasSuffix(path, ".go") &&
25+
!strings.HasSuffix(path, "_test.go") {
26+
expectedRules++
27+
}
28+
return nil
29+
})
30+
31+
if err != nil {
32+
t.Fatalf("Error walking rules directory: %v", err)
33+
}
34+
35+
if actualRules != expectedRules {
36+
t.Errorf("Number of rules does not match number of rule files. Got %d rules, expected %d",
37+
actualRules, expectedRules)
38+
}
39+
}

rules/azurerm_linux_web_app_ftps_state.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ package rules
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
78
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
89
)
910

10-
// AzurermLinuxWebAppFtpsState checks if ftps_state is disabled
11+
// AzurermLinuxWebAppFtpsState checks that ftps_state is set to "Disabled"
1112
type AzurermLinuxWebAppFtpsState struct {
1213
tflint.DefaultRule
1314

1415
resourceType string
15-
attributeName string
16+
attributePath []string
1617
expectedValue string
1718
}
1819

19-
// NewAzurermLinuxWebAppFtpsState creates a new rule instance
20+
// NewAzurermLinuxWebAppFtpsState returns a new rule instance
2021
func NewAzurermLinuxWebAppFtpsState() *AzurermLinuxWebAppFtpsState {
2122
return &AzurermLinuxWebAppFtpsState{
2223
resourceType: "azurerm_linux_web_app",
23-
attributeName: "ftps_state",
24+
attributePath: []string{"site_config", "ftps_state"},
2425
expectedValue: "Disabled",
2526
}
2627
}
@@ -37,7 +38,7 @@ func (r *AzurermLinuxWebAppFtpsState) Enabled() bool {
3738

3839
// Severity returns the rule severity
3940
func (r *AzurermLinuxWebAppFtpsState) Severity() tflint.Severity {
40-
return tflint.ERROR
41+
return tflint.WARNING
4142
}
4243

4344
// Link returns the rule reference link
@@ -48,36 +49,53 @@ func (r *AzurermLinuxWebAppFtpsState) Link() string {
4849
// Check verifies that ftps_state is set to "Disabled"
4950
func (r *AzurermLinuxWebAppFtpsState) Check(runner tflint.Runner) error {
5051
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
51-
Attributes: []hclext.AttributeSchema{
52-
{Name: r.attributeName},
52+
Blocks: []hclext.BlockSchema{
53+
{
54+
Type: "site_config",
55+
Body: &hclext.BodySchema{
56+
Attributes: []hclext.AttributeSchema{
57+
{Name: "ftps_state"},
58+
},
59+
},
60+
},
5361
},
5462
}, nil)
5563
if err != nil {
5664
return err
5765
}
5866

5967
for _, resource := range resources.Blocks {
60-
attribute, exists := resource.Body.Attributes[r.attributeName]
61-
if !exists {
68+
siteConfigBlocks := resource.Body.Blocks.OfType("site_config")
69+
if len(siteConfigBlocks) == 0 {
6270
runner.EmitIssue(
6371
r,
64-
"ftps_state should be set to Disabled",
72+
"site_config block is missing, ftps_state should be set to Disabled",
6573
resource.DefRange,
6674
)
6775
continue
6876
}
6977

78+
siteConfig := siteConfigBlocks[0]
79+
attribute, exists := siteConfig.Body.Attributes["ftps_state"]
80+
if !exists {
81+
runner.EmitIssue(
82+
r,
83+
"ftps_state is missing in site_config, should be set to Disabled",
84+
siteConfig.DefRange,
85+
)
86+
continue
87+
}
88+
7089
err := runner.EvaluateExpr(attribute.Expr, func(val string) error {
71-
if val != r.expectedValue {
90+
if !strings.EqualFold(val, r.expectedValue) {
7291
runner.EmitIssue(
7392
r,
74-
fmt.Sprintf("ftps_state is set to %q, should be Disabled", val),
93+
fmt.Sprintf("ftps_state is set to %s, should be set to Disabled", val),
7594
attribute.Expr.Range(),
7695
)
7796
}
7897
return nil
7998
}, nil)
80-
8199
if err != nil {
82100
return err
83101
}

rules/azurerm_linux_web_app_ftps_state_test.go

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,98 @@ func Test_AzurermLinuxWebAppFtpsState(t *testing.T) {
1414
Expected helper.Issues
1515
}{
1616
{
17-
Name: "FTPS enabled",
17+
Name: "ftps_state not set to Disabled",
1818
Content: `
1919
resource "azurerm_linux_web_app" "example" {
20-
ftps_state = "Enabled"
20+
site_config {
21+
ftps_state = "FtpsOnly"
22+
}
2123
}`,
2224
Expected: helper.Issues{
2325
{
2426
Rule: NewAzurermLinuxWebAppFtpsState(),
25-
Message: `ftps_state is set to "Enabled", should be Disabled`,
27+
Message: "ftps_state is set to FtpsOnly, should be set to Disabled",
2628
Range: hcl.Range{
2729
Filename: "resource.tf",
28-
Start: hcl.Pos{Line: 3, Column: 18},
29-
End: hcl.Pos{Line: 3, Column: 27},
30+
Start: hcl.Pos{
31+
Line: 4,
32+
Column: 22,
33+
},
34+
End: hcl.Pos{
35+
Line: 4,
36+
Column: 32,
37+
},
3038
},
3139
},
3240
},
3341
},
3442
{
35-
Name: "FTPS state missing",
43+
Name: "ftps_state set to disabled (lowercase)",
3644
Content: `
3745
resource "azurerm_linux_web_app" "example" {
46+
site_config {
47+
ftps_state = "disabled"
48+
}
49+
}`,
50+
Expected: helper.Issues{},
51+
},
52+
{
53+
Name: "ftps_state set to DISABLED (uppercase)",
54+
Content: `
55+
resource "azurerm_linux_web_app" "example" {
56+
site_config {
57+
ftps_state = "DISABLED"
58+
}
59+
}`,
60+
Expected: helper.Issues{},
61+
},
62+
{
63+
Name: "ftps_state attribute missing",
64+
Content: `
65+
resource "azurerm_linux_web_app" "example" {
66+
site_config {
67+
}
3868
}`,
3969
Expected: helper.Issues{
4070
{
4171
Rule: NewAzurermLinuxWebAppFtpsState(),
42-
Message: `ftps_state should be set to Disabled`,
72+
Message: "ftps_state is missing in site_config, should be set to Disabled",
4373
Range: hcl.Range{
4474
Filename: "resource.tf",
45-
Start: hcl.Pos{Line: 2, Column: 1},
46-
End: hcl.Pos{Line: 2, Column: 43},
75+
Start: hcl.Pos{
76+
Line: 3,
77+
Column: 5,
78+
},
79+
End: hcl.Pos{
80+
Line: 3,
81+
Column: 16,
82+
},
4783
},
4884
},
4985
},
5086
},
5187
{
52-
Name: "FTPS disabled",
88+
Name: "site_config block missing",
5389
Content: `
5490
resource "azurerm_linux_web_app" "example" {
55-
ftps_state = "Disabled"
5691
}`,
57-
Expected: helper.Issues{},
92+
Expected: helper.Issues{
93+
{
94+
Rule: NewAzurermLinuxWebAppFtpsState(),
95+
Message: "site_config block is missing, ftps_state should be set to Disabled",
96+
Range: hcl.Range{
97+
Filename: "resource.tf",
98+
Start: hcl.Pos{
99+
Line: 2,
100+
Column: 1,
101+
},
102+
End: hcl.Pos{
103+
Line: 2,
104+
Column: 43,
105+
},
106+
},
107+
},
108+
},
58109
},
59110
}
60111

@@ -71,5 +122,4 @@ resource "azurerm_linux_web_app" "example" {
71122
helper.AssertIssues(t, test.Expected, runner.Issues)
72123
})
73124
}
74-
}
75-
125+
}

0 commit comments

Comments
 (0)