This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
102 lines (90 loc) · 2.74 KB
/
config.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
// Copyright 2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"github.com/kelseyhightower/envconfig"
"github.com/linksmart/historical-datastore/common"
"github.com/linksmart/historical-datastore/data"
"github.com/linksmart/historical-datastore/registry"
)
// loads service configuration from a file at the given path
func loadConfig(confPath *string, ignoreEnv bool) (*common.Config, error) {
file, err := ioutil.ReadFile(*confPath)
if err != nil {
return nil, err
}
var conf common.Config
err = json.Unmarshal(file, &conf)
if err != nil {
return nil, err
}
// Override loaded values with environment variables
if !ignoreEnv {
err = envconfig.Process("hds", &conf)
if err != nil {
return nil, err
}
}
// VALIDATE HTTP
if conf.HTTP.BindAddr == "" || conf.HTTP.BindPort == 0 || conf.HTTP.PublicEndpoint == "" {
return nil, fmt.Errorf("HTTP bindAddr, publicEndpoint, and bindPort have to be defined")
}
_, err = url.Parse(conf.HTTP.PublicEndpoint)
if err != nil {
return nil, fmt.Errorf("HTTP PublicEndpoint should be a valid BrokerURL")
}
// VALIDATE REGISTRY API CONFIG
// Check if backend is supported
if !registry.SupportedBackends(conf.Registry.Backend.Type) {
return nil, fmt.Errorf("backend type is not supported: %s", conf.Registry.Backend.Type)
}
// Check DSN
_, err = url.Parse(conf.Registry.Backend.DSN)
if err != nil {
return nil, err
}
// VALIDATE DATA API CONFIG
// Check if backend is supported
if !data.SupportedBackends(conf.Data.Backend.Type) {
return nil, fmt.Errorf("Data backend type is not supported: %s", conf.Data.Backend.Type)
}
// Check DSN
_, err = url.Parse(conf.Data.Backend.DSN)
if err != nil {
return nil, err
}
// VALIDATE SERVICE CATALOG CONFIG
if conf.ServiceCatalog.Enabled {
if conf.ServiceCatalog.Endpoint == "" && conf.ServiceCatalog.Discover == false {
return nil, errors.New("All ServiceCatalog entries must have either endpoint or a discovery flag defined")
}
if conf.ServiceCatalog.TTL <= 0 {
return nil, errors.New("All ServiceCatalog entries must have TTL >= 0")
}
if conf.ServiceCatalog.Auth.Enabled {
// Validate ticket obtainer config
err = conf.ServiceCatalog.Auth.Validate()
if err != nil {
return nil, fmt.Errorf("invalid Service Catalog auth: %w", err)
}
}
}
if conf.Auth.Enabled {
// Validate ticket validator config
supportedProviders := map[string]bool{
common.Keycloak: true,
}
if !supportedProviders[conf.Auth.Provider] {
return nil, fmt.Errorf("auth provider %s is not supported", conf.Auth.Provider)
}
err = conf.Auth.Validate()
if err != nil {
return nil, fmt.Errorf("invalid auth: %w", err)
}
}
return &conf, nil
}