-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.go
131 lines (110 loc) · 3.85 KB
/
app.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
/*
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 app
import (
"context"
"crypto"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"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/spf13/cobra"
"k8s.io/utils/clock"
"github.com/cert-manager/csi-driver/cmd/app/options"
"github.com/cert-manager/csi-driver/internal/version"
csiapi "github.com/cert-manager/csi-driver/pkg/apis/v1alpha1"
"github.com/cert-manager/csi-driver/pkg/filestore"
"github.com/cert-manager/csi-driver/pkg/keygen"
"github.com/cert-manager/csi-driver/pkg/requestgen"
)
const (
helpOutput = "Container Storage Interface driver to issue certificates from cert-manager"
)
// NewCommand will return a new command instance for the cert-manager CSI driver.
func NewCommand(ctx context.Context) *cobra.Command {
opts := options.New()
cmd := &cobra.Command{
Use: "cert-manager-csi-driver",
Short: helpOutput,
Long: helpOutput,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Complete()
},
RunE: func(cmd *cobra.Command, args []string) error {
log := opts.Logr.WithName("main")
log.Info("Starting driver", "version", version.VersionInfo())
store, err := storage.NewFilesystem(opts.Logr.WithName("storage"), opts.DataRoot)
if err != nil {
return fmt.Errorf("failed to setup filesystem: %w", err)
}
store.FSGroupVolumeAttributeKey = csiapi.FSGroupKey
keyGenerator := keygen.Generator{Store: store}
writer := filestore.Writer{Store: store}
var clientForMeta manager.ClientForMetadataFunc
if opts.UseTokenRequest {
clientForMeta = util.ClientForMetadataTokenRequestEmptyAud(opts.RestConfig)
}
mngrlog := opts.Logr.WithName("manager")
d, err := driver.New(opts.Endpoint, opts.Logr.WithName("driver"), driver.Options{
DriverName: opts.DriverName,
DriverVersion: version.AppVersion,
NodeID: opts.NodeID,
Store: store,
Manager: manager.NewManagerOrDie(manager.Options{
Client: opts.CMClient,
ClientForMetadata: clientForMeta,
MetadataReader: store,
Clock: clock.RealClock{},
Log: &mngrlog,
NodeID: opts.NodeID,
GeneratePrivateKey: keyGenerator.KeyForMetadata,
GenerateRequest: requestgen.RequestForMetadata,
SignRequest: signRequest,
WriteKeypair: writer.WriteKeypair,
}),
})
if err != nil {
return fmt.Errorf("failed to setup driver: " + err.Error())
}
go func() {
<-ctx.Done()
log.Info("shutting down driver", "context", ctx.Err())
d.Stop()
}()
log.Info("running driver")
if err := d.Run(); err != nil {
return fmt.Errorf("failed running driver: " + err.Error())
}
return nil
},
}
opts = opts.Prepare(cmd)
return cmd
}
// signRequest will sign an X.509 certificate signing request with the provided
// private key.
func signRequest(_ metadata.Metadata, key crypto.PrivateKey, request *x509.CertificateRequest) ([]byte, error) {
csrDer, err := x509.CreateCertificateRequest(rand.Reader, request, key)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csrDer,
}), nil
}