-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 add configuration for providers in dev env
This has now happened a few too many times: You get a PR with a change to a provider, but forgot to update your builtin.go file. After 30min you realize you have been testing the providers on disk, instead of the ones in the repo. Also configuring providers is a bit annoying (with all the things you need to change in `builtin.go`). This is now streamlined in `make providers/config`. You can configure it in `providers.yaml` in the root folder. Signed-off-by: Dominik Richter <[email protected]>
- Loading branch information
Showing
6 changed files
with
182 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 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,134 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"go.mondoo.com/cnquery/logger" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type ProvidersConf struct { | ||
Builtin []string `json:"builtin"` | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "configure [-f config] [-o file]", | ||
Short: "configure providers for cnquery", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
confPath, err := cmd.Flags().GetString("file") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Can't get --file") | ||
} | ||
outPath, err := cmd.Flags().GetString("output") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Can't get --output") | ||
} | ||
|
||
raw, err := os.ReadFile(confPath) | ||
if err != nil { | ||
log.Fatal().Err(err).Str("path", confPath).Msg("failed to read config file") | ||
} | ||
|
||
var conf ProvidersConf | ||
err = yaml.Unmarshal(raw, &conf) | ||
if err != nil { | ||
log.Fatal().Err(err).Str("path", confPath).Msg("failed to parse config file") | ||
} | ||
|
||
builtinGo, err := genBuiltinGo(conf) | ||
|
||
if err = os.WriteFile(outPath, []byte(builtinGo), 0o644); err != nil { | ||
log.Fatal().Err(err).Str("path", outPath).Msg("failed to write output") | ||
} | ||
log.Info().Str("path", outPath).Strs("providers", conf.Builtin).Msg("configured builtin providers") | ||
}, | ||
} | ||
|
||
func genBuiltinGo(conf ProvidersConf) (string, error) { | ||
var imports string | ||
var infos string | ||
var configs string | ||
|
||
for _, provider := range conf.Builtin { | ||
imports += fmt.Sprintf("\t%sconf \"go.mondoo.com/cnquery/providers/%s/config\"\n", provider, provider) | ||
imports += fmt.Sprintf("\t%s \"go.mondoo.com/cnquery/providers/%s/provider\"\n", provider, provider) | ||
infos += fmt.Sprintf( | ||
"//go:embed %s/resources/%s.resources.json\n"+ | ||
"var %sInfo []byte\n", | ||
provider, provider, provider) | ||
configs += fmt.Sprintf(` | ||
%sconf.Config.ID: { | ||
Runtime: &RunningProvider{ | ||
Name: %sconf.Config.Name, | ||
ID: %sconf.Config.ID, | ||
Plugin: %s.Init(), | ||
Schema: MustLoadSchema("%s", %sInfo), | ||
isClosed: false, | ||
}, | ||
Config: &%sconf.Config, | ||
}, | ||
`, provider, provider, provider, provider, provider, provider, provider) | ||
} | ||
|
||
return fmt.Sprintf(template, imports, infos, configs), nil | ||
} | ||
|
||
const template = `// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// This file is auto-generated by 'make providers/config' | ||
package providers | ||
// Uncomment any provider you want to load directly into the binary. | ||
// This is primarily useful for debugging purposes, if you want to | ||
// trace into any provider without having to debug the plugin | ||
// connection separately. | ||
import ( | ||
_ "embed" | ||
coreconf "go.mondoo.com/cnquery/providers/core/config" | ||
core "go.mondoo.com/cnquery/providers/core/provider" | ||
%s) | ||
//go:embed core/resources/core.resources.json | ||
var coreInfo []byte | ||
%s | ||
var builtinProviders = map[string]*builtinProvider{ | ||
coreconf.Config.ID: { | ||
Runtime: &RunningProvider{ | ||
Name: coreconf.Config.Name, | ||
ID: coreconf.Config.ID, | ||
Plugin: core.Init(), | ||
Schema: MustLoadSchema("core", coreInfo), | ||
isClosed: false, | ||
}, | ||
Config: &coreconf.Config, | ||
}, | ||
%s | ||
} | ||
` | ||
|
||
func init() { | ||
rootCmd.Flags().StringP("file", "f", "providers.yaml", "config file for providers") | ||
rootCmd.Flags().StringP("output", "o", "providers/builtin.go", "output builtin.go file") | ||
} | ||
|
||
func main() { | ||
logger.CliCompactLogger(logger.LogOutputWriter) | ||
zerolog.SetGlobalLevel(zerolog.InfoLevel) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains 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 @@ | ||
# Copyright (c) Mondoo, Inc. | ||
# SPDX-License-Identifier: BUSL-1.1 | ||
|
||
# Configure builtin providers for the dev environment. | ||
# This only covers additional builtin providers, ie those | ||
# that are normally not builtin. Things like `core` are | ||
# always builtin. | ||
# | ||
# Builtin providers are great testing. Example: if you | ||
# review a PR where someone changed the `os` provider, | ||
# you'll want to set it as builtin for fast testing. | ||
# In this case it will be pulled from your local repo | ||
# instead of using the pre-installed provider in | ||
# your OS (like ~/.mondoo/providers/os). | ||
builtin: [] |
This file contains 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 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 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