Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new rule configuration syntax #197

Merged
merged 4 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
}
Expand Down Expand Up @@ -238,6 +239,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -287,6 +289,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -310,6 +313,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -339,6 +343,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars", "example1.tfvars", "example2.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -362,6 +367,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.example.hcl",
},
Expand All @@ -385,6 +391,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -412,6 +419,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand All @@ -438,6 +446,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand Down Expand Up @@ -506,6 +515,7 @@ func TestCLIRun(t *testing.T) {
Varfile: []string{"terraform.tfvars"},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*config.Rule{},
},
ConfigFile: ".tflint.hcl",
},
Expand Down
89 changes: 67 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,38 @@ import (

"io/ioutil"

"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/wata727/tflint/loader"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclparse"
)

type rawConfig struct {
Config *struct {
DeepCheck *bool `hcl:"deep_check"`
AwsCredentials *map[string]string `hcl:"aws_credentials"`
IgnoreModule *map[string]bool `hcl:"ignore_module"`
IgnoreRule *map[string]bool `hcl:"ignore_rule"`
Varfile *[]string `hcl:"varfile"`
TerraformVersion *string `hcl:"terraform_version"`
} `hcl:"config,block"`
Rules []Rule `hcl:"rule,block"`
}

type Config struct {
Debug bool
DeepCheck bool `hcl:"deep_check"`
AwsCredentials map[string]string `hcl:"aws_credentials"`
IgnoreModule map[string]bool `hcl:"ignore_module"`
IgnoreRule map[string]bool `hcl:"ignore_rule"`
Varfile []string `hcl:"varfile"`
TerraformVersion string `hcl:"terraform_version"`
DeepCheck bool
AwsCredentials map[string]string
IgnoreModule map[string]bool
IgnoreRule map[string]bool
Varfile []string
TerraformVersion string
TerraformEnv string
TerraformWorkspace string
Rules map[string]*Rule
}

type Rule struct {
Name string `hcl:"name,label"`
Enabled bool `hcl:"enabled"`
}

func Init() *Config {
Expand All @@ -33,6 +50,7 @@ func Init() *Config {
Varfile: []string{},
TerraformEnv: "default",
TerraformWorkspace: "default",
Rules: map[string]*Rule{},
}
}

Expand All @@ -42,27 +60,24 @@ func (c *Config) LoadConfig(files ...string) error {
c.TerraformWorkspace = string(b)
}

var configs []*ast.ObjectItem
parser := hclparse.NewParser()
for _, file := range files {
if _, err := os.Stat(file); err != nil {
continue
}

l := loader.NewLoader(c.Debug)
if err := l.LoadTemplate(file); err != nil {
continue
f, diags := parser.ParseHCLFile(file)
if diags.HasErrors() {
return diags
}

configs = l.Templates[file].Node.(*ast.ObjectList).Filter("config").Items
break
}

if len(configs) == 0 {
return nil
}
var raw rawConfig
diags = gohcl.DecodeBody(f.Body, nil, &raw)
if diags.HasErrors() {
return diags
}

if err := hcl.DecodeObject(c, configs[0]); err != nil {
return err
c.setConfigFromRaw(raw)
}

return nil
Expand Down Expand Up @@ -127,3 +142,33 @@ func (c *Config) SetVarfile(varfile string) {
varfiles := strings.Split(varfile, ",")
c.Varfile = append(c.Varfile, varfiles...)
}

func (c *Config) setConfigFromRaw(raw rawConfig) {
rc := raw.Config

if rc != nil {
if rc.DeepCheck != nil {
c.DeepCheck = *rc.DeepCheck
}
if rc.AwsCredentials != nil {
c.AwsCredentials = *rc.AwsCredentials
}
if rc.IgnoreModule != nil {
c.IgnoreModule = *rc.IgnoreModule
}
if rc.IgnoreRule != nil {
c.IgnoreRule = *rc.IgnoreRule
}
if rc.Varfile != nil {
c.Varfile = *rc.Varfile
}
if rc.TerraformVersion != nil {
c.TerraformVersion = *rc.TerraformVersion
}
}

for _, r := range raw.Rules {
var rule Rule = r
c.Rules[rule.Name] = &rule
}
}
20 changes: 20 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func TestLoadConfig(t *testing.T) {
TerraformVersion: "0.9.11",
TerraformEnv: "dev",
TerraformWorkspace: "dev",
Rules: map[string]*Rule{
"aws_instance_invalid_type": {
Name: "aws_instance_invalid_type",
Enabled: false,
},
"aws_instance_previous_type": {
Name: "aws_instance_previous_type",
Enabled: false,
},
},
},
},
{
Expand Down Expand Up @@ -74,6 +84,16 @@ func TestLoadConfig(t *testing.T) {
TerraformVersion: "0.9.11",
TerraformEnv: "dev",
TerraformWorkspace: "dev",
Rules: map[string]*Rule{
"aws_instance_invalid_type": {
Name: "aws_instance_invalid_type",
Enabled: false,
},
"aws_instance_previous_type": {
Name: "aws_instance_previous_type",
Enabled: false,
},
},
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions config/test-fixtures/dev_environment/.tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ config {

varfile = ["example1.tfvars", "example2.tfvars"]
}

rule "aws_instance_invalid_type" {
enabled = false
}

rule "aws_instance_previous_type" {
enabled = false
}
1 change: 1 addition & 0 deletions detector/aws_alb_duplicate_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsALBDuplicateNameDetector() *AwsALBDuplicateNameDetec
nd.TargetType = "resource"
nd.Target = "aws_alb"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_alb_invaid_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsALBInvalidSecurityGroupDetector() *AwsALBInvalidSecu
nd.TargetType = "resource"
nd.Target = "aws_alb"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_alb_invalid_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsALBInvalidSubnetDetector() *AwsALBInvalidSubnetDetec
nd.TargetType = "resource"
nd.Target = "aws_alb"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_cloudwatch_metric_alarm_invalid_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsCloudWatchMetricAlarmInvalidUnitDetector() *AwsCloud
nd.Target = "aws_cloudwatch_metric_alarm"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_cloudwatch_metric_alarm_invalid_unit.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_default_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (d *Detector) CreateAwsDBInstanceDefaultParameterGroupDetector() *AwsDBInst
nd.Target = "aws_db_instance"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_db_instance_default_parameter_group.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_duplicate_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsDBInstanceDuplicateIdentifierDetector() *AwsDBInstan
nd.TargetType = "resource"
nd.Target = "aws_db_instance"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_invalid_db_subnet_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsDBInstanceInvalidDBSubnetGroupDetector() *AwsDBInsta
nd.TargetType = "resource"
nd.Target = "aws_db_instance"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_invalid_option_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsDBInstanceInvalidOptionGroupDetector() *AwsDBInstanc
nd.TargetType = "resource"
nd.Target = "aws_db_instance"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_invalid_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsDBInstanceInvalidParameterGroupDetector() *AwsDBInst
nd.TargetType = "resource"
nd.Target = "aws_db_instance"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_invalid_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsDBInstanceInvalidTypeDetector() *AwsDBInstanceInvali
nd.Target = "aws_db_instance"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_db_instance_invalid_type.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_invalid_vpc_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsDBInstanceInvalidVPCSecurityGroupDetector() *AwsDBIn
nd.TargetType = "resource"
nd.Target = "aws_db_instance"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_previous_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsDBInstancePreviousTypeDetector() *AwsDBInstancePrevi
nd.Target = "aws_db_instance"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_db_instance_previous_type.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_db_instance_readable_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (d *Detector) CreateAwsDBInstanceReadablePasswordDetector() *AwsDBInstanceR
nd.Target = "aws_db_instance"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_db_instance_readable_password.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_ecs_cluster_duplicate_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsECSClusterDuplicateNameDetector() *AwsECSClusterDupl
nd.TargetType = "resource"
nd.Target = "aws_ecs_cluster"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (d *Detector) CreateAwsElastiCacheClusterDefaultParameterGroupDetector() *A
nd.Target = "aws_elasticache_cluster"
nd.DeepCheck = false
nd.Link = "https://github.com/wata727/tflint/blob/master/docs/aws_elasticache_cluster_default_parameter_group.md"
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_elasticache_cluster_duplicate_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsElastiCacheClusterDuplicateIDDetector() *AwsElastiCa
nd.TargetType = "resource"
nd.Target = "aws_elasticache_cluster"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (d *Detector) CreateAwsElastiCacheClusterInvalidParameterGroupDetector() *A
nd.TargetType = "resource"
nd.Target = "aws_elasticache_cluster"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
1 change: 1 addition & 0 deletions detector/aws_elasticache_cluster_invalid_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (d *Detector) CreateAwsElastiCacheClusterInvalidSecurityGroupDetector() *Aw
nd.TargetType = "resource"
nd.Target = "aws_elasticache_cluster"
nd.DeepCheck = true
nd.Enabled = true
return nd
}

Expand Down
Loading