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

     1  // This file generated by `generator/`. DO NOT EDIT
     2  
     3  package models
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"regexp"
     9  
    10  	hcl "github.com/hashicorp/hcl/v2"
    11  	"github.com/terraform-linters/tflint/tflint"
    12  )
    13  
    14  // AwsLambdaFunctionInvalidKmsKeyArnRule checks the pattern is valid
    15  type AwsLambdaFunctionInvalidKmsKeyArnRule struct {
    16  	resourceType  string
    17  	attributeName string
    18  	pattern       *regexp.Regexp
    19  }
    20  
    21  // NewAwsLambdaFunctionInvalidKmsKeyArnRule returns new rule with default attributes
    22  func NewAwsLambdaFunctionInvalidKmsKeyArnRule() *AwsLambdaFunctionInvalidKmsKeyArnRule {
    23  	return &AwsLambdaFunctionInvalidKmsKeyArnRule{
    24  		resourceType:  "aws_lambda_function",
    25  		attributeName: "kms_key_arn",
    26  		pattern:       regexp.MustCompile(`^(arn:(aws[a-zA-Z-]*)?:[a-z0-9-.]+:.*)|()$`),
    27  	}
    28  }
    29  
    30  // Name returns the rule name
    31  func (r *AwsLambdaFunctionInvalidKmsKeyArnRule) Name() string {
    32  	return "aws_lambda_function_invalid_kms_key_arn"
    33  }
    34  
    35  // Enabled returns whether the rule is enabled by default
    36  func (r *AwsLambdaFunctionInvalidKmsKeyArnRule) Enabled() bool {
    37  	return true
    38  }
    39  
    40  // Severity returns the rule severity
    41  func (r *AwsLambdaFunctionInvalidKmsKeyArnRule) Severity() string {
    42  	return tflint.ERROR
    43  }
    44  
    45  // Link returns the rule reference link
    46  func (r *AwsLambdaFunctionInvalidKmsKeyArnRule) Link() string {
    47  	return ""
    48  }
    49  
    50  // Check checks the pattern is valid
    51  func (r *AwsLambdaFunctionInvalidKmsKeyArnRule) Check(runner *tflint.Runner) error {
    52  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    53  
    54  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    55  		var val string
    56  		err := runner.EvaluateExpr(attribute.Expr, &val)
    57  
    58  		return runner.EnsureNoError(err, func() error {
    59  			if !r.pattern.MatchString(val) {
    60  				runner.EmitIssue(
    61  					r,
    62  					fmt.Sprintf(`"%s" does not match valid pattern %s`, truncateLongMessage(val), `^(arn:(aws[a-zA-Z-]*)?:[a-z0-9-.]+:.*)|()$`),
    63  					attribute.Expr.Range(),
    64  				)
    65  			}
    66  			return nil
    67  		})
    68  	})
    69  }