Skip to content

Commit

Permalink
use tlsConfig to send webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
titolins committed Sep 20, 2024
1 parent 30c68c3 commit 757f5a5
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions pkg/alertmanager/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,14 @@ import (
"github.com/grafana/mimir/pkg/util/version"
)

var (
ErrInvalidMethod = errors.New("webhook only supports HTTP methods PUT or POST")
)
var ErrInvalidMethod = errors.New("webhook only supports HTTP methods PUT or POST")

type Sender struct {
c *http.Client
log log.Logger
}

func NewSender(log log.Logger) *Sender {
netTransport := &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateFreelyAsClient,
},
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
c := &http.Client{
Timeout: time.Second * 30,
Transport: netTransport,
}
return &Sender{
c: c,
log: log,
}
}
Expand Down Expand Up @@ -86,7 +68,7 @@ func (s *Sender) SendWebhook(ctx context.Context, cmd *alertingReceivers.SendWeb
request.Header.Set(k, v)
}

resp, err := s.c.Do(request)
resp, err := tlsClient(cmd.TLSConfig).Do(request)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,3 +99,25 @@ func (s *Sender) SendWebhook(ctx context.Context, cmd *alertingReceivers.SendWeb
level.Debug(s.log).Log("msg", "Webhook failed", "url", cmd.URL, "statuscode", resp.Status, "body", string(body))
return fmt.Errorf("webhook response status %v", resp.Status)
}

func tlsClient(tlsConfig *tls.Config) *http.Client {
nc := func(tlsConfig *tls.Config) *http.Client {
return &http.Client{
Timeout: time.Second * 30,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
},
}
}

if tlsConfig == nil {
return nc(&tls.Config{Renegotiation: tls.RenegotiateFreelyAsClient})
}

return nc(tlsConfig)
}

0 comments on commit 757f5a5

Please sign in to comment.