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 e5c2810 commit 91ebee0
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions pkg/alertmanager/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,11 @@ var (
)

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 +70,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 +101,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 91ebee0

Please sign in to comment.