-
Notifications
You must be signed in to change notification settings - Fork 107
/
sentry.go
207 lines (172 loc) · 6.44 KB
/
sentry.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
package oasis
import (
"fmt"
"net"
"strconv"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
fileSigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/file"
"github.com/oasisprotocol/oasis-core/go/common/identity"
commonNode "github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/crypto"
)
// Sentry is an Oasis sentry node.
type Sentry struct {
*Node
validatorIndices []int
computeIndices []int
keymanagerIndices []int
p2pPublicKey signature.PublicKey
tlsPublicKey signature.PublicKey
tmAddress string
consensusPort uint16
controlPort uint16
sentryPort uint16
}
// SentryCfg is the Oasis sentry node configuration.
type SentryCfg struct {
NodeCfg
ValidatorIndices []int
ComputeIndices []int
KeymanagerIndices []int
}
// TLSCertPath returns the path to the node's TLS certificate.
func (sentry *Sentry) TLSCertPath() string {
return nodeTLSCertPath(sentry.dir)
}
// GetTLSPubKey returns the sentry TLS public key.
func (sentry *Sentry) GetTLSPubKey() signature.PublicKey {
return sentry.tlsPublicKey
}
// GetSentryAddress returns the sentry grpc endpoint address.
func (sentry *Sentry) GetSentryAddress() string {
return fmt.Sprintf("127.0.0.1:%d", sentry.sentryPort)
}
// GetSentryControlAddress returns the sentry control endpoint address.
func (sentry *Sentry) GetSentryControlAddress() string {
return fmt.Sprintf("127.0.0.1:%d", sentry.controlPort)
}
func (sentry *Sentry) AddArgs(args *argBuilder) error {
args.
configureDebugCrashPoints(sentry.crashPointsProbability).
appendNetwork(sentry.net)
return nil
}
func (sentry *Sentry) ModifyConfig() error {
sentry.Config.Consensus.ListenAddress = allInterfacesAddr + ":" + strconv.Itoa(int(sentry.consensusPort))
sentry.Config.Consensus.ExternalAddress = localhostAddr + ":" + strconv.Itoa(int(sentry.consensusPort))
if sentry.supplementarySanityInterval > 0 {
sentry.Config.Consensus.SupplementarySanity.Enabled = true
sentry.Config.Consensus.SupplementarySanity.Interval = sentry.supplementarySanityInterval
}
sentry.Config.Sentry.Enabled = true
sentry.Config.Sentry.Control.Port = sentry.controlPort
sentry.AddSeedNodesToConfig()
validators, err := resolveValidators(sentry.net, sentry.validatorIndices)
if err != nil {
return err
}
computeWorkers, err := resolveComputeWorkers(sentry.net, sentry.computeIndices)
if err != nil {
return err
}
keymanagerWorkers, err := resolveKeymanagerWorkers(sentry.net, sentry.keymanagerIndices)
if err != nil {
return err
}
if len(validators) > 0 {
var addrs, sentryPubKeys []string
for _, val := range validators {
addr := commonNode.ConsensusAddress{
ID: val.p2pSigner,
Address: commonNode.Address{
IP: net.ParseIP("127.0.0.1"),
Port: int64(val.consensusPort),
},
}
addrs = append(addrs, addr.String())
key, _ := val.sentryPubKey.MarshalText()
sentryPubKeys = append(sentryPubKeys, string(key))
}
sentry.Config.Sentry.Control.AuthorizedPubkeys = append(sentry.Config.Sentry.Control.AuthorizedPubkeys, sentryPubKeys...)
sentry.Config.Consensus.SentryUpstreamAddresses = append(sentry.Config.Consensus.SentryUpstreamAddresses, addrs...)
}
if len(computeWorkers) > 0 {
var tmAddrs, sentryPubKeys []string
for _, computeWorker := range computeWorkers {
addr := commonNode.ConsensusAddress{
ID: computeWorker.p2pSigner,
Address: commonNode.Address{
IP: net.ParseIP("127.0.0.1"),
Port: int64(computeWorker.consensusPort),
},
}
tmAddrs = append(tmAddrs, addr.String())
key, _ := computeWorker.sentryPubKey.MarshalText()
sentryPubKeys = append(sentryPubKeys, string(key))
}
sentry.Config.Sentry.Control.AuthorizedPubkeys = append(sentry.Config.Sentry.Control.AuthorizedPubkeys, sentryPubKeys...)
sentry.Config.Consensus.SentryUpstreamAddresses = append(sentry.Config.Consensus.SentryUpstreamAddresses, tmAddrs...)
}
if len(keymanagerWorkers) > 0 {
var tmAddrs, sentryPubKeys []string
for _, keymanager := range keymanagerWorkers {
addr := commonNode.ConsensusAddress{
ID: keymanager.p2pSigner,
Address: commonNode.Address{
IP: net.ParseIP("127.0.0.1"),
Port: int64(keymanager.consensusPort),
},
}
tmAddrs = append(tmAddrs, addr.String())
key, _ := keymanager.sentryPubKey.MarshalText()
sentryPubKeys = append(sentryPubKeys, string(key))
}
sentry.Config.Sentry.Control.AuthorizedPubkeys = append(sentry.Config.Sentry.Control.AuthorizedPubkeys, sentryPubKeys...)
sentry.Config.Consensus.SentryUpstreamAddresses = append(sentry.Config.Consensus.SentryUpstreamAddresses, tmAddrs...)
}
return nil
}
// NewSentry provisions a new sentry node and adds it to the network.
func (net *Network) NewSentry(cfg *SentryCfg) (*Sentry, error) {
sentryName := fmt.Sprintf("sentry-%d", len(net.sentries))
host, err := net.GetNamedNode(sentryName, &cfg.NodeCfg)
if err != nil {
return nil, err
}
// Pre-provision node's identity to pass the sentry node's consensus
// address to the validator so it can configure the sentry node's consensus
// address as its consensus address.
signerFactory, err := fileSigner.NewFactory(host.dir.String(), identity.RequiredSignerRoles...)
if err != nil {
net.logger.Error("failed to create sentry signer factory",
"err", err,
"sentry_name", sentryName,
)
return nil, fmt.Errorf("oasis/sentry: failed to create sentry file signer: %w", err)
}
sentryIdentity, err := identity.LoadOrGenerate(host.dir.String(), signerFactory)
if err != nil {
net.logger.Error("failed to provision sentry identity",
"err", err,
"sentry_name", sentryName,
)
return nil, fmt.Errorf("oasis/sentry: failed to provision sentry identity: %w", err)
}
sentryP2PPublicKey := sentryIdentity.P2PSigner.Public()
sentryTLSPublicKey := sentryIdentity.TLSSigner.Public()
sentry := &Sentry{
Node: host,
validatorIndices: cfg.ValidatorIndices,
computeIndices: cfg.ComputeIndices,
keymanagerIndices: cfg.KeymanagerIndices,
p2pPublicKey: sentryP2PPublicKey,
tlsPublicKey: sentryTLSPublicKey,
tmAddress: crypto.PublicKeyToCometBFT(&sentryP2PPublicKey).Address().String(),
consensusPort: host.getProvisionedPort(nodePortConsensus),
controlPort: host.getProvisionedPort("sentry-control"),
sentryPort: host.getProvisionedPort("sentry-client"),
}
net.sentries = append(net.sentries, sentry)
host.features = append(host.features, sentry)
return sentry, nil
}