2022-04-19 10:07:35 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-24 13:46:48 +00:00
|
|
|
"path/filepath"
|
2022-04-19 10:07:35 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2022-05-24 13:46:48 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
2022-04-19 10:07:35 +00:00
|
|
|
"github.com/enescakir/emoji"
|
2022-05-24 13:46:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-04-19 10:07:35 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-24 13:46:48 +00:00
|
|
|
"golang.org/x/mod/semver"
|
2022-04-19 10:07:35 +00:00
|
|
|
)
|
|
|
|
|
2022-05-24 13:46:48 +00:00
|
|
|
// pick a hub branch corresponding to the current crowdsec version.
|
|
|
|
func chooseHubBranch() (string, error) {
|
|
|
|
latest, err := cwversion.Latest()
|
|
|
|
if err != nil {
|
|
|
|
return "master", err
|
|
|
|
}
|
|
|
|
|
|
|
|
csVersion := cwversion.VersionStrip()
|
|
|
|
if csVersion == latest {
|
|
|
|
return "master", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if current version is greater than the latest we are in pre-release
|
|
|
|
if semver.Compare(csVersion, latest) == 1 {
|
|
|
|
log.Debugf("Your current crowdsec version seems to be a pre-release (%s)", csVersion)
|
|
|
|
return "master", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if csVersion == "" {
|
2022-06-22 07:38:23 +00:00
|
|
|
log.Warning("Crowdsec version is not set, using master branch for the hub")
|
2022-05-24 13:46:48 +00:00
|
|
|
return "master", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("Crowdsec is not the latest version. "+
|
|
|
|
"Current version is '%s' and the latest stable version is '%s'. Please update it!",
|
|
|
|
csVersion, latest)
|
|
|
|
log.Warnf("As a result, you will not be able to use parsers/scenarios/collections "+
|
|
|
|
"added to Crowdsec Hub after CrowdSec %s", latest)
|
|
|
|
return csVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHubBranch sets the package variable that points to the hub branch.
|
|
|
|
func SetHubBranch() error {
|
|
|
|
// a branch is already set, or specified from the flags
|
|
|
|
if HubBranch != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the branch corresponding to the crowdsec version
|
|
|
|
branch, err := chooseHubBranch()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
HubBranch = branch
|
|
|
|
log.Debugf("Using branch '%s' for the hub", HubBranch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
func InstallItem(csConfig *csconfig.Config, name string, obtype string, force bool, downloadOnly bool) error {
|
|
|
|
it := GetItem(obtype, name)
|
|
|
|
if it == nil {
|
2022-05-24 13:46:48 +00:00
|
|
|
return fmt.Errorf("unable to retrieve item: %s", name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
item := *it
|
|
|
|
if downloadOnly && item.Downloaded && item.UpToDate {
|
|
|
|
log.Warningf("%s is already downloaded and up-to-date", item.Name)
|
|
|
|
if !force {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
item, err := DownloadLatest(csConfig.Hub, item, force, true)
|
|
|
|
if err != nil {
|
2022-05-24 13:46:48 +00:00
|
|
|
return errors.Wrapf(err, "while downloading %s", item.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if err := AddItem(obtype, item); err != nil {
|
2022-05-24 13:46:48 +00:00
|
|
|
return errors.Wrapf(err, "while adding %s", item.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if downloadOnly {
|
2022-05-24 13:46:48 +00:00
|
|
|
log.Infof("Downloaded %s to %s", item.Name, filepath.Join(csConfig.Hub.HubDir, item.RemotePath))
|
2022-04-19 10:07:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
item, err = EnableItem(csConfig.Hub, item)
|
|
|
|
if err != nil {
|
2022-05-24 13:46:48 +00:00
|
|
|
return errors.Wrapf(err, "while enabling %s", item.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if err := AddItem(obtype, item); err != nil {
|
2022-05-24 13:46:48 +00:00
|
|
|
return errors.Wrapf(err, "while adding %s", item.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
log.Infof("Enabled %s", item.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:46:48 +00:00
|
|
|
// XXX this must return errors instead of log.Fatal
|
2022-04-19 10:07:35 +00:00
|
|
|
func RemoveMany(csConfig *csconfig.Config, itemType string, name string, all bool, purge bool, forceAction bool) {
|
2022-05-24 13:46:48 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
disabled int
|
|
|
|
)
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if name != "" {
|
|
|
|
it := GetItem(itemType, name)
|
|
|
|
if it == nil {
|
|
|
|
log.Fatalf("unable to retrieve: %s", name)
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
item := *it
|
|
|
|
item, err = DisableItem(csConfig.Hub, item, purge, forceAction)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to disable %s : %v", item.Name, err)
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if err := AddItem(itemType, item); err != nil {
|
|
|
|
log.Fatalf("unable to add %s: %v", item.Name, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
|
|
|
if !all {
|
|
|
|
log.Fatal("removing item: no item specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all
|
|
|
|
for _, v := range GetItemMap(itemType) {
|
2022-10-25 12:10:51 +00:00
|
|
|
if !v.Installed {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
v, err = DisableItem(csConfig.Hub, v, purge, forceAction)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to disable %s : %v", v.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AddItem(itemType, v); err != nil {
|
|
|
|
log.Fatalf("unable to add %s: %v", v.Name, err)
|
|
|
|
}
|
|
|
|
disabled++
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
log.Infof("Disabled %d items", disabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpgradeConfig(csConfig *csconfig.Config, itemType string, name string, force bool) {
|
2022-05-24 13:46:48 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
updated int
|
|
|
|
found bool
|
|
|
|
)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
|
|
|
for _, v := range GetItemMap(itemType) {
|
|
|
|
if name != "" && name != v.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.Installed {
|
|
|
|
log.Tracef("skip %s, not installed", v.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.Downloaded {
|
|
|
|
log.Warningf("%s : not downloaded, please install.", v.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if v.UpToDate {
|
|
|
|
log.Infof("%s : up-to-date", v.Name)
|
|
|
|
|
|
|
|
if err = DownloadDataIfNeeded(csConfig.Hub, v, force); err != nil {
|
|
|
|
log.Fatalf("%s : download failed : %v", v.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !force {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
v, err = DownloadLatest(csConfig.Hub, v, force, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s : download failed : %v", v.Name, err)
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if !v.UpToDate {
|
|
|
|
if v.Tainted {
|
|
|
|
log.Infof("%v %s is tainted, --force to overwrite", emoji.Warning, v.Name)
|
|
|
|
} else if v.Local {
|
|
|
|
log.Infof("%v %s is local", emoji.Prohibited, v.Name)
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-28 11:55:59 +00:00
|
|
|
// this is used while scripting to know if the hub has been upgraded
|
|
|
|
// and a configuration reload is required
|
|
|
|
fmt.Printf("updated %s\n", v.Name)
|
2022-04-19 10:07:35 +00:00
|
|
|
log.Infof("%v %s : updated", emoji.Package, v.Name)
|
|
|
|
updated++
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if err := AddItem(itemType, v); err != nil {
|
|
|
|
log.Fatalf("unable to add %s: %v", v.Name, err)
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 13:46:48 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if !found && name == "" {
|
|
|
|
log.Infof("No %s installed, nothing to upgrade", itemType)
|
|
|
|
} else if !found {
|
|
|
|
log.Errorf("Item '%s' not found in hub", name)
|
|
|
|
} else if updated == 0 && found {
|
|
|
|
if name == "" {
|
|
|
|
log.Infof("All %s are already up-to-date", itemType)
|
|
|
|
} else {
|
|
|
|
log.Infof("Item '%s' is up-to-date", name)
|
|
|
|
}
|
|
|
|
} else if updated != 0 {
|
|
|
|
log.Infof("Upgraded %d items", updated)
|
|
|
|
}
|
|
|
|
}
|