-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
delegate.go
386 lines (317 loc) · 10.4 KB
/
delegate.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
385
386
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package spire
import (
"bytes"
"context"
"crypto/x509"
"errors"
"fmt"
"os"
delegatedidentityv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/agent/delegatedidentity/v1"
spiffeTypes "github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/cilium/cilium/pkg/auth/certs"
"github.com/cilium/cilium/pkg/backoff"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/time"
)
type SpireDelegateClient struct {
cfg SpireDelegateConfig
log logrus.FieldLogger
connectionAttempts int
stream delegatedidentityv1.DelegatedIdentity_SubscribeToX509SVIDsClient
trustStream delegatedidentityv1.DelegatedIdentity_SubscribeToX509BundlesClient
svidStore map[string]*delegatedidentityv1.X509SVIDWithKey
svidStoreMutex lock.RWMutex
trustBundle *x509.CertPool
cancelListenForUpdates context.CancelFunc
rotatedIdentitiesChan chan certs.CertificateRotationEvent
logLimiter logging.Limiter
connected bool
lastConnectError error
connectedMutex lock.RWMutex
}
type SpireDelegateConfig struct {
SpireAdminSocketPath string `mapstructure:"mesh-auth-spire-admin-socket"`
SpiffeTrustDomain string `mapstructure:"mesh-auth-spiffe-trust-domain"`
RotatedQueueSize int `mapstructure:"mesh-auth-rotated-identities-queue-size"`
}
var Cell = cell.Module(
"spire-delegate",
"Spire Delegate API Client",
cell.Provide(newSpireDelegateClient),
cell.Config(SpireDelegateConfig{}),
)
func newSpireDelegateClient(lc cell.Lifecycle, cfg SpireDelegateConfig, log logrus.FieldLogger) certs.CertificateProvider {
if cfg.SpireAdminSocketPath == "" {
log.Info("Spire Delegate API Client is disabled as no socket path is configured")
return nil
}
client := &SpireDelegateClient{
cfg: cfg,
log: log,
svidStore: map[string]*delegatedidentityv1.X509SVIDWithKey{},
rotatedIdentitiesChan: make(chan certs.CertificateRotationEvent, cfg.RotatedQueueSize),
logLimiter: logging.NewLimiter(10*time.Second, 3),
}
lc.Append(cell.Hook{OnStart: client.onStart, OnStop: client.onStop})
return client
}
func (cfg SpireDelegateConfig) Flags(flags *pflag.FlagSet) {
flags.StringVar(&cfg.SpireAdminSocketPath, "mesh-auth-spire-admin-socket", "", "The path for the SPIRE admin agent Unix socket.") // default is /run/spire/sockets/admin.sock
flags.StringVar(&cfg.SpiffeTrustDomain, "mesh-auth-spiffe-trust-domain", "spiffe.cilium", "The trust domain for the SPIFFE identity.")
flags.IntVar(&cfg.RotatedQueueSize, "mesh-auth-rotated-identities-queue-size", 1024, "The size of the queue for signaling rotated identities.")
}
func (s *SpireDelegateClient) onStart(ctx cell.HookContext) error {
s.log.Info("Spire Delegate API Client is running")
listenCtx, cancel := context.WithCancel(context.Background())
go s.listenForUpdates(listenCtx)
s.cancelListenForUpdates = cancel
return nil
}
func (s *SpireDelegateClient) onStop(ctx cell.HookContext) error {
s.log.Info("SPIFFE Delegate API Client is stopping")
s.cancelListenForUpdates()
if s.stream != nil {
s.stream.CloseSend()
}
return nil
}
func (s *SpireDelegateClient) listenForUpdates(ctx context.Context) {
s.openStream(ctx)
listenCtx, cancel := context.WithCancel(ctx)
err := make(chan error)
go s.listenForSVIDUpdates(listenCtx, err)
go s.listenForBundleUpdates(listenCtx, err)
backoffTime := backoff.Exponential{Min: 100 * time.Millisecond, Max: 10 * time.Second}
for {
select {
case <-ctx.Done():
cancel()
return
case e := <-err:
s.log.WithError(e).Error("Error in delegate stream, restarting")
time.Sleep(backoffTime.Duration(s.connectionAttempts))
cancel()
s.connectionAttempts++
s.listenForUpdates(ctx)
return
}
}
}
func (s *SpireDelegateClient) listenForSVIDUpdates(ctx context.Context, errorChan chan error) {
for {
select {
case <-ctx.Done():
return
default:
resp, err := s.stream.Recv()
if err != nil {
errorChan <- err
return
}
s.log.
WithField("nr_of_svids", len(resp.X509Svids)).
Debug("Received X509-SVID update")
s.handleX509SVIDUpdate(resp.X509Svids)
}
}
}
func (s *SpireDelegateClient) listenForBundleUpdates(ctx context.Context, errorChan chan error) {
for {
select {
case <-ctx.Done():
return
default:
resp, err := s.trustStream.Recv()
if err != nil {
errorChan <- err
return
}
s.log.
WithField("nr_of_bundles", len(resp.CaCertificates)).
Debug("Received X509-Bundle update", len(resp.CaCertificates))
s.handleX509BundleUpdate(resp.CaCertificates)
}
}
}
func (s *SpireDelegateClient) handleX509SVIDUpdate(svids []*delegatedidentityv1.X509SVIDWithKey) {
newSvidStore := map[string]*delegatedidentityv1.X509SVIDWithKey{}
s.svidStoreMutex.RLock()
updatedKeys := []string{}
deletedKeys := []string{}
for _, svid := range svids {
if svid.X509Svid.Id.TrustDomain != s.cfg.SpiffeTrustDomain {
s.log.
WithField("trust_domain", svid.X509Svid.Id.TrustDomain).
Debug("Skipping X509-SVID update as it does not match ours")
s.svidStoreMutex.RUnlock()
return
}
key := fmt.Sprintf("spiffe://%s%s", svid.X509Svid.Id.TrustDomain, svid.X509Svid.Id.Path)
if _, exists := s.svidStore[key]; exists {
old := s.svidStore[key]
if old.X509Svid.ExpiresAt != svid.X509Svid.ExpiresAt || !equalCertChains(old.X509Svid.CertChain, svid.X509Svid.CertChain) {
updatedKeys = append(updatedKeys, key)
}
} else {
s.log.
WithField("spiffe_id", key).
Debug("Adding newly discovered X509-SVID")
}
newSvidStore[key] = svid
}
// check for deleted keys
for key := range s.svidStore {
if _, exists := newSvidStore[key]; !exists {
deletedKeys = append(deletedKeys, key)
}
}
s.svidStoreMutex.RUnlock()
s.svidStoreMutex.Lock()
s.svidStore = newSvidStore
s.svidStoreMutex.Unlock()
for _, key := range deletedKeys {
// we send an update event to re-trigger a handshake if needed
id, err := s.spiffeIDToNumericIdentity(key)
if err != nil {
s.log.
WithError(err).
WithField("spiffe_id", key).
Error("Failed to convert SPIFFE ID to numeric identity")
continue
}
select {
case s.rotatedIdentitiesChan <- certs.CertificateRotationEvent{Identity: id, Deleted: true}:
s.log.
WithField("spiffe_id", key).
Debug("X509-SVID has been deleted, signaling this")
default:
if s.logLimiter.Allow() {
s.log.
WithField("identity", id).
Warn("Skip sending deleted identity as channel is full")
}
}
}
for _, key := range updatedKeys {
// we send an update event to re-trigger a handshake if needed
id, err := s.spiffeIDToNumericIdentity(key)
if err != nil {
s.log.
WithError(err).
WithField("spiffe_id", key).
Error("Failed to convert SPIFFE ID to numeric identity")
continue
}
select {
case s.rotatedIdentitiesChan <- certs.CertificateRotationEvent{Identity: id}:
s.log.
WithField("spiffe_id", key).
Debug("X509-SVID has changed, signaling this")
default:
if s.logLimiter.Allow() {
s.log.
WithField("identity", id).
Warn("Skip sending rotated identity as channel is full")
}
}
}
}
func (s *SpireDelegateClient) handleX509BundleUpdate(bundles map[string][]byte) {
pool := x509.NewCertPool()
for trustDomain, bundle := range bundles {
s.log.
WithField("trust_domain", trustDomain).
Debug("Processing trust domain cert bundle", trustDomain)
certs, err := x509.ParseCertificates(bundle)
if err != nil {
s.log.
WithError(err).
WithField("trust_domain", trustDomain).
Error("Failed to parse X.509 DER bundle")
continue
}
for _, cert := range certs {
pool.AddCert(cert)
}
}
s.trustBundle = pool
}
func (s *SpireDelegateClient) openStream(ctx context.Context) {
// try to init the watcher with a backoff
backoffTime := backoff.Exponential{Min: 100 * time.Millisecond, Max: 10 * time.Second}
// a retry might have happened, signal that we are disconnected
s.connectedMutex.Lock()
s.connected = false
s.connectedMutex.Unlock()
for {
s.log.Info("Connecting to SPIRE Delegate API Client")
var err error
s.stream, s.trustStream, err = s.initWatcher(ctx)
if err != nil {
s.log.WithError(err).Warn("SPIRE Delegate API Client failed to init watcher, retrying")
s.connectedMutex.Lock()
s.connected = false
s.lastConnectError = err
s.connectedMutex.Unlock()
time.Sleep(backoffTime.Duration(s.connectionAttempts))
s.connectionAttempts++
continue
}
s.connectedMutex.Lock()
s.connected = true
s.lastConnectError = nil
s.connectedMutex.Unlock()
break
}
}
func (s *SpireDelegateClient) initWatcher(ctx context.Context) (delegatedidentityv1.DelegatedIdentity_SubscribeToX509SVIDsClient, delegatedidentityv1.DelegatedIdentity_SubscribeToX509BundlesClient, error) {
if _, err := os.Stat(s.cfg.SpireAdminSocketPath); errors.Is(err, os.ErrNotExist) {
return nil, nil, fmt.Errorf("SPIRE admin socket (%s) does not exist: %w", s.cfg.SpireAdminSocketPath, err)
}
unixPath := fmt.Sprintf("unix://%s", s.cfg.SpireAdminSocketPath)
conn, err := grpc.Dial(unixPath, grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(20*1024*1024),
grpc.MaxCallSendMsgSize(20*1024*1024))) // setting this to 20MB to handle large bundles TODO: improve this once fixed upstream (https://github.com/cilium/cilium/issues/24297)
if err != nil {
return nil, nil, fmt.Errorf("grpc.Dial() failed on %s: %w", unixPath, err)
}
client := delegatedidentityv1.NewDelegatedIdentityClient(conn)
stream, err := client.SubscribeToX509SVIDs(ctx, &delegatedidentityv1.SubscribeToX509SVIDsRequest{
Selectors: []*spiffeTypes.Selector{
{
Type: "cilium",
Value: "mutual-auth",
},
},
})
if err != nil {
conn.Close()
return nil, nil, fmt.Errorf("stream failed on %s: %w", unixPath, err)
}
trustStream, err := client.SubscribeToX509Bundles(ctx, &delegatedidentityv1.SubscribeToX509BundlesRequest{})
if err != nil {
conn.Close()
return nil, nil, fmt.Errorf("stream for x509 bundle failed on %s: %w", unixPath, err)
}
return stream, trustStream, nil
}
func equalCertChains(a, b [][]byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !bytes.Equal(a[i], b[i]) {
return false
}
}
return true
}