-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
service.go
222 lines (201 loc) · 6.13 KB
/
service.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
package service
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"regexp"
"sync"
"google.golang.org/grpc"
connectorStore "github.com/plgd-dev/cloud/cloud2cloud-connector/store"
"github.com/plgd-dev/cloud/cloud2cloud-connector/uri"
"github.com/plgd-dev/kit/log"
kitNetHttp "github.com/plgd-dev/kit/net/http"
"github.com/plgd-dev/kit/security/oauth/manager"
"google.golang.org/grpc/credentials"
pbAS "github.com/plgd-dev/cloud/authorization/pb"
pbGRPC "github.com/plgd-dev/cloud/grpc-gateway/pb"
pbRA "github.com/plgd-dev/cloud/resource-aggregate/pb"
kitNetGrpc "github.com/plgd-dev/kit/net/grpc"
)
//Server handle HTTP request
type Server struct {
server *http.Server
cfg Config
handler *RequestHandler
ln net.Listener
cancel context.CancelFunc
doneWg *sync.WaitGroup
raConn *grpc.ClientConn
authConn *grpc.ClientConn
rdConn *grpc.ClientConn
}
type DialCertManager = interface {
GetClientTLSConfig() *tls.Config
}
type ListenCertManager = interface {
GetServerTLSConfig() *tls.Config
}
func runDevicePulling(ctx context.Context,
config Config,
s *Store,
asClient pbAS.AuthorizationServiceClient,
raClient pbRA.ResourceAggregateClient,
devicesSubscription *DevicesSubscription,
subscriptionManager *SubscriptionManager,
triggerTask func(Task),
) bool {
ctx, cancel := context.WithTimeout(ctx, config.PullDevicesInterval)
defer cancel()
err := pullDevices(ctx, s, asClient, raClient, devicesSubscription, subscriptionManager, config.OAuthCallback, triggerTask)
if err != nil {
log.Errorf("cannot pull devices: %v", err)
}
select {
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return false
}
}
return true
}
//New create new Server with provided store and bus
func New(config Config, dialCertManager DialCertManager, listenCertManager ListenCertManager, db connectorStore.Store) *Server {
dialTLSConfig := dialCertManager.GetClientTLSConfig()
var ln net.Listener
var err error
if listenCertManager != nil {
listenTLSConfig := listenCertManager.GetServerTLSConfig()
ln, err = tls.Listen("tcp", config.Addr, listenTLSConfig)
if err != nil {
log.Fatalf("cannot listen and serve: %v", err)
}
} else {
ln, err = net.Listen("tcp", config.Addr)
if err != nil {
log.Fatalf("cannot listen and serve: %v", err)
}
}
oauthMgr, err := manager.NewManagerFromConfiguration(config.OAuth, dialTLSConfig)
if err != nil {
log.Fatalf("cannot create oauth manager: %w", err)
}
raConn, err := grpc.Dial(config.ResourceAggregateAddr,
grpc.WithTransportCredentials(credentials.NewTLS(dialTLSConfig)),
grpc.WithPerRPCCredentials(kitNetGrpc.NewOAuthAccess(oauthMgr.GetToken)),
)
if err != nil {
log.Fatalf("cannot create server: %v", err)
}
raClient := pbRA.NewResourceAggregateClient(raConn)
authConn, err := grpc.Dial(config.AuthServerAddr,
grpc.WithTransportCredentials(credentials.NewTLS(dialTLSConfig)),
grpc.WithPerRPCCredentials(kitNetGrpc.NewOAuthAccess(oauthMgr.GetToken)),
)
if err != nil {
log.Fatalf("cannot create server: %v", err)
}
asClient := pbAS.NewAuthorizationServiceClient(authConn)
rdConn, err := grpc.Dial(config.ResourceDirectoryAddr,
grpc.WithTransportCredentials(credentials.NewTLS(dialTLSConfig)),
grpc.WithPerRPCCredentials(kitNetGrpc.NewOAuthAccess(oauthMgr.GetToken)),
)
if err != nil {
log.Fatalf("cannot create server: %v", err)
}
rdClient := pbGRPC.NewGrpcGatewayClient(rdConn)
_, err = url.Parse(config.OAuthCallback)
if err != nil {
log.Fatalf("cannot create server: %v", err)
}
store, err := NewStore(context.Background(), db)
if err != nil {
log.Fatalf("cannot create server: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
devicesSubscription := NewDevicesSubscription(ctx, rdClient, raClient, config.ReconnectInterval)
taskProcessor := NewTaskProcessor(raClient, config.TaskProcessor.MaxParallel, config.TaskProcessor.CacheSize, config.TaskProcessor.Timeout, config.TaskProcessor.Delay)
subscriptionManager := NewSubscriptionManager(config.EventsURL, asClient, raClient, store, devicesSubscription, config.OAuthCallback, taskProcessor.Trigger, config.ResubscribeInterval)
requestHandler := NewRequestHandler(config.OAuthCallback, subscriptionManager, asClient, raClient, store, taskProcessor.Trigger)
var wg sync.WaitGroup
if !config.PullDevicesDisabled {
wg.Add(1)
go func() {
defer wg.Done()
for runDevicePulling(ctx, config, store, asClient, raClient, devicesSubscription, subscriptionManager, taskProcessor.Trigger) {
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
taskProcessor.Run(ctx, subscriptionManager)
}()
wg.Add(1)
go func() {
defer wg.Done()
subscriptionManager.Run(ctx)
}()
oauthURL, _ := url.Parse(config.OAuthCallback)
auth := kitNetHttp.NewInterceptor(config.JwksURL, dialCertManager.GetClientTLSConfig(), authRules, kitNetHttp.RequestMatcher{
Method: http.MethodGet,
URI: regexp.MustCompile(regexp.QuoteMeta(oauthURL.Path)),
}, kitNetHttp.RequestMatcher{
Method: http.MethodPost,
URI: regexp.MustCompile(regexp.QuoteMeta(oauthURL.Path)),
}, kitNetHttp.RequestMatcher{
Method: http.MethodPost,
URI: regexp.MustCompile(regexp.QuoteMeta(uri.Events)),
},
)
server := Server{
server: NewHTTP(requestHandler, auth),
cfg: config,
handler: requestHandler,
ln: ln,
cancel: cancel,
doneWg: &wg,
raConn: raConn,
rdConn: rdConn,
authConn: authConn,
}
return &server
}
// Serve starts the service's HTTP server and blocks.
func (s *Server) Serve() error {
defer func() {
s.raConn.Close()
s.rdConn.Close()
s.authConn.Close()
}()
return s.server.Serve(s.ln)
}
// Shutdown ends serving
func (s *Server) Shutdown() error {
s.cancel()
s.doneWg.Wait()
return s.server.Shutdown(context.Background())
}
var authRules = map[string][]kitNetHttp.AuthArgs{
http.MethodGet: {
{
URI: regexp.MustCompile(regexp.QuoteMeta(uri.API) + `\/.*`),
},
},
http.MethodPost: {
{
URI: regexp.MustCompile(regexp.QuoteMeta(uri.API) + `\/.*`),
},
},
http.MethodDelete: {
{
URI: regexp.MustCompile(regexp.QuoteMeta(uri.API) + `\/.*`),
},
},
http.MethodPut: {
{
URI: regexp.MustCompile(regexp.QuoteMeta(uri.API) + `\/.*`),
},
},
}