Skip to content

Commit

Permalink
REFACTOR RULE aws_s3_bucket_name
Browse files Browse the repository at this point in the history
  • Loading branch information
davimmt committed Sep 25, 2023
1 parent 21c9afb commit 2f6f2ed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 12 additions & 3 deletions docs/rules/aws_s3_bucket_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ rule "aws_s3_bucket_name" {
resource "aws_s3_bucket" "foo" {
bucket = "foo"
}
resource "aws_s3_bucket" "too_long" {
bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
}
```

```sh
$ tflint
1 issue(s) found:
2 issue(s) found:

Warning: Bucket name "foo" does not have prefix "my-org" (aws_s3_bucket_name)
Error: Bucket name "foo" does not have prefix "my-org" (aws_s3_bucket_name)

on main.tf line 2:
2: bucket = "foo"

Error: Bucket name "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test" length must be within 3 - 63 character range (aws_s3_bucket_name)

on main.tf line 2:
2: bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
```

## Why

Amazon S3 bucket names must be globally unique and have [restrictive naming rules](https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html#bucketnamingrules).
Amazon S3 bucket names must be globally unique and have [restrictive naming rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).

* Prefixing bucket names with an organization name can help avoid naming conflicts
* You may wish to enforce other naming conventions (e.g., disallowing dots)
Expand Down
17 changes: 14 additions & 3 deletions rules/aws_s3_bucket_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ func (r *AwsS3BucketNameRule) Name() string {

// Enabled returns whether the rule is enabled by default
func (r *AwsS3BucketNameRule) Enabled() bool {
return false
return true
}

// Severity returns the rule severity
func (r *AwsS3BucketNameRule) Severity() tflint.Severity {
return tflint.WARNING
return tflint.ERROR
}

// Link returns the rule reference link
func (r *AwsS3BucketNameRule) Link() string {
return project.ReferenceLink(r.Name())
}

// Check if the name of the s3 bucket matches the regex defined in the rule
// Check if the name of the s3 bucket is valid
func (r *AwsS3BucketNameRule) Check(runner tflint.Runner) error {
config := awsS3BucketNameConfig{}
if err := runner.DecodeRuleConfig(r.Name(), &config); err != nil {
Expand All @@ -70,6 +70,9 @@ func (r *AwsS3BucketNameRule) Check(runner tflint.Runner) error {
return err
}

bucketNameMinLength := 3
bucketNameMaxLength := 63

for _, resource := range resources.Blocks {
attribute, exists := resource.Body.Attributes[r.attributeName]
if !exists {
Expand All @@ -96,6 +99,14 @@ func (r *AwsS3BucketNameRule) Check(runner tflint.Runner) error {
)
}
}

if len(name) < bucketNameMinLength || len(name) > bucketNameMaxLength {
runner.EmitIssue(
r,
fmt.Sprintf("Bucket name %q must be between %d and %d characters", name, bucketNameMinLength, bucketNameMaxLength),
attribute.Expr.Range(),
)
}
return nil
}, nil)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions rules/aws_s3_bucket_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ rule "aws_s3_bucket_name" {
},
},
},
{
Name: "length",
Content: `
resource "aws_s3_bucket" "too_long" {
bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
}
`,
Expected: helper.Issues{
{
Rule: NewAwsS3BucketNameRule(),
Message: `Bucket name "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test" must be between 3 and 63 characters`,
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 3, Column: 12},
End: hcl.Pos{Line: 3, Column: 81},
},
},
},
},
}

rule := NewAwsS3BucketNameRule()
Expand Down

0 comments on commit 2f6f2ed

Please sign in to comment.