-
Notifications
You must be signed in to change notification settings - Fork 28
/
sm.go
461 lines (397 loc) · 16.8 KB
/
sm.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
/*
* Copyright 2018 The Service Manager 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 sm
import (
"context"
"database/sql"
"fmt"
"github.com/Peripli/service-manager/operations"
"net/http"
"sync"
secFilters "github.com/Peripli/service-manager/pkg/security/filters"
"github.com/Peripli/service-manager/pkg/env"
"github.com/Peripli/service-manager/pkg/health"
"github.com/Peripli/service-manager/pkg/httpclient"
"github.com/Peripli/service-manager/api/osb"
"github.com/Peripli/service-manager/storage/catalog"
"github.com/Peripli/service-manager/pkg/security"
"github.com/Peripli/service-manager/pkg/types"
"github.com/Peripli/service-manager/storage/interceptors"
"github.com/Peripli/service-manager/api"
"github.com/Peripli/service-manager/api/healthcheck"
"github.com/Peripli/service-manager/config"
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/server"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/storage"
"github.com/Peripli/service-manager/storage/postgres"
"github.com/Peripli/service-manager/api/filters"
"github.com/Peripli/service-manager/pkg/web"
)
// ServiceManagerBuilder type is an extension point that allows adding additional filters, plugins and
// controllers before running ServiceManager.
type ServiceManagerBuilder struct {
*web.API
authnDynamicFilter *web.DynamicMatchingFilter
authzDynamicFilter *web.DynamicMatchingFilter
Storage *storage.InterceptableTransactionalRepository
Notificator storage.Notificator
NotificationCleaner *storage.NotificationCleaner
OperationMaintainer *operations.Maintainer
ctx context.Context
wg *sync.WaitGroup
cfg *config.Settings
secBuilder *securityBuilder
}
// ServiceManager struct
type ServiceManager struct {
ctx context.Context
wg *sync.WaitGroup
Server *server.Server
Notificator storage.Notificator
NotificationCleaner *storage.NotificationCleaner
}
// New returns service-manager Server with default setup
func New(ctx context.Context, cancel context.CancelFunc, e env.Environment, cfg *config.Settings) (*ServiceManagerBuilder, error) {
var err error
if err = cfg.Validate(); err != nil {
return nil, fmt.Errorf("error validating configuration: %s", err)
}
httpclient.Configure(cfg.HTTPClient)
// Setup logging
ctx, err = log.Configure(ctx, cfg.Log)
if err != nil {
return nil, fmt.Errorf("error configuring logging,: %s", err)
}
util.HandleInterrupts(ctx, cancel)
// Setup storage
log.C(ctx).Info("Setting up Service Manager storage...")
smStorage := &postgres.Storage{
ConnectFunc: func(driver string, url string) (*sql.DB, error) {
return sql.Open(driver, url)
},
}
// Decorate the storage with credentials encryption/decryption
encryptingDecorator := storage.EncryptingDecorator(ctx, &security.AESEncrypter{}, smStorage)
// Initialize the storage with graceful termination
var transactionalRepository storage.TransactionalRepository
waitGroup := &sync.WaitGroup{}
if transactionalRepository, err = storage.InitializeWithSafeTermination(ctx, smStorage, cfg.Storage, waitGroup, encryptingDecorator); err != nil {
return nil, fmt.Errorf("error opening storage: %s", err)
}
// Wrap the repository with logic that runs interceptors
interceptableRepository := storage.NewInterceptableTransactionalRepository(transactionalRepository)
// Setup core API
log.C(ctx).Info("Setting up Service Manager core API...")
pgNotificator, err := postgres.NewNotificator(smStorage, cfg.Storage)
if err != nil {
return nil, fmt.Errorf("could not create notificator: %v", err)
}
apiOptions := &api.Options{
Repository: interceptableRepository,
APISettings: cfg.API,
OperationSettings: cfg.Operations,
WSSettings: cfg.WebSocket,
Notificator: pgNotificator,
WaitGroup: waitGroup,
}
API, err := api.New(ctx, e, apiOptions)
if err != nil {
return nil, fmt.Errorf("error creating core api: %s", err)
}
authnDynamicFilter := web.NewDynamicMatchingFilter(secFilters.AuthenticationFilterName)
authzDynamicFilter := web.NewDynamicMatchingFilter(secFilters.AuthorizationFilterName)
API.RegisterFiltersAfter(filters.LoggingFilterName, authnDynamicFilter, authzDynamicFilter)
storageHealthIndicator, err := storage.NewSQLHealthIndicator(storage.PingFunc(smStorage.PingContext))
if err != nil {
return nil, fmt.Errorf("error creating storage health indicator: %s", err)
}
API.SetIndicator(storageHealthIndicator)
API.SetIndicator(healthcheck.NewPlatformIndicator(ctx, interceptableRepository, nil))
notificationCleaner := &storage.NotificationCleaner{
Storage: interceptableRepository,
Settings: *cfg.Storage,
}
operationMaintainer := operations.NewMaintainer(ctx, interceptableRepository, cfg.Operations)
smb := &ServiceManagerBuilder{
API: API,
Storage: interceptableRepository,
Notificator: pgNotificator,
NotificationCleaner: notificationCleaner,
OperationMaintainer: operationMaintainer,
authnDynamicFilter: authnDynamicFilter,
authzDynamicFilter: authzDynamicFilter,
ctx: ctx,
wg: waitGroup,
cfg: cfg,
}
smb.RegisterPlugins(osb.NewCatalogFilterByVisibilityPlugin(interceptableRepository))
smb.RegisterPluginsBefore(osb.CheckInstanceOwnerPluginName, osb.NewStoreServiceInstancesPlugin(interceptableRepository))
smb.RegisterPluginsBefore(osb.StoreServiceInstancePluginName, osb.NewCheckVisibilityPlugin(interceptableRepository))
smb.RegisterPlugins(osb.NewCheckPlatformIDPlugin(interceptableRepository))
// Register default interceptors that represent the core SM business logic
smb.
WithCreateInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerCreateCatalogInterceptorProvider{
CatalogFetcher: osb.CatalogFetcher(http.DefaultClient.Do, cfg.API.OSBVersion),
}).Register().
WithUpdateInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerUpdateCatalogInterceptorProvider{
CatalogFetcher: osb.CatalogFetcher(http.DefaultClient.Do, cfg.API.OSBVersion),
CatalogLoader: catalog.Load,
}).Register().
WithDeleteInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerDeleteCatalogInterceptorProvider{
CatalogLoader: catalog.Load,
}).Register().
WithCreateAroundTxInterceptorProvider(types.PlatformType, &interceptors.GenerateCredentialsInterceptorProvider{}).Register().
WithCreateOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityCreateNotificationsInterceptorProvider{}).Register().
WithUpdateOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityUpdateNotificationsInterceptorProvider{}).Register().
WithDeleteOnTxInterceptorProvider(types.VisibilityType, &interceptors.VisibilityDeleteNotificationsInterceptorProvider{}).Register().
WithCreateOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsCreateInterceptorProvider{}).Before(interceptors.BrokerCreateCatalogInterceptorName).Register().
WithUpdateOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsUpdateInterceptorProvider{}).Before(interceptors.BrokerUpdateCatalogInterceptorName).Register().
WithDeleteOnTxInterceptorProvider(types.ServiceBrokerType, &interceptors.BrokerNotificationsDeleteInterceptorProvider{}).After(interceptors.BrokerDeleteCatalogInterceptorName).Register()
return smb, nil
}
// Build builds the Service Manager
func (smb *ServiceManagerBuilder) Build() *ServiceManager {
if smb.secBuilder != nil {
smb.secBuilder.build()
}
if err := smb.installHealth(); err != nil {
log.C(smb.ctx).Panic(err)
}
// setup server and add relevant global middleware
srv := server.New(smb.cfg.Server, smb.API)
srv.Use(filters.NewRecoveryMiddleware())
// start the operation maintainer
smb.OperationMaintainer.Run()
return &ServiceManager{
ctx: smb.ctx,
wg: smb.wg,
Server: srv,
Notificator: smb.Notificator,
NotificationCleaner: smb.NotificationCleaner,
}
}
func (smb *ServiceManagerBuilder) installHealth() error {
healthz, thresholds, err := health.Configure(smb.ctx, smb.HealthIndicators, smb.cfg.Health)
if err != nil {
return err
}
smb.RegisterControllers(healthcheck.NewController(healthz, thresholds))
if err := healthz.Start(); err != nil {
return err
}
util.StartInWaitGroupWithContext(smb.ctx, func(c context.Context) {
<-c.Done()
log.C(c).Debug("Context cancelled. Stopping health checks...")
if err := healthz.Stop(); err != nil {
log.C(c).Error(err)
}
}, smb.wg)
return nil
}
// Run starts the Service Manager
func (sm *ServiceManager) Run() {
log.C(sm.ctx).Info("Running Service Manager...")
if err := sm.Notificator.Start(sm.ctx, sm.wg); err != nil {
log.C(sm.ctx).WithError(err).Panicf("could not start Service Manager notificator")
}
if err := sm.NotificationCleaner.Start(sm.ctx, sm.wg); err != nil {
log.C(sm.ctx).WithError(err).Panicf("could not start Service Manager notification cleaner")
}
sm.Server.Run(sm.ctx, sm.wg)
sm.wg.Wait()
}
func (smb *ServiceManagerBuilder) RegisterNotificationReceiversFilter(filterFunc storage.ReceiversFilterFunc) {
smb.Notificator.RegisterFilter(filterFunc)
}
func (smb *ServiceManagerBuilder) RegisterExtension(registry Extendable) *ServiceManagerBuilder {
if err := registry.Extend(smb.ctx, smb); err != nil {
log.D().Panicf("Could not register extension: %s", err)
}
return smb
}
func (smb *ServiceManagerBuilder) WithCreateAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.CreateAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithCreateOnTxInterceptorProvider(objectType types.ObjectType, provider storage.CreateOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithCreateInterceptorProvider(objectType types.ObjectType, provider storage.CreateInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddCreateInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithUpdateAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.UpdateAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithUpdateOnTxInterceptorProvider(objectType types.ObjectType, provider storage.UpdateOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithUpdateInterceptorProvider(objectType types.ObjectType, provider storage.UpdateInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddUpdateInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithDeleteAroundTxInterceptorProvider(objectType types.ObjectType, provider storage.DeleteAroundTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteAroundTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithDeleteOnTxInterceptorProvider(objectType types.ObjectType, provider storage.DeleteOnTxInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteOnTxInterceptorProvider(objectType, provider, order)
return smb
},
}
}
func (smb *ServiceManagerBuilder) WithDeleteInterceptorProvider(objectType types.ObjectType, provider storage.DeleteInterceptorProvider) *interceptorRegistrationBuilder {
return &interceptorRegistrationBuilder{
order: storage.InterceptorOrder{
OnTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
AroundTxPosition: storage.InterceptorPosition{
PositionType: storage.PositionNone,
},
},
registrationFunc: func(order storage.InterceptorOrder) *ServiceManagerBuilder {
smb.Storage.AddDeleteInterceptorProvider(objectType, provider, order)
return smb
},
}
}
// EnableMultitenancy enables multitenancy resources for Service Manager by labeling them with appropriate tenant value
func (smb *ServiceManagerBuilder) EnableMultitenancy(labelKey string, extractTenantFunc func(*web.Request) (string, error)) *ServiceManagerBuilder {
if len(labelKey) == 0 {
log.D().Panic("labelKey should be provided")
}
if extractTenantFunc == nil {
log.D().Panic("extractTenantFunc should be provided")
}
multitenancyFilters := filters.NewMultitenancyFilters(labelKey, extractTenantFunc)
smb.RegisterFiltersAfter(filters.ProtectedLabelsFilterName, multitenancyFilters...)
smb.RegisterPlugins(osb.NewCheckInstanceOwnerPlugin(smb.Storage, labelKey))
smb.WithCreateOnTxInterceptorProvider(types.ServiceInstanceType, &interceptors.ServiceInstanceCreateInsterceptorProvider{
TenantIdentifier: labelKey,
}).Register()
smb.WithCreateOnTxInterceptorProvider(types.OperationType, &interceptors.OperationsCreateInsterceptorProvider{
TenantIdentifier: labelKey,
}).Register()
return smb
}
// Security provides mechanism to apply authentication and authorization with a builder pattern
func (smb *ServiceManagerBuilder) Security() *securityBuilder {
if smb.secBuilder == nil {
smb.secBuilder = &securityBuilder{
smb: smb,
}
}
return smb.secBuilder.reset()
}