This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
140 lines (118 loc) · 3.43 KB
/
endpoint.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
// SPDX-FileCopyrightText: 2023 Risk.Ident GmbH <contact@riskident.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"github.com/gin-gonic/gin"
)
type endpointHandler struct {
Endpoint
method string
endpointPath string
client *http.Client
githubWebhookToken string
}
func (e endpointHandler) handleRequest(c *gin.Context) {
b, ok := e.readBody(c)
if !ok {
return
}
if !e.handleRequestAuth(c, b) {
return
}
resp, err := e.doForwardedReq(c, b)
if err != nil {
c.Error(err)
c.AbortWithError(http.StatusBadGateway, err)
return
}
e.forwardResponse(c, resp)
}
func (e endpointHandler) readBody(c *gin.Context) ([]byte, bool) {
defer c.Request.Body.Close()
b, err := io.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithError(http.StatusInternalServerError,
fmt.Errorf("read request body: %w", err))
return nil, false
}
return b, true
}
func (e endpointHandler) doForwardedReq(c *gin.Context, bodyBytes []byte) (*http.Response, error) {
req, err := e.newForwardedReq(c, bytes.NewReader(bodyBytes))
if err != nil {
return nil, err
}
resp, err := e.client.Do(req)
if err != nil {
return nil, err
}
slog.Info("Forwarded request",
"endpoint", e.endpointPath,
"method", e.method,
"forwardTo", req.URL.String(),
"status", resp.StatusCode,
"requestType", req.Header.Get("Content-Type"),
"responseType", resp.Header.Get("Content-Type"))
return resp, err
}
func (e endpointHandler) newForwardedReq(c *gin.Context, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(e.method, e.ForwardTo.String(), body)
if err != nil {
return nil, err
}
req.Header = c.Request.Header.Clone()
return req, nil
}
func (e endpointHandler) forwardResponse(c *gin.Context, resp *http.Response) {
c.Status(resp.StatusCode)
headerWriter := c.Writer.Header()
for key, values := range resp.Header {
for _, value := range values {
headerWriter.Add(key, value)
}
}
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
c.Error(err)
c.AbortWithError(http.StatusBadGateway, err)
}
}
func (e endpointHandler) handleRequestAuth(c *gin.Context, bodyBytes []byte) bool {
if e.githubWebhookToken == "" {
return true
}
defer c.Request.Body.Close()
signature := c.Request.Header.Get("X-Hub-Signature-256")
if signature == "" {
c.AbortWithError(http.StatusUnauthorized, errors.New("missing header: X-Hub-Signature-256"))
return false
}
if !isValidGitHubWebhookSignature(e.githubWebhookToken, signature, bodyBytes) {
c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("signature did not match: %s", signature))
return false
}
slog.Debug("Request passed X-Hub-Signature-256 verification",
"endpoint", e.endpointPath,
"method", e.method,
"signature", signature)
return true
}