forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mastercerts.go
266 lines (222 loc) · 8.91 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
package admin
import (
"errors"
"fmt"
"io"
"path/filepath"
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
utilerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/openshift/origin/pkg/util/parallel"
)
const CreateMasterCertsCommandName = "create-master-certs"
const masterCertLong = `Create keys and certificates for an OpenShift master
This command creates keys and certs necessary to run a secure OpenShift 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}
openshift-router.{crt,key,kubeconfig}
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 an
OpenShift-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 "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 "openshift start" which rewrites
certs/confs for certain configuration changes.
`
type CreateMasterCertsOptions struct {
CertDir string
SignerName string
Hostnames util.StringList
APIServerURL string
PublicAPIServerURL string
Overwrite bool
Output io.Writer
}
func NewCommandCreateMasterCerts(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateMasterCertsOptions{Output: out}
cmd := &cobra.Command{
Use: commandName,
Short: "Create certificates for an OpenShift 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.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.Var(&options.Hostnames, "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)")
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 len(o.APIServerURL) == 0 {
return errors.New("master must be provided")
}
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),
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 := GetSignerCertOptions{
CertFile: DefaultCertFilename(o.CertDir, CAFilePrefix),
KeyFile: DefaultKeyFilename(o.CertDir, CAFilePrefix),
SerialFile: DefaultSerialFilename(o.CertDir, CAFilePrefix),
}
errs := parallel.Run(
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.createServiceAccountKeys() },
)
return utilerrors.NewAggregate(errs)
}
func (o CreateMasterCertsOptions) createAPIClients(getSignerCertOptions *GetSignerCertOptions) 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,
APIServerCAFile: getSignerCertOptions.CertFile,
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 *GetSignerCertOptions) error {
for _, clientCertInfo := range DefaultEtcdClientCerts(o.CertDir) {
if err := o.createClientCert(clientCertInfo, getSignerCertOptions); err != nil {
return err
}
}
return nil
}
func (o CreateMasterCertsOptions) createKubeletClientCerts(getSignerCertOptions *GetSignerCertOptions) 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 *GetSignerCertOptions) error {
clientCertOptions := CreateClientCertOptions{
GetSignerCertOptions: getSignerCertOptions,
CertFile: clientCertInfo.CertLocation.CertFile,
KeyFile: clientCertInfo.CertLocation.KeyFile,
User: clientCertInfo.User,
Groups: util.StringList(clientCertInfo.Groups.List()),
Overwrite: o.Overwrite,
Output: o.Output,
}
if _, err := clientCertOptions.CreateClientCert(); err != nil {
return err
}
return nil
}
func (o CreateMasterCertsOptions) createServerCerts(getSignerCertOptions *GetSignerCertOptions) error {
for _, serverCertInfo := range DefaultServerCerts(o.CertDir) {
serverCertOptions := CreateServerCertOptions{
GetSignerCertOptions: getSignerCertOptions,
CertFile: serverCertInfo.CertFile,
KeyFile: serverCertInfo.KeyFile,
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
}