2018-05-08 02:09:37 +00:00
package main
import (
2020-06-23 15:11:47 +00:00
"encoding/json"
2021-11-22 14:13:26 +00:00
"errors"
2018-05-08 02:09:37 +00:00
"fmt"
2020-11-18 01:12:37 +00:00
"io"
2022-02-15 18:48:09 +00:00
"net/url"
2018-05-08 02:09:37 +00:00
"os"
2020-10-21 23:29:27 +00:00
"strconv"
2022-12-05 16:35:45 +00:00
"time"
2018-05-08 02:09:37 +00:00
2022-12-05 16:35:45 +00:00
"github.com/fatih/color"
2021-08-24 12:50:03 +00:00
"github.com/fleetdm/fleet/v4/pkg/secure"
2021-07-16 18:28:13 +00:00
"gopkg.in/guregu/null.v3"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
2021-07-23 14:48:40 +00:00
"github.com/fleetdm/fleet/v4/server/service"
2020-11-18 01:12:37 +00:00
"github.com/ghodss/yaml"
2018-05-08 02:09:37 +00:00
"github.com/olekukonko/tablewriter"
2021-03-13 00:42:38 +00:00
"github.com/urfave/cli/v2"
2018-05-08 02:09:37 +00:00
)
2019-08-13 16:41:40 +00:00
const (
2021-10-07 13:19:10 +00:00
yamlFlagName = "yaml"
jsonFlagName = "json"
withQueriesFlagName = "with-queries"
expiredFlagName = "expired"
includeServerConfigFlagName = "include-server-config"
2019-08-13 16:41:40 +00:00
)
2018-05-09 00:10:01 +00:00
type specGeneric struct {
Kind string ` json:"kind" `
Version string ` json:"apiVersion" `
Spec interface { } ` json:"spec" `
}
2021-07-16 18:28:13 +00:00
func defaultTable ( writer io . Writer ) * tablewriter . Table {
w := writerOrStdout ( writer )
table := tablewriter . NewWriter ( w )
2018-05-08 02:09:37 +00:00
table . SetRowLine ( true )
return table
}
2022-12-05 16:35:45 +00:00
func borderlessTabularTable ( writer io . Writer ) * tablewriter . Table {
w := writerOrStdout ( writer )
table := tablewriter . NewWriter ( w )
table . SetRowLine ( false )
table . SetAutoWrapText ( false )
table . SetAlignment ( tablewriter . ALIGN_LEFT )
table . SetCenterSeparator ( "" )
table . SetColumnSeparator ( "" )
table . SetRowSeparator ( "" )
table . SetHeaderLine ( false )
table . SetBorder ( false )
table . SetTablePadding ( "\t" )
table . SetNoWhiteSpace ( true )
return table
}
2021-07-16 18:28:13 +00:00
func writerOrStdout ( writer io . Writer ) io . Writer {
var w io . Writer
w = os . Stdout
if writer != nil {
w = writer
}
return w
}
2021-03-13 00:42:38 +00:00
func yamlFlag ( ) cli . Flag {
return & cli . BoolFlag {
2019-08-13 16:41:40 +00:00
Name : yamlFlagName ,
2020-06-23 15:11:47 +00:00
Usage : "Output in yaml format" ,
2019-08-13 16:41:40 +00:00
}
}
2021-03-13 00:42:38 +00:00
func jsonFlag ( ) cli . Flag {
return & cli . BoolFlag {
2020-06-23 15:11:47 +00:00
Name : jsonFlagName ,
Usage : "Output in JSON format" ,
}
}
2021-07-16 18:28:13 +00:00
func printJSON ( spec interface { } , writer io . Writer ) error {
w := writerOrStdout ( writer )
2020-06-23 15:11:47 +00:00
b , err := json . Marshal ( spec )
if err != nil {
return err
2019-08-14 17:44:23 +00:00
}
2021-07-16 18:28:13 +00:00
fmt . Fprintf ( w , "%s\n" , b )
2020-06-23 15:11:47 +00:00
return nil
}
2019-08-14 17:44:23 +00:00
2021-07-16 18:28:13 +00:00
func printYaml ( spec interface { } , writer io . Writer ) error {
w := writerOrStdout ( writer )
2019-08-14 17:44:23 +00:00
b , err := yaml . Marshal ( spec )
if err != nil {
return err
}
2021-07-16 18:28:13 +00:00
fmt . Fprintf ( w , "---\n%s" , string ( b ) )
2020-06-23 15:11:47 +00:00
return nil
}
2021-06-06 22:07:29 +00:00
func printLabel ( c * cli . Context , label * fleet . LabelSpec ) error {
2020-06-23 15:11:47 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . LabelKind ,
Version : fleet . ApiVersion ,
2020-06-23 15:11:47 +00:00
Spec : label ,
}
2019-08-14 17:44:23 +00:00
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
2019-08-14 17:44:23 +00:00
}
2021-06-06 22:07:29 +00:00
func printQuery ( c * cli . Context , query * fleet . QuerySpec ) error {
2020-06-23 15:11:47 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . QueryKind ,
Version : fleet . ApiVersion ,
2020-06-23 15:11:47 +00:00
Spec : query ,
}
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
2020-06-23 15:11:47 +00:00
}
2021-06-06 22:07:29 +00:00
func printPack ( c * cli . Context , pack * fleet . PackSpec ) error {
2019-08-14 17:44:23 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . PackKind ,
Version : fleet . ApiVersion ,
2019-08-14 17:44:23 +00:00
Spec : pack ,
}
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
2020-06-23 15:11:47 +00:00
}
2021-06-06 22:07:29 +00:00
func printSecret ( c * cli . Context , secret * fleet . EnrollSecretSpec ) error {
2020-06-23 15:11:47 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . EnrollSecretKind ,
Version : fleet . ApiVersion ,
2020-06-23 15:11:47 +00:00
Spec : secret ,
}
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
2020-06-23 15:11:47 +00:00
}
2022-10-14 14:14:18 +00:00
func printHost ( c * cli . Context , host * fleet . HostResponse ) error {
2020-06-23 15:11:47 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . HostKind ,
Version : fleet . ApiVersion ,
2020-06-23 15:11:47 +00:00
Spec : host ,
}
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
2020-06-23 15:11:47 +00:00
}
2021-08-31 15:37:03 +00:00
func printHostDetail ( c * cli . Context , host * service . HostDetailResponse ) error {
spec := specGeneric {
Kind : fleet . HostKind ,
Version : fleet . ApiVersion ,
Spec : host ,
}
return printSpec ( c , spec )
}
2022-04-19 13:29:50 +00:00
type enrichedAppConfigPresenter fleet . EnrichedAppConfig
func ( eacp enrichedAppConfigPresenter ) MarshalJSON ( ) ( [ ] byte , error ) {
type UpdateIntervalConfigPresenter struct {
OSQueryDetail string ` json:"osquery_detail" `
OSQueryPolicy string ` json:"osquery_policy" `
* fleet . UpdateIntervalConfig
}
type VulnerabilitiesConfigPresenter struct {
Periodicity string ` json:"periodicity" `
RecentVulnerabilityMaxAge string ` json:"recent_vulnerability_max_age" `
* fleet . VulnerabilitiesConfig
}
return json . Marshal ( & struct {
fleet . EnrichedAppConfig
UpdateInterval UpdateIntervalConfigPresenter ` json:"update_interval,omitempty" `
Vulnerabilities VulnerabilitiesConfigPresenter ` json:"vulnerabilities,omitempty" `
} {
EnrichedAppConfig : fleet . EnrichedAppConfig ( eacp ) ,
UpdateInterval : UpdateIntervalConfigPresenter {
eacp . UpdateInterval . OSQueryDetail . String ( ) ,
eacp . UpdateInterval . OSQueryPolicy . String ( ) ,
eacp . UpdateInterval ,
} ,
Vulnerabilities : VulnerabilitiesConfigPresenter {
eacp . Vulnerabilities . Periodicity . String ( ) ,
eacp . Vulnerabilities . RecentVulnerabilityMaxAge . String ( ) ,
eacp . Vulnerabilities ,
} ,
} )
}
2021-10-07 13:19:10 +00:00
func printConfig ( c * cli . Context , config interface { } ) error {
2020-06-23 15:11:47 +00:00
spec := specGeneric {
2021-06-06 22:07:29 +00:00
Kind : fleet . AppConfigKind ,
Version : fleet . ApiVersion ,
2020-06-23 15:11:47 +00:00
Spec : config ,
}
2021-07-16 18:28:13 +00:00
return printSpec ( c , spec )
}
type UserRoles struct {
Roles map [ string ] UserRole ` json:"roles" `
}
type TeamRole struct {
Team string ` json:"team" `
Role string ` json:"role" `
}
type UserRole struct {
GlobalRole * string ` json:"global_role" `
Teams [ ] TeamRole ` json:"teams" `
}
func usersToUserRoles ( users [ ] fleet . User ) UserRoles {
roles := make ( map [ string ] UserRole )
for _ , u := range users {
var teams [ ] TeamRole
for _ , t := range u . Teams {
teams = append ( teams , TeamRole {
Team : t . Name ,
Role : t . Role ,
} )
}
roles [ u . Email ] = UserRole {
GlobalRole : u . GlobalRole ,
Teams : teams ,
}
}
return UserRoles { Roles : roles }
}
func printUserRoles ( c * cli . Context , users [ ] fleet . User ) error {
spec := specGeneric {
Kind : fleet . UserRolesKind ,
Version : fleet . ApiVersion ,
Spec : usersToUserRoles ( users ) ,
}
return printSpec ( c , spec )
}
2021-07-19 19:48:49 +00:00
func printTeams ( c * cli . Context , teams [ ] fleet . Team ) error {
for _ , team := range teams {
spec := specGeneric {
Kind : fleet . TeamKind ,
Version : fleet . ApiVersion ,
Spec : map [ string ] interface { } {
"team" : team ,
} ,
}
if err := printSpec ( c , spec ) ; err != nil {
return err
}
}
return nil
}
2021-07-16 18:28:13 +00:00
func printSpec ( c * cli . Context , spec specGeneric ) error {
2020-06-23 15:11:47 +00:00
var err error
if c . Bool ( jsonFlagName ) {
2021-07-16 18:28:13 +00:00
err = printJSON ( spec , c . App . Writer )
2020-06-23 15:11:47 +00:00
} else {
2021-07-16 18:28:13 +00:00
err = printYaml ( spec , c . App . Writer )
2020-06-23 15:11:47 +00:00
}
return err
}
2021-03-13 00:42:38 +00:00
func getCommand ( ) * cli . Command {
return & cli . Command {
2020-06-23 15:11:47 +00:00
Name : "get" ,
Usage : "Get/list resources" ,
2021-03-13 00:42:38 +00:00
Subcommands : [ ] * cli . Command {
2020-06-23 15:11:47 +00:00
getQueriesCommand ( ) ,
getPacksCommand ( ) ,
getLabelsCommand ( ) ,
getHostsCommand ( ) ,
getEnrollSecretCommand ( ) ,
getAppConfigCommand ( ) ,
2020-11-05 04:45:16 +00:00
getCarveCommand ( ) ,
getCarvesCommand ( ) ,
2021-07-16 18:28:13 +00:00
getUserRolesCommand ( ) ,
2021-07-19 19:48:49 +00:00
getTeamsCommand ( ) ,
2021-09-14 13:58:48 +00:00
getSoftwareCommand ( ) ,
2022-12-05 16:35:45 +00:00
getMDMAppleCommand ( ) ,
2020-06-23 15:11:47 +00:00
} ,
}
2019-08-14 17:44:23 +00:00
}
2021-03-13 00:42:38 +00:00
func getQueriesCommand ( ) * cli . Command {
return & cli . Command {
2018-05-08 02:09:37 +00:00
Name : "queries" ,
Aliases : [ ] string { "query" , "q" } ,
Usage : "List information about one or more queries" ,
Flags : [ ] cli . Flag {
2020-06-23 15:11:47 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
2018-05-08 02:09:37 +00:00
configFlag ( ) ,
contextFlag ( ) ,
2021-02-03 02:55:16 +00:00
debugFlag ( ) ,
2018-05-08 02:09:37 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2018-05-08 02:09:37 +00:00
if err != nil {
return err
}
name := c . Args ( ) . First ( )
// if name wasn't provided, list all queries
if name == "" {
2021-06-06 22:07:29 +00:00
queries , err := client . GetQueries ( )
2018-05-08 02:09:37 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list queries: %w" , err )
2018-05-08 02:09:37 +00:00
}
if len ( queries ) == 0 {
2020-11-05 04:45:16 +00:00
fmt . Println ( "No queries found" )
2018-05-08 02:09:37 +00:00
return nil
}
2020-06-23 15:11:47 +00:00
if c . Bool ( yamlFlagName ) || c . Bool ( jsonFlagName ) {
for _ , query := range queries {
if err := printQuery ( c , query ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to print query: %w" , err )
2020-06-23 15:11:47 +00:00
}
}
} else {
// Default to printing as a table
data := [ ] [ ] string { }
2018-05-08 02:09:37 +00:00
2020-06-23 15:11:47 +00:00
for _ , query := range queries {
data = append ( data , [ ] string {
query . Name ,
query . Description ,
query . Query ,
} )
}
2018-05-08 02:09:37 +00:00
2021-07-16 18:28:13 +00:00
columns := [ ] string { "name" , "description" , "query" }
printTable ( c , columns , data )
2018-05-09 00:10:01 +00:00
}
2020-06-23 15:11:47 +00:00
return nil
}
2018-05-09 00:10:01 +00:00
2021-06-06 22:07:29 +00:00
query , err := client . GetQuery ( name )
2020-06-23 15:11:47 +00:00
if err != nil {
return err
}
2018-05-09 00:10:01 +00:00
2020-06-23 15:11:47 +00:00
if err := printQuery ( c , query ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to print query: %w" , err )
2018-05-08 02:09:37 +00:00
}
2020-06-23 15:11:47 +00:00
return nil
2018-05-08 02:09:37 +00:00
} ,
}
}
2021-03-13 00:42:38 +00:00
func getPacksCommand ( ) * cli . Command {
return & cli . Command {
2018-05-08 02:09:37 +00:00
Name : "packs" ,
Aliases : [ ] string { "pack" , "p" } ,
Usage : "List information about one or more packs" ,
Flags : [ ] cli . Flag {
2021-03-13 00:42:38 +00:00
& cli . BoolFlag {
2019-08-14 17:44:23 +00:00
Name : withQueriesFlagName ,
Usage : "Output queries included in pack(s) too" ,
} ,
2021-02-03 02:55:16 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
2018-05-08 02:09:37 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2018-05-08 02:09:37 +00:00
if err != nil {
return err
}
name := c . Args ( ) . First ( )
2019-08-14 17:44:23 +00:00
shouldPrintQueries := c . Bool ( withQueriesFlagName )
queriesToPrint := make ( map [ string ] bool )
2021-06-06 22:07:29 +00:00
addQueries := func ( pack * fleet . PackSpec ) {
2019-08-14 17:44:23 +00:00
if shouldPrintQueries {
for _ , q := range pack . Queries {
queriesToPrint [ q . QueryName ] = true
}
}
}
printQueries := func ( ) error {
if ! shouldPrintQueries {
return nil
}
2021-06-06 22:07:29 +00:00
queries , err := client . GetQueries ( )
2019-08-14 17:44:23 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list queries: %w" , err )
2019-08-14 17:44:23 +00:00
}
// Getting all queries then filtering is usually faster than getting
// one query at a time.
for _ , query := range queries {
if ! queriesToPrint [ query . Name ] {
continue
}
2020-06-23 15:11:47 +00:00
if err := printQuery ( c , query ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to print query: %w" , err )
2019-08-14 17:44:23 +00:00
}
}
return nil
}
2018-05-08 02:09:37 +00:00
// if name wasn't provided, list all packs
if name == "" {
2021-06-06 22:07:29 +00:00
packs , err := client . GetPacks ( )
2018-05-08 02:09:37 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list packs: %w" , err )
2018-05-08 02:09:37 +00:00
}
2021-08-31 15:37:03 +00:00
if c . Bool ( yamlFlagName ) || c . Bool ( jsonFlagName ) {
2019-08-13 16:41:40 +00:00
for _ , pack := range packs {
2020-06-23 15:11:47 +00:00
if err := printPack ( c , pack ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to print pack: %w" , err )
2019-08-13 16:41:40 +00:00
}
2019-08-14 17:44:23 +00:00
addQueries ( pack )
2019-08-13 16:41:40 +00:00
}
2019-08-14 17:44:23 +00:00
return printQueries ( )
2019-08-13 16:41:40 +00:00
}
2018-05-08 02:09:37 +00:00
if len ( packs ) == 0 {
2020-11-05 04:45:16 +00:00
fmt . Println ( "No packs found" )
2018-05-08 02:09:37 +00:00
return nil
}
data := [ ] [ ] string { }
for _ , pack := range packs {
data = append ( data , [ ] string {
pack . Name ,
pack . Platform ,
pack . Description ,
2020-10-21 23:29:27 +00:00
strconv . FormatBool ( pack . Disabled ) ,
2018-05-08 02:09:37 +00:00
} )
}
2021-07-16 18:28:13 +00:00
columns := [ ] string { "name" , "platform" , "description" , "disabled" }
printTable ( c , columns , data )
2018-05-08 02:09:37 +00:00
return nil
2020-06-23 15:11:47 +00:00
}
2018-05-09 00:10:01 +00:00
2020-06-23 15:11:47 +00:00
// Name was specified
2021-06-06 22:07:29 +00:00
pack , err := client . GetPack ( name )
2020-06-23 15:11:47 +00:00
if err != nil {
return err
}
2018-05-09 00:10:01 +00:00
2020-06-23 15:11:47 +00:00
addQueries ( pack )
2018-05-09 00:10:01 +00:00
2020-06-23 15:11:47 +00:00
if err := printPack ( c , pack ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to print pack: %w" , err )
2018-05-08 02:09:37 +00:00
}
2020-06-23 15:11:47 +00:00
return printQueries ( )
2018-05-08 02:09:37 +00:00
} ,
}
}
2021-03-13 00:42:38 +00:00
func getLabelsCommand ( ) * cli . Command {
return & cli . Command {
2018-05-08 02:09:37 +00:00
Name : "labels" ,
Aliases : [ ] string { "label" , "l" } ,
Usage : "List information about one or more labels" ,
Flags : [ ] cli . Flag {
2020-06-23 15:11:47 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
2018-05-08 02:09:37 +00:00
configFlag ( ) ,
contextFlag ( ) ,
2021-02-03 02:55:16 +00:00
debugFlag ( ) ,
2018-05-08 02:09:37 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2018-05-08 02:09:37 +00:00
if err != nil {
return err
}
name := c . Args ( ) . First ( )
// if name wasn't provided, list all labels
if name == "" {
2021-06-06 22:07:29 +00:00
labels , err := client . GetLabels ( )
2018-05-08 02:09:37 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list labels: %w" , err )
2018-05-08 02:09:37 +00:00
}
2020-06-23 15:11:47 +00:00
if c . Bool ( yamlFlagName ) || c . Bool ( jsonFlagName ) {
2019-08-13 16:41:40 +00:00
for _ , label := range labels {
2022-12-05 22:50:49 +00:00
printLabel ( c , label ) //nolint:errcheck
2019-08-13 16:41:40 +00:00
}
return nil
}
2018-05-08 02:09:37 +00:00
if len ( labels ) == 0 {
2020-11-05 04:45:16 +00:00
fmt . Println ( "No labels found" )
2018-05-08 02:09:37 +00:00
return nil
}
2020-06-23 15:11:47 +00:00
// Default to printing as a table
2018-05-08 02:09:37 +00:00
data := [ ] [ ] string { }
for _ , label := range labels {
data = append ( data , [ ] string {
label . Name ,
label . Platform ,
label . Description ,
label . Query ,
} )
}
2021-07-16 18:28:13 +00:00
columns := [ ] string { "name" , "platform" , "description" , "query" }
printTable ( c , columns , data )
2018-05-08 02:09:37 +00:00
return nil
2020-06-23 15:11:47 +00:00
}
2018-05-09 00:10:01 +00:00
2020-06-23 15:11:47 +00:00
// Label name was specified
2021-06-06 22:07:29 +00:00
label , err := client . GetLabel ( name )
2020-06-23 15:11:47 +00:00
if err != nil {
return err
}
2018-05-09 00:10:01 +00:00
2022-12-05 22:50:49 +00:00
printLabel ( c , label ) //nolint:errcheck
2020-06-23 15:11:47 +00:00
return nil
2018-05-08 02:09:37 +00:00
} ,
}
}
2018-05-21 16:25:58 +00:00
2021-03-13 00:42:38 +00:00
func getEnrollSecretCommand ( ) * cli . Command {
return & cli . Command {
2020-05-29 16:12:39 +00:00
Name : "enroll_secret" ,
Aliases : [ ] string { "enroll_secrets" , "enroll-secret" , "enroll-secrets" } ,
Usage : "Retrieve the osquery enroll secrets" ,
2018-05-22 23:57:56 +00:00
Flags : [ ] cli . Flag {
2020-06-23 15:11:47 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
2018-05-22 23:57:56 +00:00
configFlag ( ) ,
contextFlag ( ) ,
2021-02-03 02:55:16 +00:00
debugFlag ( ) ,
2018-05-22 23:57:56 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2018-05-22 23:57:56 +00:00
if err != nil {
return err
}
2021-06-06 22:07:29 +00:00
secrets , err := client . GetEnrollSecretSpec ( )
2018-05-22 23:57:56 +00:00
if err != nil {
return err
}
2020-05-29 16:12:39 +00:00
2020-06-23 15:11:47 +00:00
err = printSecret ( c , secrets )
2020-05-29 16:12:39 +00:00
if err != nil {
return err
}
2018-05-22 23:57:56 +00:00
return nil
2018-05-21 16:25:58 +00:00
} ,
}
}
2019-01-03 22:19:23 +00:00
2021-03-13 00:42:38 +00:00
func getAppConfigCommand ( ) * cli . Command {
return & cli . Command {
2019-11-20 05:13:15 +00:00
Name : "config" ,
2021-10-07 13:19:10 +00:00
Usage : "Retrieve the Fleet app configuration" ,
2019-11-20 05:13:15 +00:00
Flags : [ ] cli . Flag {
2020-06-23 15:11:47 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
2019-11-20 05:13:15 +00:00
configFlag ( ) ,
contextFlag ( ) ,
2021-02-03 02:55:16 +00:00
debugFlag ( ) ,
2021-10-07 13:19:10 +00:00
& cli . BoolFlag {
Name : includeServerConfigFlagName ,
Usage : "Include the server configuration in the output" ,
} ,
2019-11-20 05:13:15 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2019-11-20 05:13:15 +00:00
if err != nil {
return err
}
2021-06-06 22:07:29 +00:00
config , err := client . GetAppConfig ( )
2019-11-20 05:13:15 +00:00
if err != nil {
return err
}
2021-10-07 13:19:10 +00:00
if c . Bool ( includeServerConfigFlagName ) {
2022-04-19 13:29:50 +00:00
err = printConfig ( c , enrichedAppConfigPresenter ( * config ) )
2021-10-07 13:19:10 +00:00
} else {
err = printConfig ( c , config . AppConfig )
}
2019-11-20 05:13:15 +00:00
if err != nil {
return err
}
return nil
} ,
}
}
2021-03-13 00:42:38 +00:00
func getHostsCommand ( ) * cli . Command {
return & cli . Command {
2019-01-03 22:19:23 +00:00
Name : "hosts" ,
Aliases : [ ] string { "host" , "h" } ,
Usage : "List information about one or more hosts" ,
Flags : [ ] cli . Flag {
2021-08-11 14:40:56 +00:00
& cli . UintFlag {
2021-08-20 15:27:41 +00:00
Name : "team" ,
Usage : "filter hosts by team_id" ,
Required : false ,
2021-08-11 14:40:56 +00:00
} ,
2020-06-23 15:11:47 +00:00
jsonFlag ( ) ,
yamlFlag ( ) ,
2019-01-03 22:19:23 +00:00
configFlag ( ) ,
contextFlag ( ) ,
2021-02-03 02:55:16 +00:00
debugFlag ( ) ,
2019-01-03 22:19:23 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2019-01-03 22:19:23 +00:00
if err != nil {
return err
}
2020-04-22 20:54:32 +00:00
identifier := c . Args ( ) . First ( )
2019-01-03 22:19:23 +00:00
2020-04-22 20:54:32 +00:00
if identifier == "" {
2022-02-15 18:48:09 +00:00
query := url . Values { }
query . Set ( "additional_info_filters" , "*" )
if teamID := c . Uint ( "team" ) ; teamID > 0 {
2022-02-15 22:03:27 +00:00
query . Set ( "team_id" , strconv . FormatUint ( uint64 ( teamID ) , 10 ) )
2021-08-11 14:40:56 +00:00
}
2022-02-15 18:48:09 +00:00
queryStr := query . Encode ( )
hosts , err := client . GetHosts ( queryStr )
2020-04-22 20:54:32 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list hosts: %w" , err )
2020-04-22 20:54:32 +00:00
}
2019-01-03 22:19:23 +00:00
2020-04-22 20:54:32 +00:00
if len ( hosts ) == 0 {
2020-11-05 04:45:16 +00:00
fmt . Println ( "No hosts found" )
2020-04-22 20:54:32 +00:00
return nil
}
if c . Bool ( jsonFlagName ) || c . Bool ( yamlFlagName ) {
for _ , host := range hosts {
2021-07-23 14:48:40 +00:00
err = printHost ( c , & host )
2020-04-22 20:54:32 +00:00
if err != nil {
return err
}
2020-06-23 15:11:47 +00:00
}
2020-04-22 20:54:32 +00:00
return nil
2020-06-23 15:11:47 +00:00
}
2020-04-22 20:54:32 +00:00
// Default to printing as table
data := [ ] [ ] string { }
2019-01-03 22:19:23 +00:00
2020-04-22 20:54:32 +00:00
for _ , host := range hosts {
data = append ( data , [ ] string {
host . Host . UUID ,
host . DisplayText ,
host . Host . Platform ,
2020-09-25 17:15:42 +00:00
host . OsqueryVersion ,
2020-04-22 20:54:32 +00:00
string ( host . Status ) ,
} )
}
2019-01-03 22:19:23 +00:00
2021-07-16 18:28:13 +00:00
columns := [ ] string { "uuid" , "hostname" , "platform" , "osquery_version" , "status" }
printTable ( c , columns , data )
2020-04-22 20:54:32 +00:00
} else {
2021-06-06 22:07:29 +00:00
host , err := client . HostByIdentifier ( identifier )
2020-04-22 20:54:32 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not get host: %w" , err )
2020-04-22 20:54:32 +00:00
}
2021-08-31 15:37:03 +00:00
err = printHostDetail ( c , host )
2020-04-22 20:54:32 +00:00
if err != nil {
return err
}
}
2019-01-03 22:19:23 +00:00
return nil
} ,
}
}
2020-11-05 04:45:16 +00:00
2021-03-13 00:42:38 +00:00
func getCarvesCommand ( ) * cli . Command {
return & cli . Command {
2020-11-18 01:12:37 +00:00
Name : "carves" ,
Usage : "Retrieve the file carving sessions" ,
2020-11-05 04:45:16 +00:00
Flags : [ ] cli . Flag {
2021-03-13 00:42:38 +00:00
& cli . BoolFlag {
2020-11-05 04:45:16 +00:00
Name : expiredFlagName ,
Usage : "Include expired carves" ,
} ,
2021-02-03 02:55:16 +00:00
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
2020-11-05 04:45:16 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2020-11-05 04:45:16 +00:00
if err != nil {
return err
}
expired := c . Bool ( expiredFlagName )
2021-06-06 22:07:29 +00:00
carves , err := client . ListCarves ( fleet . CarveListOptions { Expired : expired } )
2020-11-05 04:45:16 +00:00
if err != nil {
return err
}
if len ( carves ) == 0 {
fmt . Println ( "No carves found" )
return nil
}
data := [ ] [ ] string { }
for _ , c := range carves {
completion := fmt . Sprintf (
"%d%%" ,
int64 ( ( float64 ( c . MaxBlock + 1 ) / float64 ( c . BlockCount ) ) * 100 ) ,
)
if c . Expired {
completion = "Expired"
}
data = append ( data , [ ] string {
strconv . FormatInt ( c . ID , 10 ) ,
c . CreatedAt . Local ( ) . String ( ) ,
c . RequestId ,
strconv . FormatInt ( c . CarveSize , 10 ) ,
completion ,
} )
}
2021-07-16 18:28:13 +00:00
columns := [ ] string { "id" , "created_at" , "request_id" , "carve_size" , "completion" }
printTable ( c , columns , data )
2020-11-05 04:45:16 +00:00
return nil
} ,
}
}
2021-03-13 00:42:38 +00:00
func getCarveCommand ( ) * cli . Command {
return & cli . Command {
2020-11-18 01:12:37 +00:00
Name : "carve" ,
Usage : "Retrieve details for a carve by ID" ,
2020-11-05 04:45:16 +00:00
Flags : [ ] cli . Flag {
2022-05-10 13:44:06 +00:00
stdoutFlag ( ) ,
2021-02-03 02:55:16 +00:00
configFlag ( ) ,
contextFlag ( ) ,
outfileFlag ( ) ,
debugFlag ( ) ,
2020-11-05 04:45:16 +00:00
} ,
Action : func ( c * cli . Context ) error {
2021-06-06 22:07:29 +00:00
client , err := clientFromCLI ( c )
2020-11-05 04:45:16 +00:00
if err != nil {
return err
}
idString := c . Args ( ) . First ( )
if idString == "" {
2021-11-22 14:13:26 +00:00
return errors . New ( "must provide carve ID as first argument" )
2020-11-05 04:45:16 +00:00
}
id , err := strconv . ParseInt ( idString , 10 , 64 )
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "unable to parse carve ID as int: %w" , err )
2020-11-05 04:45:16 +00:00
}
2020-11-18 01:12:37 +00:00
outFile := getOutfile ( c )
2020-11-05 04:45:16 +00:00
stdout := c . Bool ( stdoutFlagName )
if stdout && outFile != "" {
2021-11-22 14:13:26 +00:00
return errors . New ( "-stdout and -outfile must not be specified together" )
2020-11-05 04:45:16 +00:00
}
if stdout || outFile != "" {
out := os . Stdout
if outFile != "" {
2021-08-11 14:02:22 +00:00
f , err := secure . OpenFile ( outFile , os . O_CREATE | os . O_WRONLY , defaultFileMode )
2020-11-05 04:45:16 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "open out file: %w" , err )
2020-11-05 04:45:16 +00:00
}
defer f . Close ( )
out = f
}
2021-06-06 22:07:29 +00:00
reader , err := client . DownloadCarve ( id )
2020-11-05 04:45:16 +00:00
if err != nil {
return err
}
if _ , err := io . Copy ( out , reader ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "download carve contents: %w" , err )
2020-11-05 04:45:16 +00:00
}
return nil
}
2021-06-06 22:07:29 +00:00
carve , err := client . GetCarve ( id )
2020-11-05 04:45:16 +00:00
if err != nil {
return err
}
2021-07-16 18:28:13 +00:00
if err := printYaml ( carve , c . App . Writer ) ; err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "print carve yaml: %w" , err )
2020-11-05 04:45:16 +00:00
}
return nil
} ,
}
}
2021-07-16 18:28:13 +00:00
func log ( c * cli . Context , msg ... interface { } ) {
fmt . Fprint ( c . App . Writer , msg ... )
}
func getUserRolesCommand ( ) * cli . Command {
return & cli . Command {
Name : "user_roles" ,
Aliases : [ ] string { "user_role" , "ur" } ,
Usage : "List global and team roles for users" ,
Flags : [ ] cli . Flag {
jsonFlag ( ) ,
yamlFlag ( ) ,
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
} ,
Action : func ( c * cli . Context ) error {
client , err := clientFromCLI ( c )
if err != nil {
return err
}
users , err := client . ListUsers ( )
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list users: %w" , err )
2021-07-16 18:28:13 +00:00
}
if len ( users ) == 0 {
log ( c , "No users found" )
return nil
}
if c . Bool ( jsonFlagName ) || c . Bool ( yamlFlagName ) {
err = printUserRoles ( c , users )
if err != nil {
return err
}
return nil
}
// Default to printing as table
data := [ ] [ ] string { }
for _ , u := range users {
data = append ( data , [ ] string {
u . Name ,
null . StringFromPtr ( u . GlobalRole ) . ValueOrZero ( ) ,
} )
}
columns := [ ] string { "User" , "Global Role" }
printTable ( c , columns , data )
return nil
} ,
}
}
func printTable ( c * cli . Context , columns [ ] string , data [ ] [ ] string ) {
table := defaultTable ( c . App . Writer )
table . SetHeader ( columns )
table . AppendBulk ( data )
table . Render ( )
}
2021-07-19 19:48:49 +00:00
2022-12-05 16:35:45 +00:00
func printKeyValueTable ( c * cli . Context , rows [ ] [ ] string ) {
table := borderlessTabularTable ( c . App . Writer )
table . AppendBulk ( rows )
table . Render ( )
}
2021-07-19 19:48:49 +00:00
func getTeamsCommand ( ) * cli . Command {
return & cli . Command {
Name : "teams" ,
Aliases : [ ] string { "t" } ,
Usage : "List teams" ,
Flags : [ ] cli . Flag {
jsonFlag ( ) ,
yamlFlag ( ) ,
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
2022-02-15 18:48:09 +00:00
& cli . StringFlag {
Name : nameFlagName ,
Usage : "filter by name" ,
} ,
2021-07-19 19:48:49 +00:00
} ,
Action : func ( c * cli . Context ) error {
client , err := clientFromCLI ( c )
if err != nil {
return err
}
2022-02-15 18:48:09 +00:00
query := url . Values { }
if name := c . String ( nameFlagName ) ; name != "" {
query . Set ( "query" , name )
}
queryStr := query . Encode ( )
teams , err := client . ListTeams ( queryStr )
2021-07-19 19:48:49 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list teams: %w" , err )
2021-07-19 19:48:49 +00:00
}
if len ( teams ) == 0 {
log ( c , "No teams found" )
return nil
}
if c . Bool ( jsonFlagName ) || c . Bool ( yamlFlagName ) {
err = printTeams ( c , teams )
if err != nil {
return err
}
return nil
}
// Default to printing as table
data := [ ] [ ] string { }
for _ , team := range teams {
data = append ( data , [ ] string {
team . Name ,
team . Description ,
fmt . Sprintf ( "%d" , team . UserCount ) ,
} )
}
columns := [ ] string { "Team Name" , "Description" , "User count" }
printTable ( c , columns , data )
return nil
} ,
}
}
2021-09-14 13:58:48 +00:00
func getSoftwareCommand ( ) * cli . Command {
return & cli . Command {
Name : "software" ,
Aliases : [ ] string { "s" } ,
Usage : "List software" ,
Flags : [ ] cli . Flag {
& cli . UintFlag {
Name : teamFlagName ,
Usage : "Only list software of hosts that belong to the specified team" ,
} ,
jsonFlag ( ) ,
yamlFlag ( ) ,
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
} ,
Action : func ( c * cli . Context ) error {
client , err := clientFromCLI ( c )
if err != nil {
return err
}
if c . Bool ( yamlFlagName ) && c . Bool ( jsonFlagName ) {
return errors . New ( "Can't specify both yaml and json flags." )
}
2022-07-19 21:11:51 +00:00
query := url . Values { }
2021-09-14 13:58:48 +00:00
2022-07-19 21:11:51 +00:00
teamID := c . Uint ( teamFlagName )
if teamID != 0 {
query . Set ( "team_id" , strconv . FormatUint ( uint64 ( teamID ) , 10 ) )
2021-09-14 13:58:48 +00:00
}
2022-07-19 21:11:51 +00:00
software , err := client . ListSoftware ( query . Encode ( ) )
2021-09-14 13:58:48 +00:00
if err != nil {
2021-11-22 14:13:26 +00:00
return fmt . Errorf ( "could not list software: %w" , err )
2021-09-14 13:58:48 +00:00
}
if len ( software ) == 0 {
log ( c , "No software found" )
return nil
}
if c . Bool ( jsonFlagName ) || c . Bool ( yamlFlagName ) {
spec := specGeneric {
Kind : "software" ,
Version : "1" ,
Spec : software ,
}
err = printSpec ( c , spec )
if err != nil {
return err
}
return nil
}
// Default to printing as table
data := [ ] [ ] string { }
for _ , s := range software {
data = append ( data , [ ] string {
s . Name ,
s . Version ,
s . Source ,
s . GenerateCPE ,
fmt . Sprint ( len ( s . Vulnerabilities ) ) ,
} )
}
columns := [ ] string { "Name" , "Version" , "Source" , "CPE" , "# of CVEs" }
printTable ( c , columns , data )
return nil
} ,
}
}
2022-12-05 16:35:45 +00:00
func getMDMAppleCommand ( ) * cli . Command {
return & cli . Command {
Name : "mdm_apple" ,
Hidden : true , // TODO: temporary, until the MDM feature is officially released
Aliases : [ ] string { "mdm-apple" } ,
Usage : "Show Apple Push Notification Service (APNs) information" ,
Flags : [ ] cli . Flag {
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
} ,
Action : func ( c * cli . Context ) error {
const expirationWarning = 30 * 24 * time . Hour // 30 days
client , err := clientFromCLI ( c )
if err != nil {
return err
}
mdm , err := client . GetAppleMDM ( )
if err != nil {
var nfe service . NotFoundErr
if errors . As ( err , & nfe ) {
log ( c , "Error: No Apple Push Notification service (APNs) certificate found. Use `fleetctl generate mdm-apple` and then `fleet serve` with `mdm` configuration to turn on MDM features.\n" )
return nil
}
return err
}
printKeyValueTable ( c , [ ] [ ] string {
{ "Common name (CN):" , mdm . CommonName } ,
{ "Serial number:" , mdm . SerialNumber } ,
{ "Issuer:" , mdm . Issuer } ,
{ "Renew date:" , mdm . RenewDate . Format ( "January 2, 2006" ) } ,
} )
warnDate := time . Now ( ) . Add ( expirationWarning )
if mdm . RenewDate . Before ( time . Now ( ) ) {
// certificate is expired, print an error
color . New ( color . FgRed ) . Fprintln ( writerOrStdout ( c . App . Writer ) , "\nERROR: Your Apple Push Notification service (APNs) certificate is expired. MDM features are turned off. To renew your APNs certificate, follow these instructions: [TODO link to documentation]" )
} else if mdm . RenewDate . Before ( warnDate ) {
// certificate will soon expire, print a warning
color . New ( color . FgYellow ) . Fprintln ( writerOrStdout ( c . App . Writer ) , "\nWARNING: Your Apple Push Notification service (APNs) certificate is less than 30 days from expiration. If it expires, MDM features will be turned off. To renew your APNs certificate, follow these instructions: [TODO link to documentation]" )
}
return nil
} ,
}
}