forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mastercerts.go
211 lines (173 loc) · 6.08 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
package admin
import (
"errors"
"fmt"
"io"
"path"
"path/filepath"
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
const CreateMasterCertsCommandName = "create-master-certs"
type CreateMasterCertsOptions struct {
CertDir string
SignerName string
Hostnames util.StringList
APIServerURL string
PublicAPIServerURL string
Overwrite bool
}
func NewCommandCreateMasterCerts(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateMasterCertsOptions{}
cmd := &cobra.Command{
Use: commandName,
Short: "Create all certificates for an OpenShift master. To create node certificates, try openshift admin create-node-config",
Run: func(c *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
fmt.Fprintln(c.Out(), err.Error())
c.Help()
return
}
if err := options.CreateMasterCerts(); err != nil {
glog.Fatal(err)
}
},
}
cmd.SetOutput(out)
flags := cmd.Flags()
flags.StringVar(&options.CertDir, "cert-dir", "openshift.local.certificates", "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 you want server certs to be valid for. Comma delimited list")
flags.BoolVar(&options.Overwrite, "overwrite", true, "Overwrite existing cert files if found. If false, any existing file will be left as-is.")
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(2).Infof("Creating all certs with: %#v", o)
signerCertOptions := CreateSignerCertOptions{
CertFile: DefaultCertFilename(o.CertDir, DefaultCADir),
KeyFile: DefaultKeyFilename(o.CertDir, DefaultCADir),
SerialFile: DefaultSerialFilename(o.CertDir, DefaultCADir),
Name: o.SignerName,
Overwrite: o.Overwrite,
}
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, DefaultCADir),
KeyFile: DefaultKeyFilename(o.CertDir, DefaultCADir),
SerialFile: DefaultSerialFilename(o.CertDir, DefaultCADir),
}
if err := o.createServerCerts(&getSignerCertOptions); err != nil {
return err
}
if err := o.createAPIClients(&getSignerCertOptions); err != nil {
return err
}
if err := o.createEtcdClientCerts(&getSignerCertOptions); err != nil {
return err
}
if err := o.createKubeletClientCerts(&getSignerCertOptions); err != nil {
return err
}
return nil
}
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,
ServerNick: "master",
CertFile: clientCertInfo.CertLocation.CertFile,
KeyFile: clientCertInfo.CertLocation.KeyFile,
UserNick: clientCertInfo.User,
ContextNamespace: kapi.NamespaceDefault,
KubeConfigFile: path.Join(filepath.Dir(clientCertInfo.CertLocation.CertFile), ".kubeconfig"),
}
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,
}
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,
}
if err := serverCertOptions.Validate(nil); err != nil {
return err
}
if _, err := serverCertOptions.CreateServerCert(); err != nil {
return err
}
}
return nil
}