-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
289 lines (236 loc) · 5.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"syscall"
"time"
uuid "github.com/satori/go.uuid"
yaml "gopkg.in/yaml.v2"
"github.com/spf13/cobra"
"github.com/summerwind/cloudevents-webhook-gateway/config"
"github.com/summerwind/cloudevents-webhook-gateway/proxy"
"github.com/summerwind/cloudevents-webhook-gateway/webhook"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/alertmanager"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/anchoreengine"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/clair"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/dockerhub"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/github"
"github.com/summerwind/cloudevents-webhook-gateway/webhook/slack"
)
var (
VERSION = "0.0.1"
COMMIT = "HEAD"
)
// loadConfig loads the specified configuration file and returns
// config.
func loadConfig(configPath string) (*config.Config, error) {
buf, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
c := config.New()
err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, err
}
return c, nil
}
func newProxyHandler(backend *url.URL, parser webhook.Parser) (*httputil.ReverseProxy, error) {
director := func(req *http.Request) {
// Copy request body
body := req.Body
if body != nil && body != http.NoBody {
var buf bytes.Buffer
_, err := buf.ReadFrom(body)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read request body: %s\n", err)
return
}
err = body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return
}
body = ioutil.NopCloser(&buf)
req.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
}
ce, err := parser.Parse(req)
if err != nil {
fmt.Fprintf(os.Stderr, "parse error: %s\n", err)
return
}
if ce.ID == "" {
id := uuid.NewV4()
if err != nil {
fmt.Fprintf(os.Stderr, "unable to generate event ID: %s\n", err)
return
}
ce.ID = id.String()
}
if ce.Time == nil {
t := time.Now()
ce.Time = &t
}
req.Body = body
req.Host = backend.Host
req.URL.Scheme = backend.Scheme
req.URL.Host = backend.Host
req.URL.Path = backend.Path
req.Header.Set("ce-specversion", "1.0")
req.Header.Set("ce-type", ce.Type)
req.Header.Set("ce-source", ce.Source.String())
req.Header.Set("ce-id", ce.ID)
if ce.Subject != "" {
req.Header.Set("ce-subject", ce.Subject)
}
if ce.Time != nil {
req.Header.Set("ce-time", ce.Time.Format(time.RFC3339))
}
if ce.DataSchema.String() != "" {
req.Header.Set("ce-dataschema", ce.DataSchema.String())
}
if ce.DataContentType != "" {
req.Header.Set("Content-Type", ce.DataContentType)
}
log.Printf("remote_addr:%s event_id:%s event_type:%s source:%s", req.RemoteAddr, ce.ID, ce.Type, ce.Source.String())
}
transport := proxy.NewTransport()
return &httputil.ReverseProxy{Director: director, Transport: transport}, nil
}
// run starts the HTTP server to process authentication.
func run(cmd *cobra.Command, args []string) error {
v, err := cmd.Flags().GetBool("version")
if err != nil {
return err
}
if v {
fmt.Printf("%s (%s)\n", VERSION, COMMIT)
return nil
}
configPath, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
c, err := loadConfig(configPath)
if err != nil {
return err
}
mux := http.NewServeMux()
if c.GitHub.Backend != "" {
backend, err := url.Parse(c.GitHub.Backend)
if err != nil {
return err
}
parser := github.NewParser(c.GitHub.Secret)
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.GitHub.Path, handler)
}
if c.DockerHub.Backend != "" {
backend, err := url.Parse(c.DockerHub.Backend)
if err != nil {
return err
}
parser := dockerhub.NewParser()
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.DockerHub.Path, handler)
}
if c.Alertmanager.Backend != "" {
backend, err := url.Parse(c.Alertmanager.Backend)
if err != nil {
return err
}
parser := alertmanager.NewParser()
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.Alertmanager.Path, handler)
}
if c.AnchoreEngine.Backend != "" {
backend, err := url.Parse(c.AnchoreEngine.Backend)
if err != nil {
return err
}
parser := anchoreengine.NewParser()
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.AnchoreEngine.Path, handler)
}
if c.Clair.Backend != "" {
backend, err := url.Parse(c.Clair.Backend)
if err != nil {
return err
}
parser := clair.NewParser()
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.Clair.Path, handler)
}
if c.Slack.Backend != "" {
backend, err := url.Parse(c.Slack.Backend)
if err != nil {
return err
}
parser := slack.NewParser()
handler, err := newProxyHandler(backend, parser)
if err != nil {
return err
}
mux.Handle(c.Slack.Path, handler)
}
server := &http.Server{
Addr: c.Listen,
Handler: mux,
}
go func() {
if c.TLS.CertFile != "" {
server.ListenAndServeTLS(c.TLS.CertFile, c.TLS.KeyFile)
} else {
server.ListenAndServe()
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM)
<-sigCh
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
return err
}
return nil
}
func main() {
var cmd = &cobra.Command{
Use: "cloudevents-webhook-gateway",
Short: "A gateway that converts webhook requests to CloudEvents",
RunE: run,
SilenceErrors: true,
SilenceUsage: true,
}
cmd.Flags().StringP("config", "c", "config.yml", "Path to the configuration file")
cmd.Flags().BoolP("version", "v", false, "Display version information and exit")
err := cmd.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}