-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsidecar.go
413 lines (354 loc) · 11.4 KB
/
sidecar.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package sidecar
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/spiffe/go-spiffe/v2/bundle/jwtbundle"
"github.com/spiffe/go-spiffe/v2/svid/jwtsvid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/spiffe/spiffe-helper/pkg/disk"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Sidecar is the component that consumes the Workload API and renews certs
// implements the interface Sidecar
type Sidecar struct {
config *Config
client *workloadapi.Client
jwtSource *workloadapi.JWTSource
processRunning int32
process *os.Process
certReadyChan chan struct{}
}
// New creates a new SPIFFE sidecar
func New(config *Config) *Sidecar {
return &Sidecar{
config: config,
certReadyChan: make(chan struct{}, 1),
}
}
// RunDaemon starts the main loop
// Starts the workload API client to listen for new SVID updates
// When a new SVID is received on the updateChan, the SVID certificates
// are stored in disk and a restart signal is sent to the proxy's process
func (s *Sidecar) RunDaemon(ctx context.Context) error {
var wg sync.WaitGroup
if err := s.setupClients(ctx); err != nil {
return err
}
if s.client != nil {
defer s.client.Close()
}
if s.jwtSource != nil {
defer s.jwtSource.Close()
}
if s.x509Enabled() {
s.config.Log.Info("Watching for X509 Context")
wg.Add(1)
go func() {
defer wg.Done()
err := s.client.WatchX509Context(ctx, &x509Watcher{sidecar: s})
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Fatalf("Error watching X.509 context: %v", err)
}
}()
}
if s.jwtBundleEnabled() {
s.config.Log.Info("Watching for JWT Bundles")
wg.Add(1)
go func() {
defer wg.Done()
err := s.client.WatchJWTBundles(ctx, &JWTBundlesWatcher{sidecar: s})
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Fatalf("Error watching JWT bundle updates: %v", err)
}
}()
}
if s.jwtSVIDsEnabled() {
for _, jwtConfig := range s.config.JWTSVIDs {
jwtConfig := jwtConfig
wg.Add(1)
go func() {
defer wg.Done()
s.updateJWTSVID(ctx, jwtConfig.JWTAudience, jwtConfig.JWTExtraAudiences, jwtConfig.JWTSVIDFilename)
}()
}
}
wg.Wait()
return nil
}
func (s *Sidecar) Run(ctx context.Context) error {
if err := s.setupClients(ctx); err != nil {
return err
}
if s.client != nil {
defer s.client.Close()
}
if s.jwtSource != nil {
defer s.jwtSource.Close()
}
if s.x509Enabled() {
s.config.Log.Debug("Fetching x509 certificates")
if err := s.fetchAndWriteX509Context(ctx); err != nil {
s.config.Log.WithError(err).Error("Error fetching x509 certificates")
return err
}
s.config.Log.Info("Successfully fetched x509 certificates")
}
if s.jwtBundleEnabled() {
s.config.Log.Debug("Fetching JWT Bundle")
if err := s.fetchAndWriteJWTBundle(ctx); err != nil {
s.config.Log.WithError(err).Error("Error fetching JWT bundle")
return err
}
s.config.Log.Info("Successfully fetched JWT bundle")
}
if s.jwtSVIDsEnabled() {
s.config.Log.Debug("Fetching JWT SVIDs")
if err := s.fetchAndWriteJWTSVIDs(ctx); err != nil {
s.config.Log.WithError(err).Error("Error fetching JWT SVIDs")
return err
}
s.config.Log.Info("Successfully fetched JWT SVIDs")
}
return nil
}
// CertReadyChan returns a channel to know when the certificates are ready
func (s *Sidecar) CertReadyChan() <-chan struct{} {
return s.certReadyChan
}
// setupClients create the necessary workloadapi clients
func (s *Sidecar) setupClients(ctx context.Context) error {
if s.x509Enabled() || s.jwtBundleEnabled() {
client, err := workloadapi.New(ctx, s.getWorkloadAPIAddress())
if err != nil {
return err
}
s.client = client
}
if s.jwtSVIDsEnabled() {
jwtSource, err := workloadapi.NewJWTSource(ctx, workloadapi.WithClientOptions(s.getWorkloadAPIAddress()))
if err != nil {
return err
}
s.jwtSource = jwtSource
}
return nil
}
// updateCertificates Updates the certificates stored in disk and signal the Process to restart
func (s *Sidecar) updateCertificates(svidResponse *workloadapi.X509Context) {
s.config.Log.Debug("Updating X.509 certificates")
if err := disk.WriteX509Context(svidResponse, s.config.AddIntermediatesToBundle, s.config.IncludeFederatedDomains, s.config.CertDir, s.config.SVIDFileName, s.config.SVIDKeyFileName, s.config.SVIDBundleFileName, s.config.CertFileMode, s.config.KeyFileMode); err != nil {
s.config.Log.WithError(err).Error("Unable to dump bundle")
return
}
s.config.Log.Info("X.509 certificates updated")
if s.config.Cmd != "" {
if err := s.signalProcess(); err != nil {
s.config.Log.WithError(err).Error("Unable to signal process")
}
}
if s.config.PIDFileName != "" {
if err := s.signalPID(); err != nil {
s.config.Log.WithError(err).Error("Unable to signal PID file")
}
}
// TODO: is ReloadExternalProcess still used?
if s.config.ReloadExternalProcess != nil {
if err := s.config.ReloadExternalProcess(); err != nil {
s.config.Log.WithError(err).Error("Unable to reload external process")
}
}
select {
case s.certReadyChan <- struct{}{}:
default:
}
}
// signalProcessCMD sends the renew signal to the process or starts it if its first time
func (s *Sidecar) signalProcess() error {
if atomic.LoadInt32(&s.processRunning) == 0 {
cmdArgs, err := getCmdArgs(s.config.CmdArgs)
if err != nil {
return fmt.Errorf("error parsing cmd arguments: %w", err)
}
cmd := exec.Command(s.config.Cmd, cmdArgs...) // #nosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("error executing process \"%v\": %w", s.config.Cmd, err)
}
s.process = cmd.Process
go s.checkProcessExit()
} else {
if err := SignalProcess(s.process, s.config.RenewSignal); err != nil {
return err
}
}
return nil
}
// signalPID sends the renew signal to the PID file
func (s *Sidecar) signalPID() error {
fileBytes, err := os.ReadFile(s.config.PIDFileName)
if err != nil {
return fmt.Errorf("failed to read pid file \"%s\": %w", s.config.PIDFileName, err)
}
pid, err := strconv.Atoi(string(bytes.TrimSpace(fileBytes)))
if err != nil {
return fmt.Errorf("failed to parse pid file \"%s\": %w", s.config.PIDFileName, err)
}
pidProcess, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("failed to find process id %d: %w", pid, err)
}
return SignalProcess(pidProcess, s.config.RenewSignal)
}
func (s *Sidecar) checkProcessExit() {
atomic.StoreInt32(&s.processRunning, 1)
_, err := s.process.Wait()
if err != nil {
s.config.Log.Errorf("error waiting for process exit: %v", err)
}
atomic.StoreInt32(&s.processRunning, 0)
}
func (s *Sidecar) fetchJWTSVIDs(ctx context.Context, jwtAudience string, jwtExtraAudiences []string) (*jwtsvid.SVID, error) {
jwtSVID, err := s.jwtSource.FetchJWTSVID(ctx, jwtsvid.Params{Audience: jwtAudience, ExtraAudiences: jwtExtraAudiences})
if err != nil {
s.config.Log.Errorf("Unable to fetch JWT SVID: %v", err)
return nil, err
}
_, err = jwtsvid.ParseAndValidate(jwtSVID.Marshal(), s.jwtSource, []string{jwtAudience})
if err != nil {
s.config.Log.Errorf("Unable to parse or validate token: %v", err)
return nil, err
}
return jwtSVID, nil
}
func createRetryIntervalFunc() func() time.Duration {
const (
initialBackoff = 1 * time.Second
maxBackoff = 60 * time.Second
multiplier = 2
)
backoffInterval := initialBackoff
return func() time.Duration {
currentBackoff := backoffInterval
// Update backoffInterval for next call, capped at maxBackoff
backoffInterval *= multiplier
if backoffInterval > maxBackoff {
backoffInterval = maxBackoff
}
return currentBackoff
}
}
func getRefreshInterval(svid *jwtsvid.SVID) time.Duration {
return time.Until(svid.Expiry)/2 + time.Second
}
func (s *Sidecar) performJWTSVIDUpdate(ctx context.Context, jwtAudience string, jwtExtraAudiences []string, jwtSVIDFilename string) (*jwtsvid.SVID, error) {
s.config.Log.Debug("Updating JWT SVID")
jwtSVID, err := s.fetchJWTSVIDs(ctx, jwtAudience, jwtExtraAudiences)
if err != nil {
s.config.Log.Errorf("Unable to update JWT SVID: %v", err)
return nil, err
}
if err = disk.WriteJWTSVID(jwtSVID, s.config.CertDir, jwtSVIDFilename, s.config.JWTSVIDFileMode); err != nil {
s.config.Log.Errorf("Unable to update JWT SVID: %v", err)
return nil, err
}
s.config.Log.Info("JWT SVID updated")
return jwtSVID, nil
}
func (s *Sidecar) updateJWTSVID(ctx context.Context, jwtAudience string, jwtExtraAudiences []string, jwtSVIDFilename string) {
retryInterval := createRetryIntervalFunc()
var initialInterval time.Duration
jwtSVID, err := s.performJWTSVIDUpdate(ctx, jwtAudience, jwtExtraAudiences, jwtSVIDFilename)
if err != nil {
// If the first update fails, use the retry interval
initialInterval = retryInterval()
} else {
// If the update succeeds, use the refresh interval
initialInterval = getRefreshInterval(jwtSVID)
}
ticker := time.NewTicker(initialInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
jwtSVID, err = s.performJWTSVIDUpdate(ctx, jwtAudience, jwtExtraAudiences, jwtSVIDFilename)
if err == nil {
retryInterval = createRetryIntervalFunc()
ticker.Reset(getRefreshInterval(jwtSVID))
} else {
ticker.Reset(retryInterval())
}
}
}
}
func (s *Sidecar) x509Enabled() bool {
return s.config.SVIDFileName != "" && s.config.SVIDKeyFileName != "" && s.config.SVIDBundleFileName != ""
}
func (s *Sidecar) jwtBundleEnabled() bool {
return s.config.JWTBundleFilename != ""
}
func (s *Sidecar) jwtSVIDsEnabled() bool {
return len(s.config.JWTSVIDs) > 0
}
// x509Watcher is a sample implementation of the workload.X509SVIDWatcher interface
type x509Watcher struct {
sidecar *Sidecar
}
// OnX509ContextUpdate is run every time an SVID is updated
func (w x509Watcher) OnX509ContextUpdate(svids *workloadapi.X509Context) {
for _, svid := range svids.SVIDs {
w.sidecar.config.Log.WithField("spiffe_id", svid.ID).Info("Received update")
}
w.sidecar.updateCertificates(svids)
}
// OnX509ContextWatchError is run when the client runs into an error
func (w x509Watcher) OnX509ContextWatchError(err error) {
if status.Code(err) != codes.Canceled {
w.sidecar.config.Log.Errorf("Error while watching x509 context: %v", err)
}
}
// getCmdArgs receives the command line arguments as a string
// and split it at spaces, except when the space is inside quotation marks
func getCmdArgs(args string) ([]string, error) {
if args == "" {
return []string{}, nil
}
r := csv.NewReader(strings.NewReader(args))
r.Comma = ' ' // space
cmdArgs, err := r.Read()
if err != nil {
return nil, err
}
return cmdArgs, nil
}
// JWTBundlesWatcher is an implementation of workload.JWTBundleWatcher interface
type JWTBundlesWatcher struct {
sidecar *Sidecar
}
// OnJWTBundlesUpdate is run every time a bundle is updated
func (w JWTBundlesWatcher) OnJWTBundlesUpdate(jwkSet *jwtbundle.Set) {
w.sidecar.config.Log.Debug("Updating JWT bundle")
if err := disk.WriteJWTBundleSet(jwkSet, w.sidecar.config.CertDir, w.sidecar.config.JWTBundleFilename, w.sidecar.config.JWTBundleFileMode); err != nil {
w.sidecar.config.Log.Errorf("Error writing JWT Bundle to disk: %v", err)
return
}
w.sidecar.config.Log.Info("JWT bundle updated")
}
// OnJWTBundlesWatchError is run when the client runs into an error
func (w JWTBundlesWatcher) OnJWTBundlesWatchError(err error) {
if status.Code(err) != codes.Canceled {
w.sidecar.config.Log.Errorf("Error while watching JWT bundles: %v", err)
}
}