-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
security.go
442 lines (375 loc) · 13.4 KB
/
security.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package security
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/spiffe/go-spiffe/v2/spiffegrpc/grpccredentials"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/utils/clock"
"github.com/dapr/dapr/pkg/diagnostics"
"github.com/dapr/dapr/pkg/modes"
"github.com/dapr/dapr/pkg/security/legacy"
"github.com/dapr/kit/concurrency"
"github.com/dapr/kit/fswatcher"
"github.com/dapr/kit/logger"
)
var log = logger.NewLogger("dapr.runtime.security")
type RequestFn func(ctx context.Context, der []byte) ([]*x509.Certificate, error)
// Handler implements middleware for client and server connection security.
//
//nolint:interfacebloat
type Handler interface {
GRPCServerOptionMTLS() grpc.ServerOption
GRPCServerOptionNoClientAuth() grpc.ServerOption
GRPCDialOptionMTLSUnknownTrustDomain(ns, appID string) grpc.DialOption
GRPCDialOptionMTLS(spiffeid.ID) grpc.DialOption
TLSServerConfigNoClientAuth() *tls.Config
NetListenerID(net.Listener, spiffeid.ID) net.Listener
NetDialerID(context.Context, spiffeid.ID, time.Duration) func(network, addr string) (net.Conn, error)
ControlPlaneTrustDomain() spiffeid.TrustDomain
ControlPlaneNamespace() string
CurrentTrustAnchors() ([]byte, error)
MTLSEnabled() bool
WatchTrustAnchors(context.Context, chan<- []byte)
}
// Provider is the security provider.
type Provider interface {
Run(context.Context) error
Handler(context.Context) (Handler, error)
}
// Options are the options for the security authenticator.
type Options struct {
// SentryAddress is the network address of the sentry server.
SentryAddress string
// ControlPlaneTrustDomain is the trust domain of the control plane
// components.
ControlPlaneTrustDomain string
// ControlPlaneNamespace is the dapr namespace of the control plane
// components.
ControlPlaneNamespace string
// TrustAnchors is the X.509 PEM encoded CA certificates for this Dapr
// installation. Cannot be used with TrustAnchorsFile. TrustAnchorsFile is
// preferred so changes to the file are automatically picked up.
TrustAnchors []byte
// TrustAnchorsFile is the path to the X.509 PEM encoded CA certificates for
// this Dapr installation. Prefer this over TrustAnchors so changes to the
// file are automatically picked up. Cannot be used with TrustAnchors.
TrustAnchorsFile string
// AppID is the application ID of this workload.
AppID string
// MTLS is true if mTLS is enabled.
MTLSEnabled bool
// OverrideCertRequestSource is used to override where certificates are requested
// from. Default to an implementation requesting from Sentry.
OverrideCertRequestSource RequestFn
// Mode is the operation mode of this security instance (self-hosted or
// Kubernetes).
Mode modes.DaprMode
}
type provider struct {
sec *security
running atomic.Bool
readyCh chan struct{}
trustAnchorsFile string
// fswatcherInterval is the interval at which the trust anchors file changes
// are batched. Used for testing only, and 500ms otherwise.
fswatcherInterval time.Duration
}
// security implements the Security interface.
type security struct {
controlPlaneTrustDomain spiffeid.TrustDomain
controlPlaneNamespace string
source *x509source
mtls bool
}
func New(ctx context.Context, opts Options) (Provider, error) {
if len(opts.ControlPlaneTrustDomain) == 0 {
return nil, errors.New("control plane trust domain is required")
}
td, err := spiffeid.TrustDomainFromString(opts.ControlPlaneTrustDomain)
if err != nil {
return nil, fmt.Errorf("invalid control plane trust domain: %w", err)
}
// Always request certificates from Sentry if mTLS is enabled or running in
// Kubernetes. In Kubernetes, Daprd always communicates mTLS with the control
// plane.
var source *x509source
if opts.MTLSEnabled || opts.Mode == modes.KubernetesMode {
if len(opts.TrustAnchors) > 0 && len(opts.TrustAnchorsFile) > 0 {
return nil, errors.New("trust anchors cannot be specified in both TrustAnchors and TrustAnchorsFile")
}
if len(opts.TrustAnchors) == 0 && len(opts.TrustAnchorsFile) == 0 {
return nil, errors.New("trust anchors are required")
}
var err error
source, err = newX509Source(ctx, clock.RealClock{}, td, opts)
if err != nil {
return nil, err
}
} else {
log.Warn("mTLS is disabled. Skipping certificate request and tls validation")
}
return &provider{
fswatcherInterval: time.Millisecond * 500,
readyCh: make(chan struct{}),
trustAnchorsFile: opts.TrustAnchorsFile,
sec: &security{
source: source,
mtls: opts.MTLSEnabled,
controlPlaneTrustDomain: td,
controlPlaneNamespace: opts.ControlPlaneNamespace,
},
}, nil
}
// Run is a blocking function which starts the security provider, handling
// rotation of credentials.
func (p *provider) Run(ctx context.Context) error {
if !p.running.CompareAndSwap(false, true) {
return errors.New("security provider already started")
}
// If the security source has not been initialized, then just wait to exit.
if p.sec.source == nil {
close(p.readyCh)
<-ctx.Done()
return nil
}
if p.sec.source.requestFn == nil {
p.sec.source.requestFn = p.sec.source.requestFromSentry
log.Infof("Fetching initial identity certificate from %s", p.sec.source.sentryAddress)
}
initialCert, err := p.sec.source.renewIdentityCertificate(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve the initial identity certificate: %w", err)
}
mngr := concurrency.NewRunnerManager(func(ctx context.Context) error {
p.sec.source.startRotation(ctx, p.sec.source.renewIdentityCertificate, initialCert)
return nil
})
if len(p.trustAnchorsFile) > 0 {
caEvent := make(chan struct{})
fs, err := fswatcher.New(fswatcher.Options{
Targets: []string{filepath.Dir(p.trustAnchorsFile)},
Interval: &p.fswatcherInterval,
})
if err != nil {
return err
}
err = mngr.Add(
func(ctx context.Context) error {
log.Infof("Watching trust anchors file '%s' for changes", p.trustAnchorsFile)
return fs.Run(ctx, caEvent)
},
func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-caEvent:
log.Info("Trust anchors file changed, reloading trust anchors")
if uErr := p.sec.source.updateTrustAnchorFromFile(ctx, p.trustAnchorsFile); uErr != nil {
log.Errorf("Failed to read trust anchors file '%s': %v", p.trustAnchorsFile, uErr)
}
}
}
},
)
if err != nil {
return err
}
}
diagnostics.DefaultMonitoring.MTLSInitCompleted()
close(p.readyCh)
log.Infof("Security is initialized successfully")
return mngr.Run(ctx)
}
// Handler returns a ready handler from the security provider. Blocks until
// the provider is ready.
func (p *provider) Handler(ctx context.Context) (Handler, error) {
select {
case <-p.readyCh:
return p.sec, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
// GRPCDialOptionMTLS returns a gRPC dial option which instruments client
// authentication using the current signed client certificate.
func (s *security) GRPCDialOptionMTLS(appID spiffeid.ID) grpc.DialOption {
// If source has not been initialized, then just return an insecure dial
// option. We don't check on `mtls` here as we still want to use mTLS with
// control plane peers when running in Kubernetes mode even if mTLS is
// disabled.
if s.source == nil {
return grpc.WithTransportCredentials(insecure.NewCredentials())
}
return grpc.WithTransportCredentials(credentials.NewTLS(
legacy.NewDialClient(s.source, s.source, tlsconfig.AuthorizeID(appID)),
))
}
// GRPCServerOptionMTLS returns a gRPC server option which instruments
// authentication of clients using the current trust anchors.
func (s *security) GRPCServerOptionMTLS() grpc.ServerOption {
if !s.mtls {
return grpc.Creds(insecure.NewCredentials())
}
return grpc.Creds(
// TODO: It would be better if we could give a subset of trust domains in
// which this server authorizes.
grpccredentials.MTLSServerCredentials(s.source, s.source, tlsconfig.AuthorizeAny()),
)
}
// GRPCServerOptionNoClientAuth returns a gRPC server option which instruments
// authentication of clients using the current trust anchors. Doesn't require
// clients to present a certificate.
func (s *security) GRPCServerOptionNoClientAuth() grpc.ServerOption {
return grpc.Creds(grpccredentials.TLSServerCredentials(s.source))
}
// GRPCDialOptionMTLSUnknownTrustDomain returns a gRPC dial option which
// instruments client authentication using the current signed client
// certificate. Doesn't verify the servers trust domain, but does authorize the
// SPIFFE ID path.
// Used for clients which don't know the servers Trust Domain.
func (s *security) GRPCDialOptionMTLSUnknownTrustDomain(ns, appID string) grpc.DialOption {
if !s.mtls {
return grpc.WithTransportCredentials(insecure.NewCredentials())
}
expID := "/ns/" + ns + "/" + appID
matcher := func(actual spiffeid.ID) error {
if actual.Path() != expID {
return fmt.Errorf("unexpected SPIFFE ID: %q", actual)
}
return nil
}
return grpc.WithTransportCredentials(credentials.NewTLS(
legacy.NewDialClient(s.source, s.source, tlsconfig.AdaptMatcher(matcher)),
))
}
// CurrentTrustAnchors returns the current trust anchors for this Dapr
// installation.
func (s *security) CurrentTrustAnchors() ([]byte, error) {
if s.source == nil {
return nil, nil
}
ta, err := s.source.trustAnchors.Marshal()
if err != nil {
return nil, fmt.Errorf("failed to marshal trust anchors: %w", err)
}
return ta, nil
}
// ControlPlaneTrustDomain returns the trust domain of the control plane.
func (s *security) ControlPlaneTrustDomain() spiffeid.TrustDomain {
return s.controlPlaneTrustDomain
}
// ControlPlaneNamespace returns the dapr namespace of the control plane.
func (s *security) ControlPlaneNamespace() string {
return s.controlPlaneNamespace
}
// WatchTrustAnchors watches for changes to the trust domains and returns the
// PEM encoded trust domain roots.
// Returns when the given context is canceled.
func (s *security) WatchTrustAnchors(ctx context.Context, trustAnchors chan<- []byte) {
sub := make(chan struct{})
s.source.lock.Lock()
s.source.trustAnchorSubscribers = append(s.source.trustAnchorSubscribers, sub)
s.source.lock.Unlock()
for {
select {
case <-ctx.Done():
return
case <-sub:
caBundle, err := s.CurrentTrustAnchors()
if err != nil {
log.Errorf("failed to marshal trust anchors: %s", err)
continue
}
select {
case trustAnchors <- caBundle:
case <-ctx.Done():
}
}
}
}
// TLSServerConfigNoClientAuth returns a TLS server config which instruments
// using the current signed server certificate. Authorizes client certificate
// chains against the trust anchors.
func (s *security) TLSServerConfigNoClientAuth() *tls.Config {
return tlsconfig.TLSServerConfig(s.source)
}
// NetListenerID returns a mTLS net listener which instruments using the
// current signed server certificate. Authorizes client matches against the
// given SPIFFE ID.
func (s *security) NetListenerID(lis net.Listener, id spiffeid.ID) net.Listener {
if !s.mtls {
return lis
}
return tls.NewListener(lis,
tlsconfig.MTLSServerConfig(s.source, s.source, tlsconfig.AuthorizeID(id)),
)
}
// NetDialerID returns a mTLS net dialer which instruments using the current
// signed client certificate. Authorizes server matches against the given
// SPIFFE ID.
func (s *security) NetDialerID(ctx context.Context, spiffeID spiffeid.ID, timeout time.Duration) func(string, string) (net.Conn, error) {
if !s.mtls {
return (&net.Dialer{Timeout: timeout, Cancel: ctx.Done()}).Dial
}
return (&tls.Dialer{
NetDialer: (&net.Dialer{Timeout: timeout, Cancel: ctx.Done()}),
Config: tlsconfig.MTLSClientConfig(s.source, s.source, tlsconfig.AuthorizeID(spiffeID)),
}).Dial
}
// MTLSEnabled returns true if mTLS is enabled.
func (s *security) MTLSEnabled() bool {
return s.mtls
}
// CurrentNamespace returns the namespace of this workload.
func CurrentNamespace() string {
namespace, ok := os.LookupEnv("NAMESPACE")
if !ok {
return "default"
}
return namespace
}
// CurrentNamespaceOrError returns the namespace of this workload. If current
// Namespace is not found, error.
func CurrentNamespaceOrError() (string, error) {
namespace, ok := os.LookupEnv("NAMESPACE")
if !ok {
return "", errors.New("'NAMESPACE' environment variable not set")
}
if len(namespace) == 0 {
return "", errors.New("'NAMESPACE' environment variable is empty")
}
return namespace, nil
}
// SentryID returns the SPIFFE ID of the sentry server.
func SentryID(sentryTrustDomain spiffeid.TrustDomain, sentryNamespace string) (spiffeid.ID, error) {
sentryID, err := spiffeid.FromSegments(sentryTrustDomain, "ns", sentryNamespace, "dapr-sentry")
if err != nil {
return spiffeid.ID{}, fmt.Errorf("failed to parse sentry SPIFFE ID: %w", err)
}
return sentryID, nil
}