-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathworkload_api.go
178 lines (153 loc) · 4 KB
/
workload_api.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
package main
import (
"bytes"
"context"
"encoding/json"
"net"
"sync"
"time"
"github.com/andres-erbsen/clock"
"github.com/go-jose/go-jose/v4"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/bundle/jwtbundle"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/spiffe/spire/pkg/common/telemetry"
"github.com/spiffe/spire/pkg/common/util"
)
const (
DefaultWorkloadAPIPollInterval = time.Second * 10
)
type WorkloadAPISourceConfig struct {
Log logrus.FieldLogger
Addr net.Addr
TrustDomain string
PollInterval time.Duration
Clock clock.Clock
}
type WorkloadAPISource struct {
log logrus.FieldLogger
clock clock.Clock
trustDomain spiffeid.TrustDomain
cancel context.CancelFunc
mu sync.RWMutex
wg sync.WaitGroup
rawBundle []byte
jwks *jose.JSONWebKeySet
modTime time.Time
pollTime time.Time
}
func NewWorkloadAPISource(config WorkloadAPISourceConfig) (*WorkloadAPISource, error) {
if config.PollInterval <= 0 {
config.PollInterval = DefaultWorkloadAPIPollInterval
}
if config.Clock == nil {
config.Clock = clock.New()
}
var opts []workloadapi.ClientOption
if config.Addr != nil {
o, err := util.GetWorkloadAPIClientOption(config.Addr)
if err != nil {
return nil, err
}
opts = append(opts, o)
}
trustDomain, err := spiffeid.TrustDomainFromString(config.TrustDomain)
if err != nil {
return nil, err
}
client, err := workloadapi.New(context.Background(), opts...)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
s := &WorkloadAPISource{
log: config.Log,
clock: config.Clock,
cancel: cancel,
trustDomain: trustDomain,
}
go s.pollEvery(ctx, client, config.PollInterval)
return s, nil
}
func (s *WorkloadAPISource) Close() error {
s.cancel()
s.wg.Wait()
return nil
}
func (s *WorkloadAPISource) FetchKeySet() (*jose.JSONWebKeySet, time.Time, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.jwks == nil {
return nil, time.Time{}, false
}
return s.jwks, s.modTime, true
}
func (s *WorkloadAPISource) LastSuccessfulPoll() time.Time {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pollTime
}
func (s *WorkloadAPISource) pollEvery(ctx context.Context, client *workloadapi.Client, interval time.Duration) {
s.wg.Add(1)
defer s.wg.Done()
defer client.Close()
s.log.WithField("interval", interval).Debug("Polling started")
for {
s.pollOnce(ctx, client)
select {
case <-ctx.Done():
s.log.WithError(ctx.Err()).Debug("Polling done")
return
case <-s.clock.After(interval):
}
}
}
func (s *WorkloadAPISource) pollOnce(ctx context.Context, client *workloadapi.Client) {
jwtBundles, err := client.FetchJWTBundles(ctx)
if err != nil {
s.log.WithError(err).Warn("Failed to fetch JWKS from the Workload API")
return
}
jwtBundle, ok := jwtBundles.Get(s.trustDomain)
if !ok {
s.log.WithField(telemetry.TrustDomainID, s.trustDomain.IDString()).Error("No bundle for trust domain in Workload API response")
return
}
// update pollTime when setJWKS was successful
if s.setJWKS(jwtBundle) == nil {
s.mu.Lock()
s.pollTime = s.clock.Now()
s.mu.Unlock()
}
}
func (s *WorkloadAPISource) setJWKS(bundle *jwtbundle.Bundle) error {
rawBundle, err := bundle.Marshal()
if err != nil {
s.log.WithError(err).Error("Failed to marshal JWKS bundle received from the Workload API")
return err
}
// If the bundle hasn't changed, don't bother continuing
s.mu.RLock()
unchanged := s.rawBundle != nil && bytes.Equal(s.rawBundle, rawBundle)
s.mu.RUnlock()
if unchanged {
return nil
}
// Clean the JWKS
jwks := new(jose.JSONWebKeySet)
if err := json.Unmarshal(rawBundle, jwks); err != nil {
s.log.WithError(err).Error("Failed to parse trust domain bundle received from the Workload API")
return err
}
for i, key := range jwks.Keys {
key.Use = ""
jwks.Keys[i] = key
}
s.mu.Lock()
defer s.mu.Unlock()
s.rawBundle = rawBundle
s.jwks = jwks
s.modTime = s.clock.Now()
return nil
}