goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.58k stars 470 forks source link

if statement returns "invalid memory address or nil pointer dereference" error #810

Closed xescab closed 1 year ago

xescab commented 1 year ago

Describe the bug

After adding an extra condition (and (eq .name "argocd-applicationset-controller") (eq .port 7000)) when running goss -g test.yaml --vars vars.yaml validate I get this error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x77b9e0]

How To Reproduce

vars.yaml

tcp_services:
  - name: argo-rollouts-dashboard
    namespace:  argo-rollouts
    port: 3100
  - name: argo-rollouts-metrics
    namespace:  argo-rollouts
    port: 8090
  - name: argocd-applicationset-controller
    namespace:  argocd
    port: 7000
  - name: argocd-applicationset-controller
    namespace:  argocd
    port: 8080

test.yaml

command:
  # Ensure TCP services are not reachable by any pod, except Traefik and some other core services
  {{ range index .Vars "tcp_services" }}
  "kubectl -n demo exec test-network-policy -ti -- /bin/nc -vz -w 1 {{.name}}.{{.namespace}} {{.port}}":
    timeout: 30000 # in milliseconds
    {{ if or (eq .name "lb-traefik") (eq .name "kube-dns") (eq .name "prometheus-adapter") (and (eq .name "argocd-applicationset-controller") (eq .port 7000)) }}
    exit-status: 0
    stderr:
    - "/{{.name}}.{{.namespace}} .*:{{.port}}.* open/"
    {{ else }}
    exit-status: 1
    stderr:
    - "/{{.name}}.{{.namespace}} .*:{{.port}}.* Connection timed out/"
    {{ end }}
  {{end}}

Expected Behavior

I want to be able to test for 2 conditions at the same time in a complex if statement.

Actual Behavior

I got a segfault error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x77b9e0]

goroutine 65 [running]:
github.com/goss-org/goss/system.(*DefHTTP).Status(0xc0004ee0d0)
        /home/travis/gopath/src/github.com/goss-org/goss/system/http.go:184 +0x40
github.com/goss-org/goss/resource.ValidateValue({0xc2d958, 0xc0005ca8c0}, {0xb29627, 0x6}, {0xa4aa80, 0x1018980}, {0xa51580?, 0xc0002820a8?}, _)
        /home/travis/gopath/src/github.com/goss-org/goss/resource/validate.go:135 +0x369
github.com/goss-org/goss/resource.(*HTTP).Validate(0xc0005ca8c0, 0x0?)
        /home/travis/gopath/src/github.com/goss-org/goss/resource/http.go:77 +0x305
github.com/goss-org/goss.validate.func2()
        /home/travis/gopath/src/github.com/goss-org/goss/validate.go:171 +0x91
created by github.com/goss-org/goss.validate
        /home/travis/gopath/src/github.com/goss-org/goss/validate.go:168 +0x190

Environment:

xescab commented 1 year ago

Issue was due to a comment in vars.yaml

aelsabbahy commented 1 year ago

If you don't mind, can you provide the exact vars.yaml and goss.yaml to reproduce this?

Goss should not panic, this is definitely a bug.

xescab commented 1 year ago

Yes, sure! :)

Our vars.yaml starts with:

domain_name: #DOMAIN#
k8s_version: 1.25

and then on a pipeline we replace #DOMAIN# by a proper string, but when I was testing locally I forgot to change the value and that is why I got the segfault. Maybe it could be detected and show a proper error message when the variables are not well declared.

Thanks for looking into this, @aelsabbahy !

aelsabbahy commented 1 year ago

Just following up on this, I think I'm doing something wrong while trying to reproduce, can you also provide the matching goss.yaml that causes it to panic.

$ cat vars.yaml 
domain_name: #DOMAIN#
k8s_version: 1.25

$ cat goss.yaml
command:
  echo "hi":
    exit-status: 0
    stdout:
    - {{.Vars.domain_name}}
    stderr: []
    timeout: 10000

$ goss.3.18 --vars vars.yaml render
command:
  echo "hi":
    exit-status: 0
    stdout:
    - <no value>
    stderr: []
    timeout: 10000

$ goss.3.18 --vars vars.yaml v
.F

Failures/Skipped:

Command: echo "hi": stdout: patterns not found: [<no value>]

Total Duration: 0.002s
Count: 2, Failed: 1, Skipped: 0
xescab commented 1 year ago

Sorry, I am also unable to reproduce with the previous goss.yaml that caused the segfault. I think we can close it.

aelsabbahy commented 1 year ago

It's okay, I have some theories on how it may have happened, but can't reproduce it.

I'll roll out those "fixes" in a future PR. We can leave the ticket open for that, I won't know for sure I fixed the problem, but should help.

Thank you for following back up.

xescab commented 1 year ago

Got it again! It's related with the http module:

$ ./goss --version
goss version v0.4.1

$ cat goss.yaml
http:
  # Ensure argocd is available and redirected to HTTPS
  http://argocd.mgmt.{{ .Vars.domain_name }}/:
    status: 302
    no-follow-redirects: true
    headers:
    - "Location: https://argocd"

$ cat vars.yaml
domain_name: #DOMAIN#
k8s_version: 1.25

$ ./goss -g goss.yaml --vars vars.yaml render
http:
  http://argocd.mgmt.<no value>/:
    status: 302
    allow-insecure: false
    no-follow-redirects: true
    headers:
    - 'Location: https://argocd'

$ ./goss -g goss.yaml --vars vars.yaml  validate
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7ac440]

goroutine 69 [running]:
github.com/goss-org/goss/system.(*DefHTTP).Status(0xc00012b380)
        /home/travis/gopath/src/github.com/goss-org/goss/system/http.go:186 +0x40
github.com/goss-org/goss/resource.ValidateGomegaValue({_, _}, {_, _}, {_, _}, {_, _}, _)
        /home/travis/gopath/src/github.com/goss-org/goss/resource/validate.go:160 +0x3f6
github.com/goss-org/goss/resource.ValidateValue({_, _}, {_, _}, {_, _}, {_, _}, _)
        /home/travis/gopath/src/github.com/goss-org/goss/resource/validate.go:130 +0x195
github.com/goss-org/goss/resource.(*HTTP).Validate(0xc000460a20, 0xc000330370?)
        /home/travis/gopath/src/github.com/goss-org/goss/resource/http.go:81 +0x305
github.com/goss-org/goss.validate.func2()
        /home/travis/gopath/src/github.com/goss-org/goss/validate.go:184 +0x91
created by github.com/goss-org/goss.validate
        /home/travis/gopath/src/github.com/goss-org/goss/validate.go:181 +0x190

Hope that with this new info you can fix it :)

aelsabbahy commented 1 year ago

Awesome, I was able to reproduce it! Will fix it in the next goss release.. surprised not too many people ran into this.

aelsabbahy commented 1 year ago

Fixed in pr, will be in the next release.