-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdriver.go
378 lines (309 loc) · 11.4 KB
/
driver.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
/*
Copyright 2021 The cert-manager 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 driver
import (
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net/url"
"strings"
"sync"
"time"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
cmclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
"github.com/cert-manager/csi-lib/driver"
"github.com/cert-manager/csi-lib/manager"
"github.com/cert-manager/csi-lib/manager/util"
"github.com/cert-manager/csi-lib/metadata"
"github.com/cert-manager/csi-lib/storage"
"github.com/go-logr/logr"
"gopkg.in/square/go-jose.v2/jwt"
"k8s.io/client-go/rest"
"k8s.io/utils/clock"
"github.com/cert-manager/csi-driver-spiffe/internal/annotations"
"github.com/cert-manager/csi-driver-spiffe/internal/csi/rootca"
"github.com/cert-manager/csi-driver-spiffe/internal/version"
)
// Options holds the Options needed for the CSI driver.
type Options struct {
// DriverName is the driver name as installed in Kubernetes.
DriverName string
// NodeID is the name of the node the driver is running on.
NodeID string
// DataRoot is the path to the in-memory data directory used to store data.
DataRoot string
// Endpoint is the endpoint which is used to listen for gRPC requests.
Endpoint string
// TrustDomain is the trust domain of this SPIFFE PKI. The TrustDomain will
// appear in signed certificate's URI SANs.
TrustDomain string
// CertificateRequestAnnotations are annotations that are to be added to certificate requests created by the driver
CertificateRequestAnnotations map[string]string
// CertificateRequestDuration is the duration CertificateRequests will be
// requested with.
// Defaults to 1 hour if empty.
CertificateRequestDuration time.Duration
// IssuerRef is the IssuerRef used when creating CertificateRequests.
IssuerRef cmmeta.ObjectReference
// CertificateFileName is the name of the file that the signed certificate
// will be written to inside the Pod's volume.
// Default to `tls.crt` if empty.
CertificateFileName string
// KeyFileName is the name of the file that the private key will be written
// to inside the Pod's volume.
// Default to `tls.key` if empty.
KeyFileName string
// CAFileName is the name of the file that the root CA certificates will be
// written to inside the Pod's volume. Ignored if RootCAs is nil.
CAFileName string
// RestConfig is used for interacting with the Kubernetes API server.
RestConfig *rest.Config
// RootCAs is optionally used to write root CA certificate data to Pod's
// volume. If nil, no root CA data is written to Pod's volume. If defined,
// root CA data will be written to the file with the name defined in
// CAFileName. If the root CA certificate data changes, all managed volume's
// file will be updated.
RootCAs rootca.Interface
}
// Driver is used for running the actual CSI driver. Driver will respond to
// NodePubishVolume events, and attempt to sign SPIFFE certificates for
// mounting pod's identity.
type Driver struct {
// log is the Driver logger.
log logr.Logger
// trustDomain is the trust domain that will form pod identities.
trustDomain string
// certificateRequestAnnotations are annotations that are to be added to certificate requests created by the driver
certificateRequestAnnotations map[string]string
// certificateRequestDuration is the duration which will be set of all
// created CertificateRequests.
certificateRequestDuration time.Duration
// issuerRef is the issuerRef that will be set on all created
// CertificateRequests.
issuerRef cmmeta.ObjectReference
// certFileName, keyFileName, caFileName are the names used when writing file
// to volumes.
certFileName, keyFileName, caFileName string
// rootCAs provides the root CA certificates to write to file. No CA file is
// written if this is nil.
rootCAs rootca.Interface
// driver is the csi-lib implementation of a cert-manager CSI driver.
driver *driver.Driver
// store is the csi-lib implementation of a cert-manager CSI storage manager.
store storage.Interface
// camanager is used to update all managed volumes with the current root CA
// certificates PEM.
camanager *camanager
}
// New constructs a new Driver instance.
func New(log logr.Logger, opts Options) (*Driver, error) {
sanitizedAnnotations, err := sanitizeAnnotations(opts.CertificateRequestAnnotations)
if err != nil {
log.Error(err, "some custom annotations were removed")
// don't exit, not a fatal error as sanitizeAnnotations will trim bad annotations
}
d := &Driver{
log: log.WithName("csi"),
trustDomain: opts.TrustDomain,
certFileName: opts.CertificateFileName,
keyFileName: opts.KeyFileName,
issuerRef: opts.IssuerRef,
rootCAs: opts.RootCAs,
certificateRequestDuration: opts.CertificateRequestDuration,
certificateRequestAnnotations: sanitizedAnnotations,
}
if len(d.certFileName) == 0 {
d.certFileName = "tls.crt"
}
if len(d.keyFileName) == 0 {
d.keyFileName = "tls.key"
}
if len(d.caFileName) == 0 {
d.caFileName = "ca.crt"
}
if d.certificateRequestDuration == 0 {
d.certificateRequestDuration = time.Hour
}
store, err := storage.NewFilesystem(d.log, opts.DataRoot)
if err != nil {
return nil, fmt.Errorf("failed to setup filesystem: %w", err)
}
// Used by clients to set the stored file's file-system group before
// mounting.
store.FSGroupVolumeAttributeKey = "spiffe.csi.cert-manager.io/fs-group"
d.store = store
d.camanager = newCAManager(log, store, opts.RootCAs,
opts.CertificateFileName, opts.KeyFileName, opts.CAFileName)
cmclient, err := cmclient.NewForConfig(opts.RestConfig)
if err != nil {
return nil, fmt.Errorf("failed to build cert-manager client: %w", err)
}
mngrLog := d.log.WithName("manager")
d.driver, err = driver.New(opts.Endpoint, d.log.WithName("driver"), driver.Options{
DriverName: opts.DriverName,
DriverVersion: version.AppVersion,
NodeID: opts.NodeID,
Store: d.store,
Manager: manager.NewManagerOrDie(manager.Options{
Client: cmclient,
// Use Pod's service account to request CertificateRequests.
ClientForMetadata: util.ClientForMetadataTokenRequestEmptyAud(opts.RestConfig),
MaxRequestsPerVolume: 1,
MetadataReader: d.store,
Clock: clock.RealClock{},
Log: &mngrLog,
NodeID: opts.NodeID,
GeneratePrivateKey: generatePrivateKey,
GenerateRequest: d.generateRequest,
SignRequest: signRequest,
WriteKeypair: d.writeKeypair,
}),
})
if err != nil {
return nil, fmt.Errorf("failed to setup csi driver: %w", err)
}
return d, nil
}
// Run is a blocking func that run the CSI driver.
func (d *Driver) Run(ctx context.Context) error {
var wg sync.WaitGroup
go func() {
<-ctx.Done()
d.driver.Stop()
}()
wg.Add(1)
go func() {
defer wg.Done()
d.camanager.run(ctx, time.Second*5)
}()
wg.Add(1)
var err error
go func() {
defer wg.Done()
err = d.driver.Run()
}()
wg.Wait()
return err
}
// generateRequest will generate a SPIFFE manager.CertificateRequestBundle
// based upon the identity contained in the metadata service account token.
func (d *Driver) generateRequest(meta metadata.Metadata) (*manager.CertificateRequestBundle, error) {
// Extract the service account token from the volume metadata in order to
// derive the service account, and thus identity of the pod.
token, err := util.EmptyAudienceTokenFromMetadata(meta)
if err != nil {
return nil, err
}
jwttoken, err := jwt.ParseSigned(token)
if err != nil {
return nil, fmt.Errorf("failed to parse token request token: %w", err)
}
claims := struct {
KubernetesIO struct {
Namespace string `json:"namespace"`
ServiceAccount struct {
Name string `json:"name"`
} `json:"serviceaccount"`
} `json:"kubernetes.io"`
}{}
// We don't need to verify the token since we will be using it against the
// API server anyway which is the source of trust for auth by definition.
if err := jwttoken.UnsafeClaimsWithoutVerification(&claims); err != nil {
return nil, fmt.Errorf("failed to decode token request token: %w", err)
}
saName := claims.KubernetesIO.ServiceAccount.Name
saNamespace := claims.KubernetesIO.Namespace
if len(saName) == 0 || len(saNamespace) == 0 {
return nil, fmt.Errorf("missing namespace or serviceaccount name in request token: %v", claims)
}
spiffeID := fmt.Sprintf("spiffe://%s/ns/%s/sa/%s", d.trustDomain, saNamespace, saName)
uri, err := url.Parse(spiffeID)
if err != nil {
return nil, fmt.Errorf("internal error crafting X.509 URI, this is a bug, please report on GitHub: %w", err)
}
crAnnotations := map[string]string{
annotations.SPIFFEIdentityAnnnotationKey: spiffeID,
}
for key, value := range d.certificateRequestAnnotations {
crAnnotations[key] = value
}
return &manager.CertificateRequestBundle{
Request: &x509.CertificateRequest{
URIs: []*url.URL{uri},
},
IsCA: false,
Namespace: saNamespace,
Duration: d.certificateRequestDuration,
Usages: []cmapi.KeyUsage{
cmapi.UsageDigitalSignature,
cmapi.UsageKeyEncipherment,
cmapi.UsageServerAuth,
cmapi.UsageClientAuth,
},
IssuerRef: d.issuerRef,
Annotations: crAnnotations,
}, nil
}
// writeKeypair writes the private key and certificate chain to file that will
// be mounted into the pod.
func (d *Driver) writeKeypair(meta metadata.Metadata, key crypto.PrivateKey, chain []byte, _ []byte) error {
pemBytes, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return fmt.Errorf("failed to marshal ECDSA private key for PEM encoding: %w", err)
}
keyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "PRIVATE KEY",
Bytes: pemBytes,
},
)
// Calculate the next issuance time before we write any data to file, so in
// the cases where this errors, we are not left in a bad state.
nextIssuanceTime, err := calculateNextIssuanceTime(chain)
if err != nil {
return fmt.Errorf("failed to calculate next issuance time: %w", err)
}
data := map[string][]byte{
d.certFileName: chain,
d.keyFileName: keyPEM,
}
// If configured, write the CA certificates as defined in RootCAs.
if d.rootCAs != nil {
data[d.caFileName] = d.rootCAs.CertificatesPEM()
}
// Write data to the actual volume that gets mounted.
if err := d.store.WriteFiles(meta, data); err != nil {
return fmt.Errorf("writing data: %w", err)
}
meta.NextIssuanceTime = &nextIssuanceTime
if err := d.store.WriteMetadata(meta.VolumeID, meta); err != nil {
return fmt.Errorf("writing metadata: %w", err)
}
return nil
}
func sanitizeAnnotations(in map[string]string) (map[string]string, error) {
out := map[string]string{}
var errs []error
for key, value := range in {
if strings.HasPrefix(key, annotations.Prefix) {
errs = append(errs, fmt.Errorf("custom annotation %q was not valid; must not begin with %s", key, annotations.Prefix))
continue
}
out[key] = value
}
return out, errors.Join(errs...)
}