-
Notifications
You must be signed in to change notification settings - Fork 107
/
sentry.go
348 lines (315 loc) · 10.3 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
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
package runtime
import (
"context"
"crypto/tls"
"errors"
"fmt"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
"github.com/oasisprotocol/oasis-core/go/common/identity"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/crypto"
control "github.com/oasisprotocol/oasis-core/go/control/api"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/log"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario"
"github.com/oasisprotocol/oasis-core/go/sentry/api"
)
var (
// Sentry is the Sentry node basic scenario.
Sentry scenario.Scenario = newSentryImpl()
validatorExtraLogWatcherHandlerFactories = []log.WatcherHandlerFactory{
oasis.LogAssertPeerExchangeDisabled(),
}
)
const sentryChecksContextTimeout = 30 * time.Second
type sentryImpl struct {
Scenario
}
func newSentryImpl() scenario.Scenario {
return &sentryImpl{
Scenario: *NewScenario(
"sentry",
NewTestClient().WithScenario(SimpleKeyValueScenario),
),
}
}
func (s *sentryImpl) Clone() scenario.Scenario {
return &sentryImpl{
Scenario: *s.Scenario.Clone().(*Scenario),
}
}
func (s *sentryImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := s.Scenario.Fixture()
if err != nil {
return nil, err
}
// Provision sentry nodes and validators with the following topology:
//
// +----------+
// +--->| Sentry 0 |
// +-------------+ | +----------+
// | Validator 0 +<----+ +----------+
// | +<-------->+ Sentry 1 |
// +-------------+ +----------+
//
// +-------------+
// | Validator 1 +<----+
// +-------------+ | +----------+
// +-------------+ +--->+ Sentry 2 |
// | Validator 2 +<-------->+ |
// +-------------+ +----------+
//
// +-----------+ +----------+
// | Compute 0 +<---------->+ Sentry 3 |
// +-----------+ +----------+
//
// +-----------+ +----------+
// | Compute 1 +<---------->+ Sentry 4 |
// +-----------+ +----------+
f.Sentries = []oasis.SentryFixture{
{
Validators: []int{0},
},
{
Validators: []int{0},
},
{
Validators: []int{1, 2},
},
{
ComputeWorkers: []int{0},
},
{
ComputeWorkers: []int{1},
},
}
f.Validators = []oasis.ValidatorFixture{
{
Entity: 1,
LogWatcherHandlerFactories: validatorExtraLogWatcherHandlerFactories,
Sentries: []int{0, 1},
Consensus: oasis.ConsensusFixture{SupplementarySanityInterval: 1},
},
{
Entity: 1,
LogWatcherHandlerFactories: validatorExtraLogWatcherHandlerFactories,
Sentries: []int{2},
},
{
Entity: 1,
LogWatcherHandlerFactories: validatorExtraLogWatcherHandlerFactories,
Sentries: []int{2},
},
}
f.ComputeWorkers = []oasis.ComputeWorkerFixture{
{
Entity: 1,
Sentries: []int{3},
// Also disable public RPC on one, so we can check access control.
DisablePublicRPC: true,
Runtimes: []int{1},
},
{
Entity: 1,
Sentries: []int{4},
Runtimes: []int{1},
},
}
return f, nil
}
func (s *sentryImpl) dial(address string, clientOpts *cmnGrpc.ClientOptions) (*grpc.ClientConn, error) {
creds, err := cmnGrpc.NewClientCreds(clientOpts)
if err != nil {
return nil, err
}
opts := grpc.WithTransportCredentials(creds)
conn, err := cmnGrpc.Dial(address, opts) // nolint: staticcheck
if err != nil {
return nil, err
}
return conn, nil
}
func (s *sentryImpl) Run(ctx context.Context, childEnv *env.Env) error { // nolint: gocyclo
// Run the basic runtime test.
if err := s.Scenario.Run(ctx, childEnv); err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, sentryChecksContextTimeout)
defer cancel()
// Load identities and addresses used in the sanity checks.
sentry0, _, sentry0CtrlAddress, sentry0P2PPubkey := loadSentryNodeInfo(s.Net.Sentries()[0])
_, _, _, sentry1P2PPubkey := loadSentryNodeInfo(s.Net.Sentries()[1])
_, _, _, sentry2P2PPubkey := loadSentryNodeInfo(s.Net.Sentries()[2])
validator0 := s.Net.Validators()[0]
validator0Identity, err := validator0.LoadIdentity()
if err != nil {
return fmt.Errorf("sentry: error loading validator node identity: %w", err)
}
validator0Ctrl, err := oasis.NewController(validator0.SocketPath())
if err != nil {
return err
}
validator1 := s.Net.Validators()[1]
validator1Identity, err := validator1.LoadIdentity()
if err != nil {
return fmt.Errorf("sentry: error loading validator node identity: %w", err)
}
validator1Ctrl, err := oasis.NewController(validator1.SocketPath())
if err != nil {
return err
}
validator2 := s.Net.Validators()[2]
if err != nil {
return fmt.Errorf("sentry: error loading validator node identity: %w", err)
}
validator2Ctrl, err := oasis.NewController(validator2.SocketPath())
if err != nil {
return err
}
// Sanity check sentry control endpoints. Only configured upstream nodes are
// allowed to access their corresponding sentry control endpoint.
// Check Sentry-0 control endpoint without client certificates.
opts := &cmnGrpc.ClientOptions{
CommonName: identity.CommonName,
ServerPubKeys: map[signature.PublicKey]bool{
sentry0.GetTLSPubKey(): true,
},
Certificates: []tls.Certificate{},
}
conn, err := s.dial(sentry0CtrlAddress, opts)
if err != nil {
return fmt.Errorf("sentry: dial error: %w", err)
}
defer conn.Close()
sentry0Client := api.NewSentryClient(conn)
_, err = sentry0Client.GetAddresses(ctx)
s.Logger.Debug("sentry0.GetAddress without cert", "err", err)
if status.Code(err) != codes.PermissionDenied {
return errors.New("sentry0 control endpoint should deny connection without certificate")
}
// Check Sentry-0 control endpoint with Validator-1 client certificates.
opts = &cmnGrpc.ClientOptions{
CommonName: identity.CommonName,
ServerPubKeys: map[signature.PublicKey]bool{
sentry0.GetTLSPubKey(): true,
},
Certificates: []tls.Certificate{*validator1Identity.TLSSentryClientCertificate},
}
conn, err = s.dial(sentry0CtrlAddress, opts)
if err != nil {
return fmt.Errorf("sentry: dial error: %w", err)
}
defer conn.Close()
sentry0Client = api.NewSentryClient(conn)
_, err = sentry0Client.GetAddresses(ctx)
s.Logger.Debug("sentry0.GetAddress with validator1 sentry cert", "err", err)
if status.Code(err) != codes.PermissionDenied {
return errors.New("sentry0 control endpoint should deny connection with validator1 certificate")
}
// Check Sentry-0 control endpoint with Validator-0 client certificates.
opts = &cmnGrpc.ClientOptions{
CommonName: identity.CommonName,
ServerPubKeys: map[signature.PublicKey]bool{
sentry0.GetTLSPubKey(): true,
},
Certificates: []tls.Certificate{*validator0Identity.TLSSentryClientCertificate},
}
conn, err = s.dial(sentry0CtrlAddress, opts)
if err != nil {
return fmt.Errorf("sentry: dial error: %w", err)
}
defer conn.Close()
sentry0Client = api.NewSentryClient(conn)
_, err = sentry0Client.GetAddresses(ctx)
s.Logger.Debug("sentry0.GetAddress with validator0 sentry cert", "err", err)
if err != nil {
return errors.New("sentry0 control endpoint should allow connection with validator0 certificate")
}
// Sanity check validator peers - only sentry nodes should be present.
// Expected consensus peers.
validator0ExpectedPeerKeys := []string{
strings.ToLower(crypto.PublicKeyToCometBFT(&sentry0P2PPubkey).Address().String()),
strings.ToLower(crypto.PublicKeyToCometBFT(&sentry1P2PPubkey).Address().String()),
}
validator12ExpectedPeerKeys := []string{
strings.ToLower(crypto.PublicKeyToCometBFT(&sentry2P2PPubkey).Address().String()),
}
// Sanity check validator0.
validator0Status, err := validator0Ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("validator0.GetStatus: %w", err)
}
validator0ConsensusPeers := consensusCometBFTAddrs(validator0Status)
if err = sanityCheckValidatorPeers(validator0ExpectedPeerKeys, validator0ConsensusPeers); err != nil {
s.Logger.Error("validator0 invalid consensus peers",
"expected", validator0ExpectedPeerKeys,
"actual", validator0ConsensusPeers,
)
return err
}
// Sanity check validator1.
validator1Status, err := validator1Ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("validator1.GetStatus: %w", err)
}
validator1ConsensusPeers := consensusCometBFTAddrs(validator1Status)
if err = sanityCheckValidatorPeers(validator12ExpectedPeerKeys, validator1ConsensusPeers); err != nil {
s.Logger.Error("validator1 invalid consensus peers",
"expected", validator12ExpectedPeerKeys,
"actual", validator1ConsensusPeers,
)
return err
}
// Sanity check validator2.
validator2Status, err := validator2Ctrl.GetStatus(ctx)
if err != nil {
return fmt.Errorf("validator2.GetStatus: %w", err)
}
validator2ConsensusPeers := consensusCometBFTAddrs(validator2Status)
if err = sanityCheckValidatorPeers(validator12ExpectedPeerKeys, validator2ConsensusPeers); err != nil {
s.Logger.Error("validator2 invalid consensus peers",
"expected", validator12ExpectedPeerKeys,
"actual", validator2ConsensusPeers,
)
return err
}
return nil
}
func loadSentryNodeInfo(s *oasis.Sentry) (*oasis.Sentry, string, string, signature.PublicKey) {
sentryCtrlAddress := s.GetSentryControlAddress()
sentryAddress := s.GetSentryAddress()
sentryIdentity, _ := s.LoadIdentity()
sentryP2PPubkey := sentryIdentity.P2PSigner.Public()
return s, sentryAddress, sentryCtrlAddress, sentryP2PPubkey
}
func consensusCometBFTAddrs(status *control.Status) (consensusPeers []string) {
for _, np := range status.Consensus.P2P.Peers {
consensusPeers = append(consensusPeers, strings.Split(np, "@")[0])
}
return
}
func sanityCheckValidatorPeers(expected, actual []string) error {
if len(expected) != len(actual) {
return fmt.Errorf("consensus peers length mismatch, expected: %d, actual: %d",
len(expected), len(actual))
}
for _, expect := range expected {
var found bool
for _, key := range actual {
if key == expect {
found = true
break
}
}
if !found {
return fmt.Errorf("expected consensus peer missing: %s", expect)
}
}
return nil
}