|
| 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 | +) |
| 10 | + |
| 11 | +// AwsEphemeralResourcesRule checks for data sources which can be replaced by ephemeral resources |
| 12 | +type AwsEphemeralResourcesRule struct { |
| 13 | + tflint.DefaultRule |
| 14 | + |
| 15 | + replacingEphemeralResources []string |
| 16 | +} |
| 17 | + |
| 18 | +// NewAwsEphemeralResourcesRule returns new rule with default attributes |
| 19 | +func NewAwsEphemeralResourcesRule() *AwsEphemeralResourcesRule { |
| 20 | + return &AwsEphemeralResourcesRule{ |
| 21 | + replacingEphemeralResources: replacingEphemeralResources, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Name returns the rule name |
| 26 | +func (r *AwsEphemeralResourcesRule) Name() string { |
| 27 | + return "aws_ephemeral_resources" |
| 28 | +} |
| 29 | + |
| 30 | +// Enabled returns whether the rule is enabled by default |
| 31 | +func (r *AwsEphemeralResourcesRule) Enabled() bool { |
| 32 | + return false |
| 33 | +} |
| 34 | + |
| 35 | +// Severity returns the rule severity |
| 36 | +func (r *AwsEphemeralResourcesRule) Severity() tflint.Severity { |
| 37 | + return tflint.WARNING |
| 38 | +} |
| 39 | + |
| 40 | +// Link returns the rule reference link |
| 41 | +func (r *AwsEphemeralResourcesRule) Link() string { |
| 42 | + return project.ReferenceLink(r.Name()) |
| 43 | +} |
| 44 | + |
| 45 | +// Check checks if there is an ephemeral resource which can replace an data source |
| 46 | +func (r *AwsEphemeralResourcesRule) Check(runner tflint.Runner) error { |
| 47 | + for _, resourceType := range r.replacingEphemeralResources { |
| 48 | + resources, err := GetDataSourceContent(runner, resourceType, &hclext.BodySchema{}, nil) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + for _, resource := range resources.Blocks { |
| 54 | + if err := runner.EmitIssue( |
| 55 | + r, |
| 56 | + fmt.Sprintf("\"%s\" is a non-ephemeral data source, which means that all (sensitive) attributes are stored in state. Please use ephemeral resource \"%s\" instead.", resourceType, resourceType), |
| 57 | + resource.TypeRange, |
| 58 | + ); err != nil { |
| 59 | + return fmt.Errorf("failed to call EmitIssue(): %w", err) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func GetDataSourceContent(r tflint.Runner, name string, schema *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) { |
| 68 | + body, err := r.GetModuleContent(&hclext.BodySchema{ |
| 69 | + Blocks: []hclext.BlockSchema{ |
| 70 | + {Type: "data", LabelNames: []string{"type", "name"}, Body: schema}, |
| 71 | + }, |
| 72 | + }, opts) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + content := &hclext.BodyContent{Blocks: []*hclext.Block{}} |
| 78 | + for _, resource := range body.Blocks { |
| 79 | + if resource.Labels[0] != name { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + content.Blocks = append(content.Blocks, resource) |
| 84 | + } |
| 85 | + |
| 86 | + return content, nil |
| 87 | +} |
0 commit comments