-
Notifications
You must be signed in to change notification settings - Fork 46
/
generator.go
232 lines (199 loc) · 6.19 KB
/
generator.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
/*
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 requestgen
import (
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/cert-manager/csi-lib/manager"
"github.com/cert-manager/csi-lib/metadata"
"github.com/cert-manager/csi-driver/pkg/apis"
"github.com/cert-manager/csi-driver/pkg/apis/defaults"
csiapi "github.com/cert-manager/csi-driver/pkg/apis/v1alpha1"
"github.com/cert-manager/csi-driver/pkg/apis/validation"
)
// RequestForMetadata returns a csi-lib CertificateRequestBundle built using
// the volume attributed contained within the passed metadata.
func RequestForMetadata(meta metadata.Metadata) (*manager.CertificateRequestBundle, error) {
attrs, err := defaults.SetDefaultAttributes(meta.VolumeContext)
if err != nil {
return nil, err
}
if err := validation.ValidateAttributes(attrs); err != nil {
return nil, err.ToAggregate()
}
duration := cmapi.DefaultCertificateDuration
if durStr, ok := attrs[csiapi.DurationKey]; ok {
duration, err = time.ParseDuration(durStr)
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.DurationKey, err)
}
}
commonName, err := expand(meta, attrs[csiapi.CommonNameKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.CommonNameKey, err)
}
dns, err := parseDNSNames(meta, attrs[csiapi.DNSNamesKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.DNSNamesKey, err)
}
uris, err := parseURIs(meta, attrs[csiapi.URISANsKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.URISANsKey, err)
}
ips, err := parseIPAddresses(attrs[csiapi.IPSANsKey])
if err != nil {
return nil, fmt.Errorf("%q: %w", csiapi.IPSANsKey, err)
}
annotations := make(map[string]string)
for key, val := range attrs {
group, _, found := strings.Cut(key, "/")
if !found {
continue
}
if group != apis.GroupName &&
group != "csi.storage.k8s.io" {
annotations[key] = val
}
}
return &manager.CertificateRequestBundle{
Request: &x509.CertificateRequest{
Subject: pkix.Name{
CommonName: commonName,
},
DNSNames: dns,
IPAddresses: ips,
URIs: uris,
},
IsCA: strings.ToLower(attrs[csiapi.IsCAKey]) == "true",
Namespace: attrs[csiapi.K8sVolumeContextKeyPodNamespace],
Duration: duration,
Usages: keyUsagesFromAttributes(attrs[csiapi.KeyUsagesKey]),
IssuerRef: cmmeta.ObjectReference{
Name: attrs[csiapi.IssuerNameKey],
Kind: attrs[csiapi.IssuerKindKey],
Group: attrs[csiapi.IssuerGroupKey],
},
Annotations: annotations,
}, nil
}
// parseDNSNames parses a csi.cert-manager.io/dns-names value, and returns the
// set of DNS names to be requested. Executes metadata expand on string.
func parseDNSNames(meta metadata.Metadata, dnsNames string) ([]string, error) {
if len(dnsNames) == 0 {
return nil, nil
}
dns, err := expand(meta, dnsNames)
if err != nil {
return nil, err
}
return splitList(dns), nil
}
// parseIPAddresses parses a csi.cert-manager.io/ip-sans value, and returns the
// set IP addresses to be requested for.
func parseIPAddresses(ipCSV string) ([]net.IP, error) {
if len(ipCSV) == 0 {
return nil, nil
}
var ips []net.IP
var errs []string
for _, ipStr := range splitList(ipCSV) {
ip := net.ParseIP(ipStr)
if ip == nil {
errs = append(errs, ipStr)
continue
}
ips = append(ips, ip)
}
if len(errs) > 0 {
return nil, fmt.Errorf(`failed to parse IP address: ["%s"]`, strings.Join(errs, `","`))
}
return ips, nil
}
// parseIPAddresses parses a csi.cert-manager.io/uri-sans value, and returns
// the set of URI SANs to be requested. Executes metadata expand on string.
func parseURIs(meta metadata.Metadata, uriCSV string) ([]*url.URL, error) {
if len(uriCSV) == 0 {
return nil, nil
}
csv, err := expand(meta, uriCSV)
if err != nil {
return nil, err
}
var uris []*url.URL
var errs []string
for _, rawURI := range splitList(csv) {
uri, err := url.ParseRequestURI(rawURI)
if err != nil {
errs = append(errs, err.Error())
continue
}
uris = append(uris, uri)
}
if len(errs) > 0 {
return nil, errors.New(strings.Join(errs, ", "))
}
return uris, nil
}
// keyUsagesFromAttributes returns the set of key usages from the given CSV.
func keyUsagesFromAttributes(usagesCSV string) []cmapi.KeyUsage {
if len(usagesCSV) == 0 {
return nil
}
var keyUsages []cmapi.KeyUsage
for _, usage := range splitList(usagesCSV) {
keyUsages = append(keyUsages, cmapi.KeyUsage(usage))
}
return keyUsages
}
// expand executes os.Expand on the given csv with volume context variables
// provided by the metadata.
func expand(meta metadata.Metadata, csv string) (string, error) {
vars := map[string]string{
"POD_NAME": meta.VolumeContext[csiapi.K8sVolumeContextKeyPodName],
"POD_NAMESPACE": meta.VolumeContext[csiapi.K8sVolumeContextKeyPodNamespace],
"POD_UID": meta.VolumeContext[csiapi.K8sVolumeContextKeyPodUID],
"SERVICE_ACCOUNT_NAME": meta.VolumeContext[csiapi.K8sVolumeContextKeyServiceAccountName],
}
var errs []string
exp := os.Expand(csv, func(s string) string {
v, ok := vars[s]
if !ok {
errs = append(errs, fmt.Sprintf("undefined variable %q", s))
}
return v
})
if len(errs) > 0 {
return "", fmt.Errorf("%v, known variables: %v",
strings.Join(errs, ", "),
[]string{"POD_NAME", "POD_NAMESPACE", "POD_UID", "SERVICE_ACCOUNT_NAME"},
)
}
return exp, nil
}
// splitList returns the given csv as a slice. Trims space of each element.
func splitList(csv string) []string {
var list []string
for _, s := range strings.Split(csv, ",") {
list = append(list, strings.TrimSpace(s))
}
return list
}