sensu-go icon indicating copy to clipboard operation
sensu-go copied to clipboard

sensuctl env command doesn't work with API Key authentication

Open calebhailey opened this issue 2 years ago • 1 comments

Expected Behavior

sensuctl env command works with API Key authentication.

Current Behavior

$ ./sensuctl version
sensuctl version 6.6.6+ee, enterprise edition, build 1ec7a895d1b5710bc20f772342689cc8f99dbf3a, built 2022-03-08T21:20:50Z, built with go1.17.6
$ ./sensuctl env
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xc24612]

goroutine 1 [running]:
github.com/sensu/sensu-go/cli/client.(*RestClient).RefreshAccessToken(0xf9845d, 0x0)
        github.com/sensu/[email protected]/cli/client/authentication.go:86 +0x72
github.com/sensu/sensu-go/cli/commands/env.refreshAccessToken.func1(0xc0003caa00, {0x11ff69b, 0x0, 0x0})
        github.com/sensu/[email protected]/cli/commands/env/env.go:145 +0x4c
github.com/spf13/cobra.(*Command).execute(0xc0003caa00, {0x1d100c0, 0x0, 0x0})
        github.com/spf13/[email protected]/command.go:845 +0x583
github.com/spf13/cobra.(*Command).ExecuteC(0xc0003ca000)
        github.com/spf13/[email protected]/command.go:960 +0x3ad
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/[email protected]/command.go:897
main.main()
        github.com/sensu/sensu-enterprise-go/cmd/sensuctl/main.go:39 +0xaf

Possible Solution

Steps to Reproduce (for bugs)

  1. Delete the contents of .config/sensu/sensuctl
  2. Configure SENSU_API_URL and SENSU_API_KEY environment variables
  3. Verify sensuctl authentication with any Sensu command (e.g. sensuctl entity list)
  4. Run sensuctl env
  5. 💥

Context

Your Environment

  • Sensu version used (sensuctl, sensu-backend, and/or sensu-agent): sensuctl version 6.6.6+ee, enterprise edition, build 1ec7a895d1b5710bc20f772342689cc8f99dbf3a, built 2022-03-08T21:20:50Z, built with go1.17.6
  • Installation method (packages, binaries, docker etc.): docker images
  • Operating System and version (e.g. Ubuntu 14.04): Linux

calebhailey avatar Apr 09 '22 00:04 calebhailey

When using the env command it's asking to refresh a token which, because we only have API Key, is not existing.

in env.go function refreshAccessToken we could add a check to check if an API Key is present and if a token is not present. If thats the case then we can just return from this function.

Then in the same file, in execute function we should only set shellCfg.AccessToken, shellCfg.AccessTokenExpiresAt, shellCfg.RefreshToken parameters if a token exist. Otherwise leave it empty.

By doing so the env command is working:

./sensuctl env
export SENSU_API_URL="http://192.168.1.39:8080"
export SENSU_NAMESPACE="default"
export SENSU_FORMAT="tabular"
export SENSU_API_KEY="83abef1e-e7d7-4beb-91fc-79ad90084d5b"
export SENSU_ACCESS_TOKEN=""
export SENSU_ACCESS_TOKEN_EXPIRES_AT="0"
export SENSU_REFRESH_TOKEN=""
export SENSU_TRUSTED_CA_FILE=""
export SENSU_INSECURE_SKIP_TLS_VERIFY="false"
export SENSU_TIMEOUT="15s"
# Run this command to configure your shell:
# eval $(./sensuctl env)

Now I'm just wondering if it's wise to output TOKEN information if you have the API Key. I don't think so, so I will look at a way not to display them.

agm650 avatar May 15 '22 22:05 agm650