Skip to content

Commit 83e7f11

Browse files
authored
s3_bucket_name: add length validation (#554)
1 parent 5eae983 commit 83e7f11

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

docs/rules/aws_s3_bucket_name.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@ rule "aws_s3_bucket_name" {
2121
resource "aws_s3_bucket" "foo" {
2222
bucket = "foo"
2323
}
24+
25+
resource "aws_s3_bucket" "too_long" {
26+
bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
27+
}
2428
```
2529

2630
```sh
2731
$ tflint
28-
1 issue(s) found:
32+
2 issue(s) found:
2933

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

3236
on main.tf line 2:
3337
2: bucket = "foo"
38+
39+
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)
40+
41+
on main.tf line 2:
42+
2: bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
3443
```
3544

3645
## Why
3746

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

4049
* Prefixing bucket names with an organization name can help avoid naming conflicts
4150
* You may wish to enforce other naming conventions (e.g., disallowing dots)

integration/rule-config/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"rule": {
55
"name": "aws_s3_bucket_name",
6-
"severity": "warning",
6+
"severity": "error",
77
"link": "https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.27.0/docs/rules/aws_s3_bucket_name.md"
88
},
99
"message": "Bucket name \"foo_bar\" does not match regex \"^[a-z\\-]+$\"",

rules/aws_s3_bucket_name.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func (r *AwsS3BucketNameRule) Name() string {
3838

3939
// Enabled returns whether the rule is enabled by default
4040
func (r *AwsS3BucketNameRule) Enabled() bool {
41-
return false
41+
return true
4242
}
4343

4444
// Severity returns the rule severity
4545
func (r *AwsS3BucketNameRule) Severity() tflint.Severity {
46-
return tflint.WARNING
46+
return tflint.ERROR
4747
}
4848

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

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

73+
bucketNameMinLength := 3
74+
bucketNameMaxLength := 63
75+
7376
for _, resource := range resources.Blocks {
7477
attribute, exists := resource.Body.Attributes[r.attributeName]
7578
if !exists {
@@ -96,6 +99,14 @@ func (r *AwsS3BucketNameRule) Check(runner tflint.Runner) error {
9699
)
97100
}
98101
}
102+
103+
if len(name) < bucketNameMinLength || len(name) > bucketNameMaxLength {
104+
runner.EmitIssue(
105+
r,
106+
fmt.Sprintf("Bucket name %q must be between %d and %d characters", name, bucketNameMinLength, bucketNameMaxLength),
107+
attribute.Expr.Range(),
108+
)
109+
}
99110
return nil
100111
}, nil)
101112
if err != nil {

rules/aws_s3_bucket_name_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ rule "aws_s3_bucket_name" {
117117
},
118118
},
119119
},
120+
{
121+
Name: "length",
122+
Content: `
123+
resource "aws_s3_bucket" "too_long" {
124+
bucket = "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test"
125+
}
126+
`,
127+
Expected: helper.Issues{
128+
{
129+
Rule: NewAwsS3BucketNameRule(),
130+
Message: `Bucket name "a-really-ultra-hiper-super-long-foo-bar-baz-bucket-name.domain.test" must be between 3 and 63 characters`,
131+
Range: hcl.Range{
132+
Filename: "resource.tf",
133+
Start: hcl.Pos{Line: 3, Column: 12},
134+
End: hcl.Pos{Line: 3, Column: 81},
135+
},
136+
},
137+
},
138+
},
120139
}
121140

122141
rule := NewAwsS3BucketNameRule()

0 commit comments

Comments
 (0)