-
Notifications
You must be signed in to change notification settings - Fork 544
feat: Offline validate-config via local plugin spec schemas #22819
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a3afb81
feat(cli): offline validate-config via local plugin spec schemas
marianogappa fd41629
chore(main): fix lint warnings in plugin_spec_schema.go
marianogappa fc4348f
docs(main): regenerate CLI reference for plugin spec-schema and --sch…
marianogappa 5a8ee21
fix(main): use single quotes in --schemas-dir flag help
marianogappa 7033325
docs(main): restore Unimplemented-response note in validateSpecAgains…
marianogappa 50b472c
refactor(main): version-suffix exported spec-schema filenames
marianogappa f89727e
fix(main): harden offline schema validation and lookup against silent…
marianogappa b8d6244
docs(main): document plugin spec-schema supports cloudquery registry …
marianogappa e5b3333
test(main): include new spec-schema page in doc-generation expected f…
marianogappa 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| cqapiauth "github.com/cloudquery/cloudquery-api-go/auth" | ||
| "github.com/cloudquery/cloudquery/cli/v6/internal/auth" | ||
| "github.com/cloudquery/cloudquery/cli/v6/internal/hub" | ||
| "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 ( | ||
| pluginSpecSchemaShort = "Export a plugin's spec JSON schema." | ||
| pluginSpecSchemaLong = `Export a plugin's spec JSON schema. | ||
|
|
||
| Without --schemas-dir the schema is printed to stdout. With --schemas-dir the | ||
| schema is written to <dir>/<plugin-name>@<version>.json, which is the | ||
| filename format expected by ` + "`cloudquery validate-config --schemas-dir`" + `. | ||
| Including the version in the filename ensures validation always runs against | ||
| the schema matching the plugin version in the config.` | ||
| pluginSpecSchemaExample = ` | ||
| # Print schema to stdout | ||
| cloudquery plugin spec-schema cloudquery/source/aws@v33.0.0 | ||
|
|
||
| # Write to ./schemas/aws@v33.0.0.json | ||
| cloudquery plugin spec-schema cloudquery/source/aws@v33.0.0 -D ./schemas` | ||
| ) | ||
|
|
||
| func newCmdPluginSpecSchema() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "spec-schema <team_name>/<plugin_kind>/<plugin_name>@<version>", | ||
| Short: pluginSpecSchemaShort, | ||
| Long: pluginSpecSchemaLong, | ||
| Example: pluginSpecSchemaExample, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: runPluginSpecSchema, | ||
| } | ||
| cmd.Flags().StringP("schemas-dir", "D", "", "Write schema to <dir>/<plugin-name>@<version>.json. If omitted, the schema is printed to stdout.") | ||
| return cmd | ||
| } | ||
|
|
||
| func runPluginSpecSchema(cmd *cobra.Command, args []string) error { | ||
| schemasDir, err := cmd.Flags().GetString("schemas-dir") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cqDir, err := cmd.Flags().GetString("cq-dir") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ref, err := hub.ParseHubPluginRef(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| pluginType, err := pluginTypeFromKind(ref.Kind) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
|
|
||
| pluginCfg := managedplugin.Config{ | ||
| Name: ref.Name, | ||
| Version: ref.Version, | ||
| Path: fmt.Sprintf("%s/%s", ref.TeamName, ref.Name), | ||
| Registry: managedplugin.RegistryCloudQuery, | ||
| } | ||
|
|
||
| // CloudQuery-registry plugins always need an auth token. | ||
| tc := cqapiauth.NewTokenClient() | ||
| authToken, err := tc.GetToken() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get auth token: %w", err) | ||
| } | ||
| teamName, err := auth.GetTeamForToken(ctx, 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 logConsole { | ||
| opts = append(opts, managedplugin.WithNoProgress()) | ||
| } | ||
| if cqDir != "" { | ||
| opts = append(opts, managedplugin.WithDirectory(cqDir)) | ||
| } | ||
| if disableSentry { | ||
| opts = append(opts, managedplugin.WithNoSentry()) | ||
| } | ||
|
|
||
| clients, err := managedplugin.NewClients(ctx, pluginType, []managedplugin.Config{pluginCfg}, opts...) | ||
| if err != nil { | ||
| return enrichClientError(clients, []bool{false}, err) | ||
| } | ||
| defer func() { | ||
| if err := clients.Terminate(); err != nil { | ||
| fmt.Println(err) | ||
| } | ||
| }() | ||
| if len(clients) == 0 { | ||
| return errors.New("plugin client not initialized") | ||
| } | ||
|
|
||
| pluginClient := plugin.NewPluginClient(clients[0].Conn) | ||
| jsonSchema, err := getSpecSchemaFromPlugin(ctx, pluginClient) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fetch spec schema: %w", err) | ||
| } | ||
| if len(jsonSchema) == 0 { | ||
| return fmt.Errorf("plugin %s did not return a spec schema", ref.String()) | ||
| } | ||
|
|
||
| return writeSchemaOutput(jsonSchema, ref.Name, ref.Version, schemasDir) | ||
| } | ||
|
|
||
| func pluginTypeFromKind(kind string) (managedplugin.PluginType, error) { | ||
| switch kind { | ||
| case "source": | ||
| return managedplugin.PluginSource, nil | ||
| case "destination": | ||
| return managedplugin.PluginDestination, nil | ||
| default: | ||
| return 0, fmt.Errorf("unsupported plugin kind %q (expected source or destination)", kind) | ||
| } | ||
| } | ||
|
|
||
| func writeSchemaOutput(jsonSchema, pluginName, pluginVersion, schemasDir string) error { | ||
| if schemasDir == "" { | ||
| _, err := fmt.Print(jsonSchema) | ||
| return err | ||
| } | ||
| if err := os.MkdirAll(schemasDir, 0o755); err != nil { | ||
| return err | ||
| } | ||
| return os.WriteFile(filepath.Join(schemasDir, schemaFileName(pluginName, pluginVersion)), []byte(jsonSchema), 0o644) | ||
| } | ||
|
|
||
| // schemaFileName returns the canonical filename for a plugin's schema under --schemas-dir. | ||
| // Version is included whenever non-empty so consumers can pin validation to the right plugin version. | ||
| func schemaFileName(pluginName, pluginVersion string) string { | ||
| if pluginVersion == "" { | ||
| return pluginName + ".json" | ||
| } | ||
| return pluginName + "@" + pluginVersion + ".json" | ||
| } |
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,49 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "testing" | ||
|
|
||
| "github.com/cloudquery/plugin-pb-go/managedplugin" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPluginTypeFromKind(t *testing.T) { | ||
| src, err := pluginTypeFromKind("source") | ||
| require.NoError(t, err) | ||
| require.Equal(t, managedplugin.PluginSource, src) | ||
|
|
||
| dst, err := pluginTypeFromKind("destination") | ||
| require.NoError(t, err) | ||
| require.Equal(t, managedplugin.PluginDestination, dst) | ||
|
|
||
| _, err = pluginTypeFromKind("transformer") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestSchemaFileName(t *testing.T) { | ||
| require.Equal(t, "aws@v33.0.0.json", schemaFileName("aws", "v33.0.0")) | ||
| require.Equal(t, "aws.json", schemaFileName("aws", "")) | ||
| } | ||
|
|
||
| func TestWriteSchemaOutput(t *testing.T) { | ||
| const schema = `{"type":"object"}` | ||
|
|
||
| t.Run("to schemas dir with versioned name", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| sub := path.Join(dir, "nested") | ||
| require.NoError(t, writeSchemaOutput(schema, "aws", "v33.0.0", sub)) | ||
| got, err := os.ReadFile(path.Join(sub, "aws@v33.0.0.json")) | ||
| require.NoError(t, err) | ||
| require.Equal(t, schema, string(got)) | ||
| }) | ||
|
|
||
| t.Run("to schemas dir without version falls back to unversioned name", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| require.NoError(t, writeSchemaOutput(schema, "aws", "", dir)) | ||
| got, err := os.ReadFile(path.Join(dir, "aws.json")) | ||
| require.NoError(t, err) | ||
| require.Equal(t, schema, string(got)) | ||
| }) | ||
| } |
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,4 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": ["object", "null"] | ||
| } |
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,8 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": ["object", "null"], | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "field": { "type": "string" } | ||
| } | ||
| } |
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,15 @@ | ||
| kind: source | ||
| spec: | ||
| name: src | ||
| path: ./nonexistent-src-binary | ||
| registry: local | ||
| destinations: [dst] | ||
| tables: ["*"] | ||
| spec: | ||
| bogus: field | ||
| --- | ||
| kind: destination | ||
| spec: | ||
| name: dst | ||
| path: ./nonexistent-dst-binary | ||
| registry: local |
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,13 @@ | ||
| kind: source | ||
| spec: | ||
| name: src | ||
| path: ./nonexistent-src-binary | ||
| registry: local | ||
| destinations: [dst] | ||
| tables: ["*"] | ||
| --- | ||
| kind: destination | ||
| spec: | ||
| name: dst | ||
| path: ./nonexistent-dst-binary | ||
| registry: local |
Oops, something went wrong.
Oops, something went wrong.
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.