github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/awsrules/aws_s3_bucket_invalid_acl.go (about)

     1  package awsrules
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	hcl "github.com/hashicorp/hcl/v2"
     8  	"github.com/terraform-linters/tflint/tflint"
     9  )
    10  
    11  // AwsS3BucketInvalidACLRule checks the pattern is valid
    12  type AwsS3BucketInvalidACLRule struct {
    13  	resourceType  string
    14  	attributeName string
    15  	enum          []string
    16  }
    17  
    18  // NewAwsS3BucketInvalidACLRule returns new rule with default attributes
    19  func NewAwsS3BucketInvalidACLRule() *AwsS3BucketInvalidACLRule {
    20  	return &AwsS3BucketInvalidACLRule{
    21  		resourceType:  "aws_s3_bucket",
    22  		attributeName: "acl",
    23  		enum: []string{
    24  			"private",
    25  			"public-read",
    26  			"public-read-write",
    27  			"aws-exec-read",
    28  			"authenticated-read",
    29  			"log-delivery-write",
    30  			"bucket-owner-read",
    31  			"bucket-owner-full-control",
    32  		},
    33  	}
    34  }
    35  
    36  // Name returns the rule name
    37  func (r *AwsS3BucketInvalidACLRule) Name() string {
    38  	return "aws_s3_bucket_invalid_acl"
    39  }
    40  
    41  // Enabled returns whether the rule is enabled by default
    42  func (r *AwsS3BucketInvalidACLRule) Enabled() bool {
    43  	return true
    44  }
    45  
    46  // Severity returns the rule severity
    47  func (r *AwsS3BucketInvalidACLRule) Severity() string {
    48  	return tflint.ERROR
    49  }
    50  
    51  // Link returns the rule reference link
    52  func (r *AwsS3BucketInvalidACLRule) Link() string {
    53  	return ""
    54  }
    55  
    56  // Check checks the pattern is valid
    57  func (r *AwsS3BucketInvalidACLRule) Check(runner *tflint.Runner) error {
    58  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    59  
    60  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    61  		var val string
    62  		err := runner.EvaluateExpr(attribute.Expr, &val)
    63  
    64  		return runner.EnsureNoError(err, func() error {
    65  			found := false
    66  			for _, item := range r.enum {
    67  				if item == val {
    68  					found = true
    69  				}
    70  			}
    71  			if !found {
    72  				runner.EmitIssue(
    73  					r,
    74  					fmt.Sprintf(`"%s" is an invalid value as acl`, val),
    75  					attribute.Expr.Range(),
    76  				)
    77  			}
    78  			return nil
    79  		})
    80  	})
    81  }