Skip to content

Commit 3abb3ca

Browse files
authored
⭐️ add provider dependencies as builtin (#5503)
This change uses the new schema changes added at #5495 which exposes dependencies that need to be consider in two scenarios: 1. Developing providers 2. Air-gapped environments Now, when configuring a builtin provider, we will look at its dependencies and add them to the providers configuration and build them too. Remote dependencies are not yet supported, we need to revisit this change once we support them. Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
1 parent 6e38146 commit 3abb3ca

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

providers-sdk/v1/util/configure/configure.go

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"go/format"
11+
"maps"
1112
"os"
1213
"os/exec"
1314
"regexp"
@@ -19,6 +20,8 @@ import (
1920
"github.com/rs/zerolog/log"
2021
"github.com/spf13/cobra"
2122
"go.mondoo.com/cnquery/v11/logger"
23+
cproviders "go.mondoo.com/cnquery/v11/providers"
24+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/resources"
2225
"golang.org/x/mod/modfile"
2326
"sigs.k8s.io/yaml"
2427
)
@@ -65,6 +68,13 @@ var rootCmd = &cobra.Command{
6568
log.Fatal().Err(err).Str("path", confPath).Msg("failed to parse config file")
6669
}
6770

71+
deps := buildProviders(conf.Builtin)
72+
buildDependencies(deps)
73+
for _, dep := range deps {
74+
conf.AddProvider(dep)
75+
}
76+
log.Info().Strs("providers", conf.Providers()).Msg("(1/3) built providers")
77+
6878
builtinGo, err := genBuiltinGo(conf)
6979
if err != nil {
7080
log.Fatal().Err(err).Str("path", confPath).Msg("failed to generate builtin go")
@@ -73,10 +83,7 @@ var rootCmd = &cobra.Command{
7383
if err = os.WriteFile(outPath, builtinGo, 0o644); err != nil {
7484
log.Fatal().Err(err).Str("path", outPath).Msg("failed to write output")
7585
}
76-
log.Info().Str("path", outPath).Strs("providers", conf.Providers()).Msg("(1/3) configured builtin providers")
77-
78-
buildProviders(conf.Builtin)
79-
log.Info().Strs("providers", conf.Providers()).Msg("(2/3) built providers")
86+
log.Info().Str("path", outPath).Strs("providers", conf.Providers()).Msg("(2/3) configured builtin providers")
8087

8188
rewireDependencies(conf.Builtin)
8289
log.Info().Str("path", outPath).Strs("providers", conf.Providers()).Msg("(3/3) rewired dependencies/files")
@@ -208,7 +215,8 @@ func init() {
208215
}
209216
`
210217

211-
func buildProviders(providers []Builtin) {
218+
func buildProviders(providers []Builtin) map[string]*resources.ProviderInfo {
219+
deps := map[string]*resources.ProviderInfo{}
212220
for i, provider := range providers {
213221
cmd := exec.Command("make", "providers/build/"+provider.Name)
214222
if provider.Remote != "" {
@@ -219,7 +227,9 @@ func buildProviders(providers []Builtin) {
219227
var out bytes.Buffer
220228
cmd.Stdout = &out
221229
cmd.Stderr = &out
222-
log.Debug().Str("provider", provider.Name).Msg("build provider " + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(providers)))
230+
log.Debug().
231+
Str("provider", provider.Name).
232+
Msg("build provider " + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(providers)))
223233
if err := cmd.Run(); err != nil {
224234
fmt.Println(out.String())
225235
log.Error().Err(err).Str("provider", provider.Name).Msg("failed to build provider")
@@ -237,6 +247,46 @@ func buildProviders(providers []Builtin) {
237247
if err != nil {
238248
log.Fatal().Err(err).Str("dst", dst).Msg("failed to write resources json")
239249
}
250+
251+
// check for dependencies
252+
schema := cproviders.MustLoadSchema(provider.Name, raw)
253+
maps.Copy(deps, schema.Dependencies)
254+
}
255+
return deps
256+
}
257+
258+
func buildDependencies(deps map[string]*resources.ProviderInfo) {
259+
i := 0
260+
for name, provider := range deps {
261+
cmd := exec.Command("make", "providers/build/"+name)
262+
var out bytes.Buffer
263+
cmd.Stdout = &out
264+
cmd.Stderr = &out
265+
log.Debug().
266+
Str("dependency", provider.Id).
267+
Msg("build dependency " + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(deps)))
268+
if err := cmd.Run(); err != nil {
269+
fmt.Println(out.String())
270+
log.Error().Err(err).
271+
Str("dependency", name).
272+
Msg("failed to build provider dependency")
273+
}
274+
275+
// Remote dependencies are not yet supported, we need to
276+
// modify this once we support them.
277+
// src := provider.Resource()
278+
src := "providers/" + name + "/resources/" + name + ".resources.json"
279+
raw, err := os.ReadFile(src)
280+
if err != nil {
281+
log.Fatal().Err(err).Str("src", src).Msg("failed to read resources json")
282+
}
283+
284+
// dst := provider.Dist()
285+
dst := "providers/" + name + ".resources.json"
286+
err = os.WriteFile(dst, raw, 0o644)
287+
if err != nil {
288+
log.Fatal().Err(err).Str("dst", dst).Msg("failed to write resources json")
289+
}
240290
}
241291
}
242292

providers-sdk/v1/util/configure/provider_conf.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"encoding/json"
77
"fmt"
88
"os"
9+
"slices"
910
"strings"
11+
12+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/resources"
1013
)
1114

1215
type ProvidersConf struct {
@@ -15,7 +18,26 @@ type ProvidersConf struct {
1518
providers []string // Providers names
1619
}
1720

18-
func (c ProvidersConf) Providers() []string {
21+
// AddProvider registers a new builtin provider into the providers configuration
22+
func (c *ProvidersConf) AddProvider(provider *resources.ProviderInfo) {
23+
if slices.Contains(c.Providers(), provider.Name) {
24+
return // provider already exist
25+
}
26+
27+
// add provider
28+
c.Builtin = append(c.Builtin, Builtin{
29+
GoPackage: provider.Id,
30+
Name: provider.Name,
31+
// Remote dependencies are not yet supported, we need to
32+
// modify this once we support them.
33+
// Remote: provider.Remote,
34+
})
35+
36+
// invalidate the internal list of providers since it changed
37+
c.providers = nil
38+
}
39+
40+
func (c *ProvidersConf) Providers() []string {
1941
if len(c.providers) == 0 {
2042
for _, b := range c.Builtin {
2143
c.providers = append(c.providers, b.Name)

0 commit comments

Comments
 (0)