-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
218 lines (192 loc) · 6.87 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
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
package service
import (
"fmt"
"time"
"github.com/plgd-dev/hub/v2/pkg/config"
"github.com/plgd-dev/hub/v2/pkg/log"
coapService "github.com/plgd-dev/hub/v2/pkg/net/coap/service"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/client"
otelClient "github.com/plgd-dev/hub/v2/pkg/opentelemetry/collector/client"
"github.com/plgd-dev/hub/v2/pkg/security/oauth2"
"github.com/plgd-dev/hub/v2/pkg/security/oauth2/oauth"
"github.com/plgd-dev/hub/v2/pkg/sync/task/queue"
natsClient "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/client"
)
// Config represent application configuration
type Config struct {
Log LogConfig `yaml:"log" json:"log"`
APIs APIsConfig `yaml:"apis" json:"apis"`
Clients ClientsConfig `yaml:"clients" json:"clients"`
TaskQueue queue.Config `yaml:"taskQueue" json:"taskQueue"`
}
func (c *Config) Validate() error {
if err := c.Log.Validate(); err != nil {
return fmt.Errorf("log.%w", err)
}
if err := c.APIs.Validate(); err != nil {
return fmt.Errorf("apis.%w", err)
}
if err := c.Clients.Validate(); err != nil {
return fmt.Errorf("clients.%w", err)
}
if err := c.TaskQueue.Validate(); err != nil {
return fmt.Errorf("taskQueue.%w", err)
}
return nil
}
// Config represent application configuration
type LogConfig struct {
DumpBody bool `yaml:"dumpBody" json:"dumpBody"`
log.Config `yaml:",inline"`
}
type APIsConfig struct {
COAP COAPConfig `yaml:"coap" json:"coap"`
}
func (c *APIsConfig) Validate() error {
if err := c.COAP.Validate(); err != nil {
return fmt.Errorf("coap.%w", err)
}
return nil
}
type ProvidersConfig struct {
Name string `yaml:"name" json:"name"`
oauth2.Config `yaml:",inline"`
}
func (c *ProvidersConfig) Validate(firstAuthority string, providerNames map[string]bool) error {
if c.Authority != firstAuthority {
return fmt.Errorf("authority('%v' != authorization[0].authority('%v'))", c.Authority, firstAuthority)
}
if _, ok := providerNames[c.Name]; ok {
return fmt.Errorf("name('%v' is duplicit)", c.Name)
}
providerNames[c.Name] = true
return c.Config.Validate()
}
type AuthorizationConfig struct {
DeviceIDClaim string `yaml:"deviceIDClaim" json:"deviceIdClaim"`
OwnerClaim string `yaml:"ownerClaim" json:"ownerClaim"`
Providers []ProvidersConfig `yaml:"providers" json:"providers"`
}
func (c *AuthorizationConfig) Validate() error {
if c.OwnerClaim == "" {
return fmt.Errorf("ownerClaim('%v')", c.OwnerClaim)
}
if len(c.Providers) == 0 {
return fmt.Errorf("providers('%v')", c.Providers)
}
duplicitProviderNames := make(map[string]bool)
for i := 0; i < len(c.Providers); i++ {
if c.Providers[i].GrantType == oauth.ClientCredentials && c.OwnerClaim == "sub" {
return fmt.Errorf("providers[%v].grantType - %w", i, fmt.Errorf("combination of ownerClaim set to '%v' is not compatible if at least one authorization provider uses grant type '%v'", c.OwnerClaim, c.Providers[i].GrantType))
}
if err := c.Providers[i].Validate(c.Providers[0].Authority, duplicitProviderNames); err != nil {
return fmt.Errorf("providers[%v].%w", i, err)
}
}
return nil
}
type COAPConfig struct {
ExternalAddress string `yaml:"externalAddress" json:"externalAddress"`
OwnerCacheExpiration time.Duration `yaml:"ownerCacheExpiration" json:"ownerCacheExpiration"`
SubscriptionBufferSize int `yaml:"subscriptionBufferSize" json:"subscriptionBufferSize"`
Authorization AuthorizationConfig `yaml:"authorization" json:"authorization"`
RequireBatchObserveEnabled bool `yaml:"requireBatchObserveEnabled" json:"requireBatchObserveEnabled"`
coapService.Config `yaml:",inline" json:",inline"`
}
func (c *COAPConfig) Validate() error {
if c.ExternalAddress == "" {
return fmt.Errorf("externalAddress('%v')", c.ExternalAddress)
}
if c.OwnerCacheExpiration <= 0 {
return fmt.Errorf("ownerCacheExpiration('%v')", c.OwnerCacheExpiration)
}
if c.SubscriptionBufferSize < 0 {
return fmt.Errorf("subscriptionBufferSize('%v')", c.SubscriptionBufferSize)
}
if err := c.Authorization.Validate(); err != nil {
return fmt.Errorf("authorization.%w", err)
}
return c.Config.Validate()
}
type EventBusConfig struct {
NATS natsClient.Config `yaml:"nats" json:"nats"`
}
func (c *EventBusConfig) Validate() error {
if err := c.NATS.Validate(); err != nil {
return fmt.Errorf("nats.%w", err)
}
return nil
}
type IdentityStoreConfig struct {
Connection client.Config `yaml:"grpc" json:"grpc"`
}
func (c *IdentityStoreConfig) Validate() error {
if err := c.Connection.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
type ClientsConfig struct {
Eventbus EventBusConfig `yaml:"eventBus" json:"eventBus"`
IdentityStore IdentityStoreConfig `yaml:"identityStore" json:"identityStore"`
ResourceAggregate ResourceAggregateConfig `yaml:"resourceAggregate" json:"resourceAggregate"`
ResourceDirectory GrpcServerConfig `yaml:"resourceDirectory" json:"resourceDirectory"`
OpenTelemetryCollector otelClient.Config `yaml:"openTelemetryCollector" json:"openTelemetryCollector"`
}
func (c *ClientsConfig) Validate() error {
if err := c.IdentityStore.Validate(); err != nil {
return fmt.Errorf("identityStore.%w", err)
}
if err := c.Eventbus.Validate(); err != nil {
return fmt.Errorf("eventbus.%w", err)
}
if err := c.ResourceAggregate.Validate(); err != nil {
return fmt.Errorf("resourceAggregate.%w", err)
}
if err := c.ResourceDirectory.Validate(); err != nil {
return fmt.Errorf("resourceDirectory.%w", err)
}
if err := c.OpenTelemetryCollector.Validate(); err != nil {
return fmt.Errorf("openTelemetryCollector.%w", err)
}
return nil
}
type GrpcServerConfig struct {
Connection client.Config `yaml:"grpc" json:"grpc"`
}
func (c *GrpcServerConfig) Validate() error {
if err := c.Connection.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
return nil
}
type ResourceAggregateConfig struct {
Connection client.Config `yaml:"grpc" json:"grpc"`
DeviceStatusExpiration DeviceStatusExpirationConfig `yaml:"deviceStatusExpiration" json:"deviceStatusExpiration"`
}
func (c *ResourceAggregateConfig) Validate() error {
if err := c.Connection.Validate(); err != nil {
return fmt.Errorf("grpc.%w", err)
}
if err := c.DeviceStatusExpiration.Validate(); err != nil {
return fmt.Errorf("deviceStatusExpiration.%w", err)
}
return nil
}
type DeviceStatusExpirationConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
ExpiresIn time.Duration `yaml:"expiresIn" json:"expiresIn"`
}
func (c *DeviceStatusExpirationConfig) Validate() error {
if !c.Enabled {
return nil
}
if c.ExpiresIn < time.Second {
return fmt.Errorf("expiresIn('%v')", c.ExpiresIn)
}
return nil
}
// String return string representation of Config
func (c Config) String() string {
return config.ToString(c)
}