From 04642c05d1058ec206960078c7e4a573dba918a0 Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Thu, 2 Jan 2025 18:10:41 +0100 Subject: [PATCH] copyblocks: Satisfy and enable gosec rule G114 (#10330) Signed-off-by: Arve Knudsen --- .golangci.yml | 1 + pkg/mimirtool/commands/alerts.go | 7 ++++++- pkg/mimirtool/commands/loadgen.go | 8 ++++++-- tools/copyblocks/main.go | 8 ++++++-- tools/trafficdump/main.go | 7 ++++++- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 3320bf91171..a55f34a536e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -65,6 +65,7 @@ linters-settings: gosec: includes: - G108 + - G114 run: timeout: 10m diff --git a/pkg/mimirtool/commands/alerts.go b/pkg/mimirtool/commands/alerts.go index 170424f008d..1dcd16f5156 100644 --- a/pkg/mimirtool/commands/alerts.go +++ b/pkg/mimirtool/commands/alerts.go @@ -351,7 +351,12 @@ func (a *AlertCommand) verifyConfig(_ *kingpin.ParseContext) error { )) go func() { - log.Fatal(http.ListenAndServe(":9090", nil)) + server := http.Server{ + Addr: ":9090", + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + log.Fatal(server.ListenAndServe()) }() ctx := context.Background() diff --git a/pkg/mimirtool/commands/loadgen.go b/pkg/mimirtool/commands/loadgen.go index 9e56827399f..113ff593da7 100644 --- a/pkg/mimirtool/commands/loadgen.go +++ b/pkg/mimirtool/commands/loadgen.go @@ -121,8 +121,12 @@ func (c *LoadgenCommand) run(_ *kingpin.ParseContext) error { http.Handle("/metrics", promhttp.Handler()) go func() { - err := http.ListenAndServe(c.metricsListenAddress, nil) - if err != nil { + server := http.Server{ + Addr: c.metricsListenAddress, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + if err := server.ListenAndServe(); err != nil { logrus.WithError(err).Errorln("metrics listener failed") } }() diff --git a/tools/copyblocks/main.go b/tools/copyblocks/main.go index 7f025a3785f..795794916e9 100644 --- a/tools/copyblocks/main.go +++ b/tools/copyblocks/main.go @@ -167,8 +167,12 @@ func main() { go func() { level.Info(logger).Log("msg", "HTTP server listening on "+cfg.httpListen) http.Handle("/metrics", promhttp.Handler()) - err := http.ListenAndServe(cfg.httpListen, nil) - if err != nil { + server := &http.Server{ + Addr: cfg.httpListen, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + if err := server.ListenAndServe(); err != nil { level.Error(logger).Log("msg", "failed to start HTTP server") os.Exit(1) } diff --git a/tools/trafficdump/main.go b/tools/trafficdump/main.go index 5a8ac699575..dfc68539dbf 100644 --- a/tools/trafficdump/main.go +++ b/tools/trafficdump/main.go @@ -51,7 +51,12 @@ func main() { if *httpServer != "" { go func() { log.Println("HTTP server running on", *httpServer) - log.Println(http.ListenAndServe(*httpServer, nil)) + server := &http.Server{ + Addr: *httpServer, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + log.Println(server.ListenAndServe()) }() }