mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #40054 # Checklist for submitter - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Included in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Migrated logging infrastructure from external framework to standard library structured logging, enabling improved context-aware operations and error tracking across vulnerability detection and synchronization workflows. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
91 lines
2 KiB
Go
91 lines
2 KiB
Go
package goval_dictionary
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"path/filepath"
|
|
|
|
"github.com/fleetdm/fleet/v4/pkg/download"
|
|
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/vulnerabilities/nvd"
|
|
"github.com/fleetdm/fleet/v4/server/vulnerabilities/oval"
|
|
)
|
|
|
|
func Refresh(
|
|
ctx context.Context,
|
|
versions *fleet.OSVersions,
|
|
vulnPath string,
|
|
logger *slog.Logger,
|
|
) ([]oval.Platform, error) {
|
|
toDownload := whatToDownload(versions)
|
|
if len(toDownload) > 0 {
|
|
logger.DebugContext(ctx, "goval_dictionary-sync-downloading")
|
|
err := Sync(vulnPath, toDownload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return toDownload, nil
|
|
}
|
|
|
|
func Sync(dstDir string, platforms []oval.Platform) error {
|
|
client := fleethttp.NewClient()
|
|
dwn := downloadDecompressed(client)
|
|
basePath, err := nvd.GetGitHubCVEAssetPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, platform := range platforms {
|
|
if err := downloadDatabase(platform, dwn, basePath, dstDir); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func downloadDatabase(
|
|
platform oval.Platform,
|
|
downloader func(string, string) error,
|
|
basePath string,
|
|
vulnDir string,
|
|
) error {
|
|
dstPath := filepath.Join(vulnDir, platform.ToGovalDictionaryFilename())
|
|
if err := downloader(basePath+string(platform)+".sqlite3.xz", dstPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func downloadDecompressed(client *http.Client) func(string, string) error {
|
|
return func(u, dstPath string) error {
|
|
parsedUrl, err := url.Parse(u)
|
|
if err != nil {
|
|
return fmt.Errorf("url parse: %w", err)
|
|
}
|
|
|
|
if err = download.DownloadAndExtract(client, parsedUrl, dstPath); err != nil {
|
|
return fmt.Errorf("download and extract url %s: %w", parsedUrl, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func whatToDownload(osVers *fleet.OSVersions) []oval.Platform {
|
|
var r []oval.Platform
|
|
for _, os := range osVers.OSVersions {
|
|
platform := oval.NewPlatform(os.Platform, os.Name)
|
|
if platform.IsGovalDictionarySupported() {
|
|
r = append(r, platform)
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|