-
Notifications
You must be signed in to change notification settings - Fork 546
feat: Add validate-config command
#17530
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7b6eaeb
initial
bbernays 4f9324a
Update specs.go
bbernays ba354d9
revert no-init
bbernays 3a42b33
Create validate_config.go
bbernays fc8e161
Update root.go
bbernays 5321b89
tests
bbernays 4fa27a1
Update cli/cmd/test_connection.go
erezrokah d72f04b
Merge branch 'main' into cli-no-init
bbernays 29402dc
Merge branch 'cli-no-init' of https://github.com/cloudquery/cloudquer…
bbernays 844fd2f
Update validate_config_test.go
bbernays fe80812
Update doc_test.go
bbernays 4867a4b
docs
bbernays 79eb70e
Merge branch 'main' into cli-no-init
erezrokah 38c85d8
test: Use a version that doesn't require auth
erezrokah 0697e08
test: Update tables
erezrokah 97f93c6
Merge branch 'main' into cli-no-init
erezrokah 5ebb556
Update switch_test.go
bbernays d64ff84
add caveat
bbernays File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| kind: source | ||
| spec: | ||
| name: cloudflare | ||
| path: cloudquery/cloudflare | ||
| registry: cloudquery | ||
| version: "v6.1.2" | ||
| destinations: ["postgresql"] | ||
| tables: ["*"] | ||
| spec: | ||
| invalid_key: "invalid_value" | ||
| --- | ||
| kind: destination | ||
| spec: | ||
| name: "postgresql" | ||
| path: "cloudquery/postgresql" | ||
| registry: cloudquery | ||
| version: "v7.3.5" | ||
| spec: | ||
| connection_string: "postgresql://postgres:not-a-real-password@localhost:5432/postgres?sslmode=disable" | ||
| invalid_key: "invalid_value" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/cloudquery/cloudquery/cli/internal/auth" | ||
| "github.com/cloudquery/cloudquery/cli/internal/specs/v0" | ||
| "github.com/cloudquery/plugin-pb-go/managedplugin" | ||
| "github.com/cloudquery/plugin-pb-go/pb/plugin/v3" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| validateConfigShort = "Validate config" | ||
| validateConfigLong = "Validate configuration without requiring any credentials or connections. This will not validate the tables specified in the tables list. This validation is stricter than the validation done during `sync`, but if it passes this validation it will pass the sync validation." | ||
| validateConfigExample = `# Validate configs | ||
| cloudquery validate-config ./directory | ||
| # Validate configs from directories and files | ||
| cloudquery validate-config ./directory ./aws.yml ./pg.yml | ||
| ` | ||
| ) | ||
|
|
||
| func newCmdValidateConfig() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "validate-config [files or directories]", | ||
| Short: validateConfigShort, | ||
| Long: validateConfigLong, | ||
| Example: validateConfigExample, | ||
| Args: cobra.MinimumNArgs(1), | ||
| RunE: validateConfig, | ||
| Hidden: false, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func validateConfig(cmd *cobra.Command, args []string) error { | ||
| cqDir, err := cmd.Flags().GetString("cq-dir") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
|
|
||
| log.Info().Strs("args", args).Msg("Loading spec(s)") | ||
| fmt.Printf("Loading spec(s) from %s\n", strings.Join(args, ", ")) | ||
| specReader, err := specs.NewSpecReader(args) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load spec(s) from %s. Error: %w", strings.Join(args, ", "), err) | ||
| } | ||
| sources := specReader.Sources | ||
| destinations := specReader.Destinations | ||
|
|
||
| authToken, err := auth.GetAuthTokenIfNeeded(log.Logger, sources, destinations) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get auth token: %w", err) | ||
| } | ||
| teamName, err := auth.GetTeamForToken(authToken) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get team name: %w", err) | ||
| } | ||
| opts := []managedplugin.Option{ | ||
| managedplugin.WithLogger(log.Logger), | ||
| managedplugin.WithAuthToken(authToken.Value), | ||
| managedplugin.WithTeamName(teamName), | ||
| } | ||
| if cqDir != "" { | ||
| opts = append(opts, managedplugin.WithDirectory(cqDir)) | ||
| } | ||
| if disableSentry { | ||
| opts = append(opts, managedplugin.WithNoSentry()) | ||
| } | ||
|
|
||
| sourcePluginConfigs := make([]managedplugin.Config, len(sources)) | ||
| sourceRegInferred := make([]bool, len(sources)) | ||
| for i, source := range sources { | ||
| sourcePluginConfigs[i] = managedplugin.Config{ | ||
| Name: source.Name, | ||
| Version: source.Version, | ||
| Path: source.Path, | ||
| Registry: SpecRegistryToPlugin(source.Registry), | ||
| DockerAuth: source.DockerRegistryAuthToken, | ||
| } | ||
| sourceRegInferred[i] = source.RegistryInferred() | ||
| } | ||
| destinationPluginConfigs := make([]managedplugin.Config, len(destinations)) | ||
| destinationRegInferred := make([]bool, len(destinations)) | ||
| for i, destination := range destinations { | ||
| destinationPluginConfigs[i] = managedplugin.Config{ | ||
| Name: destination.Name, | ||
| Version: destination.Version, | ||
| Path: destination.Path, | ||
| Registry: SpecRegistryToPlugin(destination.Registry), | ||
| DockerAuth: destination.DockerRegistryAuthToken, | ||
| } | ||
| destinationRegInferred[i] = destination.RegistryInferred() | ||
| } | ||
|
|
||
| sourceClients, err := managedplugin.NewClients(ctx, managedplugin.PluginSource, sourcePluginConfigs, opts...) | ||
| if err != nil { | ||
| return enrichClientError(sourceClients, sourceRegInferred, err) | ||
| } | ||
| defer func() { | ||
| if err := sourceClients.Terminate(); err != nil { | ||
| fmt.Println(err) | ||
| } | ||
| }() | ||
| destinationClients, err := managedplugin.NewClients(ctx, managedplugin.PluginDestination, destinationPluginConfigs, opts...) | ||
| if err != nil { | ||
| return enrichClientError(destinationClients, destinationRegInferred, err) | ||
| } | ||
| defer func() { | ||
| if err := destinationClients.Terminate(); err != nil { | ||
| fmt.Println(err) | ||
| } | ||
| }() | ||
|
|
||
| var initErrors []error | ||
| for i, client := range sourceClients { | ||
| pluginClient := plugin.NewPluginClient(client.Conn) | ||
| log.Info().Str("source", sources[i].VersionString()).Msg("Initializing source") | ||
| err := validatePluginSpec(ctx, pluginClient, sources[i].Spec) | ||
| if err != nil { | ||
| initErrors = append(initErrors, fmt.Errorf("failed to validate source config %v: %w", sources[i].VersionString(), err)) | ||
| } else { | ||
| log.Info().Str("source", sources[i].VersionString()).Msg("validated successfully") | ||
| } | ||
| } | ||
| for i, client := range destinationClients { | ||
| pluginClient := plugin.NewPluginClient(client.Conn) | ||
| log.Info().Str("destination", destinations[i].VersionString()).Msg("Initializing destination") | ||
| err = validatePluginSpec(ctx, pluginClient, destinations[i].Spec) | ||
| if err != nil { | ||
| initErrors = append(initErrors, fmt.Errorf("failed to validate destination config %v: %w", destinations[i].VersionString(), err)) | ||
| } else { | ||
| log.Info().Str("destination", destinations[i].VersionString()).Msg("validated successfully") | ||
| } | ||
| } | ||
|
|
||
| return errors.Join(initErrors...) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidateConfig(t *testing.T) { | ||
| configs := []struct { | ||
| name string | ||
| config string | ||
| errors []string | ||
| }{ | ||
| { | ||
| name: "multiple test sources should pass validation", | ||
| config: "multiple-sources.yml", | ||
| }, | ||
| { | ||
| name: "bad AWS and Postgres auth should fail validation", | ||
| config: "validate-config-error.yml", | ||
| errors: []string{"failed to validate source config cloudflare", "failed to validate destination config postgresql"}, | ||
| }, | ||
| } | ||
| _, filename, _, _ := runtime.Caller(0) | ||
| currentDir := path.Dir(filename) | ||
|
|
||
| for _, tc := range configs { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| cmd := NewCmdRoot() | ||
| testConfig := path.Join(currentDir, "testdata", tc.config) | ||
| baseArgs := testCommandArgs(t) | ||
|
|
||
| args := append([]string{"validate-config", testConfig}, baseArgs...) | ||
| cmd.SetArgs(args) | ||
| err := cmd.Execute() | ||
| if tc.errors != nil { | ||
| for _, e := range tc.errors { | ||
| assert.Contains(t, err.Error(), e) | ||
| } | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| // check that log was written and contains some lines from the plugin | ||
| b, logFileError := os.ReadFile(baseArgs[3]) | ||
| logContent := string(b) | ||
| require.NoError(t, logFileError, "failed to read cloudquery.log") | ||
| require.NotEmpty(t, logContent, "cloudquery.log empty; expected some logs") | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
website/pages/docs/reference/cli/cloudquery_validate-config.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| title: "validate-config" | ||
| --- | ||
| ## cloudquery validate-config | ||
|
|
||
| Validate config | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Validate configuration without requiring any credentials or connections. This will not validate the tables specified in the tables list. This validation is stricter than the validation done during `sync`, but if it passes this validation it will pass the sync validation. | ||
|
|
||
| ``` | ||
| cloudquery validate-config [files or directories] [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| # Validate configs | ||
| cloudquery validate-config ./directory | ||
| # Validate configs from directories and files | ||
| cloudquery validate-config ./directory ./aws.yml ./pg.yml | ||
|
|
||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for validate-config | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| --cq-dir string directory to store cloudquery files, such as downloaded plugins (default ".cq") | ||
| --log-console enable console logging | ||
| --log-file-name string Log filename (default "cloudquery.log") | ||
| --log-format string Logging format (json, text) (default "text") | ||
| --log-level string Logging level (trace, debug, info, warn, error) (default "info") | ||
| --no-log-file Disable logging to file | ||
| --telemetry-level string Telemetry level (none, errors, stats, all) (default "all") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [cloudquery](/docs/reference/cli/cloudquery) - CloudQuery CLI | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.