fix & improve cscli remove action + improve cscli args vars (#498)

This commit is contained in:
erenJag 2020-12-02 18:47:17 +01:00 committed by GitHub
parent 2e76097d35
commit 9d016f262f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 80 additions and 72 deletions

View file

@ -47,12 +47,12 @@ func NewCollectionsCmd() *cobra.Command {
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
for _, name := range args {
InstallItem(name, cwhub.COLLECTIONS, forceInstall)
InstallItem(name, cwhub.COLLECTIONS, forceAction)
}
},
}
cmdCollectionsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdCollectionsInstall.PersistentFlags().BoolVar(&forceInstall, "force", false, "Force install : Overwrite tainted and outdated files")
cmdCollectionsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdCollections.AddCommand(cmdCollectionsInstall)
var cmdCollectionsRemove = &cobra.Command{
@ -67,17 +67,26 @@ func NewCollectionsCmd() *cobra.Command {
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if removeAll {
if all {
RemoveMany(cwhub.COLLECTIONS, "")
} else {
for _, name := range args {
if !forceAction {
item := cwhub.GetItem(cwhub.COLLECTIONS, name)
if len(item.BelongsToCollections) > 0 {
log.Warningf("%s belongs to other collections :\n%s\n", name, item.BelongsToCollections)
log.Printf("Run 'sudo cscli collections remove %s --force' if you want to force remove this sub collection\n", name)
continue
}
}
RemoveMany(cwhub.COLLECTIONS, name)
}
}
},
}
cmdCollectionsRemove.PersistentFlags().BoolVar(&purgeRemove, "purge", false, "Delete source file too")
cmdCollectionsRemove.PersistentFlags().BoolVar(&removeAll, "all", false, "Delete all the files in selected scope")
cmdCollectionsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
cmdCollectionsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
cmdCollectionsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the collections")
cmdCollections.AddCommand(cmdCollectionsRemove)
var cmdCollectionsUpgrade = &cobra.Command{
@ -91,17 +100,17 @@ func NewCollectionsCmd() *cobra.Command {
log.Fatalf("Failed to get Hub index : %v", err)
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if upgradeAll {
UpgradeConfig(cwhub.COLLECTIONS, "", forceUpgrade)
if all {
UpgradeConfig(cwhub.COLLECTIONS, "", forceAction)
} else {
for _, name := range args {
UpgradeConfig(cwhub.COLLECTIONS, name, forceUpgrade)
UpgradeConfig(cwhub.COLLECTIONS, name, forceAction)
}
}
},
}
cmdCollectionsUpgrade.PersistentFlags().BoolVarP(&upgradeAll, "download-only", "d", false, "Only download packages, don't enable")
cmdCollectionsUpgrade.PersistentFlags().BoolVar(&forceUpgrade, "force", false, "Force install : Overwrite tainted and outdated files")
cmdCollectionsUpgrade.PersistentFlags().BoolVarP(&all, "download-only", "d", false, "Only download packages, don't enable")
cmdCollectionsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
cmdCollections.AddCommand(cmdCollectionsUpgrade)
var cmdCollectionsInspect = &cobra.Command{
@ -137,7 +146,7 @@ func NewCollectionsCmd() *cobra.Command {
ListItem(cwhub.COLLECTIONS, args)
},
}
cmdCollectionsList.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
cmdCollectionsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
cmdCollections.AddCommand(cmdCollectionsList)
return cmdCollections

View file

@ -56,7 +56,7 @@ cscli hub update # Download list of available configurations from the hub
ListItem(cwhub.PARSERS_OVFLW, args)
},
}
cmdHub.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
cmdHub.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
cmdHub.AddCommand(cmdHubList)
var cmdHubUpdate = &cobra.Command{

View file

@ -20,12 +20,9 @@ var dbClient *database.Client
var OutputFormat string
var downloadOnly bool
var forceInstall bool
var forceUpgrade bool
var removeAll bool
var purgeRemove bool
var upgradeAll bool
var listAll bool
var forceAction bool
var purge bool
var all bool
var restoreOldBackup bool
var prometheusURL string

View file

@ -51,12 +51,12 @@ cscli parsers remove crowdsecurity/sshd-logs
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
for _, name := range args {
InstallItem(name, cwhub.PARSERS, forceInstall)
InstallItem(name, cwhub.PARSERS, forceAction)
}
},
}
cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdParsersInstall.PersistentFlags().BoolVar(&forceInstall, "force", false, "Force install : Overwrite tainted and outdated files")
cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdParsers.AddCommand(cmdParsersInstall)
var cmdParsersRemove = &cobra.Command{
@ -71,7 +71,7 @@ cscli parsers remove crowdsecurity/sshd-logs
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if removeAll {
if all {
RemoveMany(cwhub.PARSERS, "")
} else {
for _, name := range args {
@ -80,8 +80,9 @@ cscli parsers remove crowdsecurity/sshd-logs
}
},
}
cmdParsersRemove.PersistentFlags().BoolVar(&purgeRemove, "purge", false, "Delete source file too")
cmdParsersRemove.PersistentFlags().BoolVar(&removeAll, "all", false, "Delete all the parsers")
cmdParsersRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
cmdParsersRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
cmdParsersRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the parsers")
cmdParsers.AddCommand(cmdParsersRemove)
var cmdParsersUpgrade = &cobra.Command{
@ -95,17 +96,17 @@ cscli parsers remove crowdsecurity/sshd-logs
log.Fatalf("Failed to get Hub index : %v", err)
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if upgradeAll {
UpgradeConfig(cwhub.PARSERS, "", forceUpgrade)
if all {
UpgradeConfig(cwhub.PARSERS, "", forceAction)
} else {
for _, name := range args {
UpgradeConfig(cwhub.PARSERS, name, forceUpgrade)
UpgradeConfig(cwhub.PARSERS, name, forceAction)
}
}
},
}
cmdParsersUpgrade.PersistentFlags().BoolVar(&upgradeAll, "all", false, "Upgrade all the parsers")
cmdParsersUpgrade.PersistentFlags().BoolVar(&forceUpgrade, "force", false, "Force install : Overwrite tainted and outdated files")
cmdParsersUpgrade.PersistentFlags().BoolVar(&all, "all", false, "Upgrade all the parsers")
cmdParsersUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
cmdParsers.AddCommand(cmdParsersUpgrade)
var cmdParsersInspect = &cobra.Command{
@ -139,7 +140,7 @@ cscli parser list crowdsecurity/xxx`,
ListItem(cwhub.PARSERS, args)
},
}
cmdParsersList.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
cmdParsersList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
cmdParsers.AddCommand(cmdParsersList)
return cmdParsers

View file

@ -50,12 +50,12 @@ func NewPostOverflowsCmd() *cobra.Command {
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
for _, name := range args {
InstallItem(name, cwhub.PARSERS_OVFLW, forceInstall)
InstallItem(name, cwhub.PARSERS_OVFLW, forceAction)
}
},
}
cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceInstall, "force", false, "Force install : Overwrite tainted and outdated files")
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
var cmdPostOverflowsRemove = &cobra.Command{
@ -70,7 +70,7 @@ func NewPostOverflowsCmd() *cobra.Command {
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if removeAll {
if all {
RemoveMany(cwhub.PARSERS_OVFLW, "")
} else {
for _, name := range args {
@ -79,8 +79,9 @@ func NewPostOverflowsCmd() *cobra.Command {
}
},
}
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purgeRemove, "purge", false, "Delete source file in ~/.cscli/hub/ too")
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&removeAll, "all", false, "Delete all the files in selected scope")
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
var cmdPostOverflowsUpgrade = &cobra.Command{
@ -94,17 +95,17 @@ func NewPostOverflowsCmd() *cobra.Command {
log.Fatalf("Failed to get Hub index : %v", err)
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if upgradeAll {
UpgradeConfig(cwhub.PARSERS_OVFLW, "", forceUpgrade)
if all {
UpgradeConfig(cwhub.PARSERS_OVFLW, "", forceAction)
} else {
for _, name := range args {
UpgradeConfig(cwhub.PARSERS_OVFLW, name, forceUpgrade)
UpgradeConfig(cwhub.PARSERS_OVFLW, name, forceAction)
}
}
},
}
cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&upgradeAll, "download-only", "d", false, "Only download packages, don't enable")
cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceUpgrade, "force", false, "Force install : Overwrite tainted and outdated files")
cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "download-only", "d", false, "Only download packages, don't enable")
cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
var cmdPostOverflowsInspect = &cobra.Command{
@ -137,7 +138,7 @@ cscli postoverflows list crowdsecurity/xxx`,
ListItem(cwhub.PARSERS_OVFLW, args)
},
}
cmdPostOverflowsList.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
cmdPostOverflows.AddCommand(cmdPostOverflowsList)
return cmdPostOverflows

View file

@ -51,12 +51,12 @@ cscli scenarios remove crowdsecurity/ssh-bf
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
for _, name := range args {
InstallItem(name, cwhub.SCENARIOS, forceInstall)
InstallItem(name, cwhub.SCENARIOS, forceAction)
}
},
}
cmdScenariosInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdScenariosInstall.PersistentFlags().BoolVar(&forceInstall, "force", false, "Force install : Overwrite tainted and outdated files")
cmdScenariosInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdScenarios.AddCommand(cmdScenariosInstall)
var cmdScenariosRemove = &cobra.Command{
@ -71,7 +71,7 @@ cscli scenarios remove crowdsecurity/ssh-bf
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if removeAll {
if all {
RemoveMany(cwhub.SCENARIOS, "")
} else {
for _, name := range args {
@ -80,8 +80,9 @@ cscli scenarios remove crowdsecurity/ssh-bf
}
},
}
cmdScenariosRemove.PersistentFlags().BoolVar(&purgeRemove, "purge", false, "Delete source file in ~/.cscli/hub/ too")
cmdScenariosRemove.PersistentFlags().BoolVar(&removeAll, "all", false, "Delete all the files in selected scope")
cmdScenariosRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
cmdScenariosRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
cmdScenariosRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the scenarios")
cmdScenarios.AddCommand(cmdScenariosRemove)
var cmdScenariosUpgrade = &cobra.Command{
@ -95,17 +96,17 @@ cscli scenarios remove crowdsecurity/ssh-bf
log.Fatalf("Failed to get Hub index : %v", err)
log.Infoln("Run 'sudo cscli hub update' to get the hub index")
}
if upgradeAll {
UpgradeConfig(cwhub.SCENARIOS, "", forceUpgrade)
if all {
UpgradeConfig(cwhub.SCENARIOS, "", forceAction)
} else {
for _, name := range args {
UpgradeConfig(cwhub.SCENARIOS, name, forceUpgrade)
UpgradeConfig(cwhub.SCENARIOS, name, forceAction)
}
}
},
}
cmdScenariosUpgrade.PersistentFlags().BoolVarP(&upgradeAll, "download-only", "d", false, "Only download packages, don't enable")
cmdScenariosUpgrade.PersistentFlags().BoolVar(&forceUpgrade, "force", false, "Force install : Overwrite tainted and outdated files")
cmdScenariosUpgrade.PersistentFlags().BoolVarP(&all, "download-only", "d", false, "Only download packages, don't enable")
cmdScenariosUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
cmdScenarios.AddCommand(cmdScenariosUpgrade)
var cmdScenariosInspect = &cobra.Command{
@ -139,7 +140,7 @@ cscli scenarios list crowdsecurity/xxx`,
ListItem(cwhub.SCENARIOS, args)
},
}
cmdScenariosList.PersistentFlags().BoolVarP(&listAll, "all", "a", false, "List as well disabled items")
cmdScenariosList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
cmdScenarios.AddCommand(cmdScenariosList)
return cmdScenarios

View file

@ -99,9 +99,9 @@ func ListItem(itemType string, args []string) {
var hubStatus []map[string]string
if len(args) == 1 {
hubStatus = cwhub.HubStatus(itemType, args[0], listAll)
hubStatus = cwhub.HubStatus(itemType, args[0], all)
} else {
hubStatus = cwhub.HubStatus(itemType, "", listAll)
hubStatus = cwhub.HubStatus(itemType, "", all)
}
if csConfig.Cscli.Output == "human" {
@ -142,7 +142,7 @@ func InstallItem(name string, obtype string, force bool) {
return
}
}
item, err := cwhub.DownloadLatest(csConfig.Cscli, item, forceInstall)
item, err := cwhub.DownloadLatest(csConfig.Cscli, item, force)
if err != nil {
log.Fatalf("error while downloading %s : %v", item.Name, err)
}
@ -169,15 +169,15 @@ func RemoveMany(itemType string, name string) {
log.Fatalf("unable to retrieve: %s", name)
}
item := *it
item, err = cwhub.DisableItem(csConfig.Cscli, item, purgeRemove)
item, err = cwhub.DisableItem(csConfig.Cscli, item, purge, forceAction)
if err != nil {
log.Fatalf("unable to disable %s : %v", item.Name, err)
}
cwhub.AddItem(itemType, item)
return
} else if name == "" && removeAll {
} else if name == "" && all {
for _, v := range cwhub.GetItemMap(itemType) {
v, err = cwhub.DisableItem(csConfig.Cscli, v, purgeRemove)
v, err = cwhub.DisableItem(csConfig.Cscli, v, purge, forceAction)
if err != nil {
log.Fatalf("unable to disable %s : %v", v.Name, err)
}
@ -185,7 +185,7 @@ func RemoveMany(itemType string, name string) {
disabled++
}
}
if name != "" && !removeAll {
if name != "" && !all {
log.Errorf("%s not found", name)
return
}
@ -220,7 +220,7 @@ func UpgradeConfig(itemType string, name string, force bool) {
continue
}
}
v, err = cwhub.DownloadLatest(csConfig.Cscli, v, forceUpgrade)
v, err = cwhub.DownloadLatest(csConfig.Cscli, v, force)
if err != nil {
log.Fatalf("%s : download failed : %v", v.Name, err)
}
@ -487,7 +487,7 @@ func silenceInstallItem(name string, obtype string) (string, error) {
if downloadOnly && it.Downloaded && it.UpToDate {
return fmt.Sprintf("%s is already downloaded and up-to-date", it.Name), nil
}
it, err := cwhub.DownloadLatest(csConfig.Cscli, it, forceInstall)
it, err := cwhub.DownloadLatest(csConfig.Cscli, it, forceAction)
if err != nil {
return "", fmt.Errorf("error while downloading %s : %v", it.Name, err)
}

View file

@ -20,7 +20,7 @@ cscli collections upgrade crowdsec/xxx crowdsec/xyz
```
-d, --download-only Only download packages, don't enable
--force Force install : Overwrite tainted and outdated files
--force Force upgrade : Overwrite tainted and outdated files
-h, --help help for upgrade
```

View file

@ -20,7 +20,7 @@ cscli parsers upgrade crowdsec/xxx crowdsec/xyz
```
--all Upgrade all the parsers
--force Force install : Overwrite tainted and outdated files
--force Force upgrade : Overwrite tainted and outdated files
-h, --help help for upgrade
```

View file

@ -20,7 +20,7 @@ cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz
```
-d, --download-only Only download packages, don't enable
--force Force install : Overwrite tainted and outdated files
--force Force upgrade : Overwrite tainted and outdated files
-h, --help help for upgrade
```

View file

@ -234,7 +234,7 @@ func GetUpstreamInstalledScenarios() ([]Item, error) {
}
//Returns a list of entries for packages : name, status, local_path, local_version, utf8_status (fancy)
func HubStatus(itemType string, name string, listAll bool) []map[string]string {
func HubStatus(itemType string, name string, all bool) []map[string]string {
if _, ok := hubIdx[itemType]; !ok {
log.Errorf("type %s doesn't exist", itemType)
@ -249,7 +249,7 @@ func HubStatus(itemType string, name string, listAll bool) []map[string]string {
continue
}
//Only enabled items ?
if !listAll && !item.Installed {
if !all && !item.Installed {
continue
}
//Check the item status

View file

@ -267,7 +267,7 @@ func testDisableItem(cfg *csconfig.CscliCfg, t *testing.T, item Item) {
t.Fatalf("disable: %s should be installed", item.Name)
}
//Remove
item, err := DisableItem(cfg, item, false)
item, err := DisableItem(cfg, item, false, false)
if err != nil {
t.Fatalf("failed to disable item : %v", err)
}
@ -285,7 +285,7 @@ func testDisableItem(cfg *csconfig.CscliCfg, t *testing.T, item Item) {
t.Fatalf("disable: %s should still be downloaded", item.Name)
}
//Purge
item, err = DisableItem(cfg, item, true)
item, err = DisableItem(cfg, item, true, false)
if err != nil {
t.Fatalf("failed to purge item : %v", err)
}

View file

@ -11,7 +11,7 @@ import (
)
//DisableItem to disable an item managed by the hub, removes the symlink if purge is true
func DisableItem(cscli *csconfig.CscliCfg, target Item, purge bool) (Item, error) {
func DisableItem(cscli *csconfig.CscliCfg, target Item, purge bool, force bool) (Item, error) {
var tdir = cscli.ConfigDir
var hdir = cscli.HubDir
@ -30,7 +30,7 @@ func DisableItem(cscli *csconfig.CscliCfg, target Item, purge bool) (Item, error
ptrtype := ItemTypes[idx]
for _, p := range ptr {
if val, ok := hubIdx[ptrtype][p]; ok {
hubIdx[ptrtype][p], err = DisableItem(cscli, val, purge)
hubIdx[ptrtype][p], err = DisableItem(cscli, val, purge, force)
if err != nil {
return target, errors.Wrap(err, fmt.Sprintf("while disabling %s", p))
}
@ -39,13 +39,12 @@ func DisableItem(cscli *csconfig.CscliCfg, target Item, purge bool) (Item, error
}
}
}
}
stat, err := os.Lstat(syml)
if os.IsNotExist(err) {
if !purge { //we only accept to "delete" non existing items if it's a purge
return target, fmt.Errorf("can't delete %s : %s doesn't exist", target.Name, syml)
if !purge && !force { //we only accept to "delete" non existing items if it's a purge
return target, fmt.Errorf("%s is tainted, won't remove unless --force", target.Name)
}
} else {
//if it's managed by hub, it's a symlink to csconfig.GConfig.Cscli.HubDir / ...