2018-05-08 02:07:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-22 14:13:26 +00:00
|
|
|
"errors"
|
2018-05-08 02:07:00 +00:00
|
|
|
"fmt"
|
2023-10-27 18:28:54 +00:00
|
|
|
"os"
|
2018-05-08 02:07:00 +00:00
|
|
|
|
2022-08-05 22:07:32 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/pkg/spec"
|
2021-11-15 14:11:38 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/service"
|
2021-03-13 00:42:38 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-05-08 02:07:00 +00:00
|
|
|
)
|
|
|
|
|
2021-03-13 00:42:38 +00:00
|
|
|
func deleteCommand() *cli.Command {
|
2022-08-05 22:07:32 +00:00
|
|
|
var flFilename string
|
2021-03-13 00:42:38 +00:00
|
|
|
return &cli.Command{
|
2018-05-08 02:07:00 +00:00
|
|
|
Name: "delete",
|
|
|
|
Usage: "Specify files to declaratively batch delete osquery configurations",
|
|
|
|
UsageText: `fleetctl delete [options]`,
|
|
|
|
Flags: []cli.Flag{
|
2021-03-13 00:42:38 +00:00
|
|
|
&cli.StringFlag{
|
2018-05-08 02:07:00 +00:00
|
|
|
Name: "f",
|
2021-03-13 00:42:38 +00:00
|
|
|
EnvVars: []string{"FILENAME"},
|
2018-05-08 02:07:00 +00:00
|
|
|
Value: "",
|
|
|
|
Destination: &flFilename,
|
|
|
|
Usage: "A file to apply",
|
|
|
|
},
|
2021-02-03 02:55:16 +00:00
|
|
|
configFlag(),
|
|
|
|
contextFlag(),
|
|
|
|
debugFlag(),
|
2018-05-08 02:07:00 +00:00
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
if flFilename == "" {
|
|
|
|
return errors.New("-f must be specified")
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:28:54 +00:00
|
|
|
b, err := os.ReadFile(flFilename)
|
2018-05-08 02:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fleet, err := clientFromCLI(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-05 22:07:32 +00:00
|
|
|
specs, err := spec.GroupFromBytes(b)
|
2018-05-08 02:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, query := range specs.Queries {
|
|
|
|
fmt.Printf("[+] deleting query %q\n", query.Name)
|
|
|
|
if err := fleet.DeleteQuery(query.Name); err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
root := ctxerr.Cause(err)
|
|
|
|
switch root.(type) {
|
2018-05-08 02:07:00 +00:00
|
|
|
case service.NotFoundErr:
|
|
|
|
fmt.Printf("[!] query %q doesn't exist\n", query.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pack := range specs.Packs {
|
|
|
|
fmt.Printf("[+] deleting pack %q\n", pack.Name)
|
|
|
|
if err := fleet.DeletePack(pack.Name); err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
root := ctxerr.Cause(err)
|
|
|
|
switch root.(type) {
|
2018-05-08 02:07:00 +00:00
|
|
|
case service.NotFoundErr:
|
|
|
|
fmt.Printf("[!] pack %q doesn't exist\n", pack.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range specs.Labels {
|
|
|
|
fmt.Printf("[+] deleting label %q\n", label.Name)
|
|
|
|
if err := fleet.DeleteLabel(label.Name); err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
root := ctxerr.Cause(err)
|
|
|
|
switch root.(type) {
|
2018-05-08 02:07:00 +00:00
|
|
|
case service.NotFoundErr:
|
|
|
|
fmt.Printf("[!] label %q doesn't exist\n", label.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|