-
Notifications
You must be signed in to change notification settings - Fork 3
/
broker.go
384 lines (325 loc) · 10.6 KB
/
broker.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package machineroom
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/choria-io/go-choria/backoff"
"github.com/choria-io/go-choria/broker/adapter"
"github.com/choria-io/go-choria/broker/network"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/choria"
"github.com/choria-io/go-choria/config"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
)
type broker struct {
cfg *config.Config
bi *build.Info
fw *choria.Framework
log *logrus.Entry
opts *Options
broker *network.Server
}
func newBroker(opts *Options, configFile string, bi *build.Info, log *logrus.Entry) (*broker, error) {
if configFile == "" {
return nil, fmt.Errorf("configuration file is required")
}
var err error
instance := &broker{
bi: bi,
opts: opts,
log: log.WithField("machine_room", "broker"),
}
instance.cfg, err = config.NewSystemConfig(configFile, true)
if err != nil {
return nil, fmt.Errorf("could not parse configuration: %s", err)
}
instance.cfg.Choria.BrokerNetwork = true
instance.cfg.CustomLogger = instance.log.Logger
instance.cfg.DisableTLSVerify = false
instance.cfg.Choria.ServerAnonTLS = false
instance.cfg.Choria.UseSRVRecords = false
instance.cfg.Choria.NetworkClientPort = defaultNetworkClientPort
instance.cfg.Choria.NetworkSystemUsername = "system"
// disable some broker things, we dont want people to edit the config file and enable stuff
instance.cfg.Choria.NetworkMappings = []string{}
instance.cfg.Choria.NetworkDenyServers = false
instance.cfg.Choria.NetworkClientTLSForce = false
instance.cfg.Choria.NetworkAllowedClientHosts = []string{}
instance.cfg.Choria.NetworkGatewayPort = 0
instance.cfg.Choria.NetworkLeafPort = 0
instance.cfg.Choria.NetworkWebSocketPort = 0
instance.cfg.Choria.NetworkPeerPort = 0
instance.cfg.Choria.BrokerAdapters = []string{}
// forcing here disables the delay in stream creation at first start
instance.cfg.Choria.NetworkEventStoreReplicas = 1
instance.cfg.Choria.NetworkLeaderElectionReplicas = 1
instance.cfg.Choria.NetworkMachineStoreReplicas = 1
instance.cfg.Choria.NetworkStreamAdvisoryReplicas = 1
// always be running jetstream
instance.cfg.Choria.NetworkStreamStore = opts.ServerStorageDirectory
err = instance.saveCert()
if err != nil {
return nil, fmt.Errorf("TLS setup failed: %v", err)
}
instance.fw, err = choria.NewWithConfig(instance.cfg)
if err != nil {
return nil, err
}
instance.cfg.Choria.BrokerAdapters = []string{"registration"}
mw, err := instance.fw.MiddlewareServers()
if err == nil {
instance.cfg.SetOption("plugin.choria.adapter.registration.stream.servers", strings.Join(mw.Strings(), ","))
instance.cfg.SetOption("plugin.choria.adapter.registration.type", "choria_streams")
instance.cfg.SetOption("plugin.choria.adapter.registration.stream.topic", "machine_room.nodes.%s")
instance.cfg.SetOption("plugin.choria.adapter.registration.stream.workers", "3")
instance.cfg.SetOption("plugin.choria.adapter.registration.ingest.topic", "choria.broadcast.agent.registration")
instance.cfg.SetOption("plugin.choria.adapter.registration.ingest.protocol", "request")
instance.cfg.SetOption("plugin.choria.adapter.registration.ingest.workers", "3")
}
return instance, nil
}
// creates a single use CA just enough to start the broker and allow local
// access, does not allow RPC using the cert that's created
//
// also does not store the CA key so no new certs can be made, remade at every
// start for the moment.
func (b *broker) saveCert() error {
// TODO: if these are valid keep them, eventually we might have more than one process using them so need to not rip it out under that
ca := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Choria.IO"},
Country: []string{"MT"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
// create our private and public key
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
// create the CA
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
return err
}
// pem encode
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
caPrivKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
})
cert := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Choria.IO"},
Country: []string{"MT"},
CommonName: b.cfg.Identity,
},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
DNSNames: []string{"localhost", b.cfg.Identity},
}
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
if err != nil {
return err
}
certPEM := new(bytes.Buffer)
pem.Encode(certPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
certPrivKeyPEM := new(bytes.Buffer)
pem.Encode(certPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
})
b.cfg.Choria.SecurityProvider = "choria"
b.cfg.Choria.ChoriaSecuritySeedFile = filepath.Join(filepath.Dir(b.cfg.ConfigFile), defaultServerSeedFileName)
b.cfg.Choria.ChoriaSecurityCA = filepath.Join(filepath.Dir(b.cfg.ConfigFile), defaultCaFile)
b.cfg.Choria.ChoriaSecurityCertificate = filepath.Join(filepath.Dir(b.cfg.ConfigFile), defaultCertFile)
b.cfg.Choria.ChoriaSecurityKey = filepath.Join(filepath.Dir(b.cfg.ConfigFile), defaultKeyFile)
err = os.WriteFile(b.cfg.Choria.ChoriaSecurityCA, caPEM.Bytes(), 0644)
if err != nil {
return fmt.Errorf("could not write certificate: %v", err)
}
err = os.WriteFile(b.cfg.Choria.ChoriaSecurityCertificate, certPEM.Bytes(), 0644)
if err != nil {
return fmt.Errorf("could not write certificate: %v", err)
}
err = os.WriteFile(b.cfg.Choria.ChoriaSecurityKey, certPrivKeyPEM.Bytes(), 0400)
if err != nil {
return fmt.Errorf("could not write certificate: %v", err)
}
return nil
}
func (b *broker) InProcessConnProvider() nats.InProcessConnProvider {
return b.broker
}
func (b *broker) Start(ctx context.Context, wg *sync.WaitGroup) error {
b.log.Warnf("Choria Machine Room Broker version %s starting with config %s", b.bi.Version(), b.cfg.ConfigFile)
broker, err := network.NewServer(b.fw, b.bi, b.log.Level == logrus.DebugLevel)
if err != nil {
return err
}
b.broker = broker
b.fw.SetInProcessConnProvider(broker)
wg.Add(1)
go b.broker.Start(ctx, wg)
for {
b.log.Infof("Waiting for broker to be started")
if broker.Started() {
break
}
err = backoff.Default.Sleep(ctx, 500*time.Millisecond)
if err != nil {
return err
}
}
go b.setupStreams(ctx)
if len(b.cfg.Choria.BrokerAdapters) > 0 {
b.log.Infof("Starting data adapters: %s", strings.Join(b.cfg.Choria.BrokerAdapters, ", "))
err = adapter.RunAdapters(ctx, b.fw, wg)
if err != nil {
b.log.Errorf("Could not start adapters: %v", err)
}
}
return nil
}
func (b *broker) createDesiredStateBucket(ctx context.Context, nc *nats.Conn) error {
js, err := nc.JetStream(nats.Context(ctx))
if err != nil {
return err
}
_, err = js.KeyValue("CONFIG")
if err == nil {
return nil
}
if errors.Is(err, nats.ErrBucketNotFound) {
_, err = js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "CONFIG", History: 1, Storage: nats.FileStorage})
if err != nil {
return err
}
b.log.Infof("Creating CONFIG bucket")
} else if err != nil {
return err
}
return nil
}
func (b *broker) createRegistrationStream(ctx context.Context, nc *nats.Conn) error {
js, err := nc.JetStream(nats.Context(ctx))
if err != nil {
b.log.Errorf("Could not connect to Machine Room JetStream: %v", err)
return err
}
_, err = js.StreamInfo("REGISTRATION")
if errors.Is(err, nats.ErrStreamNotFound) {
_, err = js.AddStream(&nats.StreamConfig{
Name: "REGISTRATION",
Subjects: []string{"machine_room.nodes.>"},
MaxAge: 24 * time.Hour,
MaxMsgsPerSubject: 5,
Storage: nats.FileStorage,
})
if err != nil {
return err
}
b.log.Infof("Created REGISTRATION stream")
} else if err != nil {
return err
}
return nil
}
func (b *broker) createSubmitStream(ctx context.Context, nc *nats.Conn) error {
js, err := nc.JetStream(nats.Context(ctx))
if err != nil {
b.log.Errorf("Could not connect to Machine Room JetStream: %v", err)
return err
}
_, err = js.StreamInfo("SUBMIT")
if errors.Is(err, nats.ErrStreamNotFound) {
_, err = js.AddStream(&nats.StreamConfig{
Name: "SUBMIT",
Subjects: []string{"choria.submission.in.>"},
MaxAge: 24 * time.Hour,
Storage: nats.FileStorage,
})
if err != nil {
return err
}
b.log.Infof("Created SUBMIT stream")
} else if err != nil {
return err
}
return nil
}
func (b *broker) setupStreams(ctx context.Context) {
b.log.Infof("Setting up Machine Room Streams")
err := backoff.Default.For(ctx, func(try int) error {
if try > 10 {
b.log.Warnf("Machine Room Stream setup still failing after %d tries", try)
}
b.log.Infof("Attempting to set up Machine Room streams: try %d", try)
conn, err := b.fw.NewConnector(ctx, b.fw.MiddlewareServers, "stream_setup", b.log)
if err != nil {
b.log.Errorf("Could not connect to Machine Room broker: %v", err)
return err
}
nc := conn.Nats()
err = b.createDesiredStateBucket(ctx, nc)
if err != nil {
b.log.Errorf("Could not create CONFIG bucket: %v", err)
return err
}
err = b.createRegistrationStream(ctx, nc)
if err != nil {
b.log.Errorf("Could not create Registration stream: %v", err)
return err
}
err = b.createSubmitStream(ctx, nc)
if err != nil {
b.log.Errorf("Could not create Registration stream: %v", err)
return err
}
return nil
})
if err == nil {
b.log.Infof("Machine Room Streams created")
} else {
b.log.Errorf("Could not set up Machine Room streams: %v", err)
}
}