-
Notifications
You must be signed in to change notification settings - Fork 19
/
context.go
284 lines (252 loc) · 7.23 KB
/
context.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
package context
import (
"io/ioutil"
"os"
"os/signal"
"sync"
"syscall"
"github.com/baetyl/baetyl-go/v2/errors"
"github.com/baetyl/baetyl-go/v2/http"
"github.com/baetyl/baetyl-go/v2/log"
"github.com/baetyl/baetyl-go/v2/mqtt"
"github.com/baetyl/baetyl-go/v2/pki"
"github.com/baetyl/baetyl-go/v2/utils"
)
var (
ErrSystemCertInvalid = errors.New("system certificate is invalid")
ErrSystemCertNotFound = errors.New("system certificate is not found")
)
// Context of service
type Context interface {
// NodeName returns node name from data.
NodeName() string
// AppName returns app name from data.
AppName() string
// AppVersion returns application version from data.
AppVersion() string
// ServiceName returns service name from data.
ServiceName() string
// ConfFile returns config file from data.
ConfFile() string
// SystemConfig returns the config of baetyl system from data.
SystemConfig() *SystemConfig
// Log returns logger interface.
Log() *log.Logger
// Wait waits until exit, receiving SIGTERM and SIGINT signals.
Wait()
// WaitChan returns wait channel.
WaitChan() <-chan os.Signal
// Load returns the value stored in the map for a key, or nil if no value is present.
// The ok result indicates whether value was found in the map.
Load(key interface{}) (value interface{}, ok bool)
// Store sets the value for a key.
Store(key, value interface{})
// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
// Delete deletes the value for a key.
Delete(key interface{})
// CheckSystemCert checks system certificate, if certificate is not found or invalid, returns an error.
CheckSystemCert() error
// LoadCustomConfig loads custom config.
// If 'files' is empty, will load config from default path,
// else the first file path will be used to load config from.
LoadCustomConfig(cfg interface{}, files ...string) error
// NewFunctionHttpClient creates a new function http client.
NewFunctionHttpClient() (*http.Client, error)
// NewSystemBrokerClientConfig creates the system config of broker
NewSystemBrokerClientConfig() (mqtt.ClientConfig, error)
// NewBrokerClient creates a new broker client.
NewBrokerClient(mqtt.ClientConfig) (*mqtt.Client, error)
}
type ctx struct {
sync.Map // global cache
log *log.Logger
}
// NewContext creates a new context
func NewContext(confFile string) Context {
if confFile == "" {
confFile = os.Getenv(KeyConfFile)
}
c := &ctx{}
c.Store(KeyConfFile, confFile)
c.Store(KeyNodeName, os.Getenv(KeyNodeName))
c.Store(KeyAppName, os.Getenv(KeyAppName))
c.Store(KeyAppVersion, os.Getenv(KeyAppVersion))
c.Store(KeySvcName, os.Getenv(KeySvcName))
var lfs []log.Field
if c.NodeName() != "" {
lfs = append(lfs, log.Any("node", c.NodeName()))
}
if c.AppName() != "" {
lfs = append(lfs, log.Any("app", c.AppName()))
}
if c.ServiceName() != "" {
lfs = append(lfs, log.Any("service", c.ServiceName()))
}
c.log = log.With(lfs...)
c.log.Info("to load config file", log.Any("file", c.ConfFile()))
sc := &SystemConfig{}
err := c.LoadCustomConfig(sc)
if err != nil {
c.log.Error("failed to load system config, to use default config", log.Error(err))
utils.UnmarshalYAML(nil, sc)
}
// populate configuration
// if not set in config file, to use value from env.
// if not set in env, to use default value.
if sc.Function.Address == "" {
sc.Function.Address = getFunctionAddress()
}
if sc.Function.CA == "" {
sc.Function.CA = sc.Certificate.CA
}
if sc.Function.Key == "" {
sc.Function.Key = sc.Certificate.Key
}
if sc.Function.Cert == "" {
sc.Function.Cert = sc.Certificate.Cert
}
if sc.Broker.Address == "" {
sc.Broker.Address = getBrokerAddress()
}
// auto subscribe link topic for service if service name not nil.
if sc.Broker.Subscriptions == nil {
sc.Broker.Subscriptions = []mqtt.QOSTopic{}
}
if c.ServiceName() != "" {
if sc.Broker.ClientID == "" {
sc.Broker.ClientID = "baetyl-link-" + c.ServiceName()
}
sc.Broker.Subscriptions = append(sc.Broker.Subscriptions, mqtt.QOSTopic{QOS: 1, Topic: "$link/" + c.ServiceName()})
}
if sc.Broker.CA == "" {
sc.Broker.CA = sc.Certificate.CA
}
if sc.Broker.Key == "" {
sc.Broker.Key = sc.Certificate.Key
}
if sc.Broker.Cert == "" {
sc.Broker.Cert = sc.Certificate.Cert
}
c.Store(KeySysConf, sc)
_log, err := log.Init(sc.Logger, lfs...)
if err != nil {
c.log.Error("failed to init logger", log.Error(err))
}
c.log = _log
c.log.Debug("context is created", log.Any("file", confFile), log.Any("conf", sc))
return c
}
func (c *ctx) NodeName() string {
v, ok := c.Load(KeyNodeName)
if !ok {
return ""
}
return v.(string)
}
func (c *ctx) AppName() string {
v, ok := c.Load(KeyAppName)
if !ok {
return ""
}
return v.(string)
}
func (c *ctx) AppVersion() string {
v, ok := c.Load(KeyAppVersion)
if !ok {
return ""
}
return v.(string)
}
func (c *ctx) ServiceName() string {
v, ok := c.Load(KeySvcName)
if !ok {
return ""
}
return v.(string)
}
func (c *ctx) ConfFile() string {
v, ok := c.Load(KeyConfFile)
if !ok {
return ""
}
return v.(string)
}
func (c *ctx) SystemConfig() *SystemConfig {
v, ok := c.Load(KeySysConf)
if !ok {
return nil
}
return v.(*SystemConfig)
}
func (c *ctx) Log() *log.Logger {
return c.log
}
func (c *ctx) Wait() {
<-c.WaitChan()
}
func (c *ctx) WaitChan() <-chan os.Signal {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
signal.Ignore(syscall.SIGPIPE)
return sig
}
func (c *ctx) CheckSystemCert() error {
cfg := c.SystemConfig().Certificate
if !utils.FileExists(cfg.CA) || !utils.FileExists(cfg.Key) || !utils.FileExists(cfg.Cert) {
return errors.Trace(ErrSystemCertNotFound)
}
crt, err := ioutil.ReadFile(cfg.Cert)
if err != nil {
return errors.Trace(err)
}
info, err := pki.ParseCertificates(crt)
if err != nil {
return errors.Trace(err)
}
if len(info) != 1 || len(info[0].Subject.OrganizationalUnit) != 1 ||
info[0].Subject.OrganizationalUnit[0] != KeyBaetyl {
return errors.Trace(ErrSystemCertInvalid)
}
return nil
}
func (c *ctx) LoadCustomConfig(cfg interface{}, files ...string) error {
f := c.ConfFile()
if len(files) > 0 && len(files[0]) > 0 {
f = files[0]
}
if utils.FileExists(f) {
return errors.Trace(utils.LoadYAML(f, cfg))
}
return errors.Trace(utils.UnmarshalYAML(nil, cfg))
}
func (c *ctx) NewFunctionHttpClient() (*http.Client, error) {
err := c.CheckSystemCert()
if err != nil {
return nil, errors.Trace(err)
}
ops, err := c.SystemConfig().Function.ToClientOptions()
if err != nil {
return nil, errors.Trace(err)
}
return http.NewClient(ops), nil
}
func (c *ctx) NewSystemBrokerClientConfig() (mqtt.ClientConfig, error) {
err := c.CheckSystemCert()
if err != nil {
return mqtt.ClientConfig{}, errors.Trace(err)
}
config := c.SystemConfig().Broker
config.Subscriptions = make([]mqtt.QOSTopic, 0)
copy(config.Subscriptions, c.SystemConfig().Broker.Subscriptions)
return config, nil
}
func (c *ctx) NewBrokerClient(config mqtt.ClientConfig) (*mqtt.Client, error) {
ops, err := config.ToClientOptions()
if err != nil {
return nil, errors.Trace(err)
}
return mqtt.NewClient(ops), nil
}