-
Notifications
You must be signed in to change notification settings - Fork 25
/
topaz.go
344 lines (297 loc) · 9.56 KB
/
topaz.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
package app
import (
"context"
"fmt"
"net/http"
"time"
cerr "github.com/aserto-dev/errors"
"github.com/aserto-dev/go-aserto/client"
eds "github.com/aserto-dev/go-edge-ds"
"github.com/aserto-dev/self-decision-logger/logger/self"
decisionlog "github.com/aserto-dev/topaz/decision_log"
"github.com/aserto-dev/topaz/decision_log/logger/file"
"github.com/aserto-dev/topaz/decision_log/logger/nop"
"github.com/aserto-dev/topaz/pkg/app/middlewares"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
console "github.com/aserto-dev/go-topaz-ui"
builder "github.com/aserto-dev/service-host"
"github.com/aserto-dev/topaz/pkg/app/ui"
)
// Topaz is an authorizer service instance, responsible for managing
// the authorizer API, user directory instance and the OPA plugins.
type Topaz struct {
Context context.Context
Logger *zerolog.Logger
ServerOptions []grpc.ServerOption
Configuration *config.Config
ServiceBuilder *builder.ServiceFactory
Manager *builder.ServiceManager
Services map[string]ServiceTypes
}
type ServiceTypes interface {
AvailableServices() []string
GetGRPCRegistrations(services ...string) builder.GRPCRegistrations
GetGatewayRegistration(services ...string) builder.HandlerRegistrations
Cleanups() []func()
}
func (e *Topaz) AddGRPCServerOptions(grpcOptions ...grpc.ServerOption) {
e.ServerOptions = append(e.ServerOptions, grpcOptions...)
}
// Start starts all services required by the engine.
func (e *Topaz) Start() error {
// build dependencies map.
for _, cfg := range e.Configuration.APIConfig.Services {
if len(cfg.Needs) > 0 {
for _, name := range cfg.Needs {
if dependencyConfig, ok := e.Configuration.APIConfig.Services[name]; ok {
if !contains(e.Manager.DependencyMap[cfg.GRPC.ListenAddress], dependencyConfig.GRPC.ListenAddress) &&
cfg.GRPC.ListenAddress != dependencyConfig.GRPC.ListenAddress {
e.Manager.DependencyMap[cfg.GRPC.ListenAddress] = append(e.Manager.DependencyMap[cfg.GRPC.ListenAddress], dependencyConfig.GRPC.ListenAddress)
}
}
}
}
}
err := e.Manager.StartServers(e.Context)
if err != nil {
return errors.Wrap(err, "failed to start engine server")
}
// Add registered services to the health service
if e.Manager.HealthServer != nil {
for serviceName := range e.Configuration.APIConfig.Services {
e.Manager.HealthServer.SetServiceStatus(serviceName, grpc_health_v1.HealthCheckResponse_SERVING)
}
}
return nil
}
func (e *Topaz) ConfigServices() error {
metricsMiddleware, err := e.setupHealthAndMetrics()
if err != nil {
return err
}
if err := e.prepareServices(); err != nil {
return err
}
if err := e.validateConfig(); err != nil {
return err
}
serviceMap := mapToGRPCPorts(e.Configuration.APIConfig.Services)
for address, config := range serviceMap {
e.Logger.Debug().Msgf("configuring address %s", address)
serviceConfig := config
// get middlewares for edge services.
opts, err := middlewares.GetMiddlewaresForService(e.Context, e.Configuration, e.Logger)
if err != nil {
return err
}
opts = append(opts, metricsMiddleware...)
var grpcs []builder.GRPCRegistrations
var gateways []builder.HandlerRegistrations
var cleanups []func()
for _, serv := range e.Services {
notAdded := true
for _, serviceName := range serv.AvailableServices() {
if contains(serviceConfig.registeredServices, serviceName) && notAdded {
grpcs = append(grpcs, serv.GetGRPCRegistrations(serviceConfig.registeredServices...))
gateways = append(gateways, serv.GetGatewayRegistration(serviceConfig.registeredServices...))
cleanups = append(cleanups, serv.Cleanups()...)
notAdded = false
}
}
}
server, err := e.ServiceBuilder.CreateService(
serviceConfig.API,
&builder.GRPCOptions{
ServerOptions: opts,
Registrations: func(server *grpc.Server) {
for _, f := range grpcs {
f(server)
}
},
},
&builder.GatewayOptions{
HandlerRegistrations: func(ctx context.Context, mux *runtime.ServeMux, grpcEndpoint string, opts []grpc.DialOption) error {
for _, f := range gateways {
err := f(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
}
return nil
},
ErrorHandler: cerr.CustomErrorHandler,
},
cleanups...,
)
if err != nil {
return err
}
if con, ok := e.Services[consoleService]; ok {
consoleConfig := con.(*ConsoleService).PrepareConfig(e.Configuration)
if contains(serviceConfig.registeredServices, "console") {
server.Gateway.Mux.Handle("/ui/", ui.UIHandler(http.FS(console.FS)))
server.Gateway.Mux.Handle("/public/", ui.UIHandler(http.FS(console.FS)))
server.Gateway.Mux.HandleFunc("/api/v1/config", ui.ConfigHandler(consoleConfig))
server.Gateway.Mux.HandleFunc("/api/v1/authorizers", ui.AuthorizersHandler(consoleConfig))
}
}
err = e.Manager.AddGRPCServer(server)
if err != nil {
return err
}
}
return nil
}
func (e *Topaz) setupHealthAndMetrics() ([]grpc.ServerOption, error) {
if e.Configuration.APIConfig.Health.ListenAddress != "" {
err := e.Manager.SetupHealthServer(e.Configuration.APIConfig.Health.ListenAddress, e.Configuration.APIConfig.Health.Certificates)
if err != nil {
return nil, err
}
}
if e.Configuration.APIConfig.Metrics.ListenAddress != "" {
metricsMiddleware, err := e.Manager.SetupMetricsServer(e.Configuration.APIConfig.Metrics.ListenAddress,
e.Configuration.APIConfig.Metrics.Certificates,
e.Configuration.APIConfig.Metrics.ZPages)
if err != nil {
return nil, err
}
return metricsMiddleware, nil
}
return nil, nil
}
func (e *Topaz) prepareServices() error {
// prepare services
if e.Configuration.Edge.DBPath != "" {
// for 0.30.* this defaults to true
// starting 0.31.* this defaults to false
// and the topaz configure command exposes the option.
e.Configuration.Edge.EnableV2 = true
dir, err := eds.New(e.Context, &e.Configuration.Edge, e.Logger)
if err != nil {
return err
}
edgeDir, err := NewEdgeDir(dir)
if err != nil {
return err
}
e.Services["edge"] = edgeDir
}
if serviceConfig, ok := e.Configuration.APIConfig.Services[authorizerService]; ok {
authorizer, err := NewAuthorizer(serviceConfig, &e.Configuration.Common, nil, e.Logger)
if err != nil {
return err
}
e.Services["authorizer"] = authorizer
}
if _, ok := e.Configuration.APIConfig.Services[consoleService]; ok {
e.Services["console"] = NewConsole()
}
return nil
}
type services struct {
registeredServices []string
API *builder.API
}
func mapToGRPCPorts(api map[string]*builder.API) map[string]services {
portMap := make(map[string]services)
for key, config := range api {
serv := services{}
if existing, ok := portMap[config.GRPC.ListenAddress]; ok {
serv = existing
serv.registeredServices = append(serv.registeredServices, key)
} else {
serv.registeredServices = append(serv.registeredServices, key)
serv.API = config
}
// set default gateway timeouts
if serv.API.Gateway.ReadTimeout == 0 {
serv.API.Gateway.ReadTimeout = 2 * time.Second
}
if serv.API.Gateway.ReadHeaderTimeout == 0 {
serv.API.Gateway.ReadHeaderTimeout = 2 * time.Second
}
if serv.API.Gateway.WriteTimeout == 0 {
serv.API.Gateway.WriteTimeout = 2 * time.Second
}
if serv.API.Gateway.IdleTimeout == 0 {
serv.API.Gateway.WriteTimeout = 30 * time.Second
}
portMap[config.GRPC.ListenAddress] = serv
}
return portMap
}
func contains[T comparable](slice []T, item T) bool {
for i := range slice {
if slice[i] == item {
return true
}
}
return false
}
func (e *Topaz) GetDecisionLogger(cfg config.DecisionLogConfig) (decisionlog.DecisionLogger, error) {
var decisionlogger decisionlog.DecisionLogger
var err error
switch cfg.Type {
case "self":
decisionlogger, err = self.New(e.Context, cfg.Config, e.Logger, client.NewDialOptionsProvider())
if err != nil {
return nil, err
}
case "file":
maxsize := 0
maxfiles := 0
logpath := cfg.Config["log_file_path"]
maxsize, _ = cfg.Config["max_file_size_mb"].(int)
maxfiles, _ = cfg.Config["max_file_count"].(int)
decisionlogger, err = file.New(e.Context, &file.Config{
LogFilePath: fmt.Sprintf("%v", logpath),
MaxFileSizeMB: maxsize,
MaxFileCount: maxfiles,
}, e.Logger)
if err != nil {
return nil, err
}
default:
decisionlogger, err = nop.New(e.Context, e.Logger)
if err != nil {
return nil, err
}
}
return decisionlogger, err
}
func (e *Topaz) validateConfig() error {
if readerConfig, ok := e.Configuration.APIConfig.Services["reader"]; ok {
if readerConfig.GRPC.ListenAddress != e.Configuration.DirectoryResolver.Address {
return errors.New("remote directory resolver address is different from reader grpc address")
}
}
if _, ok := e.Configuration.APIConfig.Services["console"]; ok {
if _, ok := e.Configuration.APIConfig.Services["reader"]; ok {
if _, ok := e.Configuration.APIConfig.Services["model"]; !ok {
return errors.New("console needs the model service to be configured")
}
}
}
if _, ok := e.Configuration.APIConfig.Services["model"]; !ok {
e.Logger.Info().Msg("model service not configured, you will not be able to read or update the directory manifest")
}
for key := range e.Configuration.APIConfig.Services {
validKey := false
for _, service := range e.Services {
if contains(service.AvailableServices(), key) {
validKey = true
break
}
}
if !validKey {
return errors.Errorf("unknown service type %s", key)
}
}
return nil
}