Fix a bunch of unhandled errors (#1501)

...as discovered by "gosec" (many other ones reported, but not all make
a lot of sense to fix).

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2018-08-05 15:38:25 +02:00 committed by Max Leonard Inden
parent 45567a804d
commit c07352015e
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
13 changed files with 70 additions and 33 deletions

View File

@ -59,8 +59,8 @@ func init() {
numReceivedAlerts.WithLabelValues("firing")
numReceivedAlerts.WithLabelValues("resolved")
prometheus.Register(numReceivedAlerts)
prometheus.Register(numInvalidAlerts)
prometheus.MustRegister(numReceivedAlerts)
prometheus.MustRegister(numInvalidAlerts)
}
var corsHeaders = map[string]string{

View File

@ -49,7 +49,10 @@ func (c *checkConfigCmd) checkConfig(ctx *kingpin.ParseContext) error {
func CheckConfig(args []string) error {
if len(args) == 0 {
stat, _ := os.Stdin.Stat()
stat, err := os.Stdin.Stat()
if err != nil {
kingpin.Fatalf("Failed to stat standard input: %v", err)
}
if (stat.Mode() & os.ModeCharDevice) != 0 {
kingpin.Fatalf("Failed to read from standard input")
}

View File

@ -54,8 +54,7 @@ func (formatter *ExtendedFormatter) FormatSilences(silences []types.Silence) err
silence.Comment,
)
}
w.Flush()
return nil
return w.Flush()
}
func (formatter *ExtendedFormatter) FormatAlerts(alerts []*client.ExtendedAlert) error {
@ -73,8 +72,7 @@ func (formatter *ExtendedFormatter) FormatAlerts(alerts []*client.ExtendedAlert)
alert.GeneratorURL,
)
}
w.Flush()
return nil
return w.Flush()
}
func (formatter *ExtendedFormatter) FormatConfig(status *client.ServerStatus) error {

View File

@ -52,8 +52,7 @@ func (formatter *SimpleFormatter) FormatSilences(silences []types.Silence) error
silence.Comment,
)
}
w.Flush()
return nil
return w.Flush()
}
func (formatter *SimpleFormatter) FormatAlerts(alerts []*client.ExtendedAlert) error {
@ -69,8 +68,7 @@ func (formatter *SimpleFormatter) FormatAlerts(alerts []*client.ExtendedAlert) e
alert.Annotations["summary"],
)
}
w.Flush()
return nil
return w.Flush()
}
func (formatter *SimpleFormatter) FormatConfig(status *client.ServerStatus) error {

View File

@ -148,7 +148,9 @@ func (c *silenceQueryCmd) query(ctx *kingpin.ParseContext) error {
if !found {
return errors.New("unknown output formatter")
}
formatter.FormatSilences(displaySilences)
if err := formatter.FormatSilences(displaySilences); err != nil {
return fmt.Errorf("error formatting silences: %v", err)
}
}
return nil
}

View File

@ -118,7 +118,9 @@ func (c *silenceUpdateCmd) update(ctx *kingpin.ParseContext) error {
if !found {
return fmt.Errorf("unknown output formatter")
}
formatter.FormatSilences(updatedSilences)
if err := formatter.FormatSilences(updatedSilences); err != nil {
return fmt.Errorf("error formatting silences: %v", err)
}
}
return nil
}

View File

