-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
107 lines (92 loc) · 3.14 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
/*
Copyright 2024 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/chainguard-dev/clog"
_ "github.com/chainguard-dev/clog/gcp/init"
"github.com/chainguard-dev/terraform-infra-common/pkg/httpmetrics"
mce "github.com/chainguard-dev/terraform-infra-common/pkg/httpmetrics/cloudevents"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/go-github/v60/github"
"github.com/kelseyhightower/envconfig"
)
type envConfig struct {
Port int `envconfig:"PORT" default:"8080" required:"true"`
IngressURI string `envconfig:"EVENT_INGRESS_URI" required:"true"`
WebhookSecret string `envconfig:"WEBHOOK_SECRET" required:"true"`
}
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
clog.Fatalf("failed to process env var: %s", err)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
go httpmetrics.ServeMetrics()
defer httpmetrics.SetupTracer(ctx)()
ceclient, err := mce.NewClientHTTP(mce.WithTarget(ctx, env.IngressURI)...)
if err != nil {
clog.FatalContextf(ctx, "failed to create cloudevents client: %v", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := clog.FromContext(ctx)
defer r.Body.Close()
// https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries
payload, err := github.ValidatePayload(r, []byte(env.WebhookSecret))
if err != nil {
log.Errorf("failed to verify webhook: %v", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "failed to verify webhook: %v", err)
return
}
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#delivery-headers
t := github.WebHookType(r)
if t == "" {
log.Errorf("missing X-GitHub-Event header")
w.WriteHeader(http.StatusBadRequest)
return
}
t = "dev.chainguard.github." + t
log = log.With("event-type", t)
log.Debugf("forwarding event: %s", t)
event := cloudevents.NewEvent()
event.SetType(t)
event.SetSource(r.Host)
// TODO: Extract organization and repo to set in subject, for better filtering.
// event.SetSubject(fmt.Sprintf("%s/%s", org, repo))
if err := event.SetData(cloudevents.ApplicationJSON, struct {
When time.Time `json:"when"`
Body json.RawMessage `json:"body"`
}{
When: time.Now(),
Body: payload,
}); err != nil {
log.Errorf("failed to set data: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
const retryDelay = 10 * time.Millisecond
const maxRetry = 3
rctx := cloudevents.ContextWithRetriesExponentialBackoff(context.WithoutCancel(ctx), retryDelay, maxRetry)
if ceresult := ceclient.Send(rctx, event); cloudevents.IsUndelivered(ceresult) || cloudevents.IsNACK(ceresult) {
log.Errorf("Failed to deliver event: %v", ceresult)
w.WriteHeader(http.StatusInternalServerError)
}
log.Debugf("event forwarded")
})
srv := &http.Server{
Addr: fmt.Sprintf(":%d", env.Port),
ReadHeaderTimeout: 10 * time.Second,
}
clog.FatalContextf(ctx, "ListenAndServe: %v", srv.ListenAndServe())
}