forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_clientcert.go
74 lines (60 loc) · 1.69 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
package admin
import (
"errors"
"io"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/auth/user"
"k8s.io/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/cmd/server/crypto"
)
type CreateClientCertOptions struct {
SignerCertOptions *SignerCertOptions
CertFile string
KeyFile string
User string
Groups util.StringList
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 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)
} else {
cert, written, err = signerCert.EnsureClientCertificate(o.CertFile, o.KeyFile, userInfo)
}
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
}