Skip to content

Commit 1a74307

Browse files
committed
[WIP] no s3 global endpoint rule
1 parent 89c6ed5 commit 1a74307

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

rules/aws_s3_no_global_endpoint.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package rules
2+
3+
import (
4+
hcl "github.com/hashicorp/hcl/v2"
5+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
6+
"github.com/terraform-linters/tflint-ruleset-aws/project"
7+
)
8+
9+
// AwsS3NoGlobalEndpointRule checks whether deprecated s3 global endpoint is used instead
10+
// of the regional endoint
11+
type AwsS3NoGlobalEndpointRule struct {
12+
tflint.DefaultRule
13+
14+
// TODO: do we need this, if so why
15+
resourceType string
16+
attributeName string
17+
}
18+
19+
// NewAwsS3NoGlobalEndpointRule returns new rule with default attributes
20+
func NewAwsS3NoGlobalEndpointRule() *AwsS3NoGlobalEndpointRule {
21+
return &AwsS3NoGlobalEndpointRule{
22+
resourceType: "aws_s3_bucket",
23+
attributeName: "",
24+
}
25+
}
26+
27+
// Name returns the rule name
28+
func (r *AwsS3NoGlobalEndpointRule) Name() string {
29+
return "aws_acm_certificate_lifecycle"
30+
}
31+
32+
// Enabled returns whether the rule is enabled by default
33+
func (r *AwsS3NoGlobalEndpointRule) Enabled() bool {
34+
return true
35+
}
36+
37+
// Severity returns the rule severity
38+
func (r *AwsS3NoGlobalEndpointRule) Severity() tflint.Severity {
39+
return tflint.WARNING
40+
}
41+
42+
// Link returns the rule reference link
43+
func (r *AwsS3NoGlobalEndpointRule) Link() string {
44+
return project.ReferenceLink(r.Name())
45+
}
46+
47+
// Check checks whether the aws_acm_certificate resource contains create_before_destroy = true in lifecycle block
48+
func (r *AwsS3NoGlobalEndpointRule) Check(runner tflint.Runner) error {
49+
var err error
50+
runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
51+
vars := expr.Variables()
52+
53+
if len(vars) == 0 {
54+
return nil
55+
}
56+
57+
// is this ever greater than 0
58+
v := vars[0]
59+
60+
if v.RootName() == "aws_s3_bucket" && len(v) == 3 && v[2].(hcl.TraverseAttr).Name == "bucket_domain_name" {
61+
err = runner.EmitIssue(r, "`bucket_domain_name` returns the legacy s3 global endpoint, use `bucket_regional_domain_name` instead", v.SourceRange())
62+
}
63+
64+
return nil
65+
}))
66+
67+
return err
68+
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_AwsS3NoGlobalEndpoint(t *testing.T) {
11+
cases := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "unrelated expression",
18+
Content: `
19+
output "test" {
20+
value = "testing"
21+
}`,
22+
Expected: helper.Issues{},
23+
},
24+
{
25+
Name: "multiple unrelated expressions",
26+
Content: `
27+
output "test1" {
28+
value = var.whatever
29+
}
30+
31+
output "test2" {
32+
value = "testing"
33+
}
34+
35+
output "test3" {
36+
value = aws_iam_role.test.arn
37+
}
38+
`,
39+
Expected: helper.Issues{},
40+
},
41+
// TODO: nested expressions ?
42+
{
43+
Name: "regional endpoint used",
44+
Content: `
45+
output "test" {
46+
value = aws_s3_bucket.test.bucket_regional_domain_name
47+
}`,
48+
Expected: helper.Issues{},
49+
},
50+
// TODO: strings of the form "bucket_name.s3.amazonaws.com" ?
51+
{
52+
Name: "legacy global endpoint used",
53+
Content: `
54+
output "test" {
55+
value = aws_s3_bucket.test.bucket_domain_name
56+
}`,
57+
Expected: helper.Issues{
58+
{
59+
Rule: NewAwsS3NoGlobalEndpointRule(),
60+
Message: "`bucket_domain_name` returns the legacy s3 global endpoint, use `bucket_regional_domain_name` instead",
61+
Range: hcl.Range{
62+
Filename: "resource.tf",
63+
Start: hcl.Pos{Line: 3, Column: 11},
64+
End: hcl.Pos{Line: 3, Column: 48},
65+
},
66+
},
67+
},
68+
},
69+
}
70+
71+
rule := NewAwsS3NoGlobalEndpointRule()
72+
73+
for _, tc := range cases {
74+
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
75+
76+
if err := rule.Check(runner); err != nil {
77+
t.Fatalf("Unexpected error occurred: %s", err)
78+
}
79+
80+
helper.AssertIssues(t, tc.Expected, runner.Issues)
81+
}
82+
}

0 commit comments

Comments
 (0)