@@ -28,6 +28,9 @@ import (
28
28
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
29
29
"k8s.io/utils/clock"
30
30
31
+ "github.com/dapr/kit/concurrency/dir"
32
+ "github.com/dapr/kit/crypto/pem"
33
+ "github.com/dapr/kit/crypto/spiffe/trustanchors"
31
34
"github.com/dapr/kit/logger"
32
35
)
33
36
@@ -38,6 +41,14 @@ type (
38
41
type Options struct {
39
42
Log logger.Logger
40
43
RequestSVIDFn RequestSVIDFn
44
+
45
+ // WriteIdentityToFile is used to write the identity private key and
46
+ // certificate chain to file. The certificate chain and private key will be
47
+ // written to the `tls.cert` and `tls.key` files respectively in the given
48
+ // directory.
49
+ WriteIdentityToFile * string
50
+
51
+ TrustAnchors trustanchors.Interface
41
52
}
42
53
43
54
// SPIFFE is a readable/writeable store of a SPIFFE X.509 SVID.
@@ -46,6 +57,9 @@ type SPIFFE struct {
46
57
currentSVID * x509svid.SVID
47
58
requestSVIDFn RequestSVIDFn
48
59
60
+ dir * dir.Dir
61
+ trustAnchors trustanchors.Interface
62
+
49
63
log logger.Logger
50
64
lock sync.RWMutex
51
65
clock clock.Clock
@@ -54,8 +68,18 @@ type SPIFFE struct {
54
68
}
55
69
56
70
func New (opts Options ) * SPIFFE {
71
+ var sdir * dir.Dir
72
+ if opts .WriteIdentityToFile != nil {
73
+ sdir = dir .New (dir.Options {
74
+ Log : opts .Log ,
75
+ Target : * opts .WriteIdentityToFile ,
76
+ })
77
+ }
78
+
57
79
return & SPIFFE {
58
80
requestSVIDFn : opts .RequestSVIDFn ,
81
+ dir : sdir ,
82
+ trustAnchors : opts .TrustAnchors ,
59
83
log : opts .Log ,
60
84
clock : clock.RealClock {},
61
85
readyCh : make (chan struct {}),
@@ -165,6 +189,31 @@ func (s *SPIFFE) fetchIdentityCertificate(ctx context.Context) (*x509svid.SVID,
165
189
return nil , fmt .Errorf ("error parsing spiffe id from newly signed certificate: %w" , err )
166
190
}
167
191
192
+ if s .dir != nil {
193
+ pkPEM , err := pem .EncodePrivateKey (key )
194
+ if err != nil {
195
+ return nil , err
196
+ }
197
+
198
+ certPEM , err := pem .EncodeX509Chain (workloadcert )
199
+ if err != nil {
200
+ return nil , err
201
+ }
202
+
203
+ td , err := s .trustAnchors .CurrentTrustAnchors (ctx )
204
+ if err != nil {
205
+ return nil , err
206
+ }
207
+
208
+ if err := s .dir .Write (map [string ][]byte {
209
+ "key.pem" : pkPEM ,
210
+ "cert.pem" : certPEM ,
211
+ "ca.pem" : td ,
212
+ }); err != nil {
213
+ return nil , err
214
+ }
215
+ }
216
+
168
217
return & x509svid.SVID {
169
218
ID : spiffeID ,
170
219
Certificates : workloadcert ,
0 commit comments