|
| 1 | +package rules |
| 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 | +// AwsWriteOnlyAttributesRule checks if a write-only attribute is available for sensitive input attributes |
| 13 | +// It emits a warning if the normal attribute is used, as that is a non-ephemeral attribute and the secret value is stored in state |
| 14 | +type AwsWriteOnlyAttributesRule struct { |
| 15 | + tflint.DefaultRule |
| 16 | + |
| 17 | + writeOnlyAttributes map[string]writeOnlyAttribute |
| 18 | +} |
| 19 | + |
| 20 | +type writeOnlyAttribute struct { |
| 21 | + original string |
| 22 | + alternative string |
| 23 | +} |
| 24 | + |
| 25 | +// NewAwsWriteOnlyAttributesRule returns new rule with default attributes |
| 26 | +func NewAwsWriteOnlyAttributesRule() *AwsWriteOnlyAttributesRule { |
| 27 | + writeOnlyAttributes := map[string]writeOnlyAttribute{ |
| 28 | + "aws_secretsmanager_secret_version": { |
| 29 | + original: "secret_string", |
| 30 | + alternative: "secret_string_wo", |
| 31 | + }, |
| 32 | + "aws_rds_cluster": { |
| 33 | + original: "master_password", |
| 34 | + alternative: "master_password_wo", |
| 35 | + }, |
| 36 | + "aws_redshift_cluster": { |
| 37 | + original: "master_password", |
| 38 | + alternative: "master_password_wo", |
| 39 | + }, |
| 40 | + "aws_docdb_cluster": { |
| 41 | + original: "master_password", |
| 42 | + alternative: "master_password_wo", |
| 43 | + }, |
| 44 | + "aws_redshiftserverless_namespace": { |
| 45 | + original: "admin_password", |
| 46 | + alternative: "admin_password_wo", |
| 47 | + }, |
| 48 | + "aws_ssm_parameter": { |
| 49 | + original: "value", |
| 50 | + alternative: "value_wo", |
| 51 | + }, |
| 52 | + } |
| 53 | + return &AwsWriteOnlyAttributesRule{ |
| 54 | + writeOnlyAttributes: writeOnlyAttributes, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Name returns the rule name |
| 59 | +func (r *AwsWriteOnlyAttributesRule) Name() string { |
| 60 | + return "aws_write_only_attributes" |
| 61 | +} |
| 62 | + |
| 63 | +// Enabled returns whether the rule is enabled by default |
| 64 | +func (r *AwsWriteOnlyAttributesRule) Enabled() bool { |
| 65 | + return true |
| 66 | +} |
| 67 | + |
| 68 | +// Severity returns the rule severity |
| 69 | +func (r *AwsWriteOnlyAttributesRule) Severity() tflint.Severity { |
| 70 | + return tflint.WARNING |
| 71 | +} |
| 72 | + |
| 73 | +// Link returns the rule reference link |
| 74 | +func (r *AwsWriteOnlyAttributesRule) Link() string { |
| 75 | + return project.ReferenceLink(r.Name()) |
| 76 | +} |
| 77 | + |
| 78 | +// Check checks whether the secret string attribute exists |
| 79 | +func (r *AwsWriteOnlyAttributesRule) Check(runner tflint.Runner) error { |
| 80 | + for resourceType, attributes := range r.writeOnlyAttributes { |
| 81 | + resources, err := runner.GetResourceContent(resourceType, &hclext.BodySchema{ |
| 82 | + Attributes: []hclext.AttributeSchema{ |
| 83 | + {Name: attributes.original}, |
| 84 | + }, |
| 85 | + }, nil) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + for _, resource := range resources.Blocks { |
| 91 | + attribute, exists := resource.Body.Attributes[attributes.original] |
| 92 | + if !exists { |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + err := runner.EvaluateExpr(attribute.Expr, func(val cty.Value) error { |
| 97 | + if !val.IsNull() { |
| 98 | + if err := runner.EmitIssueWithFix( |
| 99 | + r, |
| 100 | + fmt.Sprintf("\"%s\" is a non-ephemeral attribute, which means this secret is stored in state. Please use write-only attribute \"%s\".", attributes.original, attributes.alternative), |
| 101 | + attribute.Expr.Range(), |
| 102 | + func(f tflint.Fixer) error { |
| 103 | + return f.ReplaceText(attribute.NameRange, attributes.alternative) |
| 104 | + }, |
| 105 | + ); err != nil { |
| 106 | + return fmt.Errorf("failed to call EmitIssueWithFix(): %w", err) |
| 107 | + } |
| 108 | + } |
| 109 | + return nil |
| 110 | + }, nil) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
0 commit comments