|
| 1 | +package ephemeral |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/terraform-linters/tflint-plugin-sdk/hclext" |
| 7 | + "github.com/terraform-linters/tflint-plugin-sdk/tflint" |
| 8 | + "github.com/terraform-linters/tflint-ruleset-aws/project" |
| 9 | + "github.com/zclconf/go-cty/cty" |
| 10 | +) |
| 11 | + |
| 12 | +// AwsWriteOnlyArgumentsRule checks if a write-only argument is available for sensitive input attributes |
| 13 | +type AwsWriteOnlyArgumentsRule struct { |
| 14 | + tflint.DefaultRule |
| 15 | + |
| 16 | + writeOnlyArguments map[string][]writeOnlyArgument |
| 17 | +} |
| 18 | + |
| 19 | +type writeOnlyArgument struct { |
| 20 | + originalAttribute string |
| 21 | + writeOnlyAlternative string |
| 22 | + writeOnlyVersionAttribute string |
| 23 | +} |
| 24 | + |
| 25 | +// NewAwsWriteOnlyArgumentsRule returns new rule with default attributes |
| 26 | +func NewAwsWriteOnlyArgumentsRule() *AwsWriteOnlyArgumentsRule { |
| 27 | + return &AwsWriteOnlyArgumentsRule{ |
| 28 | + writeOnlyArguments: writeOnlyArguments, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Name returns the rule name |
| 33 | +func (r *AwsWriteOnlyArgumentsRule) Name() string { |
| 34 | + return "aws_write_only_arguments" |
| 35 | +} |
| 36 | + |
| 37 | +// Enabled returns whether the rule is enabled by default |
| 38 | +func (r *AwsWriteOnlyArgumentsRule) Enabled() bool { |
| 39 | + return false |
| 40 | +} |
| 41 | + |
| 42 | +// Severity returns the rule severity |
| 43 | +func (r *AwsWriteOnlyArgumentsRule) Severity() tflint.Severity { |
| 44 | + return tflint.WARNING |
| 45 | +} |
| 46 | + |
| 47 | +// Link returns the rule reference link |
| 48 | +func (r *AwsWriteOnlyArgumentsRule) Link() string { |
| 49 | + return project.ReferenceLink(r.Name()) |
| 50 | +} |
| 51 | + |
| 52 | +// Check checks whether the sensitive attribute exists |
| 53 | +func (r *AwsWriteOnlyArgumentsRule) Check(runner tflint.Runner) error { |
| 54 | + for resourceType, attributes := range r.writeOnlyArguments { |
| 55 | + for _, resourceAttribute := range attributes { |
| 56 | + resources, err := runner.GetResourceContent(resourceType, &hclext.BodySchema{ |
| 57 | + Attributes: []hclext.AttributeSchema{ |
| 58 | + {Name: resourceAttribute.originalAttribute}, |
| 59 | + }, |
| 60 | + }, nil) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + for _, resource := range resources.Blocks { |
| 66 | + attribute, exists := resource.Body.Attributes[resourceAttribute.originalAttribute] |
| 67 | + if !exists { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + err := runner.EvaluateExpr(attribute.Expr, func(val cty.Value) error { |
| 72 | + if !val.IsNull() { |
| 73 | + if err := runner.EmitIssueWithFix( |
| 74 | + r, |
| 75 | + fmt.Sprintf("\"%s\" is a non-ephemeral attribute, which means this secret is stored in state. Please use write-only argument \"%s\".", resourceAttribute.originalAttribute, resourceAttribute.writeOnlyAlternative), |
| 76 | + attribute.Expr.Range(), |
| 77 | + func(f tflint.Fixer) error { |
| 78 | + err := f.ReplaceText(attribute.NameRange, resourceAttribute.writeOnlyAlternative) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + return f.InsertTextAfter(attribute.Range, fmt.Sprintf("\n %s = 1", resourceAttribute.writeOnlyVersionAttribute)) |
| 83 | + }, |
| 84 | + ); err != nil { |
| 85 | + return fmt.Errorf("failed to call EmitIssueWithFix(): %w", err) |
| 86 | + } |
| 87 | + } |
| 88 | + return nil |
| 89 | + }, nil) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments