devops-rob / target-cli

A CLI tool to manage context profiles for HashiCorp tools
Apache License 2.0
32 stars 5 forks source link

`list` command throws `panic: runtime error` #13

Closed yermulnik closed 9 months ago

yermulnik commented 9 months ago

Any list command fails with the below:

> target vault list
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6be53e]

goroutine 1 [running]:
github.com/devops-rob/target-cli/cmd.glob..func22(0x9c08c0, {0x7448e1, 0x0, 0x0})
        /home/runner/work/target-cli/target-cli/cmd/list.go:25 +0x9e
github.com/spf13/cobra.(*Command).execute(0x9c08c0, {0x9fd940, 0x0, 0x0})
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:944 +0x827
github.com/spf13/cobra.(*Command).ExecuteC(0x9c22a0)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:1068 +0x3cd
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:992
github.com/devops-rob/target-cli/cmd.Execute()
        /home/runner/work/target-cli/target-cli/cmd/root.go:146 +0x25
main.main()
        /home/runner/work/target-cli/target-cli/main.go:6 +0x17

> target terraform list
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6bea9d]

goroutine 1 [running]:
github.com/devops-rob/target-cli/cmd.glob..func26(0x9c1720, {0x7448e1, 0x0, 0x0})
        /home/runner/work/target-cli/target-cli/cmd/list.go:123 +0x7d
github.com/spf13/cobra.(*Command).execute(0x9c1720, {0x9fd940, 0x0, 0x0})
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:944 +0x827
github.com/spf13/cobra.(*Command).ExecuteC(0x9c22a0)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:1068 +0x3cd
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:992
github.com/devops-rob/target-cli/cmd.Execute()
        /home/runner/work/target-cli/target-cli/cmd/root.go:146 +0x25
main.main()
        /home/runner/work/target-cli/target-cli/main.go:6 +0x17
yermulnik commented 9 months ago
> fgrep -w VERSION /etc/os-release
VERSION="22.04.3 LTS (Jammy Jellyfish)"

and

> fgrep -w VERSION /etc/os-release
VERSION="19.3 (Tricia)"

ps: the latter is a bit outdated though is for the sake of example

bradleyfrank commented 9 months ago

Similar here:

[~]% target vault create [name] --endpoint "https://vault.[hidden].com" --token "hvs.[hidden]" --namespace "secrets"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x100847fa8]

goroutine 1 [running]:
github.com/devops-rob/target-cli/cmd.glob..func52(0x100b3ba20, {0x14000229110, 0x1, 0x7})
        /home/runner/work/target-cli/target-cli/cmd/vaultcreate.go:25 +0x28
github.com/spf13/cobra.(*Command).execute(0x100b3ba20, {0x140002290a0, 0x7, 0x7})
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:944 +0x70c
github.com/spf13/cobra.(*Command).ExecuteC(0x100b394c0)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:1068 +0x41c
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/work/target-cli/target-cli/vendor/github.com/spf13/cobra/command.go:992
github.com/devops-rob/target-cli/cmd.Execute()
        /home/runner/work/target-cli/target-cli/cmd/root.go:146 +0x30
main.main()
        /home/runner/work/target-cli/target-cli/main.go:6 +0x20
[~]% uname -a
Darwin mac06770 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000 arm64 arm Darwin
[~]% sw_vers
ProductName:            macOS
ProductVersion:         14.2.1
BuildVersion:           23C71
picatz commented 9 months ago

I also stumbled upon this. Looks like it's related to the following (global config variable used by the CLI):

https://github.com/devops-rob/target-cli/blob/7a10f70ca7b6f6da387f82a00136414b66ff0ba4/cmd/root.go#L84-L86

It hits this case:

https://github.com/devops-rob/target-cli/blob/7a10f70ca7b6f6da387f82a00136414b66ff0ba4/cmd/root.go#L210-L216

But doesn't set the global config variable (c), because it never hits this:

https://github.com/devops-rob/target-cli/blob/7a10f70ca7b6f6da387f82a00136414b66ff0ba4/cmd/root.go#L216-L226

For context (pun intended), my configuration file contains:

$ cat ~/.target/profiles.json 
{
        "vault": {},
        "consul": {},
        "nomad": {}
        "terraform": {}
}

💡 We need to remove the else it is wrapped in in order for things to then ultimately flow properly into that global variable.

sereinity commented 9 months ago

The error is in https://github.com/devops-rob/target-cli/blob/main/pkg/targetdir/targetdir.go#L29 which generates a malformed profiles.json.

The document misses a comma at the end of the nomad line. Fixing the json made the command works.

yermulnik commented 9 months ago

Fixing the json made the command works.

Yay! I can confirm the fix. Thanks.