github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/models/aws_iam_policy_invalid_policy.go (about)

     1  // This file generated by `tools/model-rule-gen/main.go`. DO NOT EDIT
     2  
     3  package models
     4  
     5  import (
     6  	"log"
     7  	"regexp"
     8  
     9  	hcl "github.com/hashicorp/hcl/v2"
    10  	"github.com/wata727/tflint/tflint"
    11  )
    12  
    13  // AwsIAMPolicyInvalidPolicyRule checks the pattern is valid
    14  type AwsIAMPolicyInvalidPolicyRule struct {
    15  	resourceType  string
    16  	attributeName string
    17  	max           int
    18  	min           int
    19  	pattern       *regexp.Regexp
    20  }
    21  
    22  // NewAwsIAMPolicyInvalidPolicyRule returns new rule with default attributes
    23  func NewAwsIAMPolicyInvalidPolicyRule() *AwsIAMPolicyInvalidPolicyRule {
    24  	return &AwsIAMPolicyInvalidPolicyRule{
    25  		resourceType:  "aws_iam_policy",
    26  		attributeName: "policy",
    27  		max:           131072,
    28  		min:           1,
    29  		pattern:       regexp.MustCompile(`^[\x{0009}\x{000A}\x{000D}\x{0020}-\x{00FF}]+$`),
    30  	}
    31  }
    32  
    33  // Name returns the rule name
    34  func (r *AwsIAMPolicyInvalidPolicyRule) Name() string {
    35  	return "aws_iam_policy_invalid_policy"
    36  }
    37  
    38  // Enabled returns whether the rule is enabled by default
    39  func (r *AwsIAMPolicyInvalidPolicyRule) Enabled() bool {
    40  	return true
    41  }
    42  
    43  // Severity returns the rule severity
    44  func (r *AwsIAMPolicyInvalidPolicyRule) Severity() string {
    45  	return tflint.ERROR
    46  }
    47  
    48  // Link returns the rule reference link
    49  func (r *AwsIAMPolicyInvalidPolicyRule) Link() string {
    50  	return ""
    51  }
    52  
    53  // Check checks the pattern is valid
    54  func (r *AwsIAMPolicyInvalidPolicyRule) Check(runner *tflint.Runner) error {
    55  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    56  
    57  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    58  		var val string
    59  		err := runner.EvaluateExpr(attribute.Expr, &val)
    60  
    61  		return runner.EnsureNoError(err, func() error {
    62  			if len(val) > r.max {
    63  				runner.EmitIssue(
    64  					r,
    65  					"policy must be 131072 characters or less",
    66  					attribute.Expr.Range(),
    67  				)
    68  			}
    69  			if len(val) < r.min {
    70  				runner.EmitIssue(
    71  					r,
    72  					"policy must be 1 characters or higher",
    73  					attribute.Expr.Range(),
    74  				)
    75  			}
    76  			if !r.pattern.MatchString(val) {
    77  				runner.EmitIssue(
    78  					r,
    79  					`policy does not match valid pattern ^[\x{0009}\x{000A}\x{000D}\x{0020}-\x{00FF}]+$`,
    80  					attribute.Expr.Range(),
    81  				)
    82  			}
    83  			return nil
    84  		})
    85  	})
    86  }