generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package rules | ||
|
||
import ( | ||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/terraform-linters/tflint-ruleset-aws/project" | ||
) | ||
|
||
// AwsS3NoGlobalEndpointRule checks whether deprecated s3 global endpoint is used instead | ||
// of the regional endoint | ||
type AwsS3NoGlobalEndpointRule struct { | ||
tflint.DefaultRule | ||
|
||
// TODO: do we need this, if so why | ||
resourceType string | ||
attributeName string | ||
} | ||
|
||
// NewAwsS3NoGlobalEndpointRule returns new rule with default attributes | ||
func NewAwsS3NoGlobalEndpointRule() *AwsS3NoGlobalEndpointRule { | ||
return &AwsS3NoGlobalEndpointRule{ | ||
resourceType: "aws_s3_bucket", | ||
attributeName: "", | ||
} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *AwsS3NoGlobalEndpointRule) Name() string { | ||
return "aws_acm_certificate_lifecycle" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *AwsS3NoGlobalEndpointRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *AwsS3NoGlobalEndpointRule) Severity() tflint.Severity { | ||
return tflint.WARNING | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *AwsS3NoGlobalEndpointRule) Link() string { | ||
return project.ReferenceLink(r.Name()) | ||
} | ||
|
||
// Check checks whether the aws_acm_certificate resource contains create_before_destroy = true in lifecycle block | ||
func (r *AwsS3NoGlobalEndpointRule) Check(runner tflint.Runner) error { | ||
var err error | ||
runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics { | ||
vars := expr.Variables() | ||
|
||
if len(vars) == 0 { | ||
return nil | ||
} | ||
|
||
// is this ever greater than 0 | ||
v := vars[0] | ||
|
||
if v.RootName() == "aws_s3_bucket" && len(v) == 3 && v[2].(hcl.TraverseAttr).Name == "bucket_domain_name" { | ||
err = runner.EmitIssue(r, "`bucket_domain_name` returns the legacy s3 global endpoint, use `bucket_regional_domain_name` instead", v.SourceRange()) | ||
} | ||
|
||
return nil | ||
})) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_AwsS3NoGlobalEndpoint(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "unrelated expression", | ||
Content: ` | ||
output "test" { | ||
value = "testing" | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
{ | ||
Name: "multiple unrelated expressions", | ||
Content: ` | ||
output "test1" { | ||
value = var.whatever | ||
} | ||
output "test2" { | ||
value = "testing" | ||
} | ||
output "test3" { | ||
value = aws_iam_role.test.arn | ||
} | ||
`, | ||
Expected: helper.Issues{}, | ||
}, | ||
// TODO: nested expressions ? | ||
{ | ||
Name: "regional endpoint used", | ||
Content: ` | ||
output "test" { | ||
value = aws_s3_bucket.test.bucket_regional_domain_name | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
// TODO: strings of the form "bucket_name.s3.amazonaws.com" ? | ||
{ | ||
Name: "legacy global endpoint used", | ||
Content: ` | ||
output "test" { | ||
value = aws_s3_bucket.test.bucket_domain_name | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAwsS3NoGlobalEndpointRule(), | ||
Message: "`bucket_domain_name` returns the legacy s3 global endpoint, use `bucket_regional_domain_name` instead", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 11}, | ||
End: hcl.Pos{Line: 3, Column: 48}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
rule := NewAwsS3NoGlobalEndpointRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |