From 7adf7c457b31365915e7c4f7c24a4b05c626d653 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Fri, 3 Dec 2021 10:40:20 +0100 Subject: [PATCH 1/2] Add -S --ssl flag to use SSL (amqps) You still need to pass -P 5671 for SSL port. Unsure how to override the default port depending on if ssl flag is given --- cmd/consume.go | 3 +-- cmd/produce.go | 3 +-- cmd/root.go | 11 +++++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/consume.go b/cmd/consume.go index 3900d51..e918523 100644 --- a/cmd/consume.go +++ b/cmd/consume.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "reflect" - "strconv" "strings" "github.com/oleiade/reflections" @@ -36,8 +35,8 @@ Use comma-separated values for binding the same queue with multiple routing keys cmd.SilenceUsage = true cmd.SilenceErrors = true + uri := getUri() // Dial amqp server - uri := "amqp://" + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost conn, err := amqp.Dial(uri) if err != nil { return fmt.Errorf("connection.open: %v", err) diff --git a/cmd/produce.go b/cmd/produce.go index b11042b..b2abb2c 100644 --- a/cmd/produce.go +++ b/cmd/produce.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "reflect" - "strconv" "time" "github.com/spf13/cobra" @@ -46,7 +45,7 @@ To pass headers and properites, use '--headers' & '--properties' any number of t message = string(bytes) } - uri := "amqp://" + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost + uri := getUri() conn, err := amqp.Dial(uri) if err != nil { return fmt.Errorf("connection.open: %v", err) diff --git a/cmd/root.go b/cmd/root.go index 094acd0..9cba8df 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "strconv" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -18,6 +19,7 @@ var ( vhost string username string password string + ssl bool // exchange options exchange string @@ -34,6 +36,14 @@ var ( durableQueue bool ) +func getUri() string { + var proto string = "amqp://" + if (ssl) { + proto = "amqps://" + } + return proto + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost +} + var valid_properties = map[string]string{ "content-type": "ContentType", "content-encoding": "ContentEncoding", @@ -93,6 +103,7 @@ func commonFlagSet() *pflag.FlagSet { fs.StringVarP(&vhost, "vhost", "v", "/", "specify vhost") fs.StringVarP(&username, "username", "u", "guest", "specify username") fs.StringVarP(&password, "password", "p", "guest", "specify password") + fs.BoolVarP(&ssl, "ssl", "S", false, "use amqps") fs.StringVarP(&exchange, "exchange", "e", "", `exchange name (default "")`) fs.StringVarP(&routingkey, "key", "k", "", `routing key (default "")`) From df544aa8aff681da935efafda06144aab8a9a3d7 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Sat, 4 Dec 2021 10:03:13 +0100 Subject: [PATCH 2/2] Change the default port to amqps when -S given --- cmd/root.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 9cba8df..72ff81a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,6 +40,12 @@ func getUri() string { var proto string = "amqp://" if (ssl) { proto = "amqps://" + // Change the port from amqp default to amqps default. + // not sure how to check if -P flag was given by the user + // so the perverse situtation where amqps runs on port 5672 would not work sorry + if (port == 5672) { + port = 5671 + } } return proto + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost }