generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
135 lines (116 loc) · 3.23 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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/xmidt-org/webhook-schema"
listener "github.com/xmidt-org/wrp-listener"
)
type eventListener struct {
l *listener.Listener
}
func (el *eventListener) ServeHTTP(w http.ResponseWriter, r *http.Request) {
token, err := el.l.Tokenize(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Println("Got a request, but it was not authorized.")
return
}
err = el.l.Authorize(r, token)
if err != nil {
fmt.Println("Got a request, but it was not authorized.")
w.WriteHeader(http.StatusUnauthorized)
return
}
body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
fmt.Println("Got a request, but it had no body.")
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println(string(body))
w.WriteHeader(http.StatusOK)
}
func main() {
receiverURL := strings.TrimSpace(os.Getenv("WEBHOOK_TARGET"))
webhookURL := strings.TrimSpace(os.Getenv("WEBHOOK_URL"))
localAddress := strings.TrimSpace(os.Getenv("WEBHOOK_LISTEN_ADDR"))
certFile := strings.TrimSpace(os.Getenv("WEBHOOK_LISTEN_CERT_FILE"))
keyFile := strings.TrimSpace(os.Getenv("WEBHOOK_LISTEN_KEY_FILE"))
contentType := strings.TrimSpace(os.Getenv("WEBHOOK_CONTENT_TYPE"))
events := strings.TrimSpace(os.Getenv("WEBHOOK_EVENTS"))
useTLS := false
if certFile != "" && keyFile != "" {
useTLS = true
}
fmt.Println("WEBHOOK_TARGET : ", receiverURL)
fmt.Println("WEBHOOK_URL : ", webhookURL)
fmt.Println("WEBHOOK_LISTEN_ADDR : ", localAddress)
fmt.Println("WEBHOOK_LISTEN_CERT_FILE: ", certFile)
fmt.Println("WEBHOOK_LISTEN_KEY_FILE : ", keyFile)
fmt.Println("WEBHOOK_CONTENT_TYPE : ", contentType)
fmt.Printf(" use TLS: %t\n", useTLS)
sharedSecrets := strings.Split(os.Getenv("WEBHOOK_SHARED_SECRETS"), ",")
for i := range sharedSecrets {
sharedSecrets[i] = strings.TrimSpace(sharedSecrets[i])
}
// Create the listener.
whl, err := listener.New(webhookURL,
&webhook.Registration{
Config: webhook.DeliveryConfig{
ReceiverURL: receiverURL,
ContentType: contentType,
},
Events: []string{events},
Duration: webhook.CustomDuration(15 * time.Second),
},
listener.DecorateRequest(listener.DecoratorFunc(
func(r *http.Request) error {
if os.Getenv("WEBHOOK_BEARER_TOKEN") == "" {
return nil
}
r.Header.Set("Authorization", "Bearer "+os.Getenv("WEBHOOK_BEARER_TOKEN"))
return nil
},
)),
listener.AcceptSHA1(),
listener.Once(),
listener.AcceptedSecrets(sharedSecrets...),
)
if err != nil {
panic(err)
}
fmt.Println(whl.String())
el := eventListener{
l: whl,
}
go func() {
if useTLS {
err := http.ListenAndServeTLS(localAddress, certFile, keyFile, &el) // nolint: gosec
if err != nil {
panic(err)
}
} else {
err := http.ListenAndServe(localAddress, &el) // nolint: gosec
if err != nil {
panic(err)
}
}
}()
// Register for webhook events, using the secret "foobar" as the shared
// secret.
err = whl.Register(context.Background(), sharedSecrets[0])
if err != nil {
panic(err)
}
for {
time.Sleep(1 * time.Minute)
}
}