forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mastercerts.go
373 lines (313 loc) · 12.8 KB
/
create_mastercerts.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
package admin
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/cert"
utilerrors "k8s.io/kubernetes/pkg/util/errors"
"github.com/openshift/origin/pkg/cmd/server/crypto"
"github.com/openshift/origin/pkg/cmd/templates"
"github.com/openshift/origin/pkg/util/parallel"
)
const CreateMasterCertsCommandName = "create-master-certs"
var masterCertLong = templates.LongDesc(`
Create keys and certificates for a master
This command creates keys and certs necessary to run a secure master.
It also creates keys, certificates, and configuration necessary for most
related infrastructure components that are clients to the master.
See the related "create-node-config" command for generating per-node config.
All files are expected or created in standard locations under the cert-dir.
openshift.local.config/master/
ca.{crt,key,serial.txt}
master.server.{crt,key}
admin.{crt,key,kubeconfig}
...
Note that the certificate authority (CA aka "signer") generated automatically
is self-signed. In production usage, administrators are more likely to
want to generate signed certificates separately rather than rely on a
generated CA. Alternatively, start with an existing signed CA and
have this command use it to generate valid certificates.
This command would usually only be used once at installation. If you
need to regenerate the master server cert, DO NOT use --overwrite as this
would recreate ALL certs including the CA cert, invalidating any existing
infrastructure or client configuration. Instead, delete/rename the existing
server cert and run the command to fill it in:
mv openshift.local.config/master/master.server.crt{,.old}
%[1]s --cert-dir=... \
--master=https://internal.master.fqdn:8443 \
--public-master=https://external.master.fqdn:8443 \
--hostnames=external.master.fqdn,internal.master.fqdn,localhost,127.0.0.1,172.17.42.1,kubernetes.default.local
Alternatively, use the related "ca create-server-cert" command to explicitly
create a certificate.
Regardless of --overwrite, the master server key/cert will be updated
if --hostnames does not match the current certificate.
Regardless of --overwrite, .kubeconfig files will be updated every time this
command is run, so always specify --master (and if needed, --public-master).
This is designed to match the behavior of "start" which rewrites certs/confs
for certain configuration changes.`)
type CreateMasterCertsOptions struct {
CertDir string
SignerName string
ExpireDays int
SignerExpireDays int
APIServerCAFiles []string
CABundleFile string
Hostnames []string
APIServerURL string
PublicAPIServerURL string
Overwrite bool
Output io.Writer
}
func NewCommandCreateMasterCerts(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateMasterCertsOptions{
ExpireDays: crypto.DefaultCertificateLifetimeInDays,
SignerExpireDays: crypto.DefaultCACertificateLifetimeInDays,
Output: out,
}
cmd := &cobra.Command{
Use: commandName,
Short: "Create certificates and keys for a master",
Long: fmt.Sprintf(masterCertLong, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.CreateMasterCerts(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
flags := cmd.Flags()
flags.StringVar(&options.CertDir, "cert-dir", "openshift.local.config/master", "The certificate data directory.")
flags.StringVar(&options.SignerName, "signer-name", DefaultSignerName(), "The name to use for the generated signer.")
flags.StringSliceVar(&options.APIServerCAFiles, "certificate-authority", options.APIServerCAFiles, "Optional files containing signing authorities to use (in addition to the generated signer) to verify the API server's serving certificate.")
flags.StringVar(&options.APIServerURL, "master", "https://localhost:8443", "The API server's URL.")
flags.StringVar(&options.PublicAPIServerURL, "public-master", "", "The API public facing server's URL (if applicable).")
flags.StringSliceVar(&options.Hostnames, "hostnames", options.Hostnames, "Every hostname or IP that server certs should be valid for (comma-delimited list)")
flags.BoolVar(&options.Overwrite, "overwrite", false, "Overwrite all existing cert/key/config files (WARNING: includes signer/CA)")
flags.IntVar(&options.ExpireDays, "expire-days", options.ExpireDays, "Validity of the certificates in days (defaults to 2 years). WARNING: extending this above default value is highly discouraged.")
flags.IntVar(&options.SignerExpireDays, "signer-expire-days", options.SignerExpireDays, "Validity of the CA certificate in days (defaults to 5 years). WARNING: extending this above default value is highly discouraged.")
// set dynamic value annotation - allows man pages to be generated and verified
flags.SetAnnotation("signer-name", "manpage-def-value", []string{"openshift-signer@<current_timestamp>"})
// autocompletion hints
cmd.MarkFlagFilename("cert-dir")
cmd.MarkFlagFilename("certificate-authority")
return cmd
}
func (o CreateMasterCertsOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.Hostnames) == 0 {
return errors.New("at least one hostname must be provided")
}
if len(o.CertDir) == 0 {
return errors.New("cert-dir must be provided")
}
if len(o.SignerName) == 0 {
return errors.New("signer-name must be provided")
}
if o.ExpireDays <= 0 {
return errors.New("expire-days must be valid number of days")
}
if o.SignerExpireDays <= 0 {
return errors.New("signer-expire-days must be valid number of days")
}
if len(o.APIServerURL) == 0 {
return errors.New("master must be provided")
} else if u, err := url.Parse(o.APIServerURL); err != nil {
return errors.New("master must be a valid URL (e.g. https://10.0.0.1:8443)")
} else if len(u.Scheme) == 0 {
return errors.New("master must be a valid URL (e.g. https://10.0.0.1:8443)")
}
if len(o.PublicAPIServerURL) == 0 {
// not required
} else if u, err := url.Parse(o.PublicAPIServerURL); err != nil {
return errors.New("public master must be a valid URL (e.g. https://example.com:8443)")
} else if len(u.Scheme) == 0 {
return errors.New("public master must be a valid URL (e.g. https://example.com:8443)")
}
for _, caFile := range o.APIServerCAFiles {
if _, err := cert.NewPool(caFile); err != nil {
return fmt.Errorf("certificate authority must be a valid certificate file: %v", err)
}
}
return nil
}
func (o CreateMasterCertsOptions) CreateMasterCerts() error {
glog.V(4).Infof("Creating all certs with: %#v", o)
signerCertOptions := CreateSignerCertOptions{
CertFile: DefaultCertFilename(o.CertDir, CAFilePrefix),
KeyFile: DefaultKeyFilename(o.CertDir, CAFilePrefix),
SerialFile: DefaultSerialFilename(o.CertDir, CAFilePrefix),
ExpireDays: o.SignerExpireDays,
Name: o.SignerName,
Overwrite: o.Overwrite,
Output: o.Output,
}
if err := signerCertOptions.Validate(nil); err != nil {
return err
}
if _, err := signerCertOptions.CreateSignerCert(); err != nil {
return err
}
// once we've minted the signer, don't overwrite it
getSignerCertOptions := SignerCertOptions{
CertFile: DefaultCertFilename(o.CertDir, CAFilePrefix),
KeyFile: DefaultKeyFilename(o.CertDir, CAFilePrefix),
SerialFile: DefaultSerialFilename(o.CertDir, CAFilePrefix),
}
errs := parallel.Run(
func() error { return o.createCABundle(&getSignerCertOptions) },
func() error { return o.createServerCerts(&getSignerCertOptions) },
func() error { return o.createAPIClients(&getSignerCertOptions) },
func() error { return o.createEtcdClientCerts(&getSignerCertOptions) },
func() error { return o.createKubeletClientCerts(&getSignerCertOptions) },
func() error { return o.createProxyClientCerts(&getSignerCertOptions) },
func() error { return o.createServiceAccountKeys() },
func() error { return o.createServiceSigningCA(&getSignerCertOptions) },
)
return utilerrors.NewAggregate(errs)
}
func (o CreateMasterCertsOptions) createAPIClients(getSignerCertOptions *SignerCertOptions) error {
for _, clientCertInfo := range DefaultAPIClientCerts(o.CertDir) {
if err := o.createClientCert(clientCertInfo, getSignerCertOptions); err != nil {
return err
}
createKubeConfigOptions := CreateKubeConfigOptions{
APIServerURL: o.APIServerURL,
PublicAPIServerURL: o.PublicAPIServerURL,
APIServerCAFiles: append([]string{getSignerCertOptions.CertFile}, o.APIServerCAFiles...),
CertFile: clientCertInfo.CertLocation.CertFile,
KeyFile: clientCertInfo.CertLocation.KeyFile,
ContextNamespace: kapi.NamespaceDefault,
KubeConfigFile: DefaultKubeConfigFilename(filepath.Dir(clientCertInfo.CertLocation.CertFile), clientCertInfo.UnqualifiedUser),
Output: o.Output,
}
if err := createKubeConfigOptions.Validate(nil); err != nil {
return err
}
if _, err := createKubeConfigOptions.CreateKubeConfig(); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createEtcdClientCerts(getSignerCertOptions *SignerCertOptions) error {
for _, clientCertInfo := range DefaultEtcdClientCerts(o.CertDir) {
if err := o.createClientCert(clientCertInfo, getSignerCertOptions); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createProxyClientCerts(getSignerCertOptions *SignerCertOptions) error {
for _, clientCertInfo := range DefaultProxyClientCerts(o.CertDir) {
if err := o.createClientCert(clientCertInfo, getSignerCertOptions); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createKubeletClientCerts(getSignerCertOptions *SignerCertOptions) error {
for _, clientCertInfo := range DefaultKubeletClientCerts(o.CertDir) {
if err := o.createClientCert(clientCertInfo, getSignerCertOptions); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createClientCert(clientCertInfo ClientCertInfo, getSignerCertOptions *SignerCertOptions) error {
clientCertOptions := CreateClientCertOptions{
SignerCertOptions: getSignerCertOptions,
CertFile: clientCertInfo.CertLocation.CertFile,
KeyFile: clientCertInfo.CertLocation.KeyFile,
ExpireDays: o.ExpireDays,
User: clientCertInfo.User,
Groups: clientCertInfo.Groups.List(),
Overwrite: o.Overwrite,
Output: o.Output,
}
if err := clientCertOptions.Validate(nil); err != nil {
return err
}
if _, err := clientCertOptions.CreateClientCert(); err != nil {
return err
}
return nil
}
func (o CreateMasterCertsOptions) createCABundle(getSignerCertOptions *SignerCertOptions) error {
caFiles := []string{getSignerCertOptions.CertFile}
caFiles = append(caFiles, o.APIServerCAFiles...)
caData, err := readFiles(caFiles, []byte("\n"))
if err != nil {
return err
}
// ensure parent dir
if err := os.MkdirAll(o.CertDir, os.FileMode(0755)); err != nil {
return err
}
return ioutil.WriteFile(DefaultCABundleFile(o.CertDir), caData, 0644)
}
func (o CreateMasterCertsOptions) createServerCerts(getSignerCertOptions *SignerCertOptions) error {
for _, serverCertInfo := range DefaultServerCerts(o.CertDir) {
serverCertOptions := CreateServerCertOptions{
SignerCertOptions: getSignerCertOptions,
CertFile: serverCertInfo.CertFile,
KeyFile: serverCertInfo.KeyFile,
ExpireDays: o.ExpireDays,
Hostnames: o.Hostnames,
Overwrite: o.Overwrite,
Output: o.Output,
}
if err := serverCertOptions.Validate(nil); err != nil {
return err
}
if _, err := serverCertOptions.CreateServerCert(); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createServiceAccountKeys() error {
keypairOptions := CreateKeyPairOptions{
PublicKeyFile: DefaultServiceAccountPublicKeyFile(o.CertDir),
PrivateKeyFile: DefaultServiceAccountPrivateKeyFile(o.CertDir),
Overwrite: o.Overwrite,
Output: o.Output,
}
if err := keypairOptions.Validate(nil); err != nil {
return err
}
if err := keypairOptions.CreateKeyPair(); err != nil {
return err
}
return nil
}
func (o CreateMasterCertsOptions) createServiceSigningCA(getSignerCertOptions *SignerCertOptions) error {
caInfo := DefaultServiceSignerCAInfo(o.CertDir)
caOptions := CreateSignerCertOptions{
CertFile: caInfo.CertFile,
KeyFile: caInfo.KeyFile,
SerialFile: "", // we want the random cert serial for this one
ExpireDays: o.SignerExpireDays,
Name: DefaultServiceServingCertSignerName(),
Output: o.Output,
Overwrite: o.Overwrite,
}
if err := caOptions.Validate(nil); err != nil {
return err
}
if _, err := caOptions.CreateSignerCert(); err != nil {
return err
}
return nil
}