|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/terraform-linters/tflint-plugin-sdk/hclext" |
| 5 | + "github.com/terraform-linters/tflint-plugin-sdk/logger" |
| 6 | + "github.com/terraform-linters/tflint-plugin-sdk/tflint" |
| 7 | + "github.com/terraform-linters/tflint-ruleset-azurerm/project" |
| 8 | +) |
| 9 | + |
| 10 | +// AzurermAppServiceMissingAutoHealSettingRule checks whether auto_heal_setting is configured in site_config |
| 11 | +type AzurermAppServiceMissingAutoHealSettingRule struct { |
| 12 | + tflint.DefaultRule |
| 13 | +} |
| 14 | + |
| 15 | +const ( |
| 16 | + autoHealSettingBlockName = "auto_heal_setting" |
| 17 | +) |
| 18 | + |
| 19 | +var autoHealResourceTypes = []string{ |
| 20 | + "azurerm_linux_web_app", |
| 21 | + "azurerm_linux_web_app_slot", |
| 22 | + "azurerm_windows_web_app", |
| 23 | + "azurerm_windows_web_app_slot", |
| 24 | +} |
| 25 | + |
| 26 | +// NewAzurermAppServiceMissingAutoHealSettingRule returns new rule for checking auto_heal_setting configuration |
| 27 | +func NewAzurermAppServiceMissingAutoHealSettingRule() *AzurermAppServiceMissingAutoHealSettingRule { |
| 28 | + return &AzurermAppServiceMissingAutoHealSettingRule{} |
| 29 | +} |
| 30 | + |
| 31 | +// Name returns the rule name |
| 32 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) Name() string { |
| 33 | + return "azurerm_app_service_missing_auto_heal_setting" |
| 34 | +} |
| 35 | + |
| 36 | +// Enabled returns whether the rule is enabled by default |
| 37 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) Enabled() bool { |
| 38 | + return true |
| 39 | +} |
| 40 | + |
| 41 | +// Severity returns the rule severity |
| 42 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) Severity() tflint.Severity { |
| 43 | + return tflint.WARNING |
| 44 | +} |
| 45 | + |
| 46 | +// Link returns the rule reference link |
| 47 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) Link() string { |
| 48 | + return project.ReferenceLink(r.Name()) |
| 49 | +} |
| 50 | + |
| 51 | +// checkResourceType checks a specific resource type for auto_heal_setting configuration |
| 52 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) checkResourceType(runner tflint.Runner, resourceType string) error { |
| 53 | + resources, err := runner.GetResourceContent(resourceType, &hclext.BodySchema{ |
| 54 | + Blocks: []hclext.BlockSchema{ |
| 55 | + { |
| 56 | + Type: "site_config", |
| 57 | + Body: &hclext.BodySchema{ |
| 58 | + Blocks: []hclext.BlockSchema{ |
| 59 | + { |
| 60 | + Type: autoHealSettingBlockName, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, nil) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + for _, resource := range resources.Blocks { |
| 73 | + logger.Debug("checking", "resource type", resource.Labels[0], "resource name", resource.Labels[1]) |
| 74 | + |
| 75 | + // Check if site_config block exists |
| 76 | + hasSiteConfig := false |
| 77 | + hasAutoHealSetting := false |
| 78 | + |
| 79 | + for _, block := range resource.Body.Blocks { |
| 80 | + if block.Type == "site_config" { |
| 81 | + hasSiteConfig = true |
| 82 | + |
| 83 | + // Check for auto_heal_setting block |
| 84 | + for _, siteConfigBlock := range block.Body.Blocks { |
| 85 | + if siteConfigBlock.Type == "auto_heal_setting" { |
| 86 | + hasAutoHealSetting = true |
| 87 | + break |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // If site_config doesn't exist, skip this resource |
| 96 | + if !hasSiteConfig { |
| 97 | + logger.Debug("no site_config block found", "resource type", resource.Labels[0], "resource name", resource.Labels[1]) |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + // Emit issue if auto_heal_setting is not configured in site_config |
| 102 | + if !hasAutoHealSetting { |
| 103 | + issue := "auto_heal_setting should be configured in site_config block for robust app services." |
| 104 | + if err := runner.EmitIssue(r, issue, resource.DefRange); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// Check checks whether auto_heal_setting is configured in site_config |
| 114 | +func (r *AzurermAppServiceMissingAutoHealSettingRule) Check(runner tflint.Runner) error { |
| 115 | + for _, resourceType := range autoHealResourceTypes { |
| 116 | + if err := r.checkResourceType(runner, resourceType); err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
0 commit comments