Skip to content

Commit

Permalink
πŸ› update provider defaults + missing connection types + v8 compat (#2021
Browse files Browse the repository at this point in the history
)

* πŸ› update provider defaults + missing connection types + v8 compat

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]>

* adjust code to work with provider types

Signed-off-by: Ivan Milchev <[email protected]>

---------

Signed-off-by: Dominik Richter <[email protected]>
Signed-off-by: Ivan Milchev <[email protected]>
Co-authored-by: Ivan Milchev <[email protected]>
  • Loading branch information
arlimus and imilchev authored Oct 2, 2023
1 parent ec223c9 commit 82a0179
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 104 deletions.
4 changes: 3 additions & 1 deletion providers-sdk/v1/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"github.com/segmentio/ksuid"
"go.mondoo.com/cnquery/providers-sdk/v1/vault"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -56,7 +57,8 @@ func InventoryFromYAML(data []byte) (*Inventory, error) {
for _, asset := range res.Spec.Assets {
for _, conn := range asset.Connections {
if conn.Type == "" {
conn.Type = connBackendToType(conn.Backend)
log.Warn().Msg("no connection `type` provided in inventory, falling back to deprecated `backend` field")
conn.Type = ConnBackendToType(conn.Backend)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion providers-sdk/v1/inventory/v8_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *ProviderType) UnmarshalJSON(data []byte) error {
return nil
}

func connBackendToType(backend ProviderType) string {
func ConnBackendToType(backend ProviderType) string {
switch backend {
case ProviderType_LOCAL_OS:
return "os"
Expand Down
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 82a0179

Please sign in to comment.