-
Notifications
You must be signed in to change notification settings - Fork 20
/
orchestrator.go
394 lines (323 loc) · 12.7 KB
/
orchestrator.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
// Copyright 2016-2022 Fraunhofer AISEC
//
// 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.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package orchestrator
import (
"context"
"embed"
"errors"
"fmt"
"io"
"sync"
"clouditor.io/clouditor/api/assessment"
"clouditor.io/clouditor/api/orchestrator"
"clouditor.io/clouditor/persistence"
"clouditor.io/clouditor/persistence/inmemory"
"clouditor.io/clouditor/service"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
//go:embed *.json
var f embed.FS
var defaultMetricConfigurations map[string]*assessment.MetricConfiguration
var log *logrus.Entry
var DefaultMetricsFile = "metrics.json"
var DefaultRequirementsFile = "requirements.json"
// Service is an implementation of the Clouditor Orchestrator service
type Service struct {
orchestrator.UnimplementedOrchestratorServer
// metricConfigurations holds a double-map of metric configurations associated first by service ID and then metric ID
metricConfigurations map[string]map[string]*assessment.MetricConfiguration
// mm is a mutex for metric related maps
mm sync.Mutex
// cloudServiceHooks is a list of hook functions that can be used to inform
// about updated CloudServices
cloudServiceHooks []orchestrator.CloudServiceHookFunc
// hookMutex is used for (un)locking hook calls
hookMutex sync.RWMutex
// Currently only in-memory
results map[string]*assessment.AssessmentResult
// Hook
AssessmentResultHooks []func(result *assessment.AssessmentResult, err error)
// mu is used for (un)locking result hook calls
mu sync.Mutex
storage persistence.Storage
metricsFile string
// loadMetricsFunc is a function that is used to initially load metrics at the start of the orchestrator
loadMetricsFunc func() ([]*assessment.Metric, error)
requirements []*orchestrator.Requirement
events chan *orchestrator.MetricChangeEvent
}
func init() {
log = logrus.WithField("component", "orchestrator")
}
// ServiceOption is a function-style option to configure the Orchestrator Service
type ServiceOption func(*Service)
// WithMetricsFile can be used to load a different metrics file
func WithMetricsFile(file string) ServiceOption {
return func(s *Service) {
s.metricsFile = file
}
}
// WithExternalMetrics can be used to load metric definitions from an external source
func WithExternalMetrics(f func() ([]*assessment.Metric, error)) ServiceOption {
return func(s *Service) {
s.loadMetricsFunc = f
}
}
func WithRequirements(r []*orchestrator.Requirement) ServiceOption {
return func(s *Service) {
s.requirements = r
}
}
// WithStorage is an option to set the storage. If not set, NewService will use inmemory storage.
func WithStorage(storage persistence.Storage) ServiceOption {
return func(s *Service) {
s.storage = storage
}
}
// NewService creates a new Orchestrator service
func NewService(opts ...ServiceOption) *Service {
var err error
s := Service{
results: make(map[string]*assessment.AssessmentResult),
metricConfigurations: make(map[string]map[string]*assessment.MetricConfiguration),
metricsFile: DefaultMetricsFile,
events: make(chan *orchestrator.MetricChangeEvent, 1000),
}
// Apply service options
for _, o := range opts {
o(&s)
}
// Default to an in-memory storage, if nothing was explicitly set
if s.storage == nil {
s.storage, err = inmemory.NewStorage()
if err != nil {
log.Errorf("Could not initialize the storage: %v", err)
}
}
// Load requirements if nothing was specified
if s.requirements == nil {
if s.requirements, err = LoadRequirements(DefaultRequirementsFile); err != nil {
log.Errorf("Could not load embedded requirements. Will continue with empty requirements list: %v", err)
}
}
if err = s.loadMetrics(); err != nil {
log.Errorf("Could not load embedded metrics. Will continue with empty metric list: %v", err)
}
return &s
}
// informHooks informs the registered hook functions
func (s *Service) informHooks(cld *orchestrator.CloudService, err error) {
s.hookMutex.RLock()
hooks := s.cloudServiceHooks
defer s.hookMutex.RUnlock()
// Inform our hook, if we have any
if len(hooks) > 0 {
for _, hook := range hooks {
// We could do hook concurrent again (assuming different hooks don't interfere with each other)
hook(cld, err)
}
}
}
func (s *Service) RegisterCloudServiceHook(hook orchestrator.CloudServiceHookFunc) {
s.hookMutex.Lock()
defer s.hookMutex.Unlock()
s.cloudServiceHooks = append(s.cloudServiceHooks, hook)
}
// StoreAssessmentResult is a method implementation of the orchestrator interface: It receives an assessment result and stores it
func (s *Service) StoreAssessmentResult(_ context.Context, req *orchestrator.StoreAssessmentResultRequest) (resp *orchestrator.StoreAssessmentResultResponse, err error) {
_, err = req.Result.Validate()
if err != nil {
newError := fmt.Errorf("invalid assessment result: %w", err)
log.Error(newError)
go s.informHook(nil, newError)
resp = &orchestrator.StoreAssessmentResultResponse{
Status: false,
StatusMessage: newError.Error(),
}
return resp, status.Errorf(codes.InvalidArgument, "%v", newError)
}
s.results[req.Result.Id] = req.Result
go s.informHook(req.Result, nil)
resp = &orchestrator.StoreAssessmentResultResponse{
Status: true,
}
return resp, nil
}
func (s *Service) StoreAssessmentResults(stream orchestrator.Orchestrator_StoreAssessmentResultsServer) (err error) {
var (
result *orchestrator.StoreAssessmentResultRequest
res *orchestrator.StoreAssessmentResultResponse
)
for {
result, err = stream.Recv()
// If no more input of the stream is available, return
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
newError := fmt.Errorf("cannot receive stream request: %w", err)
log.Error(newError)
return status.Errorf(codes.Unknown, "%v", newError)
}
// Call StoreAssessmentResult() for storing a single assessment
storeAssessmentResultReq := &orchestrator.StoreAssessmentResultRequest{
Result: result.Result,
}
res, err = s.StoreAssessmentResult(context.Background(), storeAssessmentResultReq)
if err != nil {
log.Errorf("Error storing assessment result: %v", err)
}
log.Debugf("Assessment result received (%v)", result.Result.Id)
err = stream.Send(res)
// Check for send errors
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
newError := fmt.Errorf("cannot stream response to the client: %w", err)
log.Error(newError)
return status.Errorf(codes.Unknown, "%v", newError.Error())
}
}
}
// ListAssessmentResults is a method implementation of the orchestrator interface
func (svc *Service) ListAssessmentResults(_ context.Context, req *assessment.ListAssessmentResultsRequest) (res *assessment.ListAssessmentResultsResponse, err error) {
res = new(assessment.ListAssessmentResultsResponse)
// Paginate the results according to the request
res.Results, res.NextPageToken, err = service.PaginateMapValues(req, svc.results, func(a *assessment.AssessmentResult, b *assessment.AssessmentResult) bool {
return a.Id < b.Id
}, service.DefaultPaginationOpts)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not paginate results: %v", err)
}
return
}
func (s *Service) RegisterAssessmentResultHook(hook func(result *assessment.AssessmentResult, err error)) {
s.AssessmentResultHooks = append(s.AssessmentResultHooks, hook)
}
func (s *Service) informHook(result *assessment.AssessmentResult, err error) {
s.mu.Lock()
defer s.mu.Unlock()
// Inform our hook, if we have any
if s.AssessmentResultHooks != nil {
for _, hook := range s.AssessmentResultHooks {
// TODO(all): We could do hook concurrent again (assuming different hooks don't interfere with each other)
hook(result, err)
}
}
}
// CreateCertificate implements method for creating a new certificate
func (svc *Service) CreateCertificate(_ context.Context, req *orchestrator.CreateCertificateRequest) (
*orchestrator.Certificate, error) {
// Validate request
if req == nil {
return nil,
status.Errorf(codes.InvalidArgument, orchestrator.ErrRequestIsNil.Error())
}
if req.Certificate == nil {
return nil,
status.Errorf(codes.InvalidArgument, orchestrator.ErrCertificateIsNil.Error())
}
if req.Certificate.Id == "" {
return nil,
status.Errorf(codes.InvalidArgument, orchestrator.ErrCertIDIsMissing.Error())
}
// Persist the new certificate in our database
err := svc.storage.Create(req.Certificate)
if err != nil {
return nil,
status.Errorf(codes.Internal, "could not add certificate to the database: %v", err)
}
// Return certificate
return req.Certificate, nil
}
// GetCertificate implements method for getting a certificate, e.g. to show its state in the UI
func (svc *Service) GetCertificate(_ context.Context, req *orchestrator.GetCertificateRequest) (response *orchestrator.Certificate, err error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, orchestrator.ErrRequestIsNil.Error())
}
if req.CertificateId == "" {
return nil, status.Errorf(codes.NotFound, orchestrator.ErrCertIDIsMissing.Error())
}
response = new(orchestrator.Certificate)
err = svc.storage.Get(response, "Id = ?", req.CertificateId)
if errors.Is(err, persistence.ErrRecordNotFound) {
return nil, status.Errorf(codes.NotFound, "certificate not found")
} else if err != nil {
return nil, status.Errorf(codes.Internal, "database error: %v", err)
}
return response, nil
}
// ListCertificates implements method for getting a certificate, e.g. to show its state in the UI
func (svc *Service) ListCertificates(_ context.Context, req *orchestrator.ListCertificatesRequest) (res *orchestrator.ListCertificatesResponse, err error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, orchestrator.ErrRequestIsNil.Error())
}
res = new(orchestrator.ListCertificatesResponse)
res.Certificates, res.NextPageToken, err = service.PaginateStorage[*orchestrator.Certificate](req, svc.storage, service.DefaultPaginationOpts)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not paginate results: %v", err)
}
return
}
// UpdateCertificate implements method for updating an existing certificate
func (svc *Service) UpdateCertificate(_ context.Context, req *orchestrator.UpdateCertificateRequest) (response *orchestrator.Certificate, err error) {
if req.CertificateId == "" {
return nil, status.Errorf(codes.InvalidArgument, "certificate id is empty")
}
if req.Certificate == nil {
return nil, status.Errorf(codes.InvalidArgument, "certificate is empty")
}
count, err := svc.storage.Count(req.Certificate, "Certificate_id = ?", req.CertificateId)
if err != nil {
return nil, status.Errorf(codes.Internal, "database error: %v", err)
}
if count == 0 {
return nil, status.Error(codes.NotFound, "certificate not found")
}
response = req.Certificate
response.Id = req.CertificateId
err = svc.storage.Save(response, "Id = ?", response.Id)
if err != nil {
return nil, status.Errorf(codes.Internal, "database error: %v", err)
}
return
}
// RemoveCertificate implements method for removing a certificate
func (svc *Service) RemoveCertificate(_ context.Context, req *orchestrator.RemoveCertificateRequest) (response *emptypb.Empty, err error) {
if req.CertificateId == "" {
return nil, status.Errorf(codes.InvalidArgument, "certificate id is empty")
}
err = svc.storage.Delete(&orchestrator.Certificate{}, "Id = ?", req.CertificateId)
if errors.Is(err, persistence.ErrRecordNotFound) {
return nil, status.Errorf(codes.NotFound, "certificate not found")
} else if err != nil {
return nil, status.Errorf(codes.Internal, "database error: %v", err)
}
return &emptypb.Empty{}, nil
}