-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathroutes.go
397 lines (353 loc) · 11.6 KB
/
routes.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/spf13/viper"
"github.com/xmidt-org/ancla"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/arrange/arrangehttp"
"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/httpaux"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/sallust/sallusthttp"
"github.com/xmidt-org/touchstone"
"github.com/xmidt-org/touchstone/touchhttp"
"github.com/xmidt-org/tr1d1um/stat"
"github.com/xmidt-org/tr1d1um/translation"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.uber.org/fx"
"go.uber.org/zap"
)
var (
errFailedWebhookUnmarshal = errors.New("failed to JSON unmarshal webhook")
v2WarningHeader = "X-Xmidt-Warning"
)
type primaryEndpointIn struct {
fx.In
V *viper.Viper
Router *mux.Router `name:"server_primary"`
APIRouter *mux.Router `name:"api_router"`
AuthChain alice.Chain `name:"auth_chain"`
Tracing candlelight.Tracing
Logger *zap.Logger
StatServiceOptions *stat.ServiceOptions
TranslationOptions *translation.ServiceOptions
AuthAcquirer authAcquirerConfig `name:"authAcquirer"`
ReducedLoggingResponseCodes []int `name:"reducedLoggingResponseCodes"`
TranslationServices []string `name:"supportedServices"`
}
type handleWebhookRoutesIn struct {
fx.In
Logger *zap.Logger
Tracing candlelight.Tracing
APIRouter *mux.Router `name:"api_router"`
AuthChain alice.Chain `name:"auth_chain"`
AddWebhookHandler http.Handler `name:"add_webhook_handler"`
V2AddWebhookHandler http.Handler `name:"v2_add_webhook_handler"`
GetAllWebhooksHandler http.Handler `name:"get_all_webhooks_handler"`
WebhookConfig ancla.Config
}
type apiAltRouterIn struct {
fx.In
APIRouter *mux.Router `name:"api_router"`
AlternateRouter *mux.Router `name:"server_alternate"`
URLPrefix string `name:"url_prefix"`
}
type apiRouterIn struct {
fx.In
PrimaryRouter *mux.Router `name:"server_primary"`
URLPrefix string `name:"url_prefix"`
}
type provideURLPrefixIn struct {
fx.In
PrevVerSupport bool `name:"previousVersionSupport"`
}
type primaryMetricMiddlewareIn struct {
fx.In
Primary alice.Chain `name:"middleware_primary_metrics"`
}
type alternateMetricMiddlewareIn struct {
fx.In
Alternate alice.Chain `name:"middleware_alternate_metrics"`
}
type healthMetricMiddlewareIn struct {
fx.In
Health alice.Chain `name:"middleware_health_metrics"`
}
type metricMiddlewareOut struct {
fx.Out
Primary alice.Chain `name:"middleware_primary_metrics"`
Alternate alice.Chain `name:"middleware_alternate_metrics"`
Health alice.Chain `name:"middleware_health_metrics"`
}
type metricsRoutesIn struct {
fx.In
Router *mux.Router `name:"server_metrics"`
Handler touchhttp.Handler
}
func provideServers() fx.Option {
return fx.Options(
arrange.ProvideKey(reqMaxRetriesKey, 0),
arrange.ProvideKey(reqRetryIntervalKey, time.Duration(0)),
arrange.ProvideKey("previousVersionSupport", true),
arrange.ProvideKey("targetURL", ""),
arrange.ProvideKey("WRPSource", ""),
arrange.ProvideKey(translationServicesKey, []string{}),
fx.Provide(metricMiddleware),
fx.Provide(
fx.Annotated{
Name: "reducedLoggingResponseCodes",
Target: arrange.UnmarshalKey(reducedTransactionLoggingCodesKey, []int{}),
},
fx.Annotated{
Name: "api_router",
Target: provideAPIRouter,
},
fx.Annotated{
Name: "url_prefix",
Target: provideURLPrefix,
},
provideServiceOptions,
),
arrangehttp.Server{
Name: "server_primary",
Key: "servers.primary",
Inject: arrange.Inject{
primaryMetricMiddlewareIn{},
},
}.Provide(),
arrangehttp.Server{
Name: "server_alternate",
Key: "servers.alternate",
Inject: arrange.Inject{
alternateMetricMiddlewareIn{},
},
}.Provide(),
arrangehttp.Server{
Name: "server_health",
Key: "servers.health",
Inject: arrange.Inject{
healthMetricMiddlewareIn{},
},
Invoke: arrange.Invoke{
func(r *mux.Router) {
r.Handle("/health", httpaux.ConstantHandler{
StatusCode: http.StatusOK,
}).Methods("GET")
},
},
}.Provide(),
arrangehttp.Server{
Name: "server_pprof",
Key: "servers.pprof",
}.Provide(),
arrangehttp.Server{
Name: "server_metrics",
Key: "servers.metrics",
}.Provide(),
fx.Invoke(
handlePrimaryEndpoint,
handleWebhookRoutes,
buildMetricsRoutes,
buildAPIAltRouter,
),
)
}
func handlePrimaryEndpoint(in primaryEndpointIn) {
otelMuxOptions := []otelmux.Option{
otelmux.WithTracerProvider(in.Tracing.TracerProvider()),
otelmux.WithPropagators(in.Tracing.Propagator()),
}
in.Router.Use(
otelmux.Middleware("mainSpan", otelMuxOptions...),
)
if in.V.IsSet(authAcquirerKey) {
acquirer, err := createAuthAcquirer(in.AuthAcquirer)
if err != nil {
in.Logger.Error("Could not configure auth acquirer", zap.Error(err))
} else {
in.TranslationOptions.AuthAcquirer = acquirer
in.StatServiceOptions.AuthAcquirer = acquirer
in.Logger.Info("Outbound request authentication token acquirer enabled")
}
}
ss := stat.NewService(in.StatServiceOptions)
ts := translation.NewService(in.TranslationOptions)
// Must be called before translation.ConfigHandler due to mux path specificity (https://github.com/gorilla/mux#matching-routes).
stat.ConfigHandler(&stat.Options{
S: ss,
APIRouter: in.APIRouter,
Authenticate: &in.AuthChain,
Log: in.Logger,
ReducedLoggingResponseCodes: in.ReducedLoggingResponseCodes,
})
translation.ConfigHandler(&translation.Options{
S: ts,
APIRouter: in.APIRouter,
Authenticate: &in.AuthChain,
Log: in.Logger,
ValidServices: in.TranslationServices,
ReducedLoggingResponseCodes: in.ReducedLoggingResponseCodes,
})
}
func handleWebhookRoutes(in handleWebhookRoutesIn) error {
if in.AddWebhookHandler != nil && in.GetAllWebhooksHandler != nil {
fixV2Middleware, err := fixV2Duration(sallust.Get, in.WebhookConfig.Validation.TTL, in.V2AddWebhookHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize v2 endpoint middleware: %v\n", err)
return err
}
in.APIRouter.Handle("/hook", in.AuthChain.Then(fixV2Middleware(candlelight.EchoFirstTraceNodeInfo(in.Tracing.Propagator(), false)(in.AddWebhookHandler)))).Methods(http.MethodPost)
in.APIRouter.Handle("/hooks", in.AuthChain.Then(candlelight.EchoFirstTraceNodeInfo(in.Tracing.Propagator(), false)(in.GetAllWebhooksHandler)))
}
return nil
}
func metricMiddleware(f *touchstone.Factory) (out metricMiddlewareOut) {
var bundle touchhttp.ServerBundle
primary, err1 := bundle.NewInstrumenter(
touchhttp.ServerLabel, "server_primary",
)(f)
alternate, err2 := bundle.NewInstrumenter(
touchhttp.ServerLabel, "server_alternate",
)(f)
health, err3 := bundle.NewInstrumenter(
touchhttp.ServerLabel, "server_health",
)(f)
if err1 != nil || err2 != nil || err3 != nil {
return
}
out.Primary = alice.New(primary.Then)
out.Alternate = alice.New(alternate.Then)
out.Health = alice.New(health.Then)
return
}
func provideAPIRouter(in apiRouterIn) *mux.Router {
return in.PrimaryRouter.PathPrefix(in.URLPrefix).Subrouter()
}
func buildAPIAltRouter(in apiAltRouterIn) {
apiAltRouter := in.AlternateRouter.PathPrefix(in.URLPrefix).Subrouter()
apiAltRouter.Handle("/device/{deviceid}/{service}", in.APIRouter)
apiAltRouter.Handle("/device/{deviceid}/{service}/{parameter}", in.APIRouter)
apiAltRouter.Handle("/device/{deviceid}/stat", in.APIRouter)
apiAltRouter.Handle("/hook", in.APIRouter)
apiAltRouter.Handle("/hooks", in.APIRouter)
}
func provideURLPrefix(in provideURLPrefixIn) string {
// if we want to support the previous API version, then include it in the
// api base.
urlPrefix := fmt.Sprintf("/%s", apiBase)
if in.PrevVerSupport {
urlPrefix = fmt.Sprintf("/%s", apiBaseDualVersion)
}
return urlPrefix
}
//nolint:funlen
func fixV2Duration(getLogger func(context.Context) *zap.Logger, config ancla.TTLVConfig, v2Handler http.Handler) (alice.Constructor, error) {
if config.Now == nil {
config.Now = time.Now
}
durationCheck, err := ancla.CheckDuration(config.Max)
if err != nil {
return nil, fmt.Errorf("failed to create duration check: %v", err)
}
untilCheck, err := ancla.CheckUntil(config.Jitter, config.Max, config.Now)
if err != nil {
return nil, fmt.Errorf("failed to create until check: %v", err)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// if this isn't v2, do nothing.
if vars == nil || vars["version"] != prevAPIVersion {
next.ServeHTTP(w, r)
return
}
// if this is v2, we need to unmarshal and check the duration. If
// the duration is bad, change it to 5m and add a header. Then use
// the v2 handler.
logger := sallusthttp.Get(r)
requestPayload, err := ioutil.ReadAll(r.Body)
if err != nil {
v2ErrEncode(w, logger, err, 0)
return
}
var wr ancla.WebhookRegistration
err = json.Unmarshal(requestPayload, &wr)
if err != nil {
var e *json.UnmarshalTypeError
if errors.As(err, &e) {
v2ErrEncode(w, logger,
fmt.Errorf("%w: %v must be of type %v", errFailedWebhookUnmarshal, e.Field, e.Type),
http.StatusBadRequest)
return
}
v2ErrEncode(w, logger, fmt.Errorf("%w: %v", errFailedWebhookUnmarshal, err),
http.StatusBadRequest)
return
}
// check to see if the Webhook has a valid until/duration.
// If not, set the WebhookRegistration duration to 5m.
webhook := wr.ToWebhook()
if webhook.Until.IsZero() {
if webhook.Duration == 0 {
wr.Duration = ancla.CustomDuration(config.Max)
w.Header().Add(v2WarningHeader,
fmt.Sprintf("Unset duration and until fields will not be accepted in v3, webhook duration defaulted to %v", config.Max))
} else {
durationErr := durationCheck(webhook)
if durationErr != nil {
wr.Duration = ancla.CustomDuration(config.Max)
w.Header().Add(v2WarningHeader,
fmt.Sprintf("Invalid duration will not be accepted in v3: %v, webhook duration defaulted to %v", durationErr, config.Max))
}
}
} else {
untilErr := untilCheck(webhook)
if untilErr != nil {
wr.Until = time.Time{}
wr.Duration = ancla.CustomDuration(config.Max)
w.Header().Add(v2WarningHeader,
fmt.Sprintf("Invalid until value will not be accepted in v3: %v, webhook duration defaulted to 5m", untilErr))
}
}
// put the body back in the request
body, err := json.Marshal(wr)
if err != nil {
v2ErrEncode(w, logger, fmt.Errorf("failed to recreate request body: %v", err), 0)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if v2Handler == nil {
v2Handler = next
}
v2Handler.ServeHTTP(w, r)
})
}, nil
}
func v2ErrEncode(w http.ResponseWriter, logger *zap.Logger, err error, code int) {
if code == 0 {
code = http.StatusInternalServerError
}
logger.Error("sending non-200, non-404 response",
zap.Error(err), zap.Int("code", code))
w.WriteHeader(code)
json.NewEncoder(w).Encode(
map[string]interface{}{
"message": err.Error(),
})
}
func buildMetricsRoutes(in metricsRoutesIn) {
if in.Router != nil && in.Handler != nil {
in.Router.Handle("/metrics", in.Handler).Methods("GET")
}
}