Skip to content

Commit

Permalink
🐛 update provider defaults + missing connection types + v8 compat
Browse files Browse the repository at this point in the history
Connection types were missing and thus caused any request that would
look for them to fail. In most cases this is not noticable at all
(because you eg connect to github to continue to do things with github
as you discover more stuff).

But there is a very nasty case where this hurt: When we are in serve
mode and don't have a primary connection that triggers provider
installations. In this case we receive asset types (or rather v8 Backend
fields!), which then didn't find any way to look up their provider.

So:
1. Add a script to help with defaults generation (it does 80% of the
   work, you only have to copy it over):
   ```bash
   version defaults providers/*/
   ```
2. Update all providers with it
3. Support v8 `Backend` field (needs in-depth testing since both fields
   are not fully compatible; add translations where necessary)

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored and imilchev committed Oct 2, 2023
1 parent ec223c9 commit 20acc75
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 102 deletions.
57 changes: 56 additions & 1 deletion providers-sdk/v1/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"go/format"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/spf13/cobra"
"go.mondoo.com/cnquery/cli/components"
"go.mondoo.com/cnquery/logger"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"golang.org/x/mod/modfile"
)

Expand Down Expand Up @@ -192,7 +194,7 @@ func checkGoModUpdate(providerPath string, updateStrategy UpdateStrategy) {
return
}

err = os.WriteFile(goModPath, updatedModContent, 0644)
err = os.WriteFile(goModPath, updatedModContent, 0o644)
if err != nil {
log.Info().Msgf("Error writing updated go.mod file: %v", err)
return
Expand Down Expand Up @@ -226,6 +228,16 @@ func goModTidy(providerPath string) {
}
}

var defaultsCmd = &cobra.Command{
Use: "defaults [PROVIDERS]",
Short: "generates the content for the defaults list of providers",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
defaults := parseDefaults(args)
fmt.Println(defaults)
},
}

func checkUpdate(providerPath string) {
conf, err := getConfig(providerPath)
if err != nil {
Expand Down Expand Up @@ -570,6 +582,48 @@ func countChangesSince(conf *providerConf, repoPath string) int {
return count
}

func parseDefaults(paths []string) string {
confs := []*plugin.Provider{}
for _, path := range paths {
name := filepath.Base(path)
data, err := os.ReadFile(filepath.Join(path, "dist", name+".json"))
if err != nil {
log.Fatal().Err(err).Msg("failed to read config json")
}
var v plugin.Provider
if err = json.Unmarshal(data, &v); err != nil {
log.Fatal().Err(err).Msg("failed to parse config json")
}
confs = append(confs, &v)
}

var res strings.Builder
for i := range confs {
conf := confs[i]
var connectors strings.Builder
for j := range conf.Connectors {
conn := conf.Connectors[j]
connectors.WriteString(fmt.Sprintf(`
{
Name: %#v,
Short: %#v,
},`, conn.Name, conn.Short))
}

res.WriteString(fmt.Sprintf(`
"%s": {
Provider: &plugin.Provider{
Name: "%s",
ConnectionTypes: %#v,
Connectors: []plugin.Connector{%s
},
},
},`, conf.Name, conf.Name, conf.ConnectionTypes, connectors.String()))
}

return res.String()
}

var (
fastMode bool
doCommit bool
Expand All @@ -586,6 +640,7 @@ func init() {
modUpdateCmd.PersistentFlags().BoolVar(&latestVersion, "latest", false, "update versions to latest")
modUpdateCmd.PersistentFlags().BoolVar(&latestPatchVersion, "patch", false, "update versions to latest patch")
rootCmd.AddCommand(updateCmd, checkCmd, modUpdateCmd, modTidyCmd)
rootCmd.AddCommand(updateCmd, checkCmd, defaultsCmd)
}

func main() {
Expand Down
Loading

0 comments on commit 20acc75

Please sign in to comment.