-
Notifications
You must be signed in to change notification settings - Fork 413
/
webhook.go
116 lines (101 loc) · 3.44 KB
/
webhook.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
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webhook
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/triggers/pkg/interceptors"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
"go.uber.org/zap"
)
const (
// Timeout for outgoing requests to interceptor services
interceptorTimeout = 5 * time.Second
// the incoming request URL is passed through to the webhook in this header.
webhookURLHeader = "EventListener-Request-URL"
)
type Interceptor struct {
HTTPClient *http.Client
TriggerNamespace string
Logger *zap.SugaredLogger
Webhook *triggersv1.WebhookInterceptor
}
func NewInterceptor(wh *triggersv1.WebhookInterceptor, c *http.Client, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
timeoutClient := &http.Client{
Transport: c.Transport,
Timeout: interceptorTimeout,
}
return &Interceptor{
HTTPClient: timeoutClient,
TriggerNamespace: ns,
Logger: l,
Webhook: wh,
}
}
func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, error) {
u, err := getURI(w.Webhook, w.TriggerNamespace) // TODO: Cache this result or do this on initialization
if err != nil {
return nil, err
}
request.Header.Set(webhookURLHeader, request.URL.String())
request.URL = u
request.Host = u.Host
addInterceptorHeaders(request.Header, w.Webhook.Header)
resp, err := w.HTTPClient.Do(request)
if err != nil {
return resp, err
}
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return resp, errors.New("failed to parse response body")
}
return resp, fmt.Errorf("request rejected; status: %s; message: %s", resp.Status, respBody)
}
return resp, err
}
// getURI retrieves the ObjectReference to URI.
func getURI(webhook *triggersv1.WebhookInterceptor, ns string) (*url.URL, error) {
// TODO: This should work for any Addressable.
// Use something like https://github.com/knative/eventing-contrib/blob/7c0fc5cfa8bd44da0767d9e7b250264ea6eb7d8d/pkg/controller/sinks/sinks.go#L32
switch {
case webhook.URL != nil:
return webhook.URL.URL(), nil
case webhook.ObjectRef.Kind == "Service" && webhook.ObjectRef.APIVersion == "v1":
// TODO: Also assuming port 80 and http here. Use DNS/or the env vars?
if webhook.ObjectRef.Namespace != "" {
ns = webhook.ObjectRef.Namespace
}
return url.Parse(fmt.Sprintf("http://%s.%s.svc/", webhook.ObjectRef.Name, ns))
default:
return nil, errors.New("invalid objRef")
}
}
func addInterceptorHeaders(header http.Header, headerParams []pipelinev1.Param) {
// This clobbers any matching headers
for _, param := range headerParams {
if param.Value.Type == pipelinev1.ParamTypeString {
header.Set(param.Name, param.Value.StringVal)
} else {
header.Del(param.Name)
for _, v := range param.Value.ArrayVal {
header.Add(param.Name, v)
}
}
}
}