@@ -18,6 +18,7 @@ import (
18
18
"bytes"
19
19
"context"
20
20
"crypto/tls"
21
+ "encoding/base64"
21
22
"encoding/json"
22
23
"fmt"
23
24
"io"
@@ -34,6 +35,8 @@ type WebhookOutput struct {
34
35
errorChan chan error
35
36
eventChan chan event.Event
36
37
url string
38
+ username string
39
+ password string
37
40
}
38
41
39
42
func New (options ... WebhookOptionFunc ) * WebhookOutput {
@@ -78,7 +81,7 @@ func (w *WebhookOutput) Start() error {
78
81
return
79
82
}
80
83
// TODO: error handle
81
- err := SendWebhook (& evt , w . url )
84
+ err := w . SendWebhook (& evt )
82
85
if err != nil {
83
86
logger .Errorf ("ERROR: %s" , err )
84
87
}
@@ -87,32 +90,42 @@ func (w *WebhookOutput) Start() error {
87
90
return nil
88
91
}
89
92
90
- func SendWebhook (e * event.Event , url string ) error {
93
+ func basicAuth (username , password string ) string {
94
+ auth := username + ":" + password
95
+ return "Basic " + base64 .StdEncoding .EncodeToString ([]byte (auth ))
96
+ }
97
+
98
+ func (w * WebhookOutput ) SendWebhook (e * event.Event ) error {
91
99
logger := logging .GetLogger ()
92
- logger .Infof ("sending event %s to %s" , e .Type , url )
100
+ logger .Infof ("sending event %s to %s" , e .Type , w . url )
93
101
data , err := json .Marshal (e )
94
102
if err != nil {
95
103
return fmt .Errorf ("%s" , err )
96
104
}
97
105
// Setup request
98
106
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
99
107
defer cancel ()
100
- req , err := http .NewRequestWithContext (ctx , http .MethodPost , url , bytes .NewReader (data ))
108
+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , w . url , bytes .NewReader (data ))
101
109
if err != nil {
102
110
return fmt .Errorf ("%s" , err )
103
111
}
104
112
req .Header .Add ("Content-Type" , "application/json" )
105
113
req .Header .Add ("User-Agent" , fmt .Sprintf ("Snek/%s" , version .GetVersionString ()))
114
+
115
+ // Setup authorization
116
+ if w .username != "" && w .password != "" {
117
+ req .Header .Add ("Authorization" , basicAuth (w .username , w .password ))
118
+ }
106
119
// Setup custom transport to ignore self-signed SSL
107
120
defaultTransport := http .DefaultTransport .(* http.Transport )
108
121
customTransport := & http.Transport {
109
- Proxy : defaultTransport .Proxy ,
110
- DialContext : defaultTransport .DialContext ,
111
- MaxIdleConns : defaultTransport .MaxIdleConns ,
112
- IdleConnTimeout : defaultTransport .IdleConnTimeout ,
122
+ Proxy : defaultTransport .Proxy ,
123
+ DialContext : defaultTransport .DialContext ,
124
+ MaxIdleConns : defaultTransport .MaxIdleConns ,
125
+ IdleConnTimeout : defaultTransport .IdleConnTimeout ,
113
126
ExpectContinueTimeout : defaultTransport .ExpectContinueTimeout ,
114
- TLSHandshakeTimeout : defaultTransport .TLSHandshakeTimeout ,
115
- TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
127
+ TLSHandshakeTimeout : defaultTransport .TLSHandshakeTimeout ,
128
+ TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
116
129
}
117
130
client := & http.Client {Transport : customTransport }
118
131
// Send payload
@@ -127,7 +140,7 @@ func SendWebhook(e *event.Event, url string) error {
127
140
defer resp .Body .Close ()
128
141
129
142
logger .Infof ("sent: %s, payload: %s, body: %s, response: %s, status: %d" ,
130
- url ,
143
+ w . url ,
131
144
string (data ),
132
145
string (respBody ),
133
146
resp .Status ,
0 commit comments