fleetdm / fleet

Open-source platform for IT, security, and infrastructure teams. (Linux, macOS, Chrome, Windows, cloud, data center)
https://fleetdm.com
Other
3.02k stars 419 forks source link

Using the --context option with fleetctl gives unexpected results #13712

Closed dherder closed 11 months ago

dherder commented 1 year ago

Fleet version: <!-- Copy this from the "My account" page in the Fleet UI, or run fleetctl --version -->

Web browser and operating system:


💥  Actual behavior

When applying a context to fleetctl with the mdm option, you have to place the --context in an odd location.

🧑‍💻  Steps to reproduce

  1. Issue fleetctl mdm run-command --context dev --payload=restart-device.xml --host=hostname

Result

Successful command

Expected Behaviour

All other fleetctl commands when using --context seem to work when adding the context after the verb. I would expect to add the --context after run-command but if I do this, I get an error.

🕯️ More info (optional)

This workaround is documented as an FAQ here. When this bug is squashed, please remove that documentation.

noahtalerman commented 1 year ago

I would expect to add the --context after run-command but if I do this, I get an error.

@dherder if I'm understanding correctly, the steps to reproduce should be this? fleetctl mdm run-command --context dev ...

And not the following (--context before run-command): Screenshot 2023-09-11 at 1 06 45 PM

I think I would expect the above to not work because fleetctl mdm isn't a command itself.

dherder commented 1 year ago

@noahtalerman yes, you are correct apologies. I kind of described it backwards.

noahtalerman commented 1 year ago

Got it. I updated the issue description.

sabrinabuckets commented 11 months ago

@roperzh I'm having trouble following the expected behavior here. Just to confirm I was able to send a successful command only by using this formatting: fleetctl mdm --context dev run-command --payload=restart-device.xml --host=hostname

Is that correct behavior?

roperzh commented 11 months ago

@sabrinabuckets the expected behavior is that you can use --context after run-command. Maybe you have an old version? I just tried from latest main and it works (the error proofs that I was able to communicate to dogfood)

/fleet $ git fetch && git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

~/fleet $ make fleetctl
mkdir -p build/linux
mkdir -p build/darwin
# Race requires cgo
CGO_ENABLED=0 go build -race=false -o build/fleetctl -ldflags " -X github.com/kolide/kit/version.appName=fleetctl -X github.com/kolide/kit/version.version=fleet-v4.39.0-43-g0e99f5a66 -X github.com/kolide/kit/version.branch=main -X github.com/kolide/kit/version.revision=0e99f5a6602d402fc3d072411b8802766370ae72 -X github.com/kolide/kit/version.buildDate=2023-10-30 -X github.com/kolide/kit/version.buildUser=roperzh -X github.com/kolide/kit/version.goVersion=go1.21.0" ./cmd/fleetctl
go: downloading github.com/facebookincubator/nvdtools v0.1.6-0.20231010102659-d14ce526f176
go: downloading github.com/hashicorp/go-hclog v1.0.0

~/fleet $ ./build/fleetctl mdm run-command --context=dogfood --host=asdf --payload=README.md
Error: The host doesn't exist. Please provide a valid hostname, uuid, osquery_host_id or node_key.
sabrinabuckets commented 11 months ago

@roperzh I was on latest main yesterday when testing, and am starting fresh again this morning, same results.

roperzh commented 11 months ago

@sabrinabuckets sanity checking, are you using the binary from ./build/fleetctl?

sabrinabuckets commented 11 months ago

@roperzh yup, always

roperzh commented 11 months ago

@sabrinabuckets follow up, what's the output of ./build/fleetctl mdm run-command --help? mine includes --context:

NAME:
   fleetctl mdm run-command - Run a custom MDM command on one macOS host. Head to Apple's documentation for a list of available commands and example payloads here:  https://developer.apple.com/documentation/devicemanagement/commands_and_queries

USAGE:
   fleetctl mdm run-command [command options] [arguments...]

OPTIONS:
   --context value  Name of fleetctl config context to use (default: "default") [$CONTEXT]
   --debug          Enable debug http request logging (default: false) [$DEBUG]
   --host value     The host, specified by hostname, uuid, osquery_host_id or node_key, that you want to run the MDM command on.
   --payload value  A path to an XML file containing the raw MDM request payload.
   --help, -h       show help (default: false)
sabrinabuckets commented 11 months ago
bri@bris-mbp-2 fleet % ./build/fleetctl mdm run-command --help
NAME:
   fleetctl mdm run-command - Run a custom MDM command on one macOS host. Head to Apple's documentation for a list of available commands and example payloads here:  https://developer.apple.com/documentation/devicemanagement/commands_and_queries

USAGE:
   fleetctl mdm run-command [command options] [arguments...]

OPTIONS:
   --context value  Name of fleetctl config context to use (default: "default") [$CONTEXT]
   --debug          Enable debug http request logging (default: false) [$DEBUG]
   --host value     The host, specified by hostname, uuid, osquery_host_id or node_key, that you want to run the MDM command on.
   --payload value  A path to an XML file containing the raw MDM request payload.
   --help, -h       show help (default: false)
sabrinabuckets commented 11 months ago

If it helps, when I run with --context after run-command I get Error: create client: context "dev" is not found

roperzh commented 11 months ago

@sabrinabuckets thank you! so it's there, and what output you get when you run the command with --context dev at tail end?

sabrinabuckets commented 11 months ago

Same as above, Error: create client: context "dev" is not found

roperzh commented 11 months ago

jinx! okay, that helps! it means you don't have a dev context configured (so the flag is working, now we have to figure out if you really don't have a dev context or something else is going on)

@sabrinabuckets could you please check in ~/.fleet/config (a yaml file) if you have a dev context?

sabrinabuckets commented 11 months ago

I'm not sure how, but I have a .yml and a .yaml, (the first one seems to be formatted incorrectly, so I'm guessing it's not the "right" one), neither of them have a reference to dev in any way I can find.

roperzh commented 11 months ago

@sabrinabuckets I think that's the problem then 🎉 . The --context flag is working as expected but you don't have a dev context (guessing you were probably following Dave's example above?)

my recommendation would be to use any other context you have configured, or you can create a context like this:

$ ./build/fleetctl config set --address A_FLEET_SERVER_URL --context dev
$ ./build/fleetctl login --context dev
$ ./build/fleetctl mdm run-command ... --context dev
sabrinabuckets commented 11 months ago

OK, that worked. It's actually quite funny that a ticket about "context" was entirely missing it 😆.

fleet-release commented 11 months ago

In Fleet's glass city, --context finds its true place, Commands sail smoothly.