From 07c7e71662cf30883e344bf7f9bfedeb9aab1fdc Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sun, 5 Jun 2022 11:48:13 -0400 Subject: [PATCH 01/14] add systemd socket listener activation Signed-off-by: Perry Naseck --- go.mod | 1 + go.sum | 3 +++ web/tls_config.go | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/go.mod b/go.mod index c21563f9..e2eebb5e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/prometheus/exporter-toolkit go 1.17 require ( + github.com/coreos/go-systemd/v22 v22.3.2 github.com/go-kit/log v0.2.1 github.com/pkg/errors v0.9.1 github.com/prometheus/common v0.34.0 diff --git a/go.sum b/go.sum index edf315ea..a2fbd6fb 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -75,6 +77,7 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= diff --git a/web/tls_config.go b/web/tls_config.go index 328c5e0e..455b7eb1 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -22,6 +22,7 @@ import ( "net/http" "path/filepath" + "github.com/coreos/go-systemd/v22/activation" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/pkg/errors" @@ -188,6 +189,23 @@ func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger return Serve(listener, server, tlsConfigPath, logger) } +// UseActivatedSocketAndServe starts the server on the systemd socket activated +// listener. Based on the file tlsConfigPath, TLS or basic auth could be +// enabled. +func UseActivatedSocketAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { + listeners, err := activation.Listeners() + if err != nil { + return err + } + if len(listeners) < 1 { + return errors.New("No socket activation file descriptors found") + } + if len(listeners) > 1 { + return errors.New("More than one socket activation file descriptor found") + } + return Serve(listeners[0], server, tlsConfigPath, logger) +} + // Server starts the server on the given listener. Based on the file // tlsConfigPath, TLS or basic auth could be enabled. func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { From 24b6ef7106bb5fee19458de94d66e581fd5329cb Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sun, 26 Jun 2022 13:56:59 -0400 Subject: [PATCH 02/14] allow listening on multiple listeners Signed-off-by: Perry Naseck --- go.mod | 1 + go.sum | 1 + web/tls_config.go | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index e2eebb5e..1f621d2f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/common v0.34.0 golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed + golang.org/x/sync v0.0.0-20201207232520-09787c993a3a gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index a2fbd6fb..5d7ca7ac 100644 --- a/go.sum +++ b/go.sum @@ -298,6 +298,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/web/tls_config.go b/web/tls_config.go index 455b7eb1..0719ad96 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -27,6 +27,7 @@ import ( "github.com/go-kit/log/level" "github.com/pkg/errors" config_util "github.com/prometheus/common/config" + "golang.org/x/sync/errgroup" "gopkg.in/yaml.v2" ) @@ -178,6 +179,20 @@ func ConfigToTLSConfig(c *TLSStruct) (*tls.Config, error) { return cfg, nil } +// ListenAndServe starts the server on the given listeners. Based on the file +// tlsConfigPath, TLS or basic auth could be enabled. +func ServeMultiple(listeners []net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { + errs := new(errgroup.Group) + for _, l := range listeners { + l := l + errs.Go(func() error { + level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) + return Serve(l, server, tlsConfigPath, logger) + }) + } + return errs.Wait() +} + // ListenAndServe starts the server on the given address. Based on the file // tlsConfigPath, TLS or basic auth could be enabled. func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { @@ -189,10 +204,10 @@ func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger return Serve(listener, server, tlsConfigPath, logger) } -// UseActivatedSocketAndServe starts the server on the systemd socket activated +// UseActivatedSocketsAndServe starts the server on the systemd socket activated // listener. Based on the file tlsConfigPath, TLS or basic auth could be // enabled. -func UseActivatedSocketAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { +func UseActivatedSocketsAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { listeners, err := activation.Listeners() if err != nil { return err @@ -200,17 +215,14 @@ func UseActivatedSocketAndServe(server *http.Server, tlsConfigPath string, logge if len(listeners) < 1 { return errors.New("No socket activation file descriptors found") } - if len(listeners) > 1 { - return errors.New("More than one socket activation file descriptor found") - } - return Serve(listeners[0], server, tlsConfigPath, logger) + return ServeMultiple(listeners, server, tlsConfigPath, logger) } // Server starts the server on the given listener. Based on the file // tlsConfigPath, TLS or basic auth could be enabled. func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { if tlsConfigPath == "" { - level.Info(logger).Log("msg", "TLS is disabled.", "http2", false) + level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String()) return server.Serve(l) } @@ -243,10 +255,10 @@ func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler)) } // Valid TLS config. - level.Info(logger).Log("msg", "TLS is enabled.", "http2", c.HTTPConfig.HTTP2) + level.Info(logger).Log("msg", "TLS is enabled.", "http2", c.HTTPConfig.HTTP2, "address", l.Addr().String()) case errNoTLSConfig: // No TLS config, back to plain HTTP. - level.Info(logger).Log("msg", "TLS is disabled.", "http2", false) + level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String()) return server.Serve(l) default: // Invalid TLS config. From 1a788e5a2651ff811805bd5ac8f52dcf34ddf107 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sat, 13 Aug 2022 15:00:06 -0400 Subject: [PATCH 03/14] use flag struct, support multiple port listeners, support activated systemd listeners, move listener flags implementation from exporter to toolkit Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 27 +++++++++++++--- web/tls_config.go | 70 ++++++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 8cf66ea7..d88b40ad 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -16,11 +16,28 @@ import ( "gopkg.in/alecthomas/kingpin.v2" ) +type FlagStruct struct { + WebListenAddresses *[]string + WebSystemdSocket *bool + WebConfigFile *string +} + // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine) -func AddFlags(a *kingpin.Application) *string { - return a.Flag( - "web.config.file", - "[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.", - ).Default("").String() +func AddFlags(a *kingpin.Application) *FlagStruct { + flags := FlagStruct{ + WebListenAddresses: a.Flag( + "web.listen-address", + "Addresses on which to expose metrics and web interface.", + ).Default(":9100").Strings(), + WebSystemdSocket: kingpin.Flag( + "web.systemd-socket", + "Use systemd socket activation listeners instead of port listeners.", + ).Bool(), + WebConfigFile: a.Flag( + "web.config.file", + "[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.", + ).Default("").String(), + } + return &flags } diff --git a/web/tls_config.go b/web/tls_config.go index 0719ad96..a62342ed 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -27,6 +27,7 @@ import ( "github.com/go-kit/log/level" "github.com/pkg/errors" config_util "github.com/prometheus/common/config" + "github.com/prometheus/exporter-toolkit/web/kingpinflag" "golang.org/x/sync/errgroup" "gopkg.in/yaml.v2" ) @@ -179,48 +180,53 @@ func ConfigToTLSConfig(c *TLSStruct) (*tls.Config, error) { return cfg, nil } -// ListenAndServe starts the server on the given listeners. Based on the file -// tlsConfigPath, TLS or basic auth could be enabled. -func ServeMultiple(listeners []net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { +// ServeMultiple starts the server on the given listeners. The FlagStruct is +// also passed on to Serve. +func ServeMultiple(listeners []net.Listener, server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { errs := new(errgroup.Group) for _, l := range listeners { l := l errs.Go(func() error { - level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) - return Serve(l, server, tlsConfigPath, logger) + return Serve(l, server, flags, logger) }) } return errs.Wait() } -// ListenAndServe starts the server on the given address. Based on the file -// tlsConfigPath, TLS or basic auth could be enabled. -func ListenAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { - listener, err := net.Listen("tcp", server.Addr) - if err != nil { - return err - } - defer listener.Close() - return Serve(listener, server, tlsConfigPath, logger) -} - -// UseActivatedSocketsAndServe starts the server on the systemd socket activated -// listener. Based on the file tlsConfigPath, TLS or basic auth could be -// enabled. -func UseActivatedSocketsAndServe(server *http.Server, tlsConfigPath string, logger log.Logger) error { - listeners, err := activation.Listeners() - if err != nil { - return err - } - if len(listeners) < 1 { - return errors.New("No socket activation file descriptors found") +// ListenAndServe starts the server on addresses given in WebListenAddresses in +// the FlagStruct or instead uses systemd socket activated listeners if +// WebSystemdSocket in the FlagStruct is true. The FlagStruct is also passed on +// to ServeMultiple. +func ListenAndServe(server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { + if *flags.WebSystemdSocket { + level.Info(logger).Log("msg", "Listening on systemd activated listeners instead of port listeners.") + listeners, err := activation.Listeners() + if err != nil { + return err + } + if len(listeners) < 1 { + return errors.New("No socket activation file descriptors found!") + } + return ServeMultiple(listeners, server, flags, logger) + } else { + listeners := make([]net.Listener, 0, len(*flags.WebListenAddresses)) + for _, address := range *flags.WebListenAddresses { + listener, err := net.Listen("tcp", address) + if err != nil { + return err + } + defer listener.Close() + listeners = append(listeners, listener) + } + return ServeMultiple(listeners, server, flags, logger) } - return ServeMultiple(listeners, server, tlsConfigPath, logger) } -// Server starts the server on the given listener. Based on the file -// tlsConfigPath, TLS or basic auth could be enabled. -func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log.Logger) error { +// Server starts the server on the given listener. Based on the file path +// WebConfigFile in the FlagStruct, TLS or basic auth could be enabled. +func Serve(l net.Listener, server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { + level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) + tlsConfigPath := *flags.WebConfigFile if tlsConfigPath == "" { level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String()) return server.Serve(l) @@ -386,6 +392,6 @@ func (tv *tlsVersion) MarshalYAML() (interface{}, error) { // tlsConfigPath, TLS or basic auth could be enabled. // // Deprecated: Use ListenAndServe instead. -func Listen(server *http.Server, tlsConfigPath string, logger log.Logger) error { - return ListenAndServe(server, tlsConfigPath, logger) +func Listen(server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { + return ListenAndServe(server, flags, logger) } From b61e0b93a7fd68122acb1f2aad35133053afea27 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Fri, 19 Aug 2022 22:10:10 -0400 Subject: [PATCH 04/14] new flags formatting and update tests Signed-off-by: Perry Naseck --- web/handler_test.go | 26 ++++++++++++++++++++------ web/kingpinflag/flag.go | 6 +++--- web/tls_config_test.go | 18 +++++++++++++++--- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/web/handler_test.go b/web/handler_test.go index 9bd6a506..a744e3f0 100644 --- a/web/handler_test.go +++ b/web/handler_test.go @@ -18,13 +18,14 @@ import ( "net/http" "sync" "testing" + + "github.com/prometheus/exporter-toolkit/web/kingpinflag" ) // TestBasicAuthCache validates that the cache is working by calling a password // protected endpoint multiple times. func TestBasicAuthCache(t *testing.T) { server := &http.Server{ - Addr: port, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World!")) }), @@ -39,7 +40,12 @@ func TestBasicAuthCache(t *testing.T) { }) go func() { - ListenAndServe(server, "testdata/web_config_users_noTLS.good.yml", testlogger) + flags := kingpinflag.FlagStruct{ + WebListenAddresses: Of([]string{port}), + WebSystemdSocket: Of(false), + WebConfigFile: Of("testdata/web_config_users_noTLS.good.yml"), + } + ListenAndServe(server, &flags, testlogger) close(done) }() @@ -88,7 +94,6 @@ func TestBasicAuthCache(t *testing.T) { // to prevent user enumeration. func TestBasicAuthWithFakepassword(t *testing.T) { server := &http.Server{ - Addr: port, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World!")) }), @@ -103,7 +108,12 @@ func TestBasicAuthWithFakepassword(t *testing.T) { }) go func() { - ListenAndServe(server, "testdata/web_config_users_noTLS.good.yml", testlogger) + flags := kingpinflag.FlagStruct{ + WebListenAddresses: Of([]string{port}), + WebSystemdSocket: Of(false), + WebConfigFile: Of("testdata/web_config_users_noTLS.good.yml"), + } + ListenAndServe(server, &flags, testlogger) close(done) }() @@ -132,7 +142,6 @@ func TestBasicAuthWithFakepassword(t *testing.T) { // TestHTTPHeaders validates that HTTP headers are added correctly. func TestHTTPHeaders(t *testing.T) { server := &http.Server{ - Addr: port, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World!")) }), @@ -147,7 +156,12 @@ func TestHTTPHeaders(t *testing.T) { }) go func() { - ListenAndServe(server, "testdata/web_config_headers.good.yml", testlogger) + flags := kingpinflag.FlagStruct{ + WebListenAddresses: Of([]string{port}), + WebSystemdSocket: Of(false), + WebConfigFile: Of("testdata/web_config_headers.good.yml"), + } + ListenAndServe(server, &flags, testlogger) close(done) }() diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index d88b40ad..50555272 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -17,9 +17,9 @@ import ( ) type FlagStruct struct { - WebListenAddresses *[]string - WebSystemdSocket *bool - WebConfigFile *string + WebListenAddresses *[]string + WebSystemdSocket *bool + WebConfigFile *string } // AddFlags adds the flags used by this package to the Kingpin application. diff --git a/web/tls_config_test.go b/web/tls_config_test.go index 52f74676..6d8a90de 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -28,8 +28,15 @@ import ( "sync" "testing" "time" + + "github.com/prometheus/exporter-toolkit/web/kingpinflag" ) +// Helper for literal FlagStruct +func Of[E any](e E) *E { + return &e +} + var ( port = getPort() testlogger = &testLogger{} @@ -376,7 +383,8 @@ func TestConfigReloading(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - err := Listen(server, badYAMLPath, testlogger) + flagsBadYAMLPath := kingpinflag.FlagStruct{WebConfigFile: Of(badYAMLPath)} + err := Listen(server, &flagsBadYAMLPath, testlogger) recordConnectionError(err) }() @@ -434,7 +442,6 @@ func (test *TestInputs) Test(t *testing.T) { }() server := &http.Server{ - Addr: port, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World!")) }), @@ -446,7 +453,12 @@ func (test *TestInputs) Test(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - err := ListenAndServe(server, test.YAMLConfigPath, testlogger) + flags := kingpinflag.FlagStruct{ + WebListenAddresses: Of([]string{port}), + WebSystemdSocket: Of(false), + WebConfigFile: &test.YAMLConfigPath, + } + err := ListenAndServe(server, &flags, testlogger) recordConnectionError(err) }() From 1b4cfc017f8bb5fc0caf403fa9cd56493a92e2de Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sat, 20 Aug 2022 10:53:55 -0400 Subject: [PATCH 05/14] Address listener flag be more explicit that is repeatable Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 50555272..3146b8da 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -28,7 +28,7 @@ func AddFlags(a *kingpin.Application) *FlagStruct { flags := FlagStruct{ WebListenAddresses: a.Flag( "web.listen-address", - "Addresses on which to expose metrics and web interface.", + "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", ).Default(":9100").Strings(), WebSystemdSocket: kingpin.Flag( "web.systemd-socket", From 67d1deba132efc1cf5c177228f4f15f7c8716fe4 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Sat, 20 Aug 2022 11:09:10 -0400 Subject: [PATCH 06/14] Add sync dep for errgroup Signed-off-by: Perry Naseck --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 4b861131..ebbfca76 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/common v0.37.0 golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd + golang.org/x/sync v0.0.0-20201207232520-09787c993a3a gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 ) From d771011cfac9ac745187dbf2de427bb775fce0da Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Mon, 22 Aug 2022 20:47:19 -0400 Subject: [PATCH 07/14] fix lint Signed-off-by: Perry Naseck --- web/handler_test.go | 18 +++++++++--------- web/tls_config.go | 21 ++++++++++----------- web/tls_config_test.go | 15 +++++++++------ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/web/handler_test.go b/web/handler_test.go index a744e3f0..49734ffb 100644 --- a/web/handler_test.go +++ b/web/handler_test.go @@ -41,9 +41,9 @@ func TestBasicAuthCache(t *testing.T) { go func() { flags := kingpinflag.FlagStruct{ - WebListenAddresses: Of([]string{port}), - WebSystemdSocket: Of(false), - WebConfigFile: Of("testdata/web_config_users_noTLS.good.yml"), + WebListenAddresses: &([]string{port}), + WebSystemdSocket: OfBool(false), + WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), } ListenAndServe(server, &flags, testlogger) close(done) @@ -109,9 +109,9 @@ func TestBasicAuthWithFakepassword(t *testing.T) { go func() { flags := kingpinflag.FlagStruct{ - WebListenAddresses: Of([]string{port}), - WebSystemdSocket: Of(false), - WebConfigFile: Of("testdata/web_config_users_noTLS.good.yml"), + WebListenAddresses: &([]string{port}), + WebSystemdSocket: OfBool(false), + WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), } ListenAndServe(server, &flags, testlogger) close(done) @@ -157,9 +157,9 @@ func TestHTTPHeaders(t *testing.T) { go func() { flags := kingpinflag.FlagStruct{ - WebListenAddresses: Of([]string{port}), - WebSystemdSocket: Of(false), - WebConfigFile: Of("testdata/web_config_headers.good.yml"), + WebListenAddresses: &([]string{port}), + WebSystemdSocket: OfBool(false), + WebConfigFile: OfString("testdata/web_config_headers.good.yml"), } ListenAndServe(server, &flags, testlogger) close(done) diff --git a/web/tls_config.go b/web/tls_config.go index 890abc04..27b549a5 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -205,21 +205,20 @@ func ListenAndServe(server *http.Server, flags *kingpinflag.FlagStruct, logger l return err } if len(listeners) < 1 { - return errors.New("No socket activation file descriptors found!") + return errors.New("no socket activation file descriptors found") } return ServeMultiple(listeners, server, flags, logger) - } else { - listeners := make([]net.Listener, 0, len(*flags.WebListenAddresses)) - for _, address := range *flags.WebListenAddresses { - listener, err := net.Listen("tcp", address) - if err != nil { - return err - } - defer listener.Close() - listeners = append(listeners, listener) + } + listeners := make([]net.Listener, 0, len(*flags.WebListenAddresses)) + for _, address := range *flags.WebListenAddresses { + listener, err := net.Listen("tcp", address) + if err != nil { + return err } - return ServeMultiple(listeners, server, flags, logger) + defer listener.Close() + listeners = append(listeners, listener) } + return ServeMultiple(listeners, server, flags, logger) } // Server starts the server on the given listener. Based on the file path diff --git a/web/tls_config_test.go b/web/tls_config_test.go index 000d1983..46ecc11c 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -33,9 +33,12 @@ import ( "github.com/prometheus/exporter-toolkit/web/kingpinflag" ) -// Helper for literal FlagStruct -func Of[E any](e E) *E { - return &e +// Helpers for literal FlagStruct +func OfBool(i bool) *bool { + return &i +} +func OfString(i string) *string { + return &i } var ( @@ -384,7 +387,7 @@ func TestConfigReloading(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flagsBadYAMLPath := kingpinflag.FlagStruct{WebConfigFile: Of(badYAMLPath)} + flagsBadYAMLPath := kingpinflag.FlagStruct{WebConfigFile: OfString(badYAMLPath)} err := Listen(server, &flagsBadYAMLPath, testlogger) recordConnectionError(err) }() @@ -455,8 +458,8 @@ func (test *TestInputs) Test(t *testing.T) { } }() flags := kingpinflag.FlagStruct{ - WebListenAddresses: Of([]string{port}), - WebSystemdSocket: Of(false), + WebListenAddresses: &([]string{port}), + WebSystemdSocket: OfBool(false), WebConfigFile: &test.YAMLConfigPath, } err := ListenAndServe(server, &flags, testlogger) From 888e3dda233409f2709271882aaacd974826e486 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Wed, 14 Sep 2022 09:02:18 -0400 Subject: [PATCH 08/14] move flag struct to remove dependency on kingpinflag Signed-off-by: Perry Naseck --- web/handler_test.go | 8 +++----- web/kingpinflag/flag.go | 12 ++++-------- web/tls_config.go | 15 ++++++++++----- web/tls_config_test.go | 6 ++---- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/web/handler_test.go b/web/handler_test.go index 49734ffb..6f32c85d 100644 --- a/web/handler_test.go +++ b/web/handler_test.go @@ -18,8 +18,6 @@ import ( "net/http" "sync" "testing" - - "github.com/prometheus/exporter-toolkit/web/kingpinflag" ) // TestBasicAuthCache validates that the cache is working by calling a password @@ -40,7 +38,7 @@ func TestBasicAuthCache(t *testing.T) { }) go func() { - flags := kingpinflag.FlagStruct{ + flags := FlagStruct{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), @@ -108,7 +106,7 @@ func TestBasicAuthWithFakepassword(t *testing.T) { }) go func() { - flags := kingpinflag.FlagStruct{ + flags := FlagStruct{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), @@ -156,7 +154,7 @@ func TestHTTPHeaders(t *testing.T) { }) go func() { - flags := kingpinflag.FlagStruct{ + flags := FlagStruct{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_headers.good.yml"), diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 3146b8da..32852197 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -14,18 +14,14 @@ package kingpinflag import ( "gopkg.in/alecthomas/kingpin.v2" -) -type FlagStruct struct { - WebListenAddresses *[]string - WebSystemdSocket *bool - WebConfigFile *string -} + "github.com/prometheus/exporter-toolkit/web" +) // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine) -func AddFlags(a *kingpin.Application) *FlagStruct { - flags := FlagStruct{ +func AddFlags(a *kingpin.Application) *web.FlagStruct { + flags := web.FlagStruct{ WebListenAddresses: a.Flag( "web.listen-address", "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", diff --git a/web/tls_config.go b/web/tls_config.go index 27b549a5..6fce6ce7 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -27,7 +27,6 @@ import ( "github.com/go-kit/log/level" "github.com/pkg/errors" config_util "github.com/prometheus/common/config" - "github.com/prometheus/exporter-toolkit/web/kingpinflag" "golang.org/x/sync/errgroup" "gopkg.in/yaml.v2" ) @@ -54,6 +53,12 @@ type TLSStruct struct { PreferServerCipherSuites bool `yaml:"prefer_server_cipher_suites"` } +type FlagStruct struct { + WebListenAddresses *[]string + WebSystemdSocket *bool + WebConfigFile *string +} + // SetDirectory joins any relative file paths with dir. func (t *TLSStruct) SetDirectory(dir string) { t.TLSCertPath = config_util.JoinDir(dir, t.TLSCertPath) @@ -182,7 +187,7 @@ func ConfigToTLSConfig(c *TLSStruct) (*tls.Config, error) { // ServeMultiple starts the server on the given listeners. The FlagStruct is // also passed on to Serve. -func ServeMultiple(listeners []net.Listener, server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { +func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagStruct, logger log.Logger) error { errs := new(errgroup.Group) for _, l := range listeners { l := l @@ -197,7 +202,7 @@ func ServeMultiple(listeners []net.Listener, server *http.Server, flags *kingpin // the FlagStruct or instead uses systemd socket activated listeners if // WebSystemdSocket in the FlagStruct is true. The FlagStruct is also passed on // to ServeMultiple. -func ListenAndServe(server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { +func ListenAndServe(server *http.Server, flags *FlagStruct, logger log.Logger) error { if *flags.WebSystemdSocket { level.Info(logger).Log("msg", "Listening on systemd activated listeners instead of port listeners.") listeners, err := activation.Listeners() @@ -223,7 +228,7 @@ func ListenAndServe(server *http.Server, flags *kingpinflag.FlagStruct, logger l // Server starts the server on the given listener. Based on the file path // WebConfigFile in the FlagStruct, TLS or basic auth could be enabled. -func Serve(l net.Listener, server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { +func Serve(l net.Listener, server *http.Server, flags *FlagStruct, logger log.Logger) error { level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) tlsConfigPath := *flags.WebConfigFile if tlsConfigPath == "" { @@ -391,6 +396,6 @@ func (tv *tlsVersion) MarshalYAML() (interface{}, error) { // tlsConfigPath, TLS or basic auth could be enabled. // // Deprecated: Use ListenAndServe instead. -func Listen(server *http.Server, flags *kingpinflag.FlagStruct, logger log.Logger) error { +func Listen(server *http.Server, flags *FlagStruct, logger log.Logger) error { return ListenAndServe(server, flags, logger) } diff --git a/web/tls_config_test.go b/web/tls_config_test.go index 46ecc11c..18bb1e45 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -29,8 +29,6 @@ import ( "sync" "testing" "time" - - "github.com/prometheus/exporter-toolkit/web/kingpinflag" ) // Helpers for literal FlagStruct @@ -387,7 +385,7 @@ func TestConfigReloading(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flagsBadYAMLPath := kingpinflag.FlagStruct{WebConfigFile: OfString(badYAMLPath)} + flagsBadYAMLPath := FlagStruct{WebConfigFile: OfString(badYAMLPath)} err := Listen(server, &flagsBadYAMLPath, testlogger) recordConnectionError(err) }() @@ -457,7 +455,7 @@ func (test *TestInputs) Test(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flags := kingpinflag.FlagStruct{ + flags := FlagStruct{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: &test.YAMLConfigPath, From 379c4b4925a5662638c38285e04c750ba3d13cae Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Wed, 14 Sep 2022 09:04:24 -0400 Subject: [PATCH 09/14] Flag struct add linux only note to socket activation Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 32852197..0abce90d 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -28,7 +28,7 @@ func AddFlags(a *kingpin.Application) *web.FlagStruct { ).Default(":9100").Strings(), WebSystemdSocket: kingpin.Flag( "web.systemd-socket", - "Use systemd socket activation listeners instead of port listeners.", + "Use systemd socket activation listeners instead of port listeners (Linux only).", ).Bool(), WebConfigFile: a.Flag( "web.config.file", From 8ce2a7bfc99e75f06548956074cce7f97cea8b24 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Wed, 14 Sep 2022 09:17:59 -0400 Subject: [PATCH 10/14] AddFlags only show systemd socket option on Linux Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 0abce90d..fc8ad6e5 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -13,6 +13,8 @@ package kingpinflag import ( + "runtime" + "gopkg.in/alecthomas/kingpin.v2" "github.com/prometheus/exporter-toolkit/web" @@ -21,15 +23,19 @@ import ( // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine) func AddFlags(a *kingpin.Application) *web.FlagStruct { + systemdSocket := func() *bool { b := false; return &b }() // Socket activation only available on Linux + if runtime.GOOS == "linux" { + systemdSocket = kingpin.Flag( + "web.systemd-socket", + "Use systemd socket activation listeners instead of port listeners (Linux only).", + ).Bool() + } flags := web.FlagStruct{ WebListenAddresses: a.Flag( "web.listen-address", "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", ).Default(":9100").Strings(), - WebSystemdSocket: kingpin.Flag( - "web.systemd-socket", - "Use systemd socket activation listeners instead of port listeners (Linux only).", - ).Bool(), + WebSystemdSocket: systemdSocket, WebConfigFile: a.Flag( "web.config.file", "[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.", From 75fc99633641777ed1717026c42094b0d8811ed3 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Wed, 14 Sep 2022 20:38:10 -0400 Subject: [PATCH 11/14] AddFlags make default port configurable Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index fc8ad6e5..10e3cbfc 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -14,6 +14,7 @@ package kingpinflag import ( "runtime" + "strconv" "gopkg.in/alecthomas/kingpin.v2" @@ -22,7 +23,7 @@ import ( // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine) -func AddFlags(a *kingpin.Application) *web.FlagStruct { +func AddFlags(a *kingpin.Application, defaultPort uint16) *web.FlagStruct { systemdSocket := func() *bool { b := false; return &b }() // Socket activation only available on Linux if runtime.GOOS == "linux" { systemdSocket = kingpin.Flag( @@ -34,7 +35,7 @@ func AddFlags(a *kingpin.Application) *web.FlagStruct { WebListenAddresses: a.Flag( "web.listen-address", "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", - ).Default(":9100").Strings(), + ).Default(":" + strconv.FormatUint(uint64(defaultPort), 10)).Strings(), WebSystemdSocket: systemdSocket, WebConfigFile: a.Flag( "web.config.file", From d40c3468b3c6e7cf89061c4410490a8f350d7b4f Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Thu, 15 Sep 2022 08:17:56 -0400 Subject: [PATCH 12/14] AddFlags defaultAddress instead of defaultPort Signed-off-by: Perry Naseck --- web/kingpinflag/flag.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 10e3cbfc..777e90a0 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -14,7 +14,6 @@ package kingpinflag import ( "runtime" - "strconv" "gopkg.in/alecthomas/kingpin.v2" @@ -22,8 +21,9 @@ import ( ) // AddFlags adds the flags used by this package to the Kingpin application. -// To use the default Kingpin application, call AddFlags(kingpin.CommandLine) -func AddFlags(a *kingpin.Application, defaultPort uint16) *web.FlagStruct { +// To use the default Kingpin application, call +// AddFlags(kingpin.CommandLine, ":portNum") where portNum is the default port. +func AddFlags(a *kingpin.Application, defaultAddress string) *web.FlagStruct { systemdSocket := func() *bool { b := false; return &b }() // Socket activation only available on Linux if runtime.GOOS == "linux" { systemdSocket = kingpin.Flag( @@ -35,7 +35,7 @@ func AddFlags(a *kingpin.Application, defaultPort uint16) *web.FlagStruct { WebListenAddresses: a.Flag( "web.listen-address", "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", - ).Default(":" + strconv.FormatUint(uint64(defaultPort), 10)).Strings(), + ).Default(defaultAddress).Strings(), WebSystemdSocket: systemdSocket, WebConfigFile: a.Flag( "web.config.file", From 9a526036c59a157ffd58eb87c928acaf2cabd1b8 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Fri, 16 Sep 2022 08:32:19 -0400 Subject: [PATCH 13/14] rename FlagStruct to FlagConfig Signed-off-by: Perry Naseck --- web/handler_test.go | 6 +++--- web/kingpinflag/flag.go | 4 ++-- web/tls_config.go | 18 +++++++++--------- web/tls_config_test.go | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/web/handler_test.go b/web/handler_test.go index 6f32c85d..52f5df8b 100644 --- a/web/handler_test.go +++ b/web/handler_test.go @@ -38,7 +38,7 @@ func TestBasicAuthCache(t *testing.T) { }) go func() { - flags := FlagStruct{ + flags := FlagConfig{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), @@ -106,7 +106,7 @@ func TestBasicAuthWithFakepassword(t *testing.T) { }) go func() { - flags := FlagStruct{ + flags := FlagConfig{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_users_noTLS.good.yml"), @@ -154,7 +154,7 @@ func TestHTTPHeaders(t *testing.T) { }) go func() { - flags := FlagStruct{ + flags := FlagConfig{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: OfString("testdata/web_config_headers.good.yml"), diff --git a/web/kingpinflag/flag.go b/web/kingpinflag/flag.go index 777e90a0..f4caa98e 100644 --- a/web/kingpinflag/flag.go +++ b/web/kingpinflag/flag.go @@ -23,7 +23,7 @@ import ( // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call // AddFlags(kingpin.CommandLine, ":portNum") where portNum is the default port. -func AddFlags(a *kingpin.Application, defaultAddress string) *web.FlagStruct { +func AddFlags(a *kingpin.Application, defaultAddress string) *web.FlagConfig { systemdSocket := func() *bool { b := false; return &b }() // Socket activation only available on Linux if runtime.GOOS == "linux" { systemdSocket = kingpin.Flag( @@ -31,7 +31,7 @@ func AddFlags(a *kingpin.Application, defaultAddress string) *web.FlagStruct { "Use systemd socket activation listeners instead of port listeners (Linux only).", ).Bool() } - flags := web.FlagStruct{ + flags := web.FlagConfig{ WebListenAddresses: a.Flag( "web.listen-address", "Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.", diff --git a/web/tls_config.go b/web/tls_config.go index 6fce6ce7..5e37b09c 100644 --- a/web/tls_config.go +++ b/web/tls_config.go @@ -53,7 +53,7 @@ type TLSStruct struct { PreferServerCipherSuites bool `yaml:"prefer_server_cipher_suites"` } -type FlagStruct struct { +type FlagConfig struct { WebListenAddresses *[]string WebSystemdSocket *bool WebConfigFile *string @@ -185,9 +185,9 @@ func ConfigToTLSConfig(c *TLSStruct) (*tls.Config, error) { return cfg, nil } -// ServeMultiple starts the server on the given listeners. The FlagStruct is +// ServeMultiple starts the server on the given listeners. The FlagConfig is // also passed on to Serve. -func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagStruct, logger log.Logger) error { +func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagConfig, logger log.Logger) error { errs := new(errgroup.Group) for _, l := range listeners { l := l @@ -199,10 +199,10 @@ func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagStr } // ListenAndServe starts the server on addresses given in WebListenAddresses in -// the FlagStruct or instead uses systemd socket activated listeners if -// WebSystemdSocket in the FlagStruct is true. The FlagStruct is also passed on +// the FlagConfig or instead uses systemd socket activated listeners if +// WebSystemdSocket in the FlagConfig is true. The FlagConfig is also passed on // to ServeMultiple. -func ListenAndServe(server *http.Server, flags *FlagStruct, logger log.Logger) error { +func ListenAndServe(server *http.Server, flags *FlagConfig, logger log.Logger) error { if *flags.WebSystemdSocket { level.Info(logger).Log("msg", "Listening on systemd activated listeners instead of port listeners.") listeners, err := activation.Listeners() @@ -227,8 +227,8 @@ func ListenAndServe(server *http.Server, flags *FlagStruct, logger log.Logger) e } // Server starts the server on the given listener. Based on the file path -// WebConfigFile in the FlagStruct, TLS or basic auth could be enabled. -func Serve(l net.Listener, server *http.Server, flags *FlagStruct, logger log.Logger) error { +// WebConfigFile in the FlagConfig, TLS or basic auth could be enabled. +func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger log.Logger) error { level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String()) tlsConfigPath := *flags.WebConfigFile if tlsConfigPath == "" { @@ -396,6 +396,6 @@ func (tv *tlsVersion) MarshalYAML() (interface{}, error) { // tlsConfigPath, TLS or basic auth could be enabled. // // Deprecated: Use ListenAndServe instead. -func Listen(server *http.Server, flags *FlagStruct, logger log.Logger) error { +func Listen(server *http.Server, flags *FlagConfig, logger log.Logger) error { return ListenAndServe(server, flags, logger) } diff --git a/web/tls_config_test.go b/web/tls_config_test.go index 18bb1e45..df022754 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -31,7 +31,7 @@ import ( "time" ) -// Helpers for literal FlagStruct +// Helpers for literal FlagConfig func OfBool(i bool) *bool { return &i } @@ -385,7 +385,7 @@ func TestConfigReloading(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flagsBadYAMLPath := FlagStruct{WebConfigFile: OfString(badYAMLPath)} + flagsBadYAMLPath := FlagConfig{WebConfigFile: OfString(badYAMLPath)} err := Listen(server, &flagsBadYAMLPath, testlogger) recordConnectionError(err) }() @@ -455,7 +455,7 @@ func (test *TestInputs) Test(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flags := FlagStruct{ + flags := FlagConfig{ WebListenAddresses: &([]string{port}), WebSystemdSocket: OfBool(false), WebConfigFile: &test.YAMLConfigPath, From 864ade274ec0f3edab55425cd84c82c1f54523c6 Mon Sep 17 00:00:00 2001 From: Perry Naseck Date: Tue, 20 Sep 2022 22:14:45 -0400 Subject: [PATCH 14/14] FlagConfig fix failing test Signed-off-by: Perry Naseck --- web/tls_config_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/web/tls_config_test.go b/web/tls_config_test.go index 8358dddf..68aa88b4 100644 --- a/web/tls_config_test.go +++ b/web/tls_config_test.go @@ -386,7 +386,11 @@ func TestConfigReloading(t *testing.T) { recordConnectionError(errors.New("Panic starting server")) } }() - flagsBadYAMLPath := FlagConfig{WebConfigFile: OfString(badYAMLPath)} + flagsBadYAMLPath := FlagConfig{ + WebListenAddresses: &([]string{port}), + WebSystemdSocket: OfBool(false), + WebConfigFile: OfString(badYAMLPath), + } err := Listen(server, &flagsBadYAMLPath, testlogger) recordConnectionError(err) }()