forked from brocaar/chirpstack-application-server
/
root_run.go
513 lines (450 loc) · 16.5 KB
/
root_run.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
pb "github.com/brocaar/lora-app-server/api"
"github.com/brocaar/lora-app-server/internal/api"
"github.com/brocaar/lora-app-server/internal/api/auth"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/lora-app-server/internal/downlink"
"github.com/brocaar/lora-app-server/internal/gwping"
"github.com/brocaar/lora-app-server/internal/handler"
"github.com/brocaar/lora-app-server/internal/handler/gcppubsub"
"github.com/brocaar/lora-app-server/internal/handler/mqtthandler"
"github.com/brocaar/lora-app-server/internal/handler/multihandler"
"github.com/brocaar/lora-app-server/internal/migrations"
"github.com/brocaar/lora-app-server/internal/nsclient"
"github.com/brocaar/lora-app-server/internal/static"
"github.com/brocaar/lora-app-server/internal/storage"
"github.com/brocaar/loraserver/api/as"
)
func run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
tasks := []func() error{
setLogLevel,
printStartMessage,
setPostgreSQLConnection,
setRedisPool,
setHandler,
setNetworkServerClient,
runDatabaseMigrations,
setJWTSecret,
setHashIterations,
setDisableAssignExistingUsers,
handleDataDownPayloads,
startApplicationServerAPI,
startGatewayPing,
startJoinServerAPI,
startClientAPI(ctx),
}
for _, t := range tasks {
if err := t(); err != nil {
log.Fatal(err)
}
}
sigChan := make(chan os.Signal)
exitChan := make(chan struct{})
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
log.WithField("signal", <-sigChan).Info("signal received")
go func() {
log.Warning("stopping lora-app-server")
// todo: handle graceful shutdown?
exitChan <- struct{}{}
}()
select {
case <-exitChan:
case s := <-sigChan:
log.WithField("signal", s).Info("signal received, stopping immediately")
}
return nil
}
func setLogLevel() error {
log.SetLevel(log.Level(uint8(config.C.General.LogLevel)))
return nil
}
func printStartMessage() error {
log.WithFields(log.Fields{
"version": version,
"docs": "https://www.loraserver.io/",
}).Info("starting LoRa App Server")
return nil
}
func setPostgreSQLConnection() error {
log.Info("connecting to postgresql")
db, err := storage.OpenDatabase(config.C.PostgreSQL.DSN)
if err != nil {
return errors.Wrap(err, "database connection error")
}
config.C.PostgreSQL.DB = db
return nil
}
func setRedisPool() error {
// setup redis pool
log.Info("setup redis connection pool")
config.C.Redis.Pool = storage.NewRedisPool(config.C.Redis.URL, config.C.Redis.MaxIdle, config.C.Redis.IdleTimeout)
return nil
}
func setHandler() error {
var h handler.Handler
var err error
switch config.C.ApplicationServer.Integration.Backend {
case "mqtt":
h, err = mqtthandler.NewHandler(
config.C.Redis.Pool,
config.C.ApplicationServer.Integration.MQTT,
)
case "gcp_pub_sub":
h, err = gcppubsub.NewHandler(config.C.ApplicationServer.Integration.GCPPubSub)
default:
return fmt.Errorf("invalid integration backend: %s", config.C.ApplicationServer.Integration.Backend)
}
if err != nil {
return errors.Wrap(err, "setup integration backend error")
}
config.C.ApplicationServer.Integration.Handler = multihandler.NewHandler(h)
return nil
}
func setNetworkServerClient() error {
config.C.NetworkServer.Pool = nsclient.NewPool()
return nil
}
func runDatabaseMigrations() error {
if config.C.PostgreSQL.Automigrate {
log.Info("applying database migrations")
m := &migrate.AssetMigrationSource{
Asset: migrations.Asset,
AssetDir: migrations.AssetDir,
Dir: "",
}
n, err := migrate.Exec(config.C.PostgreSQL.DB.DB.DB, "postgres", m, migrate.Up)
if err != nil {
return errors.Wrap(err, "applying migrations error")
}
log.WithField("count", n).Info("migrations applied")
}
return nil
}
func setJWTSecret() error {
storage.SetUserSecret(config.C.ApplicationServer.ExternalAPI.JWTSecret)
return nil
}
func setHashIterations() error {
storage.HashIterations = config.C.General.PasswordHashIterations
return nil
}
func setDisableAssignExistingUsers() error {
auth.DisableAssignExistingUsers = config.C.ApplicationServer.ExternalAPI.DisableAssignExistingUsers
return nil
}
func handleDataDownPayloads() error {
go downlink.HandleDataDownPayloads()
return nil
}
func startApplicationServerAPI() error {
log.WithFields(log.Fields{
"bind": config.C.ApplicationServer.API.Bind,
"ca-cert": config.C.ApplicationServer.API.CACert,
"tls-cert": config.C.ApplicationServer.API.TLSCert,
"tls-key": config.C.ApplicationServer.API.TLSKey,
}).Info("starting application-server api")
apiServer := mustGetAPIServer()
ln, err := net.Listen("tcp", config.C.ApplicationServer.API.Bind)
if err != nil {
log.Fatalf("start application-server api listener error: %s", err)
}
go apiServer.Serve(ln)
return nil
}
func startGatewayPing() error {
go gwping.SendPingLoop()
return nil
}
func startJoinServerAPI() error {
log.WithFields(log.Fields{
"bind": config.C.JoinServer.Bind,
"ca_cert": config.C.JoinServer.CACert,
"tls_cert": config.C.JoinServer.TLSCert,
"tls_key": config.C.JoinServer.TLSKey,
}).Info("starting join-server api")
server := http.Server{
Handler: api.NewJoinServerAPI(),
Addr: config.C.JoinServer.Bind,
TLSConfig: &tls.Config{},
}
if config.C.JoinServer.CACert == "" && config.C.JoinServer.TLSCert == "" && config.C.JoinServer.TLSKey == "" {
go func() {
err := server.ListenAndServe()
log.WithError(err).Error("join-server api error")
}()
return nil
}
if config.C.JoinServer.CACert != "" {
caCert, err := ioutil.ReadFile(config.C.JoinServer.CACert)
if err != nil {
return errors.Wrap(err, "read ca certificate error")
}
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
return errors.New("append ca certificate error")
}
server.TLSConfig.ClientCAs = caCertPool
server.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
log.WithFields(log.Fields{
"ca_cert": config.C.JoinServer.CACert,
}).Info("join-server is configured with client-certificate authentication")
}
go func() {
err := server.ListenAndServeTLS(config.C.JoinServer.TLSCert, config.C.JoinServer.TLSKey)
log.WithError(err).Error("join-server api error")
}()
return nil
}
func startClientAPI(ctx context.Context) func() error {
return func() error {
// setup the client API interface
var validator auth.Validator
if config.C.ApplicationServer.ExternalAPI.JWTSecret != "" {
validator = auth.NewJWTValidator(config.C.PostgreSQL.DB, "HS256", config.C.ApplicationServer.ExternalAPI.JWTSecret)
} else {
log.Fatal("jwt secret must be set for external api")
}
rpID, err := uuid.FromString(config.C.ApplicationServer.ID)
if err != nil {
return errors.Wrap(err, "application-server id to uuid error")
}
clientAPIHandler := grpc.NewServer(gRPCLoggingServerOptions()...)
pb.RegisterApplicationServiceServer(clientAPIHandler, api.NewApplicationAPI(validator))
pb.RegisterDeviceQueueServiceServer(clientAPIHandler, api.NewDeviceQueueAPI(validator))
pb.RegisterDeviceServiceServer(clientAPIHandler, api.NewDeviceAPI(validator))
pb.RegisterUserServiceServer(clientAPIHandler, api.NewUserAPI(validator))
pb.RegisterInternalServiceServer(clientAPIHandler, api.NewInternalUserAPI(validator))
pb.RegisterGatewayServiceServer(clientAPIHandler, api.NewGatewayAPI(validator))
pb.RegisterGatewayProfileServiceServer(clientAPIHandler, api.NewGatewayProfileAPI(validator))
pb.RegisterOrganizationServiceServer(clientAPIHandler, api.NewOrganizationAPI(validator))
pb.RegisterNetworkServerServiceServer(clientAPIHandler, api.NewNetworkServerAPI(validator))
pb.RegisterServiceProfileServiceServer(clientAPIHandler, api.NewServiceProfileServiceAPI(validator))
pb.RegisterDeviceProfileServiceServer(clientAPIHandler, api.NewDeviceProfileServiceAPI(validator))
pb.RegisterMulticastGroupServiceServer(clientAPIHandler, api.NewMulticastGroupAPI(validator, config.C.PostgreSQL.DB, rpID, config.C.NetworkServer.Pool))
// setup the client http interface variable
// we need to start the gRPC service first, as it is used by the
// grpc-gateway
var clientHTTPHandler http.Handler
// switch between gRPC and "plain" http handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
clientAPIHandler.ServeHTTP(w, r)
} else {
if clientHTTPHandler == nil {
w.WriteHeader(http.StatusNotImplemented)
return
}
clientHTTPHandler.ServeHTTP(w, r)
}
})
// start the API server
go func() {
log.WithFields(log.Fields{
"bind": config.C.ApplicationServer.ExternalAPI.Bind,
"tls-cert": config.C.ApplicationServer.ExternalAPI.TLSCert,
"tls-key": config.C.ApplicationServer.ExternalAPI.TLSKey,
}).Info("starting client api server")
if config.C.ApplicationServer.ExternalAPI.TLSCert == "" || config.C.ApplicationServer.ExternalAPI.TLSKey == "" {
log.Fatal(http.ListenAndServe(config.C.ApplicationServer.ExternalAPI.Bind, h2c.NewHandler(handler, &http2.Server{})))
} else {
log.Fatal(http.ListenAndServeTLS(
config.C.ApplicationServer.ExternalAPI.Bind,
config.C.ApplicationServer.ExternalAPI.TLSCert,
config.C.ApplicationServer.ExternalAPI.TLSKey,
h2c.NewHandler(handler, &http2.Server{}),
))
}
}()
// give the http server some time to start
time.Sleep(time.Millisecond * 100)
// setup the HTTP handler
clientHTTPHandler, err = getHTTPHandler(ctx)
if err != nil {
return err
}
return nil
}
}
func gRPCLoggingServerOptions() []grpc.ServerOption {
logrusEntry := log.NewEntry(log.StandardLogger())
logrusOpts := []grpc_logrus.Option{
grpc_logrus.WithLevels(grpc_logrus.DefaultCodeToLevel),
}
return []grpc.ServerOption{
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, logrusOpts...),
),
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, logrusOpts...),
),
}
}
func mustGetAPIServer() *grpc.Server {
opts := gRPCLoggingServerOptions()
if config.C.ApplicationServer.API.CACert != "" && config.C.ApplicationServer.API.TLSCert != "" && config.C.ApplicationServer.API.TLSKey != "" {
creds := mustGetTransportCredentials(config.C.ApplicationServer.API.TLSCert, config.C.ApplicationServer.API.TLSKey, config.C.ApplicationServer.API.CACert, true)
opts = append(opts, grpc.Creds(creds))
}
gs := grpc.NewServer(opts...)
asAPI := api.NewApplicationServerAPI()
as.RegisterApplicationServerServiceServer(gs, asAPI)
return gs
}
func getHTTPHandler(ctx context.Context) (http.Handler, error) {
r := mux.NewRouter()
// setup json api handler
jsonHandler, err := getJSONGateway(ctx)
if err != nil {
return nil, err
}
log.WithField("path", "/api").Info("registering rest api handler and documentation endpoint")
r.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
data, err := static.Asset("swagger/index.html")
if err != nil {
log.Errorf("get swagger template error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(data)
}).Methods("get")
r.PathPrefix("/api").Handler(jsonHandler)
// setup static file server
r.PathPrefix("/").Handler(http.FileServer(&assetfs.AssetFS{
Asset: static.Asset,
AssetDir: static.AssetDir,
AssetInfo: static.AssetInfo,
Prefix: "",
}))
return wsproxy.WebsocketProxy(r), nil
}
func getJSONGateway(ctx context.Context) (http.Handler, error) {
// dial options for the grpc-gateway
var grpcDialOpts []grpc.DialOption
if config.C.ApplicationServer.ExternalAPI.TLSCert == "" || config.C.ApplicationServer.ExternalAPI.TLSKey == "" {
grpcDialOpts = append(grpcDialOpts, grpc.WithInsecure())
} else {
b, err := ioutil.ReadFile(config.C.ApplicationServer.ExternalAPI.TLSCert)
if err != nil {
return nil, errors.Wrap(err, "read external api tls cert error")
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
return nil, errors.Wrap(err, "failed to append certificate")
}
grpcDialOpts = append(grpcDialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
// given the grpc-gateway is always connecting to localhost, does
// InsecureSkipVerify=true cause any security issues?
InsecureSkipVerify: true,
RootCAs: cp,
})))
}
bindParts := strings.SplitN(config.C.ApplicationServer.ExternalAPI.Bind, ":", 2)
if len(bindParts) != 2 {
log.Fatal("get port from bind failed")
}
apiEndpoint := fmt.Sprintf("localhost:%s", bindParts[1])
mux := runtime.NewServeMux(runtime.WithMarshalerOption(
runtime.MIMEWildcard,
&runtime.JSONPb{
EnumsAsInts: false,
EmitDefaults: true,
},
))
if err := pb.RegisterApplicationServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register application handler error")
}
if err := pb.RegisterDeviceQueueServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register downlink queue handler error")
}
if err := pb.RegisterDeviceServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register node handler error")
}
if err := pb.RegisterUserServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register user handler error")
}
if err := pb.RegisterInternalServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register internal handler error")
}
if err := pb.RegisterGatewayServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register gateway handler error")
}
if err := pb.RegisterGatewayProfileServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register gateway-profile handler error")
}
if err := pb.RegisterOrganizationServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register organization handler error")
}
if err := pb.RegisterNetworkServerServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register network-server handler error")
}
if err := pb.RegisterServiceProfileServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register service-profile handler error")
}
if err := pb.RegisterDeviceProfileServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register device-profile handler error")
}
if err := pb.RegisterMulticastGroupServiceHandlerFromEndpoint(ctx, mux, apiEndpoint, grpcDialOpts); err != nil {
return nil, errors.Wrap(err, "register multicast-group handler error")
}
return mux, nil
}
func mustGetTransportCredentials(tlsCert, tlsKey, caCert string, verifyClientCert bool) credentials.TransportCredentials {
cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
log.WithFields(log.Fields{
"cert": tlsCert,
"key": tlsKey,
}).Fatalf("load key-pair error: %s", err)
}
rawCaCert, err := ioutil.ReadFile(caCert)
if err != nil {
log.WithField("ca", caCert).Fatalf("load ca cert error: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(rawCaCert)
if verifyClientCert {
return credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
})
}
return credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
})
}