From ce392de0a8da463c43edbde34b42c2a89d6f52af Mon Sep 17 00:00:00 2001 From: Hunter Kehoe Date: Sun, 9 Oct 2022 07:50:37 -0600 Subject: [PATCH 1/2] allow empty password in client.yml fixes #374 --- client/config.go | 12 +++---- client/config_test.go | 78 ++++++++++++++++++++++++++++++++++++++++--- cmd/subscribe.go | 11 +++--- 3 files changed, 86 insertions(+), 15 deletions(-) diff --git a/client/config.go b/client/config.go index 2bbe9d5..b2efc1d 100644 --- a/client/config.go +++ b/client/config.go @@ -12,14 +12,14 @@ const ( // Config is the config struct for a Client type Config struct { - DefaultHost string `yaml:"default-host"` - DefaultUser string `yaml:"default-user"` - DefaultPassword string `yaml:"default-password"` - DefaultCommand string `yaml:"default-command"` + DefaultHost string `yaml:"default-host"` + DefaultUser string `yaml:"default-user"` + DefaultPassword *string `yaml:"default-password"` + DefaultCommand string `yaml:"default-command"` Subscribe []struct { Topic string `yaml:"topic"` User string `yaml:"user"` - Password string `yaml:"password"` + Password *string `yaml:"password"` Command string `yaml:"command"` If map[string]string `yaml:"if"` } `yaml:"subscribe"` @@ -30,7 +30,7 @@ func NewConfig() *Config { return &Config{ DefaultHost: DefaultBaseURL, DefaultUser: "", - DefaultPassword: "", + DefaultPassword: nil, DefaultCommand: "", Subscribe: nil, } diff --git a/client/config_test.go b/client/config_test.go index 240d5ac..0a71c3b 100644 --- a/client/config_test.go +++ b/client/config_test.go @@ -12,7 +12,7 @@ func TestConfig_Load(t *testing.T) { filename := filepath.Join(t.TempDir(), "client.yml") require.Nil(t, os.WriteFile(filename, []byte(` default-host: http://localhost -default-user: phil +default-user: philipp default-password: mypass default-command: 'echo "Got the message: $message"' subscribe: @@ -31,14 +31,14 @@ subscribe: conf, err := client.LoadConfig(filename) require.Nil(t, err) require.Equal(t, "http://localhost", conf.DefaultHost) - require.Equal(t, "phil", conf.DefaultUser) - require.Equal(t, "mypass", conf.DefaultPassword) + require.Equal(t, "philipp", conf.DefaultUser) + require.Equal(t, "mypass", *conf.DefaultPassword) require.Equal(t, `echo "Got the message: $message"`, conf.DefaultCommand) require.Equal(t, 4, len(conf.Subscribe)) require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic) require.Equal(t, "", conf.Subscribe[0].Command) require.Equal(t, "phil", conf.Subscribe[0].User) - require.Equal(t, "mypass", conf.Subscribe[0].Password) + require.Equal(t, "mypass", *conf.Subscribe[0].Password) require.Equal(t, "echo-this", conf.Subscribe[1].Topic) require.Equal(t, `echo "Message received: $message"`, conf.Subscribe[1].Command) require.Equal(t, "alerts", conf.Subscribe[2].Topic) @@ -46,3 +46,73 @@ subscribe: require.Equal(t, "high,urgent", conf.Subscribe[2].If["priority"]) require.Equal(t, "defaults", conf.Subscribe[3].Topic) } + +func TestConfig_EmptyPassword(t *testing.T) { + filename := filepath.Join(t.TempDir(), "client.yml") + require.Nil(t, os.WriteFile(filename, []byte(` +default-host: http://localhost +default-user: philipp +default-password: "" +subscribe: + - topic: no-command-with-auth + user: phil + password: "" +`), 0600)) + + conf, err := client.LoadConfig(filename) + require.Nil(t, err) + require.Equal(t, "http://localhost", conf.DefaultHost) + require.Equal(t, "philipp", conf.DefaultUser) + require.Equal(t, "", *conf.DefaultPassword) + require.Equal(t, 1, len(conf.Subscribe)) + require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic) + require.Equal(t, "", conf.Subscribe[0].Command) + require.Equal(t, "phil", conf.Subscribe[0].User) + require.Equal(t, "", *conf.Subscribe[0].Password) +} + +func TestConfig_NullPassword(t *testing.T) { + filename := filepath.Join(t.TempDir(), "client.yml") + require.Nil(t, os.WriteFile(filename, []byte(` +default-host: http://localhost +default-user: philipp +default-password: ~ +subscribe: + - topic: no-command-with-auth + user: phil + password: ~ +`), 0600)) + + conf, err := client.LoadConfig(filename) + require.Nil(t, err) + require.Equal(t, "http://localhost", conf.DefaultHost) + require.Equal(t, "philipp", conf.DefaultUser) + require.Nil(t, conf.DefaultPassword) + require.Equal(t, 1, len(conf.Subscribe)) + require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic) + require.Equal(t, "", conf.Subscribe[0].Command) + require.Equal(t, "phil", conf.Subscribe[0].User) + require.Nil(t, conf.Subscribe[0].Password) +} + +func TestConfig_NoPassword(t *testing.T) { + filename := filepath.Join(t.TempDir(), "client.yml") + require.Nil(t, os.WriteFile(filename, []byte(` +default-host: http://localhost +default-user: philipp +subscribe: + - topic: no-command-with-auth + user: phil +`), 0600)) + + conf, err := client.LoadConfig(filename) + require.Nil(t, err) + require.Equal(t, "http://localhost", conf.DefaultHost) + require.Equal(t, "philipp", conf.DefaultUser) + require.Nil(t, conf.DefaultPassword) + require.Equal(t, 1, len(conf.Subscribe)) + require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic) + require.Equal(t, "", conf.Subscribe[0].Command) + require.Equal(t, "phil", conf.Subscribe[0].User) + require.Nil(t, conf.Subscribe[0].Password) +} diff --git a/cmd/subscribe.go b/cmd/subscribe.go index 51d9de8..d916654 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -175,19 +175,20 @@ func doSubscribe(c *cli.Context, cl *client.Client, conf *client.Config, topic, for filter, value := range s.If { topicOptions = append(topicOptions, client.WithFilter(filter, value)) } - var user, password string + var user string + var password *string if s.User != "" { user = s.User } else if conf.DefaultUser != "" { user = conf.DefaultUser } - if s.Password != "" { + if s.Password != nil { password = s.Password - } else if conf.DefaultPassword != "" { + } else if conf.DefaultPassword != nil { password = conf.DefaultPassword } - if user != "" && password != "" { - topicOptions = append(topicOptions, client.WithBasicAuth(user, password)) + if user != "" && password != nil { + topicOptions = append(topicOptions, client.WithBasicAuth(user, *password)) } subscriptionID := cl.Subscribe(s.Topic, topicOptions...) if s.Command != "" { From b7a18bd1815c4ea82e369fa342ba75b3a77ad3c1 Mon Sep 17 00:00:00 2001 From: Hunter Kehoe Date: Sun, 9 Oct 2022 07:56:39 -0600 Subject: [PATCH 2/2] update release docs --- docs/releases.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/releases.md b/docs/releases.md index 06f15e1..a4fb376 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -8,6 +8,7 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release * Subscriptions can now have a display name ([#370](https://github.com/binwiederhier/ntfy/issues/370), thanks to [@tfheen](https://github.com/tfheen) for reporting) * Bump Go version to Go 18.x ([#422](https://github.com/binwiederhier/ntfy/issues/422)) +* Allow empty passwords in `client.yml` ([#374](https://github.com/binwiederhier/ntfy/issues/374), thanks to [@cyqsimon](https://github.com/cyqsimon) for reporting, and [@wunter8](https://github.com/wunter8) for fixing) **Documentation:**