Skip to content

Commit fc2b8fe

Browse files
authored
Bump tflint-plugin-sdk for gRPC-based new plugin system (#274)
1 parent 06870a6 commit fc2b8fe

File tree

1,470 files changed

+43712
-13426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,470 files changed

+43712
-13426
lines changed

aws/config.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package aws
22

3-
import "github.com/hashicorp/hcl/v2"
4-
53
// Config is the configuration for the ruleset.
64
type Config struct {
7-
DeepCheck bool `hcl:"deep_check,optional"`
8-
AccessKey string `hcl:"access_key,optional"`
9-
SecretKey string `hcl:"secret_key,optional"`
10-
Region string `hcl:"region,optional"`
11-
Profile string `hcl:"profile,optional"`
12-
SharedCredentialsFile string `hcl:"shared_credentials_file,optional"`
13-
14-
Remain hcl.Body `hcl:",remain"`
5+
DeepCheck bool `hclext:"deep_check,optional"`
6+
AccessKey string `hclext:"access_key,optional"`
7+
SecretKey string `hclext:"secret_key,optional"`
8+
Region string `hclext:"region,optional"`
9+
Profile string `hclext:"profile,optional"`
10+
SharedCredentialsFile string `hclext:"shared_credentials_file,optional"`
1511
}
1612

1713
func (c *Config) toCredentials() Credentials {

aws/provider.go

Lines changed: 72 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,120 @@
11
package aws
22

33
import (
4-
"log"
5-
6-
"github.com/hashicorp/hcl/v2"
7-
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
4+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
85
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
96
)
107

118
// AwsProviderBlockSchema is a schema of `aws` provider block
12-
var AwsProviderBlockSchema = &hcl.BodySchema{
13-
Attributes: []hcl.AttributeSchema{
9+
var AwsProviderBlockSchema = &hclext.BodySchema{
10+
Attributes: []hclext.AttributeSchema{
1411
{Name: "access_key"},
1512
{Name: "secret_key"},
1613
{Name: "profile"},
1714
{Name: "shared_credentials_file"},
1815
{Name: "region"},
1916
},
20-
Blocks: []hcl.BlockHeaderSchema{
21-
{Type: "assume_role"},
17+
Blocks: []hclext.BlockSchema{
18+
{
19+
Type: "assume_role",
20+
Body: AwsProviderAssumeRoleBlockShema,
21+
},
2222
},
2323
}
2424

2525
// AwsProviderAssumeRoleBlockShema is a schema of `assume_role` block
26-
var AwsProviderAssumeRoleBlockShema = &hcl.BodySchema{
27-
Attributes: []hcl.AttributeSchema{
26+
var AwsProviderAssumeRoleBlockShema = &hclext.BodySchema{
27+
Attributes: []hclext.AttributeSchema{
2828
{Name: "role_arn", Required: true},
2929
{Name: "session_name"},
3030
{Name: "external_id"},
3131
{Name: "policy"},
3232
},
3333
}
3434

35-
// ProviderData represents a provider block with an eval context (runner)
36-
type ProviderData struct {
37-
provider *configs.Provider
38-
runner tflint.Runner
39-
attributes hcl.Attributes
40-
blocks hcl.Blocks
41-
}
42-
4335
// GetCredentialsFromProvider retrieves credentials from the "provider" block in the Terraform configuration
4436
func GetCredentialsFromProvider(runner tflint.Runner) (Credentials, error) {
4537
creds := Credentials{}
4638

47-
providerConfig, err := runner.RootProvider("aws")
48-
if err != nil || providerConfig == nil {
49-
return creds, err
50-
}
51-
52-
d, err := newProviderData(providerConfig, runner)
53-
if err != nil {
54-
return creds, err
55-
}
56-
57-
accessKey, exists, err := d.Get("access_key")
58-
if err != nil {
59-
return creds, err
60-
}
61-
if exists {
62-
creds.AccessKey = accessKey
63-
}
64-
65-
secretKey, exists, err := d.Get("secret_key")
39+
providers, err := runner.GetModuleContent(
40+
&hclext.BodySchema{
41+
Blocks: []hclext.BlockSchema{
42+
{
43+
Type: "provider",
44+
LabelNames: []string{"name"},
45+
Body: AwsProviderBlockSchema,
46+
},
47+
},
48+
},
49+
&tflint.GetModuleContentOption{ModuleCtx: tflint.RootModuleCtxType},
50+
)
6651
if err != nil {
6752
return creds, err
6853
}
69-
if exists {
70-
creds.SecretKey = secretKey
71-
}
7254

73-
profile, exists, err := d.Get("profile")
74-
if err != nil {
75-
return creds, err
76-
}
77-
if exists {
78-
creds.Profile = profile
79-
}
80-
81-
credsFile, exists, err := d.Get("shared_credentials_file")
82-
if err != nil {
83-
return creds, err
84-
}
85-
if exists {
86-
creds.CredsFile = credsFile
87-
}
55+
for _, provider := range providers.Blocks {
56+
if provider.Labels[0] != "aws" {
57+
continue
58+
}
8859

89-
region, exists, err := d.Get("region")
90-
if err != nil {
91-
return creds, err
92-
}
93-
if exists {
94-
creds.Region = region
95-
}
60+
opts := &tflint.EvaluateExprOption{ModuleCtx: tflint.RootModuleCtxType}
9661

97-
assumeRole, exists, err := d.GetBlock("assume_role", AwsProviderAssumeRoleBlockShema)
98-
if err != nil {
99-
return creds, err
100-
}
101-
if exists {
102-
roleARN, exists, err := assumeRole.Get("role_arn")
103-
if err != nil {
104-
return creds, err
105-
}
106-
if exists {
107-
creds.AssumeRoleARN = roleARN
62+
if attr, exists := provider.Body.Attributes["access_key"]; exists {
63+
if err := runner.EvaluateExpr(attr.Expr, &creds.AccessKey, opts); err != nil {
64+
return creds, err
65+
}
10866
}
10967

110-
sessionName, exists, err := assumeRole.Get("session_name")
111-
if err != nil {
112-
return creds, err
113-
}
114-
if exists {
115-
creds.AssumeRoleSessionName = sessionName
68+
if attr, exists := provider.Body.Attributes["secret_key"]; exists {
69+
if err := runner.EvaluateExpr(attr.Expr, &creds.SecretKey, opts); err != nil {
70+
return creds, err
71+
}
11672
}
11773

118-
externalID, exists, err := assumeRole.Get("external_id")
119-
if err != nil {
120-
return creds, err
121-
}
122-
if exists {
123-
creds.AssumeRoleExternalID = externalID
74+
if attr, exists := provider.Body.Attributes["profile"]; exists {
75+
if err := runner.EvaluateExpr(attr.Expr, &creds.Profile, opts); err != nil {
76+
return creds, err
77+
}
12478
}
12579

126-
policy, exists, err := assumeRole.Get("policy")
127-
if err != nil {
128-
return creds, err
129-
}
130-
if exists {
131-
creds.AssumeRolePolicy = policy
80+
if attr, exists := provider.Body.Attributes["shared_credentials_file"]; exists {
81+
if err := runner.EvaluateExpr(attr.Expr, &creds.CredsFile, opts); err != nil {
82+
return creds, err
83+
}
13284
}
133-
}
13485

135-
return creds, nil
136-
}
137-
138-
func newProviderData(provider *configs.Provider, runner tflint.Runner) (*ProviderData, error) {
139-
providerData := &ProviderData{
140-
provider: provider,
141-
runner: runner,
142-
attributes: map[string]*hcl.Attribute{},
143-
blocks: []*hcl.Block{},
144-
}
145-
146-
if provider != nil {
147-
content, _, diags := provider.Config.PartialContent(AwsProviderBlockSchema)
148-
if diags.HasErrors() {
149-
return nil, diags
86+
if attr, exists := provider.Body.Attributes["region"]; exists {
87+
if err := runner.EvaluateExpr(attr.Expr, &creds.Region, opts); err != nil {
88+
return creds, err
89+
}
15090
}
15191

152-
providerData.attributes = content.Attributes
153-
providerData.blocks = content.Blocks
154-
}
155-
156-
return providerData, nil
157-
}
158-
159-
// Get returns a value corresponding to the given key
160-
// It should be noted that the value is evaluated if it is evaluable
161-
// The second return value is a flag that determines whether a value exists
162-
// We assume the provider has only simple attributes, so it just returns string
163-
func (d *ProviderData) Get(key string) (string, bool, error) {
164-
attribute, exists := d.attributes[key]
165-
if !exists {
166-
log.Printf("[INFO] `%s` is not found in the provider block.", key)
167-
return "", false, nil
168-
}
169-
170-
var val string
171-
err := d.runner.EvaluateExprOnRootCtx(attribute.Expr, &val, nil)
172-
173-
err = d.runner.EnsureNoError(err, func() error { return nil })
174-
if err != nil {
175-
return "", true, err
176-
}
177-
return val, true, nil
178-
}
179-
180-
// GetBlock returns a value just like Get.
181-
// The difference is that GetBlock returns ProviderData rather than a string value.
182-
func (d *ProviderData) GetBlock(key string, schema *hcl.BodySchema) (*ProviderData, bool, error) {
183-
providerData := &ProviderData{
184-
provider: d.provider,
185-
runner: d.runner,
186-
attributes: map[string]*hcl.Attribute{},
187-
blocks: []*hcl.Block{},
188-
}
189-
190-
var ret *hcl.Block
191-
for _, block := range d.blocks {
192-
if block.Type == key {
193-
ret = block
92+
for _, assumeRole := range provider.Body.Blocks {
93+
if attr, exists := assumeRole.Body.Attributes["role_arn"]; exists {
94+
if err := runner.EvaluateExpr(attr.Expr, &creds.AssumeRoleARN, opts); err != nil {
95+
return creds, err
96+
}
97+
}
98+
99+
if attr, exists := assumeRole.Body.Attributes["session_name"]; exists {
100+
if err := runner.EvaluateExpr(attr.Expr, &creds.AssumeRoleSessionName, opts); err != nil {
101+
return creds, err
102+
}
103+
}
104+
105+
if attr, exists := assumeRole.Body.Attributes["external_id"]; exists {
106+
if err := runner.EvaluateExpr(attr.Expr, &creds.AssumeRoleExternalID, opts); err != nil {
107+
return creds, err
108+
}
109+
}
110+
111+
if attr, exists := assumeRole.Body.Attributes["policy"]; exists {
112+
if err := runner.EvaluateExpr(attr.Expr, &creds.AssumeRolePolicy, opts); err != nil {
113+
return creds, err
114+
}
115+
}
194116
}
195117
}
196-
if ret == nil {
197-
log.Printf("[INFO] `%s` is not found in the provider block.", key)
198-
return providerData, false, nil
199-
}
200118

201-
content, _, diags := ret.Body.PartialContent(schema)
202-
if diags.HasErrors() {
203-
return providerData, true, diags
204-
}
205-
206-
providerData.attributes = content.Attributes
207-
providerData.blocks = content.Blocks
208-
209-
return providerData, true, nil
119+
return creds, nil
210120
}

aws/ruleset.go

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,42 @@ package aws
33
import (
44
"fmt"
55

6-
"github.com/hashicorp/hcl/v2/gohcl"
6+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
77
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
88
)
99

1010
// RuleSet is the custom ruleset for the AWS provider plugin.
1111
type RuleSet struct {
1212
tflint.BuiltinRuleSet
13-
APIRules []tflint.Rule
14-
config *Config
13+
config *Config
1514
}
1615

17-
// RuleNames is a list of rule names provided by the plugin.
18-
func (r *RuleSet) RuleNames() []string {
19-
names := []string{}
20-
for _, rule := range r.Rules {
21-
names = append(names, rule.Name())
22-
}
23-
for _, rule := range r.APIRules {
24-
names = append(names, rule.Name())
25-
}
26-
return names
16+
func (r *RuleSet) ConfigSchema() *hclext.BodySchema {
17+
r.config = &Config{}
18+
return hclext.ImpliedBodySchema(r.config)
2719
}
2820

2921
// ApplyConfig reflects the plugin configuration to the ruleset.
30-
func (r *RuleSet) ApplyConfig(config *tflint.Config) error {
31-
r.ApplyCommonConfig(config)
32-
33-
// Apply "plugin" block config
34-
cfg := Config{}
35-
diags := gohcl.DecodeBody(config.Body, nil, &cfg)
22+
func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error {
23+
diags := hclext.DecodeBody(body, nil, r.config)
3624
if diags.HasErrors() {
3725
return diags
3826
}
39-
r.config = &cfg
4027

41-
// Apply config for API rules
42-
for _, rule := range r.APIRules {
43-
enabled := rule.Enabled()
44-
if cfg := config.Rules[rule.Name()]; cfg != nil {
45-
enabled = cfg.Enabled
46-
} else if config.DisabledByDefault {
47-
enabled = false
48-
}
28+
if r.config.DeepCheck {
29+
return nil
30+
}
4931

50-
if cfg.DeepCheck && enabled {
51-
r.EnabledRules = append(r.EnabledRules, rule)
32+
// Disable deep checking rules
33+
enabledRules := []tflint.Rule{}
34+
for _, rule := range r.EnabledRules {
35+
meta := rule.Metadata()
36+
// Deep checking rules must have metadata like `map[string]bool{"deep": true}``
37+
if meta == nil {
38+
enabledRules = append(enabledRules, rule)
5239
}
5340
}
41+
r.EnabledRules = enabledRules
5442

5543
return nil
5644
}

0 commit comments

Comments
 (0)