|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/hcl/v2" |
| 9 | + "github.com/hashicorp/hcl/v2/gohcl" |
| 10 | + "github.com/terraform-linters/tflint-plugin-sdk/hclext" |
| 11 | + "github.com/terraform-linters/tflint-plugin-sdk/tflint" |
| 12 | +) |
| 13 | + |
| 14 | +// MskModuleBackendRule checks whether an MSK module has an S3 backend defined with a key that has as suffix the name of the team. |
| 15 | +type MskModuleBackendRule struct { |
| 16 | + tflint.DefaultRule |
| 17 | +} |
| 18 | + |
| 19 | +// NewMskModuleBackendRule returns a new rule. |
| 20 | +func NewMskModuleBackendRule() *MskModuleBackendRule { |
| 21 | + return &MskModuleBackendRule{} |
| 22 | +} |
| 23 | + |
| 24 | +// Name returns the rule name. |
| 25 | +func (r *MskModuleBackendRule) Name() string { |
| 26 | + return "msk_module_backend" |
| 27 | +} |
| 28 | + |
| 29 | +// Enabled returns whether the rule is enabled by default. |
| 30 | +func (r *MskModuleBackendRule) Enabled() bool { |
| 31 | + return true |
| 32 | +} |
| 33 | + |
| 34 | +// Severity returns the rule severity. |
| 35 | +func (r *MskModuleBackendRule) Severity() tflint.Severity { |
| 36 | + return tflint.ERROR |
| 37 | +} |
| 38 | + |
| 39 | +// Link returns the rule reference link. |
| 40 | +func (r *MskModuleBackendRule) Link() string { |
| 41 | + return ReferenceLink(r.Name()) |
| 42 | +} |
| 43 | + |
| 44 | +func (r *MskModuleBackendRule) Check(runner tflint.Runner) error { |
| 45 | + path, err := runner.GetModulePath() |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("getting module path: %w", err) |
| 48 | + } |
| 49 | + if !path.IsRoot() { |
| 50 | + // This rule does not evaluate child modules. |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + // This rule is an example to get attributes of blocks other than resources. |
| 55 | + content, err := runner.GetModuleContent(&hclext.BodySchema{ |
| 56 | + Blocks: []hclext.BlockSchema{ |
| 57 | + { |
| 58 | + Type: "terraform", |
| 59 | + Body: &hclext.BodySchema{ |
| 60 | + Blocks: []hclext.BlockSchema{ |
| 61 | + { |
| 62 | + Type: "backend", |
| 63 | + LabelNames: []string{"type"}, |
| 64 | + Body: &hclext.BodySchema{ |
| 65 | + Attributes: []hclext.AttributeSchema{ |
| 66 | + {Name: "key"}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, nil) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("getting module content: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + backend := findBackendDef(content) |
| 80 | + if backend == nil { |
| 81 | + err := runner.EmitIssue(r, "an s3 backend should be configured for a kafka MSK module", hcl.Range{}) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("emitting issue: backend missing: %w", err) |
| 84 | + } |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + backendType := backend.Labels[0] |
| 89 | + if backendType != "s3" { |
| 90 | + err := runner.EmitIssue(r, "backend should always be s3 for a kafka MSK module", backend.DefRange) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("emitting issue: always s3: %w", err) |
| 93 | + } |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + keyAttr, keyExists := backend.Body.Attributes["key"] |
| 98 | + if !keyExists { |
| 99 | + err := runner.EmitIssue(r, "the s3 backend should specify the details inside the kafka MSK module", backend.DefRange) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("emitting issue: no s3 details: %w", err) |
| 102 | + } |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + return r.checkKeyHasTeamSuffix(runner, keyAttr) |
| 107 | +} |
| 108 | + |
| 109 | +func findBackendDef(content *hclext.BodyContent) *hclext.Block { |
| 110 | + if content.IsEmpty() { |
| 111 | + return nil |
| 112 | + } |
| 113 | + for _, tfConfig := range content.Blocks { |
| 114 | + if len(tfConfig.Body.Blocks) > 0 { |
| 115 | + return tfConfig.Body.Blocks[0] |
| 116 | + } |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (r *MskModuleBackendRule) checkKeyHasTeamSuffix(runner tflint.Runner, keyAttr *hclext.Attribute) error { |
| 122 | + var key string |
| 123 | + diags := gohcl.DecodeExpression(keyAttr.Expr, nil, &key) |
| 124 | + if diags.HasErrors() { |
| 125 | + return diags |
| 126 | + } |
| 127 | + |
| 128 | + modulePath, err := runner.GetOriginalwd() |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed getting module path: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + teamName := filepath.Base(modulePath) |
| 134 | + |
| 135 | + if !strings.HasSuffix(key, teamName) { |
| 136 | + err = runner.EmitIssue( |
| 137 | + r, |
| 138 | + fmt.Sprintf("backend key must have the team's name '%s' as a suffix. Current value is: %s", teamName, key), |
| 139 | + keyAttr.Range, |
| 140 | + ) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("emitting issue: no team suffix: %w", err) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments