@@ -221,6 +221,13 @@ func NewEmail(c *config.EmailConfig, t *template.Template, l log.Logger) *Email
221
221
// auth resolves a string of authentication mechanisms.
222
222
func (n * Email ) auth (mechs string ) (smtp.Auth , error ) {
223
223
username := n .conf .AuthUsername
224
+
225
+ // If no username is set, keep going without authentication.
226
+ if n .conf .AuthUsername == "" {
227
+ level .Debug (n .logger ).Log ("msg" , "smtp_auth_username is not configured. Attempting to send email without authenticating" )
228
+ return nil , nil
229
+ }
230
+
224
231
err := & types.MultiError {}
225
232
for _ , mech := range strings .Split (mechs , " " ) {
226
233
switch mech {
@@ -464,11 +471,16 @@ type PagerDuty struct {
464
471
conf * config.PagerdutyConfig
465
472
tmpl * template.Template
466
473
logger log.Logger
474
+ apiV1 string // for tests.
467
475
}
468
476
469
477
// NewPagerDuty returns a new PagerDuty notifier.
470
478
func NewPagerDuty (c * config.PagerdutyConfig , t * template.Template , l log.Logger ) * PagerDuty {
471
- return & PagerDuty {conf : c , tmpl : t , logger : l }
479
+ n := & PagerDuty {conf : c , tmpl : t , logger : l }
480
+ if c .ServiceKey != "" {
481
+ n .apiV1 = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
482
+ }
483
+ return n
472
484
}
473
485
474
486
const (
@@ -533,12 +545,6 @@ func (n *PagerDuty) notifyV1(
533
545
Details : details ,
534
546
}
535
547
536
- apiURL , err := url .Parse ("https://events.pagerduty.com/generic/2010-04-15/create_event.json" )
537
- if err != nil {
538
- return false , err
539
- }
540
- n .conf .URL = & config.URL {apiURL }
541
-
542
548
if eventType == pagerDutyEventTrigger {
543
549
msg .Client = tmpl (n .conf .Client )
544
550
msg .ClientURL = tmpl (n .conf .ClientURL )
@@ -553,7 +559,7 @@ func (n *PagerDuty) notifyV1(
553
559
return false , err
554
560
}
555
561
556
- resp , err := post (ctx , c , n .conf . URL . String () , contentTypeJSON , & buf )
562
+ resp , err := post (ctx , c , n .apiV1 , contentTypeJSON , & buf )
557
563
if err != nil {
558
564
return true , err
559
565
}
@@ -664,7 +670,7 @@ func (n *PagerDuty) Notify(ctx context.Context, as ...*types.Alert) (bool, error
664
670
return false , err
665
671
}
666
672
667
- if n .conf . ServiceKey != "" {
673
+ if n .apiV1 != "" {
668
674
return n .notifyV1 (ctx , c , eventType , key , data , details , as ... )
669
675
}
670
676
return n .notifyV2 (ctx , c , eventType , key , data , details , as ... )
@@ -838,9 +844,10 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
838
844
return false , err
839
845
}
840
846
841
- resp , err := post (ctx , c , n .conf .APIURL .String (), contentTypeJSON , & buf )
847
+ u := n .conf .APIURL .String ()
848
+ resp , err := post (ctx , c , u , contentTypeJSON , & buf )
842
849
if err != nil {
843
- return true , err
850
+ return true , redactURL ( err )
844
851
}
845
852
resp .Body .Close ()
846
853
@@ -927,7 +934,7 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
927
934
928
935
resp , err := post (ctx , c , apiURL .String (), contentTypeJSON , & buf )
929
936
if err != nil {
930
- return true , err
937
+ return true , redactURL ( err )
931
938
}
932
939
933
940
defer resp .Body .Close ()
@@ -1028,7 +1035,7 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
1028
1035
1029
1036
resp , err := c .Do (req .WithContext (ctx ))
1030
1037
if err != nil {
1031
- return true , err
1038
+ return true , redactURL ( err )
1032
1039
}
1033
1040
defer resp .Body .Close ()
1034
1041
@@ -1079,7 +1086,7 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
1079
1086
1080
1087
resp , err := c .Do (req .WithContext (ctx ))
1081
1088
if err != nil {
1082
- return true , err
1089
+ return true , redactURL ( err )
1083
1090
}
1084
1091
defer resp .Body .Close ()
1085
1092
@@ -1308,7 +1315,7 @@ func (n *VictorOps) Notify(ctx context.Context, as ...*types.Alert) (bool, error
1308
1315
1309
1316
resp , err := post (ctx , c , apiURL .String (), contentTypeJSON , buf )
1310
1317
if err != nil {
1311
- return true , err
1318
+ return true , redactURL ( err )
1312
1319
}
1313
1320
1314
1321
defer resp .Body .Close ()
@@ -1396,11 +1403,12 @@ type Pushover struct {
1396
1403
conf * config.PushoverConfig
1397
1404
tmpl * template.Template
1398
1405
logger log.Logger
1406
+ apiURL string // for tests.
1399
1407
}
1400
1408
1401
1409
// NewPushover returns a new Pushover notifier.
1402
1410
func NewPushover (c * config.PushoverConfig , t * template.Template , l log.Logger ) * Pushover {
1403
- return & Pushover {conf : c , tmpl : t , logger : l }
1411
+ return & Pushover {conf : c , tmpl : t , logger : l , apiURL : "https://api.pushover.net/1/messages.json" }
1404
1412
}
1405
1413
1406
1414
// Notify implements the Notifier interface.
@@ -1465,13 +1473,13 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
1465
1473
return false , err
1466
1474
}
1467
1475
1468
- apiURL := "https://api.pushover.net/1/messages.json"
1469
- u , err := url .Parse (apiURL )
1476
+ u , err := url .Parse (n .apiURL )
1470
1477
if err != nil {
1471
1478
return false , err
1472
1479
}
1473
1480
u .RawQuery = parameters .Encode ()
1474
- level .Debug (n .logger ).Log ("msg" , "Sending Pushover message" , "incident" , key , "url" , u .String ())
1481
+ // Don't log the URL as it contains secret data (see #1825).
1482
+ level .Debug (n .logger ).Log ("msg" , "Sending Pushover message" , "incident" , key )
1475
1483
1476
1484
c , err := commoncfg .NewClientFromConfig (* n .conf .HTTPConfig , "pushover" )
1477
1485
if err != nil {
@@ -1480,7 +1488,7 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
1480
1488
1481
1489
resp , err := post (ctx , c , u .String (), "text/plain" , nil )
1482
1490
if err != nil {
1483
- return true , err
1491
+ return true , redactURL ( err )
1484
1492
}
1485
1493
defer resp .Body .Close ()
1486
1494
@@ -1559,6 +1567,16 @@ func hashKey(s string) string {
1559
1567
return fmt .Sprintf ("%x" , h .Sum (nil ))
1560
1568
}
1561
1569
1570
+ // redactURL removes the URL part from an error of *url.Error type.
1571
+ func redactURL (err error ) error {
1572
+ e , ok := err .(* url.Error )
1573
+ if ! ok {
1574
+ return err
1575
+ }
1576
+ e .URL = "<redacted>"
1577
+ return e
1578
+ }
1579
+
1562
1580
func post (ctx context.Context , client * http.Client , url string , bodyType string , body io.Reader ) (* http.Response , error ) {
1563
1581
req , err := http .NewRequest ("POST" , url , body )
1564
1582
if err != nil {
0 commit comments