From 05af177cf64b2d8844e8c03763ba66a70c90746f Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Fri, 10 Jan 2020 10:55:07 -0800 Subject: [PATCH] Add option to opt out of saving device command history. (#640) --- cmd/micromdm/serve.go | 2 ++ platform/queue/queue.go | 24 +++++++++++++++++++----- server/server.go | 7 ++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/micromdm/serve.go b/cmd/micromdm/serve.go index 10671f54..9bee49f7 100644 --- a/cmd/micromdm/serve.go +++ b/cmd/micromdm/serve.go @@ -83,6 +83,7 @@ func serve(args []string) error { flCommandWebhookURL = flagset.String("command-webhook-url", env.String("MICROMDM_WEBHOOK_URL", ""), "URL to send command responses") flHomePage = flagset.Bool("homepage", env.Bool("MICROMDM_HTTP_HOMEPAGE", true), "Hosts a simple built-in webpage at the / address") flSCEPClientValidity = flagset.Int("scep-client-validity", env.Int("MICROMDM_SCEP_CLIENT_VALIDITY", 365), "Sets the scep certificate validity in days") + flNoCmdHistory = flagset.Bool("no-command-history", env.Bool("MICROMDM_NO_COMMAND_HISTORY", false), "disables saving of command history") flPrintArgs = flagset.Bool("print-flags", false, "Print all flags and their values") ) flagset.Usage = usageFor(flagset, "micromdm serve [flags]") @@ -125,6 +126,7 @@ func serve(args []string) error { Depsim: *flDepSim, TLSCertPath: *flTLSCert, CommandWebhookURL: *flCommandWebhookURL, + NoCmdHistory: *flNoCmdHistory, WebhooksHTTPClient: &http.Client{Timeout: time.Second * 30}, diff --git a/platform/queue/queue.go b/platform/queue/queue.go index 8d5e0b4b..a45d1766 100644 --- a/platform/queue/queue.go +++ b/platform/queue/queue.go @@ -25,7 +25,8 @@ const ( type Store struct { *bolt.DB - logger log.Logger + logger log.Logger + withoutHistory bool } type Option func(*Store) @@ -36,6 +37,12 @@ func WithLogger(logger log.Logger) Option { } } +func WithoutHistory() Option { + return func(s *Store) { + s.withoutHistory = true + } +} + func (db *Store) Next(ctx context.Context, resp mdm.Response) ([]byte, error) { cmd, err := db.nextCommand(ctx, resp) if err != nil { @@ -87,8 +94,11 @@ func (db *Store) nextCommand(ctx context.Context, resp mdm.Response) (*Command, if x == nil { break } - x.Acknowledged = time.Now().UTC() - dc.Completed = append(dc.Completed, *x) + if !db.withoutHistory { + x.Acknowledged = time.Now().UTC() + dc.Completed = append(dc.Completed, *x) + } + case "Error": // move to failed, send next x, a := cut(dc.Commands, resp.CommandUUID) @@ -96,7 +106,9 @@ func (db *Store) nextCommand(ctx context.Context, resp mdm.Response) (*Command, if x == nil { // must've already bin ackd break } - dc.Failed = append(dc.Failed, *x) + if !db.withoutHistory { + dc.Failed = append(dc.Failed, *x) + } case "CommandFormatError": // move to failed @@ -105,7 +117,9 @@ func (db *Store) nextCommand(ctx context.Context, resp mdm.Response) (*Command, if x == nil { break } - dc.Failed = append(dc.Failed, *x) + if !db.withoutHistory { + dc.Failed = append(dc.Failed, *x) + } case "Idle": diff --git a/server/server.go b/server/server.go index 277c943a..60661040 100644 --- a/server/server.go +++ b/server/server.go @@ -53,6 +53,7 @@ type Server struct { CommandWebhookURL string DEPClient *dep.Client SyncDB *syncbuiltin.DB + NoCmdHistory bool APNSPushService apns.Service CommandService command.Service @@ -158,7 +159,11 @@ func (c *Server) setupCommandService() error { } func (c *Server) setupCommandQueue(logger log.Logger) error { - q, err := queue.NewQueue(c.DB, c.PubClient, queue.WithLogger(logger)) + opts := []queue.Option{queue.WithLogger(logger)} + if c.NoCmdHistory { + opts = append(opts, queue.WithoutHistory()) + } + q, err := queue.NewQueue(c.DB, c.PubClient, opts...) if err != nil { return err }