-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
299 lines (259 loc) · 7.32 KB
/
service.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
package main
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/json"
"fmt"
"github.com/JackOfMostTrades/spiffe-user-demo/common"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/proto/spiffe/workload"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"gopkg.in/square/go-jose.v2/jwt"
"net"
"os"
"strings"
"sync"
"time"
)
type WorkloadService struct {
workload.UnimplementedSpiffeWorkloadAPIServer
logger *logrus.Logger
client UserClient
shutdownChan chan bool
newSvidsPubSub *PubSub
}
func NewWorkloadService(refreshFrequency time.Duration, logger *logrus.Logger, client UserClient) *WorkloadService {
s := &WorkloadService{
logger: logger,
client: client,
shutdownChan: make(chan bool),
newSvidsPubSub: NewPubSub(),
}
go func() {
runUpdate := func() {
s.logger.Debug("Fetching updated certificates.")
err := s.updateResponseAndNotify()
if err != nil {
logger.Errorf("failed to fetch new certificates: %v", err)
} else {
s.logger.Debug("Certificates successfully updated.")
}
}
// Prime initial data
runUpdate()
ticker := time.NewTicker(refreshFrequency)
defer ticker.Stop()
for {
select {
case <-ticker.C:
runUpdate()
case <-s.shutdownChan:
return
}
}
}()
return s
}
func (s *WorkloadService) Stop() {
s.shutdownChan <- true
}
func (s *WorkloadService) updateResponseAndNotify() error {
keypair, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return err
}
pubKeyBytes, err := x509.MarshalPKIXPublicKey(keypair.Public())
if err != nil {
return err
}
results, err := s.client.GetUserX509(&common.GetUserX509Request{
PublicKey: pubKeyBytes,
})
if err != nil {
return fmt.Errorf("failed to fetch certificates: %v", err)
}
svid, err := certChainToSvid(keypair, results.CertificateChain)
if err != nil {
return fmt.Errorf("unable to convert certificate chain in response to SVID: %v", err)
}
s.logger.Infof("Sending new certificates to %d subscribers...", s.newSvidsPubSub.GetSubscriberCount())
s.newSvidsPubSub.Publish([]*workload.X509SVID{svid})
return nil
}
func certChainToSvid(privateKey crypto.PrivateKey, certChain [][]byte) (*workload.X509SVID, error) {
leafCert, err := x509.ParseCertificate(certChain[0])
if err != nil {
return nil, fmt.Errorf("failed to decode certificate in response: %v", err)
}
allCertBytes := make([]byte, 0)
for _, cert := range certChain {
allCertBytes = append(allCertBytes, cert...)
}
privKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("failed to marshal private key: %v", err)
}
var spiffeid string
for _, uri := range leafCert.URIs {
if strings.ToLower(uri.Scheme) == "spiffe" {
// If there are multiple spiffe URIs, bail out
if spiffeid != "" {
spiffeid = ""
break
}
spiffeid = uri.String()
}
}
return &workload.X509SVID{
SpiffeId: spiffeid,
X509Svid: allCertBytes,
X509SvidKey: privKeyBytes,
Bundle: certChain[len(certChain)-1],
}, nil
}
func (s *WorkloadService) FetchX509SVID(req *workload.X509SVIDRequest, srv workload.SpiffeWorkloadAPI_FetchX509SVIDServer) error {
headers, ok := metadata.FromIncomingContext(srv.Context())
if !ok {
return status.Error(codes.InvalidArgument, "Unable to retrieve request context headers.")
}
authHeaders := headers.Get("workload.spiffe.io")
if len(authHeaders) != 1 || authHeaders[0] != "true" {
return status.Error(codes.InvalidArgument, "SPIFFE workload authentication header is invalid or missing.")
}
// If debug logging is enabled, generate a UUID to track this connection and log the connect/disconnect events
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
if peerId, err := uuid.NewRandom(); err == nil {
s.logger.Debugf("Got new subscription for client: %s", peerId)
defer s.logger.Debugf("Connection to peer %s has been ended.", peerId)
}
}
subscription := s.newSvidsPubSub.Subscribe()
defer subscription.Close()
// Channel that indicates the client has cancelled the stream
done := srv.Context().Done()
for {
select {
case <-done:
return srv.Context().Err()
case svids := <-subscription.C:
// When the channel gets closed (because the server is shutting down gracefully) the channel outputs nil
if svids == nil {
return status.Error(codes.Unavailable, "Workload agent is shutting down.")
}
if msg, ok := svids.([]*workload.X509SVID); ok {
err := srv.Send(&workload.X509SVIDResponse{Svids: msg})
if err != nil {
return err
}
} else {
s.logger.Errorf("Got unexpected subscription notification: %T", svids)
}
}
}
// Unreachable
}
func (s *WorkloadService) FetchJWTSVID(ctx context.Context, req *workload.JWTSVIDRequest) (*workload.JWTSVIDResponse, error) {
res, err := s.client.GetUserJwt(&common.GetUserJwtRequest{
Audience: req.Audience,
})
if err != nil {
return nil, err
}
token, err := jwt.ParseSigned(res.UserJwt)
if err != nil {
return nil, err
}
claims := new(jwt.Claims)
err = token.UnsafeClaimsWithoutVerification(claims)
if err != nil {
return nil, err
}
svid := &workload.JWTSVID{
SpiffeId: claims.Subject,
Svid: res.UserJwt,
}
return &workload.JWTSVIDResponse{
Svids: []*workload.JWTSVID{svid},
}, nil
}
func (s *WorkloadService) FetchJWTBundles(req *workload.JWTBundlesRequest, stream workload.SpiffeWorkloadAPI_FetchJWTBundlesServer) error {
jwks, err := s.client.GetJwks()
if err != nil {
return err
}
jwksBytes, err := json.Marshal(jwks.Jwks)
if err != nil {
return err
}
stream.Send(&workload.JWTBundlesResponse{
Bundles: map[string][]byte{
jwks.TrustDomain: jwksBytes,
},
})
// Channel that indicates the client has cancelled the stream
<-stream.Context().Done()
return stream.Context().Err()
}
type WorkloadServiceServerOptions struct {
SocketPath string
Logger *logrus.Logger
Client UserClient
RefreshFrequency time.Duration
}
type WorkloadServiceServer struct {
service *WorkloadService
server *grpc.Server
stopped bool
waitGroup *sync.WaitGroup
exitErr error
}
func (s *WorkloadServiceServer) Wait() error {
s.waitGroup.Wait()
if s.stopped {
return nil
}
return s.exitErr
}
func (s *WorkloadServiceServer) Stop() error {
s.stopped = true
s.server.Stop()
s.service.Stop()
return nil
}
func StartWorkloadServiceServer(options *WorkloadServiceServerOptions) (*WorkloadServiceServer, error) {
if _, err := os.Stat(options.SocketPath); err == nil {
err = os.Remove(options.SocketPath)
if err != nil {
return nil, fmt.Errorf("Socket path %s already exists but could not be removed.", options.SocketPath)
}
}
lis, err := net.Listen("unix", options.SocketPath)
if err != nil {
return nil, fmt.Errorf("failed to listen on socket path %s: %v", options.SocketPath, err)
}
service := NewWorkloadService(options.RefreshFrequency, options.Logger, options.Client)
server := grpc.NewServer()
workload.RegisterSpiffeWorkloadAPIServer(server, service)
wg := new(sync.WaitGroup)
wg.Add(1)
s := &WorkloadServiceServer{
service: service,
server: server,
stopped: false,
waitGroup: wg,
exitErr: nil,
}
go func() {
s.exitErr = server.Serve(lis)
s.waitGroup.Add(-1)
}()
return s, nil
}