Skip to content

Commit

Permalink
Merge pull request #314 from jmpsec/random-fixes-cli
Browse files Browse the repository at this point in the history
Improvements to `osctrl-cli`
  • Loading branch information
javuto authored Oct 30, 2022
2 parents a7425b3 + cd79cc3 commit 137993a
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 178 deletions.
6 changes: 3 additions & 3 deletions cli/carve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ func listCarves(c *cli.Context) error {
"ArchivePath",
}
// Prepare output
if jsonFlag {
if formatFlag == jsonFormat {
jsonRaw, err := json.Marshal(cs)
if err != nil {
return err
}
fmt.Println(string(jsonRaw))
} else if csvFlag {
} else if formatFlag == csvFormat {
data := carvesToData(cs, header)
w := csv.NewWriter(os.Stdout)
if err := w.WriteAll(data); err != nil {
return err
}
} else if prettyFlag {
} else if formatFlag == prettyFormat {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
if len(cs) > 0 {
Expand Down
139 changes: 101 additions & 38 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const (
appDescription string = appUsage + ", a fast and efficient osquery management"
)

const (
// Values for output format
jsonFormat = "json"
csvFormat = "csv"
prettyFormat = "pretty"
)

// Global variables
var (
err error
Expand All @@ -49,15 +56,14 @@ var (
envs *environments.Environment
db *backend.DBManager
osctrlAPI *OsctrlAPI
formats map[string]bool
)

// Variables for flags
var (
dbFlag bool
apiFlag bool
jsonFlag bool
csvFlag bool
prettyFlag bool
formatFlag string
silentFlag bool
insecureFlag bool
dbConfigFile string
Expand Down Expand Up @@ -177,26 +183,13 @@ func init() {
Usage: "Allow insecure server connections when using SSL",
Destination: &insecureFlag,
},
&cli.BoolFlag{
Name: "json",
Aliases: []string{"j"},
Value: false,
Usage: "Print output in JSON format",
Destination: &jsonFlag,
},
&cli.BoolFlag{
Name: "csv",
Aliases: []string{"c"},
Value: false,
Usage: "Print output in CSV format",
Destination: &csvFlag,
},
&cli.BoolFlag{
Name: "pretty",
Aliases: []string{"p"},
Value: true,
Usage: "Print output in pretty format (table)",
Destination: &prettyFlag,
&cli.StringFlag{
Name: "output-format",
Aliases: []string{"o"},
Value: prettyFormat,
Usage: "Format to be used for data output",
EnvVars: []string{"OUTPUT_FORMAT"},
Destination: &formatFlag,
},
&cli.BoolFlag{
Name: "silent",
Expand Down Expand Up @@ -298,9 +291,9 @@ func init() {
Action: cliWrapper(editUser),
},
{
Name: "permissions",
Aliases: []string{"p"},
Usage: "Permission actions for an existing user",
Name: "change-permissions",
Aliases: []string{"p", "access"},
Usage: "Change permission in an environment for an existing user",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Expand Down Expand Up @@ -336,20 +329,81 @@ func init() {
Hidden: false,
Usage: "Grant carve permissions",
},
},
Action: cliWrapper(changePermissions),
},
{
Name: "reset-permissions",
Aliases: []string{"R", "reset"},
Usage: "Clear and reset permissions for a user in an environment",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "User to perform the action",
},
&cli.StringFlag{
Name: "environment",
Aliases: []string{"e"},
Usage: "Environment for this user",
},
&cli.BoolFlag{
Name: "reset",
Aliases: []string{"R"},
Name: "admin",
Aliases: []string{"a"},
Hidden: false,
Usage: "Reset permissions for this user",
Usage: "Grant admin permissions",
},
&cli.BoolFlag{
Name: "show",
Aliases: []string{"s"},
Name: "user",
Aliases: []string{"U"},
Hidden: false,
Usage: "Display all permissions for this user",
Usage: "Grant user permissions",
},
&cli.BoolFlag{
Name: "query",
Aliases: []string{"q"},
Hidden: false,
Usage: "Grant query permissions",
},
&cli.BoolFlag{
Name: "carve",
Aliases: []string{"c"},
Hidden: false,
Usage: "Grant carve permissions",
},
},
Action: cliWrapper(resetPermissions),
},
{
Name: "show-permissions",
Aliases: []string{"S", "perms"},
Usage: "Show permissions for a user in an environment",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "User to perform the action",
},
&cli.StringFlag{
Name: "environment",
Aliases: []string{"e"},
Usage: "Environment for this user",
},
},
Action: cliWrapper(permissionsUser),
Action: cliWrapper(showPermissions),
},
{
Name: "all-permissions",
Aliases: []string{"A", "all-perms"},
Usage: "Show all permissions for an existing user",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "User to perform the action",
},
},
Action: cliWrapper(allPermissions),
},
{
Name: "delete",
Expand Down Expand Up @@ -1389,6 +1443,11 @@ func init() {
Action: checkAPI,
},
}
// Initialize formats values
formats = make(map[string]bool)
formats[prettyFormat] = true
formats[jsonFormat] = true
formats[csvFormat] = true
}

