-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathevent_receiver.go
318 lines (267 loc) · 10.1 KB
/
event_receiver.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Copyright 2020 The Knative 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 channel
import (
"context"
"errors"
"fmt"
nethttp "net/http"
"time"
duckv1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/pkg/apis/feature"
"knative.dev/eventing/pkg/auth"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/protocol/http"
"go.uber.org/zap"
"knative.dev/pkg/network"
"knative.dev/eventing/pkg/kncloudevents"
"knative.dev/eventing/pkg/utils"
)
// UnknownChannelError represents the error when an event is received by a channel dispatcher for a
// channel that does not exist.
type UnknownChannelError struct {
Channel ChannelReference
}
func (e *UnknownChannelError) Error() string {
return fmt.Sprint("unknown channel: ", e.Channel)
}
// UnknownHostError represents the error when a ResolveChannelFromHostHeader func cannot resolve an host
type UnknownHostError string
func (e UnknownHostError) Error() string {
return "cannot map host to channel: " + string(e)
}
type BadRequestError string
func (e BadRequestError) Error() string {
return "malformed request: " + string(e)
}
// EventReceiver starts a server to receive new events for the channel dispatcher. The new
// event is emitted via the receiver function.
type EventReceiver struct {
httpBindingsReceiver *kncloudevents.HTTPEventReceiver
receiverFunc EventReceiverFunc
logger *zap.Logger
hostToChannelFunc ResolveChannelFromHostFunc
pathToChannelFunc ResolveChannelFromPathFunc
reporter StatsReporter
tokenVerifier *auth.Verifier
audience string
getPoliciesForFunc GetPoliciesForFunc
withContext func(context.Context) context.Context
}
// EventReceiverFunc is the function to be called for handling the event.
type EventReceiverFunc func(context.Context, ChannelReference, event.Event, nethttp.Header) error
// ReceiverOptions provides functional options to EventReceiver function.
type EventReceiverOptions func(*EventReceiver) error
// ResolveChannelFromHostFunc function enables EventReceiver to get the Channel Reference from incoming request HostHeader
// before calling receiverFunc.
// Returns UnknownHostError if the channel is not found, otherwise returns a generic error.
type ResolveChannelFromHostFunc func(string) (ChannelReference, error)
// ResolveChannelFromHostHeader is a ReceiverOption for NewEventReceiver which enables the caller to overwrite the
// default behaviour defined by ParseChannelFromHost function.
func ResolveChannelFromHostHeader(hostToChannelFunc ResolveChannelFromHostFunc) EventReceiverOptions {
return func(r *EventReceiver) error {
r.hostToChannelFunc = hostToChannelFunc
return nil
}
}
// ResolveChannelFromPathFunc function enables EventReceiver to get the Channel Reference from incoming request's path
// before calling receiverFunc.
type ResolveChannelFromPathFunc func(string) (ChannelReference, error)
// ResolveChannelFromPath is a ReceiverOption for NewEventReceiver which enables the caller to overwrite the
// default behaviour defined by ParseChannelFromPath function.
func ResolveChannelFromPath(PathToChannelFunc ResolveChannelFromPathFunc) EventReceiverOptions {
return func(r *EventReceiver) error {
r.pathToChannelFunc = PathToChannelFunc
return nil
}
}
// GetPoliciesForFunc function enables the EventReceiver to get the Channels AppliedEventPoliciesStatus
type GetPoliciesForFunc func(channel ChannelReference) ([]duckv1.AppliedEventPolicyRef, error)
func ReceiverWithGetPoliciesForFunc(fn GetPoliciesForFunc) EventReceiverOptions {
return func(r *EventReceiver) error {
r.getPoliciesForFunc = fn
return nil
}
}
func OIDCTokenVerification(tokenVerifier *auth.Verifier, audience string) EventReceiverOptions {
return func(r *EventReceiver) error {
r.tokenVerifier = tokenVerifier
r.audience = audience
return nil
}
}
func ReceiverWithContextFunc(fn func(context.Context) context.Context) EventReceiverOptions {
return func(r *EventReceiver) error {
r.withContext = fn
return nil
}
}
// NewEventReceiver creates an event receiver passing new events to the
// receiverFunc.
func NewEventReceiver(receiverFunc EventReceiverFunc, logger *zap.Logger, reporter StatsReporter, opts ...EventReceiverOptions) (*EventReceiver, error) {
bindingsReceiver := kncloudevents.NewHTTPEventReceiver(8080)
receiver := &EventReceiver{
httpBindingsReceiver: bindingsReceiver,
receiverFunc: receiverFunc,
hostToChannelFunc: ResolveChannelFromHostFunc(ParseChannelFromHost),
logger: logger,
reporter: reporter,
}
for _, opt := range opts {
if err := opt(receiver); err != nil {
return nil, err
}
}
return receiver, nil
}
// Start begins to receive events for the receiver.
//
// Only HTTP POST requests to the root path (/) are accepted. If other paths or
// methods are needed, use the HandleRequest method directly with another HTTP
// server.
func (r *EventReceiver) Start(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errCh := make(chan error, 1)
go func() {
errCh <- r.httpBindingsReceiver.StartListen(ctx, r)
}()
// Stop either if the receiver stops (sending to errCh) or if the context Done channel is closed.
select {
case err := <-errCh:
return err
case <-ctx.Done():
break
}
// Done channel has been closed, we need to gracefully shutdown r.ceClient. The cancel() method will start its
// shutdown, if it hasn't finished in a reasonable amount of Time, just return an error.
cancel()
select {
case err := <-errCh:
return err
case <-time.After(network.DefaultDrainTimeout):
return errors.New("timeout shutting down http bindings receiver")
}
}
func (r *EventReceiver) ServeHTTP(response nethttp.ResponseWriter, request *nethttp.Request) {
ctx := request.Context()
if r.withContext != nil {
ctx = r.withContext(ctx)
}
response.Header().Set("Allow", "POST, OPTIONS")
if request.Method == nethttp.MethodOptions {
response.Header().Set("WebHook-Allowed-Origin", "*") // Accept from any Origin:
response.Header().Set("WebHook-Allowed-Rate", "*") // Unlimited requests/minute
response.WriteHeader(nethttp.StatusOK)
return
}
if request.Method != nethttp.MethodPost {
response.WriteHeader(nethttp.StatusMethodNotAllowed)
return
}
// The response status codes:
// 202 - the event was sent to subscribers
// 400 - the request was malformed
// 404 - the request was for an unknown channel
// 500 - an error occurred processing the request
args := ReportArgs{}
var channel ChannelReference
var err error
// prefer using pathToChannelFunc if available
if r.pathToChannelFunc != nil {
channel, err = r.pathToChannelFunc(request.URL.Path)
} else {
if request.URL.Path != "/" {
response.WriteHeader(nethttp.StatusBadRequest)
return
}
channel, err = r.hostToChannelFunc(request.Host)
}
if err != nil {
switch err.(type) {
case UnknownHostError:
response.WriteHeader(nethttp.StatusNotFound)
case BadRequestError:
response.WriteHeader(nethttp.StatusBadRequest)
default:
response.WriteHeader(nethttp.StatusInternalServerError)
}
r.logger.Info("Could not extract channel", zap.Error(err))
ReportEventCountMetricsForDispatchError(err, r.reporter, &args)
return
}
r.logger.Debug("Request mapped to channel", zap.String("channel", channel.String()))
args.Ns = channel.Namespace
if request.TLS != nil {
args.EventScheme = "https"
} else {
args.EventScheme = "http"
}
event, err := http.NewEventFromHTTPRequest(request)
if err != nil {
r.logger.Warn("failed to extract event from request", zap.Error(err))
response.WriteHeader(nethttp.StatusBadRequest)
_ = r.reporter.ReportEventCount(&args, nethttp.StatusBadRequest)
return
}
// run validation for the extracted event
if err := event.Validate(); err != nil {
r.logger.Warn("failed to validate extracted event", zap.Error(err))
response.WriteHeader(nethttp.StatusBadRequest)
return
}
/// Here we do the OIDC audience verification
features := feature.FromContext(ctx)
if features.IsOIDCAuthentication() {
r.logger.Debug("OIDC authentication is enabled")
if r.getPoliciesForFunc == nil {
r.logger.Error("getPoliciesForFunc() callback not set. Can't get applying event policies of channel")
response.WriteHeader(nethttp.StatusInternalServerError)
return
}
applyingEventPolicies, err := r.getPoliciesForFunc(channel)
if err != nil {
r.logger.Error("could not get applying event policies of channel", zap.Error(err), zap.String("channel", channel.String()))
response.WriteHeader(nethttp.StatusInternalServerError)
return
}
err = r.tokenVerifier.VerifyRequest(ctx, features, &r.audience, channel.Namespace, applyingEventPolicies, request, response)
if err != nil {
r.logger.Warn("could not verify authn and authz of request", zap.Error(err))
return
}
r.logger.Debug("Request contained a valid and authorized JWT. Continuing...")
}
err = r.receiverFunc(request.Context(), channel, *event, utils.PassThroughHeaders(request.Header))
if err != nil {
if _, ok := err.(*UnknownChannelError); ok {
response.WriteHeader(nethttp.StatusNotFound)
} else {
r.logger.Info("Error in receiver", zap.Error(err))
response.WriteHeader(nethttp.StatusInternalServerError)
}
return
}
response.WriteHeader(nethttp.StatusAccepted)
}
func ReportEventCountMetricsForDispatchError(err error, reporter StatsReporter, args *ReportArgs) {
switch err.(type) {
case *UnknownChannelError:
_ = reporter.ReportEventCount(args, nethttp.StatusNotFound)
case BadRequestError:
_ = reporter.ReportEventCount(args, nethttp.StatusBadRequest)
default:
_ = reporter.ReportEventCount(args, nethttp.StatusInternalServerError)
}
}
var _ nethttp.Handler = (*EventReceiver)(nil)