-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
304 lines (278 loc) · 9.27 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
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
package config
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/plgd-dev/device/v2/schema"
c2curi "github.com/plgd-dev/hub/v2/cloud2cloud-connector/uri"
"github.com/plgd-dev/hub/v2/pkg/config/database"
"github.com/plgd-dev/hub/v2/pkg/config/property/urischeme"
pkgCqldb "github.com/plgd-dev/hub/v2/pkg/cqldb"
"github.com/plgd-dev/hub/v2/pkg/log"
pkgMongo "github.com/plgd-dev/hub/v2/pkg/mongodb"
grpcClient "github.com/plgd-dev/hub/v2/pkg/net/grpc/client"
grpcServer "github.com/plgd-dev/hub/v2/pkg/net/grpc/server"
httpClient "github.com/plgd-dev/hub/v2/pkg/net/http/client"
httpServer "github.com/plgd-dev/hub/v2/pkg/net/http/server"
"github.com/plgd-dev/hub/v2/pkg/net/listener"
otelClient "github.com/plgd-dev/hub/v2/pkg/opentelemetry/collector/client"
"github.com/plgd-dev/hub/v2/pkg/security/certManager/client"
"github.com/plgd-dev/hub/v2/pkg/security/certManager/server"
"github.com/plgd-dev/hub/v2/pkg/security/jwt/validator"
"github.com/plgd-dev/hub/v2/pkg/security/oauth2"
"github.com/plgd-dev/hub/v2/pkg/security/oauth2/oauth"
natsClient "github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventbus/nats/client"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/cqldb"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/mongodb"
"github.com/plgd-dev/hub/v2/test/http"
"github.com/plgd-dev/hub/v2/test/oauth-server/uri"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
const (
IDENTITY_STORE_HOST = "localhost:20000"
IDENTITY_STORE_DB = "ownersDevices"
COAP_GW_HOST = "localhost:20002"
RESOURCE_AGGREGATE_HOST = "localhost:20003"
RESOURCE_DIRECTORY_HOST = "localhost:20004"
CERTIFICATE_AUTHORITY_HOST = "localhost:20011"
CERTIFICATE_AUTHORITY_HTTP_HOST = "localhost:20012"
GRPC_GW_HOST = "localhost:20005"
C2C_CONNECTOR_HOST = "localhost:20006"
C2C_CONNECTOR_DB = "cloud2cloudConnector"
C2C_GW_HOST = "localhost:20007"
C2C_GW_DB = "cloud2cloudGateway"
OAUTH_SERVER_HOST = "localhost:20009"
TEST_TIMEOUT = time.Second * 30
OAUTH_MANAGER_CLIENT_ID = "test"
OAUTH_MANAGER_AUDIENCE = "localhost"
HTTP_GW_HOST = "localhost:20010"
DEVICE_PROVIDER = "plgd"
OPENTELEMETRY_COLLECTOR_HOST = "localhost:55690"
TRUE_STRING = "true"
)
var (
CA_POOL = urischeme.URIScheme(os.Getenv("LISTEN_FILE_CA_POOL"))
KEY_FILE = urischeme.URIScheme(os.Getenv("LISTEN_FILE_CERT_DIR_PATH") + "/" + os.Getenv("LISTEN_FILE_CERT_KEY_NAME"))
CERT_FILE = urischeme.URIScheme(os.Getenv("LISTEN_FILE_CERT_DIR_PATH") + "/" + os.Getenv("LISTEN_FILE_CERT_NAME"))
MONGODB_URI = "mongodb://localhost:27017"
NATS_URL = "nats://localhost:4222"
OWNER_CLAIM = "sub"
COAP_GATEWAY_UDP_ENABLED = os.Getenv("TEST_COAP_GATEWAY_UDP_ENABLED") == TRUE_STRING
ACTIVE_COAP_SCHEME = func() string {
if os.Getenv("TEST_COAP_GATEWAY_UDP_ENABLED") == TRUE_STRING {
return string(schema.UDPSecureScheme)
}
return string(schema.TCPSecureScheme)
}()
SCYLLA_HOSTS = []string{"127.0.0.1"}
SCYLLA_PORT = pkgCqldb.DefaultPort
ACTIVE_DATABASE = func() database.DBUse {
if database.DBUse(os.Getenv("TEST_DATABASE")).ToLower() == database.CqlDB.ToLower() {
return database.CqlDB
}
return database.MongoDB
}
)
var (
OAUTH_MANAGER_ENDPOINT_AUTHURL = http.HTTPS_SCHEME + OAUTH_SERVER_HOST + uri.Authorize
OAUTH_MANAGER_ENDPOINT_TOKENURL = http.HTTPS_SCHEME + OAUTH_SERVER_HOST + uri.Token
C2C_CONNECTOR_EVENTS_URL = http.HTTPS_SCHEME + C2C_CONNECTOR_HOST + c2curi.Events
C2C_CONNECTOR_OAUTH_CALLBACK = http.HTTPS_SCHEME + C2C_CONNECTOR_HOST + c2curi.OAuthCallback
)
func MakeTLSClientConfig() client.Config {
cfg := client.Config{
CAPool: CA_POOL,
KeyFile: KEY_FILE,
CertFile: CERT_FILE,
}
_ = cfg.Validate()
return cfg
}
func MakeOpenTelemetryCollectorClient() otelClient.Config {
return otelClient.Config{
GRPC: otelClient.GRPCConfig{
Enabled: false,
Connection: MakeGrpcClientConfig(OPENTELEMETRY_COLLECTOR_HOST),
},
}
}
const DefaultGrpcMaxMsgSize = 1024 * 1024 * 128
func MakeGrpcClientConfig(address string) grpcClient.Config {
return grpcClient.Config{
Addr: address,
SendMsgSize: DefaultGrpcMaxMsgSize,
RecvMsgSize: DefaultGrpcMaxMsgSize,
TLS: MakeTLSClientConfig(),
KeepAlive: grpcClient.KeepAliveConfig{
Time: time.Second * 10,
Timeout: time.Second * 20,
PermitWithoutStream: true,
},
}
}
func MakeTLSServerConfig() server.Config {
return server.Config{
CAPool: CA_POOL,
KeyFile: KEY_FILE,
CertFile: CERT_FILE,
ClientCertificateRequired: true,
}
}
func MakeGrpcServerConfig(address string) grpcServer.Config {
return grpcServer.Config{
Addr: address,
SendMsgSize: DefaultGrpcMaxMsgSize,
RecvMsgSize: DefaultGrpcMaxMsgSize,
TLS: MakeTLSServerConfig(),
Authorization: grpcServer.AuthorizationConfig{
OwnerClaim: OWNER_CLAIM,
Config: MakeAuthorizationConfig(),
},
EnforcementPolicy: grpcServer.EnforcementPolicyConfig{
MinTime: time.Second * 5,
PermitWithoutStream: true,
},
}
}
func MakeListenerConfig(address string) listener.Config {
return listener.Config{
Addr: address,
TLS: MakeTLSServerConfig(),
}
}
func MakeHttpClientConfig() httpClient.Config {
return httpClient.Config{
MaxIdleConns: 16,
MaxConnsPerHost: 32,
MaxIdleConnsPerHost: 16,
IdleConnTimeout: time.Second * 30,
Timeout: time.Second * 10,
TLS: MakeTLSClientConfig(),
}
}
func MakeHttpServerConfig() httpServer.Config {
return httpServer.Config{
ReadTimeout: time.Second * 8,
ReadHeaderTimeout: time.Second * 4,
WriteTimeout: time.Second * 16,
IdleTimeout: time.Second * 30,
}
}
func MakePublisherConfig() natsClient.ConfigPublisher {
return natsClient.ConfigPublisher{
Config: natsClient.Config{
URL: NATS_URL,
TLS: MakeTLSClientConfig(),
FlusherTimeout: time.Second * 30,
},
}
}
func MakeSubscriberConfig() natsClient.Config {
return natsClient.Config{
URL: NATS_URL,
PendingLimits: natsClient.PendingLimitsConfig{
MsgLimit: 524288,
BytesLimit: 67108864,
},
TLS: MakeTLSClientConfig(),
}
}
func MakeEventsStoreMongoDBConfig() *mongodb.Config {
return &mongodb.Config{
Embedded: pkgMongo.Config{
MaxPoolSize: 16,
MaxConnIdleTime: 4 * time.Minute,
URI: MONGODB_URI,
Database: "eventStore",
TLS: MakeTLSClientConfig(),
},
}
}
func MakeCqlDBConfig() pkgCqldb.Config {
return pkgCqldb.Config{
Hosts: SCYLLA_HOSTS,
TLS: MakeTLSClientConfig(),
NumConns: 16,
Port: SCYLLA_PORT,
ConnectTimeout: time.Second * 10,
UseHostnameResolution: true,
ReconnectionPolicy: pkgCqldb.ReconnectionPolicyConfig{
Constant: pkgCqldb.ConstantReconnectionPolicyConfig{
Interval: time.Second * 3,
MaxRetries: 3,
},
},
Keyspace: pkgCqldb.KeyspaceConfig{
Name: "plgdhub",
Create: true,
Replication: map[string]interface{}{
"class": "SimpleStrategy",
"replication_factor": 1,
},
},
}
}
func MakeEventsStoreCqlDBConfig() *cqldb.Config {
return &cqldb.Config{
Table: "events",
Embedded: MakeCqlDBConfig(),
}
}
func MakeAuthorizationConfig() validator.Config {
return validator.Config{
Authority: http.HTTPS_SCHEME + OAUTH_SERVER_HOST,
Audience: http.HTTPS_SCHEME + OAUTH_MANAGER_AUDIENCE,
HTTP: MakeHttpClientConfig(),
}
}
func MakeDeviceAuthorization() oauth2.Config {
return oauth2.Config{
Authority: http.HTTPS_SCHEME + OAUTH_SERVER_HOST,
Config: oauth.Config{
ClientID: OAUTH_MANAGER_CLIENT_ID,
Audience: OAUTH_MANAGER_AUDIENCE,
RedirectURL: "cloud.plgd.mobile://login-callback",
ClientSecretFile: CA_POOL, // any generated file
},
HTTP: MakeHttpClientConfig(),
}
}
func HubID() string {
return os.Getenv("TEST_CLOUD_SID")
}
func MakeAuthURL() string {
return http.HTTPS_SCHEME + OAUTH_SERVER_HOST + uri.Authorize
}
const JWTSecret = "secret"
func CreateJwtToken(t *testing.T, claims jwt.Claims) string {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString([]byte(JWTSecret))
require.NoError(t, err)
return tokenString
}
func MakeLogConfig(t require.TestingT, envLogLevel, envLogDumpBody string) log.Config {
cfg := log.MakeDefaultConfig()
logLvlString := os.Getenv(envLogLevel)
logLvl := zap.NewAtomicLevelAt(log.InfoLevel)
if logLvlString != "" {
var err error
logLvl, err = zap.ParseAtomicLevel(logLvlString)
require.NoError(t, err)
}
cfg.Level = logLvl.Level()
logDumpBodyStr := strings.ToLower(os.Getenv(envLogDumpBody))
switch logDumpBodyStr {
case TRUE_STRING, "false":
cfg.DumpBody = logDumpBodyStr == TRUE_STRING
case "":
cfg.DumpBody = false
default:
require.NoError(t, fmt.Errorf("invalid value %v for %v", logDumpBodyStr, envLogDumpBody))
}
return cfg
}