forked from linksmart/service-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
155 lines (130 loc) · 3.21 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
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
// Copyright 2014-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/go-sec/authz"
"github.com/linksmart/service-catalog/v3/catalog"
)
type Config struct {
ID string `json:"id"`
Description string `json:"description"`
DNSSDEnabled bool `json:"dnssdEnabled"`
Storage StorageConf `json:"storage"`
HTTP HTTPConf `json:"http"`
MQTT catalog.MQTTConf `json:"mqtt"`
Auth ValidatorConf `json:"auth"`
}
func (c *Config) validate() error {
err := c.Storage.validate()
if err != nil {
return err
}
err = c.HTTP.validate()
if err != nil {
return err
}
err = c.MQTT.Validate()
if err != nil {
return err
}
if c.Auth.Enabled {
// Validate ticket validator config
err = c.Auth.validate()
if err != nil {
return err
}
}
return nil
}
func loadConfig(confPath string) (*Config, error) {
file, err := ioutil.ReadFile(confPath)
if err != nil {
return nil, err
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
// Override loaded values with environment variables
err = envconfig.Process("sc", &config)
if err != nil {
return nil, err
}
if err = config.validate(); err != nil {
return nil, err
}
return &config, nil
}
type StorageConf struct {
Type string `json:"type"`
DSN string `json:"dsn"`
}
func (c StorageConf) validate() error {
if !catalog.SupportedBackends[c.Type] {
return fmt.Errorf("storage: unsupported backend")
}
_, err := url.Parse(c.DSN)
if err != nil {
return fmt.Errorf("storage: DSN should be a valid URL: %v", err)
}
return nil
}
type HTTPConf struct {
BindAddr string `json:"bindAddr"`
BindPort int `json:"bindPort"`
}
func (c HTTPConf) validate() error {
if c.BindAddr == "" {
return fmt.Errorf("http: bindAddr not defined")
}
if c.BindPort == 0 {
return fmt.Errorf("http: bindPort not defined")
}
return nil
}
// Ticket Validator Config
type ValidatorConf struct {
// Auth switch
Enabled bool `json:"enabled"`
// Authentication provider name
Provider string `json:"provider"`
// Authentication provider URL
ProviderURL string `json:"providerURL"`
// Service ID
ServiceID string `json:"serviceID"`
// Basic Authentication switch
BasicEnabled bool `json:"basicEnabled"`
// Authorization config
Authz *authz.Conf `json:"authorization"`
}
func (c ValidatorConf) validate() error {
// Validate Provider
if c.Provider == "" {
return errors.New("Ticket Validator: Auth provider name (provider) is not specified.")
}
// Validate ProviderURL
if c.ProviderURL == "" {
return errors.New("Ticket Validator: Auth provider URL (providerURL) is not specified.")
}
_, err := url.Parse(c.ProviderURL)
if err != nil {
return errors.New("Ticket Validator: Auth provider URL (providerURL) is invalid: " + err.Error())
}
// Validate ServiceID
if c.ServiceID == "" {
return errors.New("Ticket Validator: Auth Service ID (serviceID) is not specified.")
}
// Validate Authorization
if c.Authz != nil {
if err := c.Authz.Validate(); err != nil {
return err
}
}
return nil
}