fleet/server/vulnerabilities/oval/downloader.go
guangwu 33858d7301
chore: remove refs to deprecated io/ioutil (#14485)
# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- [ ] Documented any permissions changes (docs/Using
Fleet/manage-access.md)
- [ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.
- [ ] Added/updated tests
- [ ] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [ ] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
2023-10-27 15:28:54 -03:00

62 lines
1.4 KiB
Go

package oval
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"time"
)
const (
ovalSourcesFileName = "oval_sources.json"
)
// OvalSources represents a platform => web url dictionary
type OvalSources map[Platform]string
// getOvalSources gets the 'oval sources' file.
// The 'oval sources' is a metadata file hosted in the NVD repo, it contains
// where to find the OVAL definitions for a given platform.
func getOvalSources(getter func(string) (io.ReadCloser, error)) (OvalSources, error) {
src, err := getter(ovalSourcesFileName)
if err != nil {
return nil, err
}
defer src.Close()
contents, err := io.ReadAll(src)
if err != nil {
return nil, err
}
sources := make(OvalSources)
err = json.Unmarshal(contents, &sources)
if err != nil {
return nil, err
}
return sources, nil
}
// downloadDefinitions downloads the OVAL definitions for a given 'platform-major os version'.
// Returns the filepath to the downloaded oval definitions.
func downloadDefinitions(
sources OvalSources,
platform Platform,
downloader func(string, string) error,
) (string, error) {
url, ok := sources[platform]
if !ok {
return "", fmt.Errorf("could not find platform %s on oval sources", platform)
}
dstPath := filepath.Join(os.TempDir(), platform.ToFilename(time.Now(), "xml"))
err := downloader(url, dstPath)
if err != nil {
return "", err
}
return dstPath, nil
}