// Action for the DB check
Expand Down Expand Up @@ -1431,18 +1490,22 @@ func checkAPI(c *cli.Context) error {
// Function to wrap actions
func cliWrapper(action func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) error {
// Verify if format is correct
if !formats[formatFlag] {
return fmt.Errorf("❌ invalid format %s", formatFlag)
}
// DB connection will be used
if dbFlag {
// Initialize backend
if dbConfigFile != "" {
db, err = backend.CreateDBManagerFile(dbConfigFile)
if err != nil {
return fmt.Errorf("CreateDBManagerFile - %v", err)
return fmt.Errorf("CreateDBManagerFile - %v", err)
}
} else {
db, err = backend.CreateDBManager(dbConfig)
if err != nil {
return fmt.Errorf("CreateDBManager - %v", err)
return fmt.Errorf("CreateDBManager - %v", err)
}
}
// Initialize users
Expand All @@ -1466,7 +1529,7 @@ func cliWrapper(action func(*cli.Context) error) func(*cli.Context) error {
if apiConfigFile != "" {
apiConfig, err = loadAPIConfiguration(apiConfigFile)
if err != nil {
return fmt.Errorf("loadAPIConfiguration - %v", err)
return fmt.Errorf("loadAPIConfiguration - %v", err)
}
}
// Initialize API
Expand All @@ -1483,7 +1546,7 @@ func cliWrapper(action func(*cli.Context) error) func(*cli.Context) error {
func cliAction(c *cli.Context) error {
if c.NumFlags() == 0 {
if err := cli.ShowAppHelp(c); err != nil {
log.Fatalf("Error with CLI help - %s", err)
log.Fatalf("Error with CLI help - %s", err)
}
return cli.Exit("\nNo flags provided", 2)
}
Expand All @@ -1502,6 +1565,6 @@ func main() {
app.Commands = commands
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
log.Fatalf("Failed to execute %v", err)
log.Fatalf("Failed to execute %v", err)
}
}
12 changes: 6 additions & 6 deletions cli/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ func listNodes(c *cli.Context) error {
"OsqueryVersion",
}
// Prepare output
if jsonFlag {
if formatFlag == jsonFormat {
jsonRaw, err := json.Marshal(nds)
if err != nil {
return fmt.Errorf("❌ error marshaling - %s", err)
}
fmt.Println(string(jsonRaw))
} else if csvFlag {
} else if formatFlag == csvFormat {
data := nodesToData(nds, header)
w := csv.NewWriter(os.Stdout)
if err := w.WriteAll(data); err != nil {
return fmt.Errorf("❌ error writting csv - %s", err)
}
} else if prettyFlag {
} else if formatFlag == prettyFormat {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
if len(nds) > 0 {
Expand Down Expand Up @@ -211,19 +211,19 @@ func showNode(c *cli.Context) error {
"OsqueryVersion",
}
// Prepare output
if jsonFlag {
if formatFlag == jsonFormat {
jsonRaw, err := json.Marshal(node)
if err != nil {
return fmt.Errorf("❌ error marshaling - %s", err)
}
fmt.Println(string(jsonRaw))
} else if csvFlag {
} else if formatFlag == csvFormat {
data := nodeToData(node, nil)
w := csv.NewWriter(os.Stdout)
if err := w.WriteAll(data); err != nil {
return fmt.Errorf("❌ error writting csv - %s", err)
}
} else if prettyFlag {
} else if formatFlag == prettyFormat {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
data := nodeToData(node, nil)
Expand Down
Loading

0 comments on commit 137993a

Please sign in to comment.