-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathendpoints.go
366 lines (317 loc) · 12.6 KB
/
endpoints.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
package endpoints
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net"
"os"
"time"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/spire/pkg/server/cache/entrycache"
"github.com/spiffe/spire/pkg/server/endpoints/bundle"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"github.com/andres-erbsen/clock"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffeid"
agentv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/agent/v1"
bundlev1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/bundle/v1"
debugv1_pb "github.com/spiffe/spire-api-sdk/proto/spire/api/server/debug/v1"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
loggerv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/logger/v1"
svidv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/svid/v1"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire/pkg/common/auth"
"github.com/spiffe/spire/pkg/common/peertracker"
"github.com/spiffe/spire/pkg/common/telemetry"
"github.com/spiffe/spire/pkg/common/util"
"github.com/spiffe/spire/pkg/server/api"
"github.com/spiffe/spire/pkg/server/api/middleware"
"github.com/spiffe/spire/pkg/server/authpolicy"
"github.com/spiffe/spire/pkg/server/datastore"
"github.com/spiffe/spire/pkg/server/svid"
)
const (
// This is the maximum amount of time an agent connection may exist before
// the server sends a hangup request. This enables agents to more dynamically
// route to the server in the case of a change in DNS membership.
defaultMaxConnectionAge = 3 * time.Minute
// This is the default amount of time between two reloads of the in-memory
// entry cache.
defaultCacheReloadInterval = 5 * time.Second
// This is the default amount of time events live before they are pruned
defaultPruneEventsOlderThan = 12 * time.Hour
// This is the default SQL transaction timeout. This value matches Postgres's default.
defaultSQLTransactionTimeout = 24 * time.Hour
)
// Server manages gRPC and HTTP endpoint lifecycle
type Server interface {
// ListenAndServe starts all endpoint servers and blocks until the context
// is canceled or any of the servers fails to run. If the context is
// canceled, the function returns nil. Otherwise, the error from the failed
// server is returned.
ListenAndServe(ctx context.Context) error
}
type Endpoints struct {
TCPAddr *net.TCPAddr
LocalAddr net.Addr
SVIDObserver svid.Observer
TrustDomain spiffeid.TrustDomain
DataStore datastore.DataStore
BundleCache *bundle.Cache
APIServers APIServers
BundleEndpointServer Server
Log logrus.FieldLogger
Metrics telemetry.Metrics
RateLimit RateLimitConfig
EntryFetcherCacheRebuildTask func(context.Context) error
EntryFetcherPruneEventsTask func(context.Context) error
CertificateReloadTask func(context.Context) error
AuditLogEnabled bool
AuthPolicyEngine *authpolicy.Engine
AdminIDs []spiffeid.ID
}
type APIServers struct {
AgentServer agentv1.AgentServer
BundleServer bundlev1.BundleServer
DebugServer debugv1_pb.DebugServer
EntryServer entryv1.EntryServer
HealthServer grpc_health_v1.HealthServer
LoggerServer loggerv1.LoggerServer
SVIDServer svidv1.SVIDServer
TrustDomainServer trustdomainv1.TrustDomainServer
}
// RateLimitConfig holds rate limiting configurations.
type RateLimitConfig struct {
// Attestation, if true, rate limits attestation
Attestation bool
// Signing, if true, rate limits JWT and X509 signing requests
Signing bool
}
// New creates new endpoints struct
func New(ctx context.Context, c Config) (*Endpoints, error) {
if err := prepareLocalAddr(c.LocalAddr); err != nil {
return nil, err
}
if c.AuthPolicyEngine == nil {
return nil, errors.New("policy engine not provided for new endpoint")
}
if c.CacheReloadInterval == 0 {
c.CacheReloadInterval = defaultCacheReloadInterval
}
if c.PruneEventsOlderThan == 0 {
c.PruneEventsOlderThan = defaultPruneEventsOlderThan
}
if c.SQLTransactionTimeout == 0 {
c.SQLTransactionTimeout = defaultSQLTransactionTimeout
}
ds := c.Catalog.GetDataStore()
var ef api.AuthorizedEntryFetcher
var cacheRebuildTask, pruneEventsTask func(context.Context) error
if c.EventsBasedCache {
efEventsBasedCache, err := NewAuthorizedEntryFetcherWithEventsBasedCache(ctx, c.Log, c.Clock, ds, c.CacheReloadInterval, c.PruneEventsOlderThan, c.SQLTransactionTimeout)
if err != nil {
return nil, err
}
cacheRebuildTask = efEventsBasedCache.RunUpdateCacheTask
pruneEventsTask = efEventsBasedCache.PruneEventsTask
ef = efEventsBasedCache
} else {
buildCacheFn := func(ctx context.Context) (_ entrycache.Cache, err error) {
call := telemetry.StartCall(c.Metrics, telemetry.Entry, telemetry.Cache, telemetry.Reload)
defer call.Done(&err)
return entrycache.BuildFromDataStore(ctx, c.Catalog.GetDataStore())
}
efFullCache, err := NewAuthorizedEntryFetcherWithFullCache(ctx, buildCacheFn, c.Log, c.Clock, ds, c.CacheReloadInterval, c.PruneEventsOlderThan)
if err != nil {
return nil, err
}
cacheRebuildTask = efFullCache.RunRebuildCacheTask
pruneEventsTask = efFullCache.PruneEventsTask
ef = efFullCache
}
bundleEndpointServer, certificateReloadTask := c.maybeMakeBundleEndpointServer()
return &Endpoints{
TCPAddr: c.TCPAddr,
LocalAddr: c.LocalAddr,
SVIDObserver: c.SVIDObserver,
TrustDomain: c.TrustDomain,
DataStore: ds,
BundleCache: bundle.NewCache(ds, c.Clock),
APIServers: c.makeAPIServers(ef),
BundleEndpointServer: bundleEndpointServer,
Log: c.Log,
Metrics: c.Metrics,
RateLimit: c.RateLimit,
EntryFetcherCacheRebuildTask: cacheRebuildTask,
EntryFetcherPruneEventsTask: pruneEventsTask,
CertificateReloadTask: certificateReloadTask,
AuditLogEnabled: c.AuditLogEnabled,
AuthPolicyEngine: c.AuthPolicyEngine,
AdminIDs: c.AdminIDs,
}, nil
}
// ListenAndServe starts all endpoint servers and blocks until the context
// is canceled or any of the servers fails to run. If the context is
// canceled, the function returns nil. Otherwise, the error from the failed
// server is returned.
func (e *Endpoints) ListenAndServe(ctx context.Context) error {
e.Log.Debug("Initializing API endpoints")
unaryInterceptor, streamInterceptor := e.makeInterceptors()
tcpServer := e.createTCPServer(ctx, unaryInterceptor, streamInterceptor)
udsServer := e.createUDSServer(unaryInterceptor, streamInterceptor)
// TCP and UDS
agentv1.RegisterAgentServer(tcpServer, e.APIServers.AgentServer)
agentv1.RegisterAgentServer(udsServer, e.APIServers.AgentServer)
bundlev1.RegisterBundleServer(tcpServer, e.APIServers.BundleServer)
bundlev1.RegisterBundleServer(udsServer, e.APIServers.BundleServer)
entryv1.RegisterEntryServer(tcpServer, e.APIServers.EntryServer)
entryv1.RegisterEntryServer(udsServer, e.APIServers.EntryServer)
svidv1.RegisterSVIDServer(tcpServer, e.APIServers.SVIDServer)
svidv1.RegisterSVIDServer(udsServer, e.APIServers.SVIDServer)
trustdomainv1.RegisterTrustDomainServer(tcpServer, e.APIServers.TrustDomainServer)
trustdomainv1.RegisterTrustDomainServer(udsServer, e.APIServers.TrustDomainServer)
// UDS only
loggerv1.RegisterLoggerServer(udsServer, e.APIServers.LoggerServer)
grpc_health_v1.RegisterHealthServer(udsServer, e.APIServers.HealthServer)
debugv1_pb.RegisterDebugServer(udsServer, e.APIServers.DebugServer)
tasks := []func(context.Context) error{
func(ctx context.Context) error {
return e.runTCPServer(ctx, tcpServer)
},
func(ctx context.Context) error {
return e.runLocalAccess(ctx, udsServer)
},
e.EntryFetcherCacheRebuildTask,
}
if e.BundleEndpointServer != nil {
tasks = append(tasks, e.BundleEndpointServer.ListenAndServe)
}
if e.EntryFetcherPruneEventsTask != nil {
tasks = append(tasks, e.EntryFetcherPruneEventsTask)
}
if e.CertificateReloadTask != nil {
tasks = append(tasks, e.CertificateReloadTask)
}
err := util.RunTasks(ctx, tasks...)
if errors.Is(err, context.Canceled) {
err = nil
}
return err
}
func (e *Endpoints) createTCPServer(ctx context.Context, unaryInterceptor grpc.UnaryServerInterceptor, streamInterceptor grpc.StreamServerInterceptor) *grpc.Server {
tlsConfig := &tls.Config{ //nolint: gosec // False positive, getTLSConfig is setting MinVersion
GetConfigForClient: e.getTLSConfig(ctx),
}
return grpc.NewServer(
grpc.UnaryInterceptor(unaryInterceptor),
grpc.StreamInterceptor(streamInterceptor),
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionAge: defaultMaxConnectionAge,
}),
)
}
func (e *Endpoints) createUDSServer(unaryInterceptor grpc.UnaryServerInterceptor, streamInterceptor grpc.StreamServerInterceptor) *grpc.Server {
options := []grpc.ServerOption{
grpc.UnaryInterceptor(unaryInterceptor),
grpc.StreamInterceptor(streamInterceptor),
}
if e.AuditLogEnabled {
options = append(options, grpc.Creds(peertracker.NewCredentials()))
} else {
options = append(options, grpc.Creds(auth.UntrackedUDSCredentials()))
}
return grpc.NewServer(options...)
}
// runTCPServer will start the server and block until it exits or we are dying.
func (e *Endpoints) runTCPServer(ctx context.Context, server *grpc.Server) error {
l, err := net.Listen(e.TCPAddr.Network(), e.TCPAddr.String())
if err != nil {
return err
}
defer l.Close()
log := e.Log.WithFields(logrus.Fields{
telemetry.Network: l.Addr().Network(),
telemetry.Address: l.Addr().String(),
})
// Skip use of tomb here so we don't pollute a clean shutdown with errors
log.Info("Starting Server APIs")
errChan := make(chan error)
go func() { errChan <- server.Serve(l) }()
select {
case err = <-errChan:
log.WithError(err).Error("Server APIs stopped prematurely")
return err
case <-ctx.Done():
log.Info("Stopping Server APIs")
server.Stop()
<-errChan
log.Info("Server APIs have stopped")
return nil
}
}
// runLocalAccess will start a grpc server to be accessed locally
// and block until it exits or we are dying.
func (e *Endpoints) runLocalAccess(ctx context.Context, server *grpc.Server) error {
os.Remove(e.LocalAddr.String())
var l net.Listener
var err error
if e.AuditLogEnabled {
l, err = e.listenWithAuditLog()
} else {
l, err = e.listen()
}
if err != nil {
return err
}
defer l.Close()
if err := e.restrictLocalAddr(); err != nil {
return err
}
log := e.Log.WithFields(logrus.Fields{
telemetry.Network: l.Addr().Network(),
telemetry.Address: l.Addr().String(),
})
// Skip use of tomb here so we don't pollute a clean shutdown with errors
log.Info("Starting Server APIs")
errChan := make(chan error)
go func() { errChan <- server.Serve(l) }()
select {
case err := <-errChan:
log.WithError(err).Error("Server APIs stopped prematurely")
return err
case <-ctx.Done():
log.Info("Stopping Server APIs")
server.Stop()
<-errChan
log.Info("Server APIs have stopped")
return nil
}
}
// getTLSConfig returns a TLS Config hook for the gRPC server
func (e *Endpoints) getTLSConfig(ctx context.Context) func(*tls.ClientHelloInfo) (*tls.Config, error) {
return func(hello *tls.ClientHelloInfo) (*tls.Config, error) {
svidSrc := newX509SVIDSource(func() svid.State {
return e.SVIDObserver.State()
})
bundleSrc := newBundleSource(func(td spiffeid.TrustDomain) ([]*x509.Certificate, error) {
return e.bundleGetter(ctx, td)
})
spiffeTLSConfig := tlsconfig.MTLSServerConfig(svidSrc, bundleSrc, nil)
// provided client certificates will be validated using the custom VerifyPeerCertificate hook
spiffeTLSConfig.ClientAuth = tls.RequestClientCert
spiffeTLSConfig.MinVersion = tls.VersionTLS12
spiffeTLSConfig.NextProtos = []string{http2.NextProtoTLS}
spiffeTLSConfig.VerifyPeerCertificate = e.serverSpiffeVerificationFunc(bundleSrc)
return spiffeTLSConfig, nil
}
}
func (e *Endpoints) makeInterceptors() (grpc.UnaryServerInterceptor, grpc.StreamServerInterceptor) {
log := e.Log.WithField(telemetry.SubsystemName, "api")
return middleware.Interceptors(Middleware(log, e.Metrics, e.DataStore, clock.New(), e.RateLimit, e.AuthPolicyEngine, e.AuditLogEnabled, e.AdminIDs))
}