-
Notifications
You must be signed in to change notification settings - Fork 21
/
primaryHandler.go
415 lines (346 loc) · 12.9 KB
/
primaryHandler.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/clortho/clorthometrics"
"github.com/xmidt-org/clortho/clorthozap"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"go.uber.org/zap"
// nolint:staticcheck
"github.com/xmidt-org/bascule/basculechecks"
// nolint:staticcheck
"github.com/xmidt-org/bascule/basculehttp"
"github.com/xmidt-org/clortho"
// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/device"
// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/service"
// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/service/servicehttp"
"github.com/xmidt-org/webpa-common/v2/xhttp"
"github.com/xmidt-org/webpa-common/v2/xhttp/xfilter"
"github.com/xmidt-org/webpa-common/v2/xhttp/xtimeout"
"github.com/xmidt-org/wrp-go/v3/wrphttp"
)
const (
baseURI = "/api"
// TODO: Should this change for talaria 2.0?
version = "v3"
v2 = "v2"
DefaultKeyID = "current"
DefaultInboundTimeout time.Duration = 120 * time.Second
)
// Paths to configuration values for convenience and protection against typos.
const (
// JWTValidatorConfigKey is the path to the JWT
// validator config for device registration endpoints.
JWTValidatorConfigKey = "jwtValidator"
// DeviceAccessCheckConfigKey is the path to the validator config for
// restricting API access to devices based on known device metadata and credentials
// presented by API consumers.
DeviceAccessCheckConfigKey = "deviceAccessCheck"
// ServiceBasicAuthConfigKey is the path to the list of accepted basic auth keys
// for the API endpoints (note: does not include device registration).
ServiceBasicAuthConfigKey = "inbound.authKey"
// InboundTimeoutConfigKey is the path to the request timeout duration for
// requests inbound to devices connected to talaria.
InboundTimeoutConfigKey = "inbound.timeout"
// RehasherServicesConfigKey is the path to the services for whose events talaria's
// rehasher should listen to.
RehasherServicesConfigKey = "device.rehasher.services"
// FailOpenConfigKey is the path to the fail open boolean which will determine
// which route to take when a device tries to connect to talaria
FailOpenConfigKey = "failOpen"
)
// NoOpConstructor provides a transparent way for constructors that make up
// our middleware chains to work out of the box even without configuration
// such as authentication layers
var NoOpConstructor = func(h http.Handler) http.Handler { return h }
// JWTValidator provides a convenient way to define jwt validator through config files
type JWTValidator struct {
// Config is used to create the clortho Resolver for JWT verification keys
Config clortho.Config `json:"config"`
// Leeway is used to set the amount of time buffer should be given to JWT
// time values, such as nbf
Leeway bascule.Leeway
}
func getInboundTimeout(v *viper.Viper) time.Duration {
if t, err := time.ParseDuration(v.GetString(InboundTimeoutConfigKey)); err == nil {
return t
}
return DefaultInboundTimeout
}
// buildUserPassMap decodes base64-encoded strings of the form user:pass and write them to a map from user -> pass
func buildUserPassMap(logger *zap.Logger, encodedBasicAuthKeys []string) (userPass map[string]string) {
userPass = make(map[string]string)
for _, encodedKey := range encodedBasicAuthKeys {
decoded, err := base64.StdEncoding.DecodeString(encodedKey)
if err != nil {
logger.Info("Failed to base64-decode basic auth key", zap.String("key", encodedKey), zap.Error(err))
}
i := bytes.IndexByte(decoded, ':')
logger.Debug("Decoded basic auth key", zap.ByteString("key", decoded), zap.Int("delimeterIndex", i))
if i > 0 {
userPass[string(decoded[:i])] = string(decoded[i+1:])
}
}
return
}
func NewPrimaryHandler(logger *zap.Logger, manager device.Manager, v *viper.Viper, a service.Accessor, e service.Environment,
controlConstructor alice.Constructor, tf *touchstone.Factory, r *mux.Router) (http.Handler, error) {
var (
inboundTimeout = getInboundTimeout(v)
apiHandler = r.PathPrefix(fmt.Sprintf("%s/{version:%s|%s}", baseURI, v2, version)).Subrouter()
authConstructor = NoOpConstructor
authEnforcer = NoOpConstructor
deviceAuthRules = bascule.Validators{} //auth rules for device registration endpoints
serviceAuthRules = bascule.Validators{} //auth rules for everything else
)
authCounter, err := tf.NewCounterVec(
prometheus.CounterOpts{
Name: basculehttp.AuthValidationOutcome,
Help: "Counter for success and failure reason results through bascule",
}, basculehttp.ServerLabel, basculehttp.OutcomeLabel)
if err != nil {
return nil, err
}
listener, err := basculehttp.NewMetricListener(
&basculehttp.AuthValidationMeasures{ValidationOutcome: authCounter},
)
if err != nil {
return nil, err
}
authConstructorOptions := []basculehttp.COption{
basculehttp.WithCLogger(getLogger),
basculehttp.WithCErrorResponseFunc(listener.OnErrorResponse),
}
if v.IsSet(JWTValidatorConfigKey) {
var jwtVal JWTValidator
v.UnmarshalKey(JWTValidatorConfigKey, &jwtVal)
kr := clortho.NewKeyRing()
// Instantiate a fetcher for the resolver
f, err := clortho.NewFetcher()
if err != nil {
return nil, errors.New("failed to create clortho fetcher")
}
resolver, err := clortho.NewResolver(
clortho.WithConfig(jwtVal.Config),
clortho.WithKeyRing(kr),
clortho.WithFetcher(f),
)
if err != nil {
return nil, errors.New("failed to create clortho reolver")
}
var zConfig sallust.Config
v.UnmarshalKey("zap", &zConfig)
zlogger := zap.Must(zConfig.Build())
// Instantiate a metric listener for the resolver
cml, err := clorthometrics.NewListener(clorthometrics.WithFactory(tf))
if err != nil {
return nil, errors.New("failed to create clortho metrics listener")
}
// Instantiate a logging listener for the resolver
czl, err := clorthozap.NewListener(
clorthozap.WithLogger(zlogger),
)
if err != nil {
return nil, errors.New("failed to create clortho zap logger listener")
}
resolver.AddListener(cml)
resolver.AddListener(czl)
authConstructorOptions = append(authConstructorOptions, basculehttp.WithTokenFactory("Bearer", basculehttp.BearerTokenFactory{
DefaultKeyID: DefaultKeyID,
Resolver: resolver,
Parser: bascule.DefaultJWTParser,
Leeway: jwtVal.Leeway,
}))
deviceAuthRules = append(deviceAuthRules,
bascule.Validators{
basculechecks.NonEmptyPrincipal(),
basculechecks.NonEmptyType(),
basculechecks.ValidType([]string{"jwt"}),
})
}
if v.IsSet(ServiceBasicAuthConfigKey) {
userPassMap := buildUserPassMap(logger, v.GetStringSlice(ServiceBasicAuthConfigKey))
if len(userPassMap) > 0 {
authConstructorOptions = append(authConstructorOptions,
basculehttp.WithTokenFactory("Basic", basculehttp.BasicTokenFactory(userPassMap)))
serviceAuthRules = append(serviceAuthRules, basculechecks.AllowAll())
}
}
wrpRouterHandler := wrpRouterHandler(logger, manager, getLogger)
if v.IsSet(DeviceAccessCheckConfigKey) {
config := new(deviceAccessCheckConfig)
if err := v.UnmarshalKey(DeviceAccessCheckConfigKey, config); err != nil {
logger.Error("Could not unmarshall wrpCheck config for api access to device.")
return nil, err
}
inboundWRPMessageCounter, err := tf.NewCounterVec(
prometheus.CounterOpts{
Name: InboundWRPMessageCounter,
Help: "Number of inbound WRP Messages successfully decoded and ready to route to device",
},
[]string{outcomeLabel, reasonLabel}...,
)
if err != nil {
logger.Error(fmt.Sprintf("Could not create %s metric.", InboundWRPMessageCounter))
return nil, err
}
deviceAccessCheck, err := buildDeviceAccessCheck(config, logger, inboundWRPMessageCounter, manager)
if err != nil {
return nil, err
}
logger.Info("Enabling Device Access Validator.")
wrpRouterHandler = withDeviceAccessCheck(logger, wrpRouterHandler, deviceAccessCheck)
}
authConstructor = basculehttp.NewConstructor(authConstructorOptions...)
authConstructorLegacy := basculehttp.NewConstructor(append([]basculehttp.COption{
basculehttp.WithCErrorHTTPResponseFunc(basculehttp.LegacyOnErrorHTTPResponse),
}, authConstructorOptions...)...)
authEnforcer = basculehttp.NewEnforcer(
basculehttp.WithELogger(getLogger),
basculehttp.WithRules("Basic", serviceAuthRules),
basculehttp.WithRules("Bearer", deviceAuthRules),
basculehttp.WithEErrorResponseFunc(listener.OnErrorResponse),
)
authChain := alice.New(setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener))
authChainV2 := alice.New(setLogger(logger), authConstructorLegacy, authEnforcer, basculehttp.NewListenerDecorator(listener))
versionCompatibleAuth := alice.New(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(r http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
if vars != nil {
if vars["version"] == v2 {
authChainV2.Then(next).ServeHTTP(r, req)
return
}
}
authChain.Then(next).ServeHTTP(r, req)
})
})
apiHandler.Handle("/device/send",
alice.New(
xtimeout.NewConstructor(xtimeout.Options{
Timeout: inboundTimeout,
})).
Extend(versionCompatibleAuth).
Then(wrphttp.NewHTTPHandler(wrpRouterHandler)),
).Methods("POST", "PATCH")
apiHandler.Handle("/devices",
versionCompatibleAuth.Then(&device.ListHandler{
Logger: logger,
Registry: manager,
})).Methods("GET")
var (
// the basic decorator chain all device connect handlers use
deviceConnectChain = alice.New(
setLogger(logger, header(device.DeviceNameHeader, device.DeviceNameHeader)),
controlConstructor,
device.UseID.FromHeader,
)
connectHandler = &device.ConnectHandler{
Logger: logger,
Connector: manager,
}
)
if a != nil && e != nil {
// if a service discovery environment was configured, append the hash filter to enforce
// device hashing
deviceConnectChain.Append(
xfilter.NewConstructor(
xfilter.WithFilters(
servicehttp.NewHashFilter(a, &xhttp.Error{Code: http.StatusGone}, e.IsRegistered),
),
),
)
}
// the secured variant of the device connect handler - compatible with v2 and v3
// default functionality is to allow for talaria to accept devices with or without authorization
// failOpen must be set to false in config in order to require authorization from any device trying to connect
failOpen := true
if v.IsSet(FailOpenConfigKey) {
err := v.UnmarshalKey(FailOpenConfigKey, &failOpen)
if err != nil {
logger.Error("failOpen parse failure", zap.Error(err))
return nil, errors.New("failed parsing FailOpen boolean")
}
}
if failOpen {
r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Extend(versionCompatibleAuth).
Append(DeviceMetadataMiddleware(getLogger)).
Then(connectHandler),
).HeadersRegexp("Authorization", ".*")
r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Append(DeviceMetadataMiddleware(getLogger)).
Then(connectHandler),
)
} else {
r.Handle(
fmt.Sprintf("%s/{version:%s|%s}/device", baseURI, v2, version),
deviceConnectChain.
Extend(versionCompatibleAuth).
Append(DeviceMetadataMiddleware(getLogger)).
Then(connectHandler),
)
}
apiHandler.Handle(
"/device/{deviceID}/stat",
alice.New(
device.UseID.FromPath("deviceID")).
Extend(versionCompatibleAuth).
Then(&device.StatHandler{
Logger: logger,
Registry: manager,
Variable: "deviceID",
}),
).Methods("GET")
return r, nil
}
func buildDeviceAccessCheck(config *deviceAccessCheckConfig, logger *zap.Logger, counter *prometheus.CounterVec, deviceRegistry device.Registry) (deviceAccess, error) {
if len(config.Checks) < 1 {
logger.Error("Potential security misconfig. Include checks for deviceAccessCheck or disable it")
return nil, errors.New("failed enabling DeviceAccessCheck")
}
if config.Type != "enforce" && config.Type != "monitor" {
logger.Error("Unexpected type for deviceAccessCheck. Supported types are 'monitor' and 'enforce'")
return nil, errors.New("failed verifying DeviceAccessCheck type")
}
// nolint:prealloc
var parsedChecks []*parsedCheck
for _, check := range config.Checks {
parsedCheck, err := parseDeviceAccessCheck(check)
if err != nil {
logger.Error("deviceAccesscheck parse failure", zap.Error(err))
return nil, errors.New("failed parsing DeviceAccessCheck checks")
}
parsedChecks = append(parsedChecks, parsedCheck)
}
if config.Sep == "" {
config.Sep = "."
}
return &talariaDeviceAccess{
strict: config.Type == "enforce",
wrpMessagesCounter: counter,
checks: parsedChecks,
deviceRegistry: deviceRegistry,
logger: logger,
sep: config.Sep,
}, nil
}