forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_clientcert.go
78 lines (63 loc) · 1.8 KB
/
create_clientcert.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
package admin
import (
"errors"
"io"
"github.com/golang/glog"
"k8s.io/apiserver/pkg/authentication/user"
"github.com/openshift/origin/pkg/cmd/server/crypto"
)
type CreateClientCertOptions struct {
SignerCertOptions *SignerCertOptions
CertFile string
KeyFile string
ExpireDays int
User string
Groups []string
Overwrite bool
Output io.Writer
}
func (o CreateClientCertOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.CertFile) == 0 {
return errors.New("cert must be provided")
}
if len(o.KeyFile) == 0 {
return errors.New("key must be provided")
}
if o.ExpireDays <= 0 {
return errors.New("expire-days must be valid number of days")
}
if len(o.User) == 0 {
return errors.New("user must be provided")
}
if o.SignerCertOptions == nil {
return errors.New("signer options are required")
}
if err := o.SignerCertOptions.Validate(); err != nil {
return err
}
return nil
}
func (o CreateClientCertOptions) CreateClientCert() (*crypto.TLSCertificateConfig, error) {
glog.V(4).Infof("Creating a client cert with: %#v and %#v", o, o.SignerCertOptions)
signerCert, err := o.SignerCertOptions.CA()
if err != nil {
return nil, err
}
var cert *crypto.TLSCertificateConfig
written := true
userInfo := &user.DefaultInfo{Name: o.User, Groups: o.Groups}
if o.Overwrite {
cert, err = signerCert.MakeClientCertificate(o.CertFile, o.KeyFile, userInfo, o.ExpireDays)
} else {
cert, written, err = signerCert.EnsureClientCertificate(o.CertFile, o.KeyFile, userInfo, o.ExpireDays)
}
if written {
glog.V(3).Infof("Generated new client cert as %s and key as %s\n", o.CertFile, o.KeyFile)
} else {
glog.V(3).Infof("Keeping existing client cert at %s and key at %s\n", o.CertFile, o.KeyFile)
}
return cert, err
}