-
Notifications
You must be signed in to change notification settings - Fork 19
/
context.go
244 lines (212 loc) · 5.72 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
package context
import (
"io/ioutil"
"os"
"os/signal"
"path"
"sync"
"syscall"
"github.com/baetyl/baetyl-go/v2/errors"
"github.com/baetyl/baetyl-go/v2/log"
"github.com/baetyl/baetyl-go/v2/pki"
"github.com/baetyl/baetyl-go/v2/utils"
)
// Env keys
const (
EnvKeyConfFile = "BAETYL_CONF_FILE"
EnvKeyNodeName = "BAETYL_NODE_NAME"
EnvKeyAppName = "BAETYL_APP_NAME"
EnvKeyServiceName = "BAETYL_SERVICE_NAME"
EnvKeyCodePath = "BAETYL_CODE_PATH"
EnvKeyCertPath = "BAETYL_CERT_PATH"
EnvKeyAppVersion = "BAETYL_APP_VERSION"
SystemCertCA = "ca.pem"
SystemCertCrt = "crt.pem"
SystemCertKey = "key.pem"
SystemCertOU = "BAETYL"
SystemAppInit = "baetyl-init"
)
var (
ErrSystemCertInvalid = errors.New("failed to verify system certificate")
)
// Context of service
type Context interface {
// NodeName returns node name.
NodeName() string
// AppName returns app name.
AppName() string
// AppVersion returns application version.
AppVersion() string
// ServiceName returns service name.
ServiceName() string
// ConfFile returns config file.
ConfFile() string
// ServiceConfig returns service config.
ServiceConfig() ServiceConfig
// 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{})
// Get get system resource object
GetSystemResource() *SystemResource
// LoadCustomConfig loads custom config, if path is empty, will load config from default path.
LoadCustomConfig(cfg interface{}, files ...string) error
// 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
}
type ctx struct {
sync.Map
cfg ServiceConfig
log *log.Logger
nodeName string
appName string
appVersion string
serviceName string
confFile string
httpAddress string
mqttAddress string
certPath string
res *SystemResource
}
// NewContext creates a new context
func NewContext(confFile string) (Context, error) {
if confFile == "" {
confFile = os.Getenv(EnvKeyConfFile)
}
c := &ctx{
confFile: confFile,
nodeName: os.Getenv(EnvKeyNodeName),
appName: os.Getenv(EnvKeyAppName),
appVersion: os.Getenv(EnvKeyAppVersion),
serviceName: os.Getenv(EnvKeyServiceName),
certPath: os.Getenv(EnvKeyCertPath),
res: &SystemResource{},
}
var fs []log.Field
if c.nodeName != "" {
fs = append(fs, log.Any("node", c.nodeName))
}
if c.appName != "" {
fs = append(fs, log.Any("app", c.appName))
}
if c.serviceName != "" {
fs = append(fs, log.Any("service", c.serviceName))
}
c.log = log.With(fs...)
err := c.LoadCustomConfig(&c.cfg)
if err != nil {
c.log.Error("failed to load service config, to use default config", log.Error(err))
utils.UnmarshalYAML(nil, &c.cfg)
}
_log, err := log.Init(c.cfg.Logger, fs...)
if err != nil {
c.log.Error("failed to init logger", log.Error(err))
}
c.log = _log
if c.serviceName != SystemAppInit {
err = c.checkAndSetCert()
if err != nil {
c.Log().Error("service has stopped with error", log.Error(err))
return nil, errors.Trace(err)
}
}
if c.cfg.HTTP.Address == "" {
if c.cfg.HTTP.Key == "" {
c.cfg.HTTP.Address = "http://baetyl-function:80"
} else {
c.cfg.HTTP.Address = "https://baetyl-function:443"
}
}
if c.cfg.MQTT.Address == "" {
if c.cfg.MQTT.Key == "" {
c.cfg.MQTT.Address = "tcp://baetyl-broker:1883"
} else {
c.cfg.MQTT.Address = "ssl://baetyl-broker:8883"
}
}
c.log.Debug("context is created", log.Any("file", confFile), log.Any("conf", c.cfg))
return c, nil
}
func (c *ctx) GetSystemResource() *SystemResource {
return c.res
}
func (c *ctx) NodeName() string {
return c.nodeName
}
func (c *ctx) AppName() string {
return c.appName
}
func (c *ctx) AppVersion() string {
return c.appVersion
}
func (c *ctx) ServiceName() string {
return c.serviceName
}
func (c *ctx) ConfFile() string {
return c.confFile
}
func (c *ctx) ServiceConfig() ServiceConfig {
return c.cfg
}
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) 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) checkAndSetCert() error {
// get and check ca
ca, err := ioutil.ReadFile(path.Join(c.certPath, SystemCertCA))
if err != nil {
return errors.Trace(err)
}
// get crt and key
crt, err := ioutil.ReadFile(path.Join(c.certPath, SystemCertCrt))
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] != SystemCertOU {
return errors.Trace(ErrSystemCertInvalid)
}
key, err := ioutil.ReadFile(path.Join(c.certPath, SystemCertKey))
if err != nil {
return errors.Trace(err)
}
// set
c.res.ca = ca
c.res.crt = crt
c.res.key = key
return nil
}