From e2f2e495586c28645b5069c90c956c73c7604ab8 Mon Sep 17 00:00:00 2001 From: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:26:50 +0530 Subject: [PATCH] NET-3860 - [Supportability] consul troubleshoot CLI for verifying ports (#19835) * cherry pick commit * fix merge conf * del import alias * fix go mod * fix go mod and licence --- .changelog/18329.txt | 4 + command/registry.go | 4 +- .../troubleshoot/ports/troubleshoot_ports.go | 89 +++++++++++++++++++ go.mod | 2 +- troubleshoot/go.mod | 13 +-- troubleshoot/go.sum | 29 +++--- troubleshoot/ports/hostport.go | 6 ++ troubleshoot/ports/troubleshoot_ports.go | 50 +++++++++++ troubleshoot/ports/troubleshoot_ports_test.go | 52 +++++++++++ troubleshoot/ports/troubleshoot_protocol.go | 5 ++ troubleshoot/ports/troubleshoot_tcp.go | 21 +++++ .../content/commands/troubleshoot/index.mdx | 2 + .../content/commands/troubleshoot/ports.mdx | 47 ++++++++++ website/data/commands-nav-data.json | 4 + 14 files changed, 309 insertions(+), 19 deletions(-) create mode 100644 .changelog/18329.txt create mode 100644 command/troubleshoot/ports/troubleshoot_ports.go create mode 100644 troubleshoot/ports/hostport.go create mode 100644 troubleshoot/ports/troubleshoot_ports.go create mode 100644 troubleshoot/ports/troubleshoot_ports_test.go create mode 100644 troubleshoot/ports/troubleshoot_protocol.go create mode 100644 troubleshoot/ports/troubleshoot_tcp.go create mode 100644 website/content/commands/troubleshoot/ports.mdx diff --git a/.changelog/18329.txt b/.changelog/18329.txt new file mode 100644 index 0000000000..ab3671de86 --- /dev/null +++ b/.changelog/18329.txt @@ -0,0 +1,4 @@ +```release-note:improvement +cli: Adds cli support for checking TCP connection for ports. If -ports flag is not given, it will check for +default ports of consul listed here - https://developer.hashicorp.com/consul/docs/install/ports +``` \ No newline at end of file diff --git a/command/registry.go b/command/registry.go index 8def9aca96..1f4f9670c1 100644 --- a/command/registry.go +++ b/command/registry.go @@ -118,7 +118,8 @@ import ( tlscacreate "github.com/hashicorp/consul/command/tls/ca/create" tlscert "github.com/hashicorp/consul/command/tls/cert" tlscertcreate "github.com/hashicorp/consul/command/tls/cert/create" - troubleshoot "github.com/hashicorp/consul/command/troubleshoot" + "github.com/hashicorp/consul/command/troubleshoot" + troubleshootports "github.com/hashicorp/consul/command/troubleshoot/ports" troubleshootproxy "github.com/hashicorp/consul/command/troubleshoot/proxy" troubleshootupstreams "github.com/hashicorp/consul/command/troubleshoot/upstreams" "github.com/hashicorp/consul/command/validate" @@ -250,6 +251,7 @@ func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory { entry{"troubleshoot", func(ui cli.Ui) (cli.Command, error) { return troubleshoot.New(), nil }}, entry{"troubleshoot proxy", func(ui cli.Ui) (cli.Command, error) { return troubleshootproxy.New(ui), nil }}, entry{"troubleshoot upstreams", func(ui cli.Ui) (cli.Command, error) { return troubleshootupstreams.New(ui), nil }}, + entry{"troubleshoot ports", func(ui cli.Ui) (cli.Command, error) { return troubleshootports.New(ui), nil }}, entry{"validate", func(ui cli.Ui) (cli.Command, error) { return validate.New(ui), nil }}, entry{"version", func(ui cli.Ui) (cli.Command, error) { return version.New(ui), nil }}, entry{"watch", func(ui cli.Ui) (cli.Command, error) { return watch.New(ui, MakeShutdownCh()), nil }}, diff --git a/command/troubleshoot/ports/troubleshoot_ports.go b/command/troubleshoot/ports/troubleshoot_ports.go new file mode 100644 index 0000000000..1088703051 --- /dev/null +++ b/command/troubleshoot/ports/troubleshoot_ports.go @@ -0,0 +1,89 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ports + +import ( + "flag" + "fmt" + "github.com/hashicorp/consul/troubleshoot/ports" + "os" + + "github.com/hashicorp/consul/command/cli" + "github.com/hashicorp/consul/command/flags" +) + +func New(ui cli.Ui) *cmd { + c := &cmd{UI: ui} + c.init() + return c +} + +type cmd struct { + UI cli.Ui + flags *flag.FlagSet + help string + + // flags + host string + ports string +} + +func (c *cmd) init() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + + c.flags.StringVar(&c.host, "host", os.Getenv("CONSUL_HTTP_ADDR"), "The consul server host") + + c.flags.StringVar(&c.ports, "ports", "", "Custom ports to troubleshoot") + + c.help = flags.Usage(help, c.flags) +} + +func (c *cmd) Run(args []string) int { + + if err := c.flags.Parse(args); err != nil { + c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err)) + return 1 + } + + if c.host == "" { + c.UI.Error("-host is required. or set environment variable CONSUL_HTTP_ADDR") + return 1 + } + + if c.ports == "" { + ports.TroubleshootDefaultPorts(c.host) + } else { + ports.TroubleShootCustomPorts(c.host, c.ports) + } + return 0 +} + +func (c *cmd) Synopsis() string { + return synopsis +} + +func (c *cmd) Help() string { + return c.help +} + +const ( + synopsis = "Prints open and closed ports on the Consul server" + help = ` +Usage: consul troubleshoot ports [options] + Checks ports for TCP connectivity. Add the -ports flag to check specific ports or omit the -ports flag to check default ports. + Refer to the following reference for default ports: https://developer.hashicorp.com/consul/docs/install/ports + + consul troubleshoot ports -host localhost + + or + export CONSUL_HTTP_ADDR=localhost + consul troubleshoot ports + + Use the -ports flag to check non-default ports, for example: + consul troubleshoot ports -host localhost -ports 1023,1024 + or + export CONSUL_HTTP_ADDR=localhost + consul troubleshoot ports -ports 1234,8500 +` +) diff --git a/go.mod b/go.mod index 1c4941813b..97d70db9b8 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/hashicorp/consul/api v1.21.1 github.com/hashicorp/consul/envoyextensions v0.2.1 github.com/hashicorp/consul/proto-public v0.3.0 - github.com/hashicorp/consul/sdk v0.14.1 + github.com/hashicorp/consul/sdk v0.15.0 github.com/hashicorp/consul/troubleshoot v0.2.0 github.com/hashicorp/go-bexpr v0.1.2 github.com/hashicorp/go-checkpoint v0.5.0 diff --git a/troubleshoot/go.mod b/troubleshoot/go.mod index 9e64b31ea6..06bebd674a 100644 --- a/troubleshoot/go.mod +++ b/troubleshoot/go.mod @@ -16,6 +16,7 @@ require ( github.com/envoyproxy/go-control-plane/xdsmatcher v0.0.0-20231026140209-dc05a22efe95 github.com/hashicorp/consul/api v1.21.0 github.com/hashicorp/consul/envoyextensions v0.2.1 + github.com/hashicorp/consul/sdk v0.15.0 github.com/stretchr/testify v1.8.4 google.golang.org/protobuf v1.31.0 ) @@ -26,23 +27,24 @@ require ( github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/envoyproxy/protoc-gen-validate v1.0.1 // indirect - github.com/fatih/color v1.13.0 // indirect + github.com/fatih/color v1.14.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-hclog v1.2.1 // indirect + github.com/hashicorp/go-hclog v1.5.0 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect github.com/hashicorp/go-msgpack v0.5.5 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect + github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/go-version v1.2.1 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/serf v0.10.1 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.4.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect @@ -54,6 +56,5 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20230526203410-71b5a4ffd15e // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // indirect google.golang.org/grpc v1.56.3 // indirect - gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/troubleshoot/go.sum b/troubleshoot/go.sum index 2d3f051519..8c88ca79c8 100644 --- a/troubleshoot/go.sum +++ b/troubleshoot/go.sum @@ -89,8 +89,9 @@ github.com/envoyproxy/protoc-gen-validate v1.0.1 h1:kt9FtLiooDc0vbwTLhdg3dyNX1K9 github.com/envoyproxy/protoc-gen-validate v1.0.1/go.mod h1:0vj8bNkYbSTNS2PIyH87KZaeN4x9zpL9Qt8fQC7d+vs= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= -github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= +github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -160,15 +161,16 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= -github.com/hashicorp/consul/sdk v0.13.1 h1:EygWVWWMczTzXGpO93awkHFzfUka6hLYJ0qhETd+6lY= +github.com/hashicorp/consul/sdk v0.15.0 h1:2qK9nDrr4tiJKRoxPGhm6B7xJjLVIQqkjiab2M4aKjU= +github.com/hashicorp/consul/sdk v0.15.0/go.mod h1:r/OmRRPbHOe0yxNahLw7G9x5WG17E1BIECMtCjcPSNo= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8YmtEw= -github.com/hashicorp/go-hclog v1.2.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c= +github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= @@ -187,7 +189,8 @@ github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0S github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= +github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= +github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -210,6 +213,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -217,14 +221,17 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY= @@ -240,14 +247,13 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -269,6 +275,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= @@ -428,6 +435,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -584,8 +592,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/troubleshoot/ports/hostport.go b/troubleshoot/ports/hostport.go new file mode 100644 index 0000000000..e7e9494789 --- /dev/null +++ b/troubleshoot/ports/hostport.go @@ -0,0 +1,6 @@ +package ports + +type hostPort struct { + host string + port string +} diff --git a/troubleshoot/ports/troubleshoot_ports.go b/troubleshoot/ports/troubleshoot_ports.go new file mode 100644 index 0000000000..d885a61cf3 --- /dev/null +++ b/troubleshoot/ports/troubleshoot_ports.go @@ -0,0 +1,50 @@ +package ports + +import ( + "fmt" + "strings" +) + +func TroubleshootDefaultPorts(host string) []string { + // Source - https://developer.hashicorp.com/consul/docs/install/ports + ports := []string{"8600", "8500", "8501", "8502", "8503", "8301", "8302", "8300"} + return troubleshootRun(ports, host) +} + +func TroubleShootCustomPorts(host string, ports string) []string { + portsArr := strings.Split(ports, ",") + return troubleshootRun(portsArr, host) +} + +func troubleshootRun(ports []string, host string) []string { + + resultsChannel := make(chan string) + defer close(resultsChannel) + + var counter = 0 + + for _, port := range ports { + counter += 1 + tcpTroubleShoot := troubleShootTcp{} + port := port + go func() { + err := tcpTroubleShoot.dialPort(&hostPort{host: host, port: port}) + var res string + if err != nil { + res = fmt.Sprintf("TCP: Port %s on %s is closed, unreachable, or the connection timed out.\n", port, host) + } else { + // If no error occurs, the connection was successful, and the port is open. + res = fmt.Sprintf("TCP: Port %s on %s is open.\n", port, host) + } + resultsChannel <- res + }() + } + + resultsArr := make([]string, counter) + for itr := 0; itr < counter; itr++ { + res := <-resultsChannel + fmt.Print(res) + resultsArr[itr] = res + } + return resultsArr +} diff --git a/troubleshoot/ports/troubleshoot_ports_test.go b/troubleshoot/ports/troubleshoot_ports_test.go new file mode 100644 index 0000000000..70111404d9 --- /dev/null +++ b/troubleshoot/ports/troubleshoot_ports_test.go @@ -0,0 +1,52 @@ +package ports + +import ( + "fmt" + "github.com/hashicorp/consul/sdk/testutil" + "github.com/stretchr/testify/require" + "strconv" + "strings" + "testing" +) + +func TestTroubleShootCustom_Ports(t *testing.T) { + // Create a test Consul server + srv1, err := testutil.NewTestServerConfigT(t, nil) + if err != nil { + t.Fatal(err) + } + + results := TroubleShootCustomPorts("127.0.0.1", strings.Join([]string{ + strconv.Itoa(srv1.Config.Ports.HTTP), + strconv.Itoa(srv1.Config.Ports.DNS), + strconv.Itoa(srv1.Config.Ports.HTTPS), + strconv.Itoa(srv1.Config.Ports.GRPC), + strconv.Itoa(srv1.Config.Ports.SerfLan), + strconv.Itoa(srv1.Config.Ports.SerfWan), + strconv.Itoa(srv1.Config.Ports.Server)}, ",")) + expectedResults := []string{ + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.HTTP)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.GRPC)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.HTTPS)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.SerfLan)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.SerfWan)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.DNS)), + fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.Server)), + } + for _, res := range expectedResults { + require.Contains(t, results, res) + } + defer srv1.Stop() +} + +func TestTroubleShootCustom_Ports_Not_Reachable(t *testing.T) { + results := TroubleShootCustomPorts("127.0.0.1", strings.Join([]string{"8777", "8888"}, ",")) + + expectedResults := []string{ + fmt.Sprintf("TCP: Port 8777 on 127.0.0.1 is closed, unreachable, or the connection timed out.\n"), + fmt.Sprintf("TCP: Port 8888 on 127.0.0.1 is closed, unreachable, or the connection timed out.\n"), + } + for _, res := range expectedResults { + require.Contains(t, results, res) + } +} diff --git a/troubleshoot/ports/troubleshoot_protocol.go b/troubleshoot/ports/troubleshoot_protocol.go new file mode 100644 index 0000000000..07a6f4e7ae --- /dev/null +++ b/troubleshoot/ports/troubleshoot_protocol.go @@ -0,0 +1,5 @@ +package ports + +type troubleShootProtocol interface { + dialPort(hostPort *hostPort) error +} diff --git a/troubleshoot/ports/troubleshoot_tcp.go b/troubleshoot/ports/troubleshoot_tcp.go new file mode 100644 index 0000000000..2babdb882b --- /dev/null +++ b/troubleshoot/ports/troubleshoot_tcp.go @@ -0,0 +1,21 @@ +package ports + +import ( + "net" + "time" +) + +type troubleShootTcp struct { +} + +func (tcp *troubleShootTcp) dialPort(hostPort *hostPort) error { + address := net.JoinHostPort(hostPort.host, hostPort.port) + + // Attempt to establish a TCP connection with a timeout. + conn, err := net.DialTimeout("tcp", address, 5*time.Second) + if err != nil { + return err + } + defer conn.Close() + return nil +} diff --git a/website/content/commands/troubleshoot/index.mdx b/website/content/commands/troubleshoot/index.mdx index 0c992aab15..74d9d9cec3 100644 --- a/website/content/commands/troubleshoot/index.mdx +++ b/website/content/commands/troubleshoot/index.mdx @@ -22,6 +22,7 @@ Subcommands: proxy Troubleshoots service mesh issues from the current Envoy instance upstreams Gets upstream Envoy identifiers and IPs configured for the proxy + ports Prints open and closed ports on the Consul server. ``` For more information, examples, and usage about a subcommand, click on the name @@ -29,3 +30,4 @@ of the subcommand in the sidebar or one of the links below: - [proxy](/consul/commands/troubleshoot/proxy) - [upstreams](/consul/commands/troubleshoot/upstreams) +- [ports](/consul/commands/troubleshoot/ports) diff --git a/website/content/commands/troubleshoot/ports.mdx b/website/content/commands/troubleshoot/ports.mdx new file mode 100644 index 0000000000..5a4d5faf50 --- /dev/null +++ b/website/content/commands/troubleshoot/ports.mdx @@ -0,0 +1,47 @@ +--- +layout: commands +page_title: 'Commands: Troubleshoot Ports' +description: >- + The `consul troubleshoot ports` Helps troubleshoot TCP ports by printing if they are open or closed. +--- + +# Consul Troubleshoot Upstreams + +Command: `consul troubleshoot ports` + +The `troubleshoot ports` prints TCP port statuses to help you troubleshoot port connectivity. + +## Usage + +Usage: `consul troubleshoot ports [options]` + +#### Command Options + +- `-host=` - Host name to troubleshoot TCP ports for. You can also set the `CONSUL_HTTP_ADDR` environment variable instead of using the `-host` flag. +- `-ports=` - Specifies a comma-separated list of custom ports to check. + +## Examples + +The following example checks the default ports Consul server uses for TCP connectivity. Note that the `CONSUL_HTTP_ADDR` environment variable is set to `localhost`. As a result, the `-host` flag is not required. +Refer to [Required Ports](/consul/docs/install/ports) for additional information. + +```shell-session +$ export CONSUL_HTTP_ADDR=localhost +$ consul troubleshoot ports +TCP: Port 8501 on localhost is open. +TCP: Port 8502 on localhost is open. +TCP: Port 8503 on localhost is open. +TCP: Port 8302 on localhost is open. +TCP: Port 8300 on localhost is open. +TCP: Port 8600 on localhost is open. +TCP: Port 8301 on localhost is open. +TCP: Port 8500 on localhost is open. +``` + +The following example checks TCP ports status on the `hashicorp.com` host. + +```shell-session +$ consul troubleshoot ports -host hashicorp.com -ports 80,8077 +TCP: Port 80 on hashicorp.com is open. +TCP: Port 8077 on hashicorp.com is closed, unreachable, or the connection timed out. +``` diff --git a/website/data/commands-nav-data.json b/website/data/commands-nav-data.json index ee491e9dfa..7ddd952fc1 100644 --- a/website/data/commands-nav-data.json +++ b/website/data/commands-nav-data.json @@ -546,6 +546,10 @@ { "title": "proxy", "path": "troubleshoot/proxy" + }, + { + "title": "ports", + "path": "troubleshoot/ports" } ] },