/
auth_registry.go
231 lines (198 loc) · 5.35 KB
/
auth_registry.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
package ias
import (
"context"
"fmt"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
ias "github.com/oasisprotocol/oasis-core/go/ias/api"
iasProxy "github.com/oasisprotocol/oasis-core/go/ias/proxy"
cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
)
const registryRetryInterval = 2 * time.Second
type registryAuthenticator struct {
logger *logging.Logger
cmd *cobra.Command
enclaves *enclaveStore
initOnce sync.Once
initCh chan struct{}
}
func (auth *registryAuthenticator) VerifyEvidence(ctx context.Context, evidence *ias.Evidence) error {
<-auth.initCh
// TODO: This could/should do something clever with respect to verifying
// the signer, but node registration currently requires attestations for
// all of the runtimes that the node supports, leading to a chicken/egg
// situation.
//
// Revisit this after we reconsider the node registration process.
err := auth.enclaves.verifyEvidence(evidence)
if err != nil {
auth.logger.Error("rejecting proxy request, invalid runtime",
"err", err,
"runtime_id", evidence.RuntimeID,
)
return err
}
auth.logger.Debug("allowing proxy request, found enclave identity",
"runtime_id", evidence.RuntimeID,
)
return nil
}
func (auth *registryAuthenticator) watchRuntimes(ctx context.Context, conn *grpc.ClientConn) (
ch <-chan *registry.Runtime,
sub pubsub.ClosableSubscription,
client registry.Backend,
err error,
) {
op := func() error {
client = registry.NewRegistryClient(conn)
// Subscribe to runtimes.
ch, sub, err = client.WatchRuntimes(ctx)
if err != nil {
return err
}
return nil
}
sched := backoff.NewConstantBackOff(registryRetryInterval)
err = backoff.Retry(op, backoff.WithContext(sched, ctx))
if err != nil {
auth.logger.Error("unable to connect to registry",
"err", err,
)
}
return
}
func (auth *registryAuthenticator) watchEpochs(ctx context.Context, conn *grpc.ClientConn) (
ch <-chan beacon.EpochTime,
sub pubsub.ClosableSubscription,
err error,
) {
op := func() error {
client := beacon.NewBeaconClient(conn)
// Subscribe to epochs.
ch, sub, err = client.WatchEpochs(ctx)
if err != nil {
return err
}
return nil
}
sched := backoff.NewConstantBackOff(registryRetryInterval)
err = backoff.Retry(op, backoff.WithContext(sched, ctx))
if err != nil {
auth.logger.Error("unable to connect to timekeeping",
"err", err,
)
}
return
}
func (auth *registryAuthenticator) refreshLoop(
ctx context.Context,
waitRuntimes int,
conn *grpc.ClientConn,
) error {
// Start monitoring the relevant events.
rtCh, rtSub, regClient, err := auth.watchRuntimes(ctx, conn)
if err != nil {
return err
}
defer rtSub.Close()
epochCh, epochSub, err := auth.watchEpochs(ctx, conn)
if err != nil {
return err
}
defer epochSub.Close()
for {
var n int
select {
case runtime := <-rtCh:
if runtime == nil {
// Return so the caller can re-dial.
auth.logger.Warn("data source stream closed by peer, re-dialing")
return nil
}
n, err = auth.enclaves.addRuntime(runtime)
if err != nil {
auth.logger.Error("failed to add runtime",
"err", err,
"id", runtime.ID,
)
continue
}
case epoch := <-epochCh:
auth.logger.Info("new epoch, refreshing all runtimes",
"epoch", epoch,
)
var runtimes []*registry.Runtime
runtimes, err = regClient.GetRuntimes(ctx, ®istry.GetRuntimesQuery{
Height: consensus.HeightLatest,
IncludeSuspended: true,
})
if err != nil {
// Return so caller can re-dial.
auth.logger.Error("failed to query all runtimes",
"err", err,
)
return nil
}
for _, runtime := range runtimes {
n, err = auth.enclaves.addRuntime(runtime)
if err != nil {
auth.logger.Error("failed to add/refresh runtime",
"err", err,
"id", runtime.ID,
)
}
}
case <-ctx.Done():
return ctx.Err()
}
if waitRuntimes > 0 && n >= waitRuntimes {
auth.initOnce.Do(func() {
auth.logger.Info("sufficient runtimes received, starting verification")
close(auth.initCh)
})
}
}
}
func (auth *registryAuthenticator) worker(ctx context.Context) {
waitRuntimes := viper.GetInt(cfgWaitRuntimes)
if waitRuntimes <= 0 {
close(auth.initCh)
}
// Create a new gRPC connection to an Oasis Node.
conn, err := cmdGrpc.NewClient(auth.cmd)
if err != nil {
auth.logger.Error("unable to dial the Oasis Node",
"err", err,
)
panic(fmt.Errorf("ias: failed to create gRPC client: %w", err))
}
defer conn.Close()
for {
if err = auth.refreshLoop(ctx, waitRuntimes, conn); err != nil {
// This can only fail in case the context is cancelled.
auth.logger.Error("terminating",
"err", err,
)
return
}
}
}
func newRegistryAuthenticator(ctx context.Context, cmd *cobra.Command) (iasProxy.Authenticator, error) {
auth := ®istryAuthenticator{
logger: logging.GetLogger("cmd/ias/proxy/auth/registry"),
cmd: cmd,
enclaves: newEnclaveStore(),
initCh: make(chan struct{}),
}
go auth.worker(ctx)
return auth, nil
}