-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathserver_api.go
156 lines (133 loc) · 3.34 KB
/
server_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
package main
import (
"context"
"crypto/x509"
"sync"
"time"
"github.com/andres-erbsen/clock"
"github.com/go-jose/go-jose/v4"
"github.com/sirupsen/logrus"
bundlev1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/bundle/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
const (
DefaultServerAPIPollInterval = time.Second * 10
)
type ServerAPISourceConfig struct {
Log logrus.FieldLogger
GRPCTarget string
PollInterval time.Duration
Clock clock.Clock
}
type ServerAPISource struct {
log logrus.FieldLogger
clock clock.Clock
cancel context.CancelFunc
mu sync.RWMutex
wg sync.WaitGroup
bundle *types.Bundle
jwks *jose.JSONWebKeySet
modTime time.Time
pollTime time.Time
}
func NewServerAPISource(config ServerAPISourceConfig) (*ServerAPISource, error) {
if config.PollInterval <= 0 {
config.PollInterval = DefaultServerAPIPollInterval
}
if config.Clock == nil {
config.Clock = clock.New()
}
conn, err := util.NewGRPCClient(config.GRPCTarget)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
s := &ServerAPISource{
log: config.Log,
clock: config.Clock,
cancel: cancel,
}
go s.pollEvery(ctx, conn, config.PollInterval)
return s, nil
}
func (s *ServerAPISource) Close() error {
s.cancel()
s.wg.Wait()
return nil
}
func (s *ServerAPISource) 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 *ServerAPISource) LastSuccessfulPoll() time.Time {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pollTime
}
func (s *ServerAPISource) pollEvery(ctx context.Context, conn *grpc.ClientConn, interval time.Duration) {
s.wg.Add(1)
defer s.wg.Done()
defer conn.Close()
client := bundlev1.NewBundleClient(conn)
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 *ServerAPISource) pollOnce(ctx context.Context, client bundlev1.BundleClient) {
// Ensure the stream gets cleaned up
ctx, cancel := context.WithCancel(ctx)
defer cancel()
bundle, err := client.GetBundle(ctx, &bundlev1.GetBundleRequest{
OutputMask: &types.BundleMask{
JwtAuthorities: true,
},
})
if err != nil {
s.log.WithError(err).Warn("Failed to fetch bundle")
return
}
s.parseBundle(bundle)
s.mu.Lock()
s.pollTime = s.clock.Now()
s.mu.Unlock()
}
func (s *ServerAPISource) parseBundle(bundle *types.Bundle) {
// If the bundle hasn't changed, don't bother continuing
s.mu.RLock()
if s.bundle != nil && proto.Equal(s.bundle, bundle) {
s.mu.RUnlock()
return
}
s.mu.RUnlock()
jwks := new(jose.JSONWebKeySet)
for _, key := range bundle.JwtAuthorities {
publicKey, err := x509.ParsePKIXPublicKey(key.PublicKey)
if err != nil {
s.log.WithError(err).WithField("kid", key.KeyId).Warn("Malformed public key in bundle")
continue
}
jwks.Keys = append(jwks.Keys, jose.JSONWebKey{
Key: publicKey,
KeyID: key.KeyId,
})
}
s.mu.Lock()
defer s.mu.Unlock()
s.bundle = bundle
s.jwks = jwks
s.modTime = s.clock.Now()
}