/
factory.go
574 lines (513 loc) · 18.2 KB
/
factory.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright 2018-2019, CS Systemes d'Information, http://www.c-s.fr
*
* 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 iaas
import (
"encoding/json"
"fmt"
"regexp"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/CS-SI/SafeScale/lib/server/iaas/objectstorage"
"github.com/CS-SI/SafeScale/lib/server/iaas/providers/api"
"github.com/CS-SI/SafeScale/lib/server/iaas/resources"
"github.com/CS-SI/SafeScale/lib/server/iaas/stacks"
"github.com/CS-SI/SafeScale/lib/utils/crypt"
)
var (
allProviders = map[string]Service{}
allTenants = map[string]string{}
)
// Register a Client referenced by the provider name. Ex: "ovh", ovh.New()
// This function shoud be called by the init function of each provider to be registered in SafeScale
func Register(name string, provider api.Provider) {
// if already registered, leave
if _, ok := allProviders[name]; ok {
return
}
allProviders[name] = &service{
Provider: provider,
}
}
// GetTenantNames returns all known tenants names
func GetTenantNames() (map[string]string, error) {
err := loadConfig()
return allTenants, err
}
// GetTenants returns all known tenants
func GetTenants() ([]interface{}, error) {
tenants, err := getTenantsFromCfg()
if err != nil {
return nil, err
}
return tenants, err
}
// UseStorages return the storageService build around storages referenced in tenantNames
func UseStorages(tenantNames []string) (*StorageServices, error) {
storageServices := NewStorageService()
for _, tenantName := range tenantNames {
err := storageServices.RegisterStorage(tenantName)
if err != nil {
return nil, fmt.Errorf("failed to register storage tenant %s : %s", tenantName, err.Error())
}
}
return &storageServices, nil
}
// UseService return the service referenced by the given name.
// If necessary, this function try to load service from configuration file
func UseService(tenantName string) (Service, error) {
tenants, err := getTenantsFromCfg()
if err != nil {
return nil, err
}
var (
tenantInCfg = false
found = false
name string
svc Service
svcProvider = "__not_found__"
)
for _, t := range tenants {
tenant, _ := t.(map[string]interface{})
name, found = tenant["name"].(string)
if !found {
log.Error("tenant found without 'name'")
continue
}
if name != tenantName {
continue
}
tenantInCfg = true
provider, found := tenant["provider"].(string)
if !found {
provider, found = tenant["client"].(string)
if !found {
log.Error("Missing field 'provider' in tenant")
continue
}
}
svcProvider = provider
svc, found = allProviders[provider]
if !found {
log.Errorf("failed to find client '%s' for tenant '%s'", svcProvider, name)
continue
}
// tenantIdentity, found := tenant["identity"].(map[string]interface{})
// if !found {
// log.Debugf("No section 'identity' found in tenant '%s', continuing.", name)
// }
// tenantCompute, found := tenant["compute"].(map[string]interface{})
// if !found {
// log.Debugf("No section 'compute' found in tenant '%s', continuing.", name)
// }
// tenantNetwork, found := tenant["network"].(map[string]interface{})
// if !found {
// log.Debugf("No section 'network' found in tenant '%s', continuing.", name)
// }
_, found = tenant["identity"].(map[string]interface{})
if !found {
log.Debugf("No section 'identity' found in tenant '%s', continuing.", name)
}
_, found = tenant["compute"].(map[string]interface{})
if !found {
log.Debugf("No section 'compute' found in tenant '%s', continuing.", name)
}
_, found = tenant["network"].(map[string]interface{})
if !found {
log.Debugf("No section 'network' found in tenant '%s', continuing.", name)
}
// tenantClient := map[string]interface{}{
// "identity": tenantIdentity,
// "compute": tenantCompute,
// "network": tenantNetwork,
// }
_, tenantObjectStorageFound := tenant["objectstorage"]
_, tenantMetadataFound := tenant["metadata"]
// Initializes Provider
providerInstance, err := svc.Build( /*tenantClient*/ tenant)
if err != nil {
return nil, fmt.Errorf("error creating tenant '%s' on provider '%s': %s", tenantName, provider, err.Error())
}
serviceCfg, err := providerInstance.GetConfigurationOptions()
if err != nil {
return nil, err
}
// Initializes Object Storage
var objectStorageLocation objectstorage.Location
if tenantObjectStorageFound {
objectStorageConfig, err := initObjectStorageLocationConfig(tenant)
if err != nil {
return nil, err
}
objectStorageLocation, err = objectstorage.NewLocation(objectStorageConfig)
if err != nil {
return nil, fmt.Errorf("error connecting to Object Storage Location: %s", err.Error())
}
} else {
log.Warnf("missing section 'objectstorage' in configuration file for tenant '%s'", tenantName)
}
// Initializes Metadata Object Storage (may be different than the Object Storage)
var (
metadataBucket objectstorage.Bucket
metadataCryptKey *crypt.Key
)
if tenantMetadataFound || tenantObjectStorageFound {
// FIXME This requires tunning too
metadataLocationConfig, err := initMetadataLocationConfig(tenant)
if err != nil {
return nil, err
}
metadataLocation, err := objectstorage.NewLocation(metadataLocationConfig)
if err != nil {
return nil, fmt.Errorf("error connecting to Object Storage Location to store metadata: %s", err.Error())
}
anon, found := serviceCfg.Get("MetadataBucketName")
if !found {
return nil, fmt.Errorf("missing configuration option 'MetadataBucketName'")
}
bucketName := anon.(string)
found, err = metadataLocation.FindBucket(bucketName)
if err != nil {
return nil, fmt.Errorf("error accessing metadata location: %s", err.Error())
}
if found {
metadataBucket, err = metadataLocation.GetBucket(bucketName)
if err != nil {
return nil, err
}
} else {
metadataBucket, err = metadataLocation.CreateBucket(bucketName)
if err != nil {
return nil, err
}
}
if metadataConfig, ok := tenant["metadata"].(map[string]interface{}); ok {
ek, err := crypt.NewEncryptionKey([]byte(metadataConfig["CryptKey"].(string)))
if err != nil {
return nil, err
}
metadataCryptKey = ek
}
} else {
return nil, fmt.Errorf("failed to build service: 'metadata' section (and 'objectstorage' as fallback) is missing in configuration file for tenant '%s'", tenantName)
}
// Service is ready
newS := &service{
Provider: providerInstance,
Location: objectStorageLocation,
metadataBucket: metadataBucket,
metadataKey: metadataCryptKey,
}
return newS, validateRegexps(newS /*tenantClient*/, tenant)
}
if !tenantInCfg {
return nil, fmt.Errorf("tenant '%s' not found in configuration", tenantName)
}
return nil, resources.ResourceNotFoundError("provider builder for", svcProvider)
}
// validatRegexps validates regexp values from tenants file
func validateRegexps(svc *service, tenant map[string]interface{}) error {
compute := tenant["compute"].(map[string]interface{})
if reStr, ok := compute["WhitelistTemplateRegexp"].(string); ok {
// Validate regular expression
re, err := regexp.Compile(reStr)
if err != nil {
return fmt.Errorf("invalid value '%s' for field 'WhitelistTemplateRegexp': %s", reStr, err.Error())
}
svc.whitelistTemplateRE = re
}
if reStr, ok := compute["BlacklistTemplateRegexp"].(string); ok {
// Validate regular expression
re, err := regexp.Compile(reStr)
if err != nil {
return fmt.Errorf("invalid value '%s' for field 'BlacklistTemplateRegexp': %s", reStr, err.Error())
}
svc.blacklistTemplateRE = re
}
if reStr, ok := compute["WhitelistImageRegexp"].(string); ok {
// Validate regular expression
re, err := regexp.Compile(reStr)
if err != nil {
return fmt.Errorf("invalid value '%s' for field 'WhitelistImageRegexp': %s", reStr, err.Error())
}
svc.whitelistImageRE = re
}
if reStr, ok := compute["BlacklistImageRegexp"].(string); ok {
// Validate regular expression
re, err := regexp.Compile(reStr)
if err != nil {
return fmt.Errorf("invalid value '%s' for field 'BlacklistImageRegexp': %s", reStr, err.Error())
}
svc.blacklistImageRE = re
}
return nil
}
// initObjectStorageLocationConfig initializes objectstorage.Config struct with map
func initObjectStorageLocationConfig(tenant map[string]interface{}) (objectstorage.Config, error) {
var (
config objectstorage.Config
ok bool
)
identity, _ := tenant["identity"].(map[string]interface{})
compute, _ := tenant["compute"].(map[string]interface{})
ostorage, _ := tenant["objectstorage"].(map[string]interface{})
if config.Type, ok = ostorage["Type"].(string); !ok {
return config, fmt.Errorf("missing setting 'Type' in 'objectstorage' section")
}
if config.Domain, ok = ostorage["Domain"].(string); !ok {
if config.Domain, ok = ostorage["DomainName"].(string); !ok {
if config.Domain, ok = compute["Domain"].(string); !ok {
if config.Domain, ok = compute["DomainName"].(string); !ok {
if config.Domain, ok = identity["Domain"].(string); !ok {
config.Domain, _ = identity["DomainName"].(string)
}
}
}
}
}
config.TenantDomain = config.Domain
if config.Tenant, ok = ostorage["Tenant"].(string); !ok {
if config.Tenant, ok = ostorage["ProjectName"].(string); !ok {
if config.Tenant, ok = ostorage["ProjectID"].(string); !ok {
if config.Tenant, ok = compute["ProjectName"].(string); !ok {
config.Tenant, _ = compute["ProjectID"].(string)
}
}
}
}
config.AuthURL, _ = ostorage["AuthURL"].(string)
config.Endpoint, _ = ostorage["Endpoint"].(string)
if config.User, ok = ostorage["AccessKey"].(string); !ok {
if config.User, ok = ostorage["OpenStackID"].(string); !ok {
if config.User, ok = ostorage["Username"].(string); !ok {
if config.User, ok = identity["OpenstackID"].(string); !ok {
config.User, _ = identity["Username"].(string)
}
}
}
}
if config.Key, ok = ostorage["ApplicationKey"].(string); !ok {
config.Key, _ = identity["ApplicationKey"].(string)
}
if config.SecretKey, ok = ostorage["SecretKey"].(string); !ok {
if config.SecretKey, ok = ostorage["OpenstackPassword"].(string); !ok {
if config.SecretKey, ok = ostorage["Password"].(string); !ok {
if config.SecretKey, ok = identity["SecretKey"].(string); !ok {
if config.SecretKey, ok = identity["OpenstackPassword"].(string); !ok {
config.SecretKey, _ = identity["Password"].(string)
}
}
}
}
}
if config.Region, ok = ostorage["Region"].(string); !ok {
config.Region, _ = compute["Region"].(string)
}
if config.AvailabilityZone, ok = ostorage["AvailabilityZone"].(string); !ok {
config.AvailabilityZone, _ = compute["AvailabilityZone"].(string)
}
// FIXME Remove google custom code
if config.Type == "google" {
if config.ProjectID, ok = identity["project_id"].(string); !ok {
return config, fmt.Errorf("problem parsing project_id")
}
googleCfg := stacks.GCPConfiguration{
Type: "service_account",
ProjectID: identity["project_id"].(string),
PrivateKeyID: identity["private_key_id"].(string),
PrivateKey: identity["private_key"].(string),
ClientEmail: identity["client_email"].(string),
ClientID: identity["client_id"].(string),
AuthURI: identity["auth_uri"].(string),
TokenURI: identity["token_uri"].(string),
AuthProvider: identity["auth_provider_x509_cert_url"].(string),
ClientCert: identity["client_x509_cert_url"].(string),
}
d1, err := json.MarshalIndent(googleCfg, "", " ")
if err != nil {
return config, err
}
config.Credentials = string(d1)
}
return config, nil
}
// initMetadataLocationConfig initializes objectstorage.Config struct with map
func initMetadataLocationConfig(tenant map[string]interface{}) (objectstorage.Config, error) {
var (
config objectstorage.Config
ok bool
)
identity, _ := tenant["identity"].(map[string]interface{})
compute, _ := tenant["compute"].(map[string]interface{})
ostorage, _ := tenant["objectstorage"].(map[string]interface{})
metadata, _ := tenant["metadata"].(map[string]interface{})
if config.Type, ok = metadata["Type"].(string); !ok {
if config.Type, ok = ostorage["Type"].(string); !ok {
return config, fmt.Errorf("missing setting 'Type' in 'metadata' section")
}
}
if config.Domain, ok = metadata["Domain"].(string); !ok {
if config.Domain, ok = metadata["DomainName"].(string); !ok {
if config.Domain, ok = ostorage["Domain"].(string); !ok {
if config.Domain, ok = ostorage["DomainName"].(string); !ok {
if config.Domain, ok = compute["Domain"].(string); !ok {
if config.Domain, ok = compute["DomainName"].(string); !ok {
if config.Domain, ok = identity["Domain"].(string); !ok {
config.Domain, _ = identity["DomainName"].(string)
}
}
}
}
}
}
}
config.TenantDomain = config.Domain
if config.Tenant, ok = metadata["Tenant"].(string); !ok {
if config.Tenant, ok = metadata["ProjectName"].(string); !ok {
if config.Tenant, ok = metadata["ProjectID"].(string); !ok {
if config.Tenant, ok = ostorage["Tenant"].(string); !ok {
if config.Tenant, ok = ostorage["ProjectName"].(string); !ok {
if config.Tenant, ok = ostorage["ProjectID"].(string); !ok {
if config.Tenant, ok = compute["Tenant"].(string); !ok {
if config.Tenant, ok = compute["ProjectName"].(string); !ok {
config.Tenant, _ = compute["ProjectID"].(string)
}
}
}
}
}
}
}
}
if config.AuthURL, ok = metadata["AuthURL"].(string); !ok {
config.AuthURL, _ = ostorage["AuthURL"].(string)
}
if config.Endpoint, ok = metadata["Endpoint"].(string); !ok {
config.Endpoint, _ = ostorage["Endpoint"].(string)
}
if config.User, ok = metadata["AccessKey"].(string); !ok {
if config.User, ok = metadata["OpenstackID"].(string); !ok {
if config.User, ok = metadata["Username"].(string); !ok {
if config.User, ok = ostorage["AccessKey"].(string); !ok {
if config.User, ok = ostorage["OpenStackID"].(string); !ok {
if config.User, ok = ostorage["Username"].(string); !ok {
if config.User, ok = identity["Username"].(string); !ok {
config.User, _ = identity["OpenstackID"].(string)
}
}
}
}
}
}
}
if config.Key, ok = metadata["ApplicationKey"].(string); !ok {
if config.Key, ok = ostorage["ApplicationKey"].(string); !ok {
config.Key, _ = identity["ApplicationKey"].(string)
}
}
if config.SecretKey, ok = metadata["SecretKey"].(string); !ok {
if config.SecretKey, ok = metadata["AccessPassword"].(string); !ok {
if config.SecretKey, ok = metadata["OpenstackPassword"].(string); !ok {
if config.SecretKey, ok = metadata["Password"].(string); !ok {
if config.SecretKey, ok = ostorage["SecretKey"].(string); !ok {
if config.SecretKey, ok = ostorage["AccessPassword"].(string); !ok {
if config.SecretKey, ok = ostorage["OpenstackPassword"].(string); !ok {
if config.SecretKey, ok = ostorage["Password"].(string); !ok {
if config.SecretKey, ok = identity["SecretKey"].(string); !ok {
if config.SecretKey, ok = identity["AccessPassword"].(string); !ok {
if config.SecretKey, ok = identity["Password"].(string); !ok {
config.SecretKey, _ = identity["OpenstackPassword"].(string)
}
}
}
}
}
}
}
}
}
}
}
if config.Region, ok = metadata["Region"].(string); !ok {
if config.Region, ok = ostorage["Region"].(string); !ok {
config.Region, _ = compute["Region"].(string)
}
}
if config.AvailabilityZone, ok = metadata["AvailabilityZone"].(string); !ok {
if config.AvailabilityZone, ok = ostorage["AvailabilityZone"].(string); !ok {
config.AvailabilityZone, _ = compute["AvailabilityZone"].(string)
}
}
// FIXME Remove google custom code
if config.Type == "google" {
if config.ProjectID, ok = identity["project_id"].(string); !ok {
return config, fmt.Errorf("problem parsing project_id")
}
googleCfg := stacks.GCPConfiguration{
Type: "service_account",
ProjectID: identity["project_id"].(string),
PrivateKeyID: identity["private_key_id"].(string),
PrivateKey: identity["private_key"].(string),
ClientEmail: identity["client_email"].(string),
ClientID: identity["client_id"].(string),
AuthURI: identity["auth_uri"].(string),
TokenURI: identity["token_uri"].(string),
AuthProvider: identity["auth_provider_x509_cert_url"].(string),
ClientCert: identity["client_x509_cert_url"].(string),
}
d1, err := json.MarshalIndent(googleCfg, "", " ")
if err != nil {
return config, err
}
config.Credentials = string(d1)
}
return config, nil
}
func loadConfig() error {
tenantsCfg, err := getTenantsFromCfg()
if err != nil {
return err
}
for _, t := range tenantsCfg {
tenant, _ := t.(map[string]interface{})
if name, ok := tenant["name"].(string); ok {
if provider, ok := tenant["client"].(string); ok {
allTenants[name] = provider
} else {
return fmt.Errorf("invalid configuration file '%s'. Tenant '%s' has no client type", v.ConfigFileUsed(), name)
}
} else {
return fmt.Errorf("invalid configuration file. A tenant has no 'name' entry in '%s'", v.ConfigFileUsed())
}
}
return nil
}
var v *viper.Viper
func getTenantsFromCfg() ([]interface{}, error) {
v = viper.New()
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.safescale")
v.AddConfigPath("$HOME/.config/safescale")
v.AddConfigPath("/etc/safescale")
v.SetConfigName("tenants")
if err := v.ReadInConfig(); err != nil { // Handle errors reading the config file
msg := fmt.Sprintf("error reading configuration file: %s", err.Error())
log.Errorf(msg)
return nil, fmt.Errorf(msg)
}
settings := v.AllSettings()
tenantsCfg, _ := settings["tenants"].([]interface{})
return tenantsCfg, nil
}