From 21c693bff05960a183244bb2109245d093103a36 Mon Sep 17 00:00:00 2001 From: roneli <38083777+roneli@users.noreply.github.com> Date: Thu, 27 Jan 2022 18:10:30 +0200 Subject: [PATCH 1/3] fix: Fail on bad semantic version in config --- pkg/config/config_parser.go | 96 ++++++++++++++++++++++--------------- pkg/config/parser_test.go | 18 +++++++ 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/pkg/config/config_parser.go b/pkg/config/config_parser.go index 03d26422c70a08..52e591a47f533b 100644 --- a/pkg/config/config_parser.go +++ b/pkg/config/config_parser.go @@ -2,15 +2,15 @@ package config import ( "fmt" + "github.com/creasty/defaults" + "github.com/hashicorp/hcl/v2/gohcl" + "github.com/spf13/viper" "os" "strings" "github.com/cloudquery/cloudquery/pkg/policy" - "github.com/creasty/defaults" "github.com/hashicorp/hcl/v2" - "github.com/hashicorp/hcl/v2/gohcl" - "github.com/spf13/viper" ) func (p *Parser) LoadConfigFromSource(name string, data []byte) (*Config, hcl.Diagnostics) { @@ -47,42 +47,9 @@ func (p *Parser) decodeConfig(body hcl.Body, diags hcl.Diagnostics) (*Config, hc for _, block := range content.Blocks { switch block.Type { case "cloudquery": - contentDiags = gohcl.DecodeBody(block.Body, &p.HCLContext, &config.CloudQuery) - diags = append(diags, contentDiags...) - // TODO: decode in a more generic way - - if config.CloudQuery.Connection == nil { - config.CloudQuery.Connection = &Connection{ - DSN: "", - } - } - if config.CloudQuery.History != nil { - if err := defaults.Set(config.CloudQuery.History); err != nil { - diags = append(diags, &hcl.Diagnostic{Severity: hcl.DiagError, Summary: "failed to set defaults in history"}) - } - } - - if dsn := viper.GetString("dsn"); dsn != "" { - config.CloudQuery.Connection.DSN = dsn - } - if dir := viper.GetString("plugin-dir"); dir != "." { - if dir == "." { - if dir, err := os.Getwd(); err == nil { - config.CloudQuery.PluginDirectory = dir - } - } else { - config.CloudQuery.PluginDirectory = dir - } - } - if dir := viper.GetString("policy-dir"); dir != "" { - if dir == "." { - if dir, err := os.Getwd(); err != nil { - config.CloudQuery.PolicyDirectory = dir - } - } else { - config.CloudQuery.PolicyDirectory = dir - } - } + cqBlock, cqDiags := decodeCloudQueryBlock(block, &p.HCLContext) + diags = diags.Extend(cqDiags) + config.CloudQuery = cqBlock case "provider": cfg, cfgDiags := decodeProviderBlock(block, &p.HCLContext, existingProviders) diags = append(diags, cfgDiags...) @@ -142,3 +109,54 @@ func ReadModuleConfigProfiles(module string, block hcl.Body) (map[string]hcl.Bod } return ret, nil } + +func decodeCloudQueryBlock(block *hcl.Block, ctx *hcl.EvalContext) (CloudQuery, hcl.Diagnostics) { + var cq CloudQuery + var diags hcl.Diagnostics + diags = diags.Extend(gohcl.DecodeBody(block.Body, ctx, &cq)) + + // TODO: decode in a more generic way + if cq.Connection == nil { + cq.Connection = &Connection{ + DSN: "", + } + } + if cq.History != nil { + if err := defaults.Set(cq.History); err != nil { + diags = append(diags, &hcl.Diagnostic{Severity: hcl.DiagError, Summary: "failed to set defaults in history"}) + } + } + if dsn := viper.GetString("dsn"); dsn != "" { + cq.Connection.DSN = dsn + } + if dir := viper.GetString("plugin-dir"); dir != "." { + if dir == "." { + if dir, err := os.Getwd(); err == nil { + cq.PluginDirectory = dir + } + } else { + cq.PluginDirectory = dir + } + } + if dir := viper.GetString("policy-dir"); dir != "" { + if dir == "." { + if dir, err := os.Getwd(); err != nil { + cq.PolicyDirectory = dir + } + } else { + cq.PolicyDirectory = dir + } + } + // validate provider versions + for _, cp := range cq.Providers { + if cp.Version != "latest" && !strings.HasPrefix(cp.Version, "v") { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: fmt.Sprintf("Provider %s version %s is invalid", cp.Name, cp.Version), + Detail: "Please set to 'latest' version or valid semantic versioning starting with vX.Y.Z", + Subject: &block.DefRange, + }) + } + } + return cq, diags +} diff --git a/pkg/config/parser_test.go b/pkg/config/parser_test.go index 3102a0e5e37bde..085a93aeaa283f 100644 --- a/pkg/config/parser_test.go +++ b/pkg/config/parser_test.go @@ -144,6 +144,17 @@ provider "aws" { ` const expectedDuplicateAliasProviderError = "test.hcl:23,3-21: Duplicate Alias; Provider with alias same-aws for provider aws already exists, give it a different alias." +const testBadVersion = `cloudquery { + connection { + dsn = "postgres://postgres:pass@localhost:5432/postgres" + } + provider "test" { + source = "cloudquery" + version = "0.0.0" + } +}` + + type Account struct { ID string `hcl:",label"` RoleARN string `hcl:"role_arn,optional"` @@ -185,6 +196,13 @@ func TestParser_LoadConfigFromSource(t *testing.T) { }, cfg) } +func TestParser_BadVersion(t *testing.T) { + p := NewParser() + _, diags := p.LoadConfigFromSource("test.hcl", []byte(testBadVersion)) + assert.NotNil(t, diags) + assert.Equal(t, "test.hcl:1,1-11: Provider test version 0.0.0 is invalid; Please set to 'latest' version or valid semantic versioning starting with vX.Y.Z", diags[0].Error()) +} + func TestParser_DuplicateProviderNaming(t *testing.T) { p := NewParser() _, diags := p.LoadConfigFromSource("test.hcl", []byte(testMultipleProviderConfig)) From d4d073843461195835e76d6d81c7a3fec7ce783a Mon Sep 17 00:00:00 2001 From: roneli <38083777+roneli@users.noreply.github.com> Date: Thu, 27 Jan 2022 18:10:46 +0200 Subject: [PATCH 2/3] linting --- pkg/config/config_parser.go | 5 +++-- pkg/config/parser_test.go | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/config/config_parser.go b/pkg/config/config_parser.go index 52e591a47f533b..49d34d6ed809b7 100644 --- a/pkg/config/config_parser.go +++ b/pkg/config/config_parser.go @@ -2,11 +2,12 @@ package config import ( "fmt" + "os" + "strings" + "github.com/creasty/defaults" "github.com/hashicorp/hcl/v2/gohcl" "github.com/spf13/viper" - "os" - "strings" "github.com/cloudquery/cloudquery/pkg/policy" diff --git a/pkg/config/parser_test.go b/pkg/config/parser_test.go index 085a93aeaa283f..4ca743296897cb 100644 --- a/pkg/config/parser_test.go +++ b/pkg/config/parser_test.go @@ -154,7 +154,6 @@ const testBadVersion = `cloudquery { } }` - type Account struct { ID string `hcl:",label"` RoleARN string `hcl:"role_arn,optional"` From d695664b2e26500c32b5e375f06e7b46cb57a9f4 Mon Sep 17 00:00:00 2001 From: Ron <38083777+roneli@users.noreply.github.com> Date: Thu, 27 Jan 2022 18:30:05 +0200 Subject: [PATCH 3/3] Update pkg/config/config_parser.go Co-authored-by: Kemal <223029+disq@users.noreply.github.com> --- pkg/config/config_parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config_parser.go b/pkg/config/config_parser.go index 49d34d6ed809b7..ce03998b7a12d2 100644 --- a/pkg/config/config_parser.go +++ b/pkg/config/config_parser.go @@ -141,7 +141,7 @@ func decodeCloudQueryBlock(block *hcl.Block, ctx *hcl.EvalContext) (CloudQuery, } if dir := viper.GetString("policy-dir"); dir != "" { if dir == "." { - if dir, err := os.Getwd(); err != nil { + if dir, err := os.Getwd(); err == nil { cq.PolicyDirectory = dir } } else {