Skip to content

Commit

Permalink
🧹 Update providers when running in serve mode (#938)
Browse files Browse the repository at this point in the history
* 🧹 Update providers when running in serve mode

Fixes mondoohq/cnquery#2461

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Nov 14, 2023
1 parent 3182384 commit dbad1b1
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion apps/cnspec/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ var serveCmd = &cobra.Command{
checkin.Start()
defer checkin.Stop()
}

}

bj, err := backgroundjob.New()
Expand All @@ -96,6 +95,11 @@ var serveCmd = &cobra.Command{
}

bj.Run(func() error {
// Try to update the os provider before each scan
err = updateProviders()
if err != nil {
log.Error().Err(err).Msg("could not update providers")
}
// TODO: check in every 5 min via timer, init time in Background job
result, err := RunScan(conf, scan.DisableProgressBar())
if err != nil {
Expand Down Expand Up @@ -208,3 +212,35 @@ func logClientInfo(spaceMrn string, clientMrn string, serviceAccountMrn string)
}
log.Info().Str("version", version).Str("space", spaceMrn).Str("service_account", serviceAccountMrn).Str("client", clientMrn).Msg("start cnspec")
}

func updateProviders() error {
log.Debug().Msg("checking for provider updates")
// force re-load from disk, in case it got updated outside the serve mode
providers.CachedProviders = nil
allProviders, err := providers.ListActive()
if err != nil {
return err
}
updatedProviders := []*providers.Provider{}
for _, provider := range allProviders {
if provider.Name == "mock" || provider.Name == "core" {
continue
}
latestVersion, err := providers.LatestVersion(provider.Name)
if err != nil {
return err
}
if latestVersion != provider.Version {
installed, err := providers.Install(provider.Name, "")
if err != nil {
return err
}
updatedProviders = append(updatedProviders, installed)
} else {
log.Debug().Str("provider", provider.Name).Str("version", provider.Version).Msg("provider is already up to date")
}

}
providers.PrintInstallResults(updatedProviders)
return nil
}

0 comments on commit dbad1b1

Please sign in to comment.