2023-06-08 14:49:51 +00:00
|
|
|
package cwhub
|
2020-05-27 14:31:08 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-24 14:57:32 +00:00
|
|
|
"errors"
|
2020-05-27 14:31:08 +00:00
|
|
|
"fmt"
|
2022-09-06 11:55:03 +00:00
|
|
|
"io"
|
2020-05-27 14:31:08 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-24 14:57:32 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-05-27 14:31:08 +00:00
|
|
|
|
2023-06-08 14:49:51 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
)
|
2020-05-27 14:31:08 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// The DataSet is a list of data sources required by an item (built from the data: section in the yaml).
|
2020-05-27 14:31:08 +00:00
|
|
|
type DataSet struct {
|
2023-11-24 14:57:32 +00:00
|
|
|
Data []types.DataSource `yaml:"data,omitempty"`
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// downloadFile downloads a file and writes it to disk, with no hash verification.
|
2020-05-27 14:31:08 +00:00
|
|
|
func downloadFile(url string, destPath string) error {
|
|
|
|
log.Debugf("downloading %s in %s", url, destPath)
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
resp, err := hubClient.Get(url)
|
2020-05-27 14:31:08 +00:00
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return fmt.Errorf("while downloading %s: %w", url, err)
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-09-06 11:55:03 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-11-24 14:57:32 +00:00
|
|
|
return fmt.Errorf("bad http code %d for %s", resp.StatusCode, url)
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
file, err := os.Create(destPath)
|
2020-05-27 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
defer file.Close()
|
2020-05-27 14:31:08 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// avoid reading the whole file in memory
|
|
|
|
_, err = io.Copy(file, resp.Body)
|
2020-05-27 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if err = file.Sync(); err != nil {
|
2020-05-27 14:31:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// downloadDataSet downloads all the data files for an item.
|
|
|
|
func downloadDataSet(dataFolder string, force bool, reader io.Reader) error {
|
|
|
|
dec := yaml.NewDecoder(reader)
|
|
|
|
|
|
|
|
for {
|
|
|
|
data := &DataSet{}
|
|
|
|
|
|
|
|
if err := dec.Decode(data); err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("while reading file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dataS := range data.Data {
|
|
|
|
destPath, err := safePath(dataFolder, dataS.DestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(destPath); os.IsNotExist(err) || force {
|
|
|
|
log.Infof("downloading data '%s' in '%s'", dataS.SourceURL, destPath)
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if err := downloadFile(dataS.SourceURL, destPath); err != nil {
|
|
|
|
return fmt.Errorf("while getting data: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|