crowdsec/pkg/cwhub/dataset.go

87 lines
1.8 KiB
Go
Raw Normal View History

package cwhub
2020-05-27 14:31:08 +00:00
import (
"errors"
2020-05-27 14:31:08 +00:00
"fmt"
"io"
2020-05-27 14:31:08 +00:00
"net/http"
"os"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
2020-05-27 14:31:08 +00:00
"github.com/crowdsecurity/crowdsec/pkg/types"
)
2020-05-27 14:31:08 +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 {
Data []types.DataSource `yaml:"data,omitempty"`
2020-05-27 14:31:08 +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)
resp, err := hubClient.Get(url)
2020-05-27 14:31:08 +00:00
if err != nil {
return fmt.Errorf("while downloading %s: %w", url, err)
2020-05-27 14:31:08 +00:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad http code %d for %s", resp.StatusCode, url)
2020-05-27 14:31:08 +00:00
}
file, err := os.Create(destPath)
2020-05-27 14:31:08 +00:00
if err != nil {
return err
}
defer file.Close()
2020-05-27 14:31:08 +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
}
if err = file.Sync(); err != nil {
2020-05-27 14:31:08 +00:00
return err
}
return nil
}
// 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)
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
}