@ -138,7 +138,10 @@ type httpStatusAPI struct {
func (h *httpStatusAPI) Get(ctx context.Context) (*ServerStatus, error) {
u := h.client.URL(epStatus, nil)
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
_, body, err := h.client.Do(ctx, req)
if err != nil {
@ -207,7 +210,10 @@ func (h *httpAlertAPI) List(ctx context.Context, filter, receiver string, silenc
params.Add("receiver", receiver)
u.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
_, body, err := h.client.Do(ctx, req)
if err != nil {
@ -228,9 +234,12 @@ func (h *httpAlertAPI) Push(ctx context.Context, alerts ...Alert) error {
return err
}
req, _ := http.NewRequest(http.MethodPost, u.String(), &buf)
req, err := http.NewRequest(http.MethodPost, u.String(), &buf)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
_, _, err := h.client.Do(ctx, req)
_, _, err = h.client.Do(ctx, req)
return err
}
@ -260,7 +269,10 @@ func (h *httpSilenceAPI) Get(ctx context.Context, id string) (*types.Silence, er
"id": id,
})
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
_, body, err := h.client.Do(ctx, req)
if err != nil {
@ -278,9 +290,12 @@ func (h *httpSilenceAPI) Expire(ctx context.Context, id string) error {
"id": id,
})
req, _ := http.NewRequest(http.MethodDelete, u.String(), nil)
req, err := http.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
_, _, err := h.client.Do(ctx, req)
_, _, err = h.client.Do(ctx, req)
return err
}
@ -292,7 +307,10 @@ func (h *httpSilenceAPI) Set(ctx context.Context, sil types.Silence) (string, er
return "", err
}
req, _ := http.NewRequest(http.MethodPost, u.String(), &buf)
req, err := http.NewRequest(http.MethodPost, u.String(), &buf)
if err != nil {
return "", fmt.Errorf("error creating request: %v", err)
}
_, body, err := h.client.Do(ctx, req)
if err != nil {
@ -315,7 +333,10 @@ func (h *httpSilenceAPI) List(ctx context.Context, filter string) ([]*types.Sile
}
u.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
_, body, err := h.client.Do(ctx, req)
if err != nil {

View File

@ -267,7 +267,9 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), *settleTimeout)
defer func() {
cancel()
peer.Leave(10 * time.Second)
if err := peer.Leave(10 * time.Second); err != nil {
level.Warn(logger).Log("msg", "unable to leave gossip mesh", "err", err)
}
}()
go peer.Settle(ctx, *gossipInterval*10)
}

View File

@ -22,7 +22,7 @@ import (
)
func main() {
http.ListenAndServe(":5001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
@ -33,5 +33,5 @@ func main() {
panic(err)
}
log.Println(buf.String())
}))
})))
}

View File

@ -116,7 +116,9 @@ func (ih *Inhibitor) Run() {
runCancel()
})
g.Run()
if err := g.Run(); err != nil {
level.Warn(ih.logger).Log("msg", "error running inhibitor", "err", err)
}
}
// Stop the Inhibitor's background processing.

View File

@ -989,7 +989,10 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return true, err
}
level.Debug(n.logger).Log("msg", "response: "+string(body), "incident", key)
if resp.StatusCode != 200 {

View File

@ -86,9 +86,9 @@ func init() {
notificationLatencySeconds.WithLabelValues("webhook")
notificationLatencySeconds.WithLabelValues("victorops")
prometheus.Register(numNotifications)
prometheus.Register(numFailedNotifications)
prometheus.Register(notificationLatencySeconds)
prometheus.MustRegister(numNotifications)
prometheus.MustRegister(numFailedNotifications)
prometheus.MustRegister(notificationLatencySeconds)
}
type notifierConfig interface {

View File

@ -301,16 +301,22 @@ func (am *Alertmanager) Start() {
// Terminate kills the underlying Alertmanager process and remove intermediate
// data.
func (am *Alertmanager) Terminate() {
syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM)
if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM); err != nil {
am.t.Fatalf("Error sending SIGTERM to Alertmanager process: %v", err)
}
}
// Reload sends the reloading signal to the Alertmanager process.
func (am *Alertmanager) Reload() {
syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP)
if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP); err != nil {
am.t.Fatalf("Error sending SIGHUP to Alertmanager process: %v", err)
}
}
func (am *Alertmanager) cleanup() {
os.RemoveAll(am.confFile.Name())
if err := os.RemoveAll(am.confFile.Name()); err != nil {
am.t.Errorf("Error removing test config file %q: %v", am.confFile.Name(), err)
}
}
// Push declares alerts that are to be pushed to the Alertmanager