Sensu-go: Creating tcp handler without socket should not be permitted

Created on 7 May 2018  路  2Comments  路  Source: sensu/sensu-go

Expected Behavior




Since a tcp handler requires a socket attribute, I would expect sensuctl create to fail in some way, or for sensuctl handler list to report invalid configurations.

Current Behavior



I can create a handler that crashes sensuctl

Possible Solution



Add some error checking in sensuctl? (That's kind of naive suggestion not knowing this codebase well)

Steps to Reproduce (for bugs)


  1. Create a json object file with the following spec, lacking socket attribute:

handler.json:

{
  "type": "handler",
  "spec": {
    "name": "tcp_handler",
    "environment": "default",
    "organization": "default",
    "timeout": 30,
    "type": "tcp"
  }
}
  1. Create the object
# sensuctl create -f handler.json
  1. Try to list it
# sensuctl handler list
  1. Observe the panic
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x88f324]

goroutine 1 [running]:
github.com/sensu/sensu-go/cli/commands/handler.printToTable.func6(0x9a0760, 0xc420238000, 0x4, 0x4)
        /go/src/github.com/sensu/sensu-go/cli/commands/handler/list.go:102 +0x234
github.com/sensu/sensu-go/cli/elements/table.(*Table).writeRow(0xc42004fbd8, 0xc42021e040)
        /go/src/github.com/sensu/sensu-go/cli/elements/table/standard.go:90 +0xcb
github.com/sensu/sensu-go/cli/elements/table.(*Table).writeRows(0xc42004fbd8, 0x8f8da0, 0xc4201f4160)
        /go/src/github.com/sensu/sensu-go/cli/elements/table/standard.go:83 +0x24e
github.com/sensu/sensu-go/cli/elements/table.(*Table).Render(0xc42004fbd8, 0xa31400, 0xc420096008, 0x8f8da0, 0xc4201f4160)
        /go/src/github.com/sensu/sensu-go/cli/elements/table/standard.go:66 +0x82
github.com/sensu/sensu-go/cli/commands/handler.printToTable(0x8f8da0, 0xc4201f4160, 0xa31400, 0xc420096008)
        /go/src/github.com/sensu/sensu-go/cli/commands/handler/list.go:132 +0x30a
github.com/sensu/sensu-go/cli/commands/helpers.Print(0xc4201a3400, 0xc4200930f0, 0x7, 0x9f21d0, 0x8f8da0, 0xc4201f4160, 0x0, 0x0)
        /go/src/github.com/sensu/sensu-go/cli/commands/helpers/print.go:23 +0xb3
github.com/sensu/sensu-go/cli/commands/handler.ListCommand.func1(0xc4201a3400, 0xd14408, 0x0, 0x0, 0x0, 0x0)
        /go/src/github.com/sensu/sensu-go/cli/commands/handler/list.go:41 +0x252
github.com/sensu/sensu-go/vendor/github.com/spf13/cobra.(*Command).execute(0xc4201a3400, 0xd14408, 0x0, 0x0, 0xc4201a3400, 0xd14408)
        /go/src/github.com/sensu/sensu-go/vendor/github.com/spf13/cobra/command.go:753 +0x468
github.com/sensu/sensu-go/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc420180280, 0xc4201b0f00, 0xc4201b2000, 0xc4201b2280)
        /go/src/github.com/sensu/sensu-go/vendor/github.com/spf13/cobra/command.go:843 +0x30a
github.com/sensu/sensu-go/vendor/github.com/spf13/cobra.(*Command).Execute(0xc420180280, 0xc4201473e0, 0xc4200d9628)
        /go/src/github.com/sensu/sensu-go/vendor/github.com/spf13/cobra/command.go:791 +0x2b
main.main()
        /go/src/github.com/sensu/sensu-go/cli/cmd/start.go:26 +0xbd

Context



How has this issue affected you? Minor pain, blurred vision, slight headache.
What are you trying to accomplish? Writing custom chef resources to manage this stuff.

Your Environment

  • Sensu version used (sensuctl, sensu-backend, and/or sensu-agent): sensu-ctl version dev-nightly#9bba4cf, build 9bba4cf6af864bbabf0a3120ec2ad6d187552a8b, built 2018-05-06T09:24:10+0000
  • Installation method (packages, binaries, docker etc.): nightly packages from packagecloud
  • Operating System and version (e.g. Ubuntu 14.04): Ubuntu 16.04
bug

Most helpful comment

After building sensuctl with PR 1488, users get the following error on invalid handlers:

$ echo '{"type":"handler","spec":{"name":"a","environment":"b","organization":"c","type":"tcp"}}' | sensuctl create
error validating resource 0 (/handlers/a): tcp and udp handlers need a valid socket
Error: resource validation failed

All 2 comments

Nice find, thank you for reporting this.

The solution here is to add some logic to the Validate() method on the type. When handler.Type == "tcp", the handler should be well-specified. (valid host and port fields)

This will cause both the client and the server to reject attempts to create these broken handlers.

After building sensuctl with PR 1488, users get the following error on invalid handlers:

$ echo '{"type":"handler","spec":{"name":"a","environment":"b","organization":"c","type":"tcp"}}' | sensuctl create
error validating resource 0 (/handlers/a): tcp and udp handlers need a valid socket
Error: resource validation failed
Was this page helpful?
0 / 5 - 0 ratings