-
Notifications
You must be signed in to change notification settings - Fork 19
/
orchestrator.go
240 lines (196 loc) · 7.54 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
// 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"
"fmt"
"sync"
"clouditor.io/clouditor/v2/api/assessment"
"clouditor.io/clouditor/v2/api/orchestrator"
"clouditor.io/clouditor/v2/api/runtime"
"clouditor.io/clouditor/v2/internal/config"
"clouditor.io/clouditor/v2/launcher"
"clouditor.io/clouditor/v2/persistence"
"clouditor.io/clouditor/v2/persistence/inmemory"
"clouditor.io/clouditor/v2/server"
"clouditor.io/clouditor/v2/service"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
//go:embed *.json
var f embed.FS
var DefaultMetricsFile = "metrics.json"
var DefaultCatalogsFolder = "catalogs"
var (
defaultMetricConfigurations map[string]*assessment.MetricConfiguration
log *logrus.Entry
)
// DefaultServiceSpec returns a [launcher.ServiceSpec] for this [Service] with all necessary options retrieved from the
// config system.
func DefaultServiceSpec() launcher.ServiceSpec {
return launcher.NewServiceSpec(
NewService,
WithStorage,
func(svc *Service) ([]server.StartGRPCServerOption, error) {
// It is possible to register hook functions for the orchestrator.
// * The hook functions in orchestrator are implemented in StoreAssessmentResult(s)
// svc.RegisterAssessmentResultHook(func(result *assessment.AssessmentResult, err error) {})
// Create default target Cloud Service
if viper.GetBool(config.CreateDefaultTargetFlag) {
_, err := svc.CreateDefaultTargetCloudService()
if err != nil {
return nil, fmt.Errorf("could not register default target cloud service: %v", err)
}
}
return nil, nil
},
)
}
// Service is an implementation of the Clouditor Orchestrator service
type Service struct {
orchestrator.UnimplementedOrchestratorServer
// cloudServiceHooks is a list of hook functions that can be used to inform
// about updated CloudServices
cloudServiceHooks []orchestrator.CloudServiceHookFunc
// toeHooks is a list of hook functions that can be used to inform about updated Target of Evaluations
toeHooks []orchestrator.TargetOfEvaluationHookFunc
// hookMutex is used for (un)locking hook calls
hookMutex sync.RWMutex
// Hook
AssessmentResultHooks []assessment.ResultHookFunc
// 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)
catalogsFolder string
// loadCatalogsFunc is a function that is used to initially load catalogs at the start of the orchestrator
loadCatalogsFunc func() ([]*orchestrator.Catalog, error)
events chan *orchestrator.MetricChangeEvent
// authz defines our authorization strategy, e.g., which user can access which cloud service and associated
// resources, such as evidences and assessment results.
authz service.AuthorizationStrategy
}
func init() {
log = logrus.WithField("component", "orchestrator")
}
// WithMetricsFile can be used to load a different metrics file
func WithMetricsFile(file string) service.Option[*Service] {
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)) service.Option[*Service] {
return func(s *Service) {
s.loadMetricsFunc = f
}
}
// WithCatalogsFolder can be used to load catalog files from a different catalogs folder
func WithCatalogsFolder(folder string) service.Option[*Service] {
return func(s *Service) {
s.catalogsFolder = folder
}
}
// WithExternalCatalogs can be used to load catalog definitions from an external source
func WithExternalCatalogs(f func() ([]*orchestrator.Catalog, error)) service.Option[*Service] {
return func(s *Service) {
s.loadCatalogsFunc = f
}
}
// WithStorage is an option to set the storage. If not set, NewService will use inmemory storage.
func WithStorage(storage persistence.Storage) service.Option[*Service] {
return func(s *Service) {
s.storage = storage
}
}
// WithAuthorizationStrategyJWT is an option that configures an JWT-based authorization strategy using a specific claim key.
func WithAuthorizationStrategyJWT(key string, allowAllKey string) service.Option[*Service] {
return func(s *Service) {
s.authz = &service.AuthorizationStrategyJWT{CloudServicesKey: key, AllowAllKey: allowAllKey}
}
}
func WithAuthorizationStrategy(authz service.AuthorizationStrategy) service.Option[*Service] {
return func(s *Service) {
s.authz = authz
}
}
// NewService creates a new Orchestrator service
func NewService(opts ...service.Option[*Service]) *Service {
var err error
s := Service{
metricsFile: DefaultMetricsFile,
catalogsFolder: DefaultCatalogsFolder,
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)
}
}
// Default to an allow-all authorization strategy
if s.authz == nil {
s.authz = &service.AuthorizationStrategyAllowAll{}
}
if err = s.loadMetrics(); err != nil {
log.Errorf("Could not load embedded metrics. Will continue with empty metric list: %v", err)
}
if err = s.loadCatalogs(); err != nil {
log.Errorf("Could not load embedded catalogs: %v", err)
}
return &s
}
func (svc *Service) Init() {}
func (svc *Service) Shutdown() {}
// informHooks informs the registered hook functions
func (s *Service) informHooks(ctx context.Context, 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(ctx, cld, err)
}
}
}
func (s *Service) RegisterCloudServiceHook(hook orchestrator.CloudServiceHookFunc) {
s.hookMutex.Lock()
defer s.hookMutex.Unlock()
s.cloudServiceHooks = append(s.cloudServiceHooks, hook)
}
// GetRuntimeInfo implements a method to retrieve runtime information
func (*Service) GetRuntimeInfo(_ context.Context, _ *runtime.GetRuntimeInfoRequest) (res *runtime.Runtime, err error) {
return service.GetRuntimeInfo()
}