forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_signercert.go
87 lines (71 loc) · 2.42 KB
/
create_signercert.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
package admin
import (
"errors"
"fmt"
"io"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openshift/origin/pkg/cmd/server/crypto"
)
const CreateSignerCertCommandName = "create-signer-cert"
type CreateSignerCertOptions struct {
CertFile string
KeyFile string
SerialFile string
Name string
Overwrite bool
}
func BindSignerCertOptions(options *CreateSignerCertOptions, flags *pflag.FlagSet, prefix string) {
flags.StringVar(&options.CertFile, prefix+"cert", "openshift.local.certificates/ca/cert.crt", "The certificate file.")
flags.StringVar(&options.KeyFile, prefix+"key", "openshift.local.certificates/ca/key.key", "The key file.")
flags.StringVar(&options.SerialFile, prefix+"serial", "openshift.local.certificates/ca/serial.txt", "The serial file that keeps track of how many certs have been signed.")
flags.StringVar(&options.Name, prefix+"name", DefaultSignerName(), "The name of the signer.")
flags.BoolVar(&options.Overwrite, prefix+"overwrite", options.Overwrite, "Overwrite existing cert files if found. If false, any existing file will be left as-is.")
}
func NewCommandCreateSignerCert(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateSignerCertOptions{Overwrite: true}
cmd := &cobra.Command{
Use: commandName,
Short: "Create signer certificate",
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.CreateSignerCert(); err != nil {
glog.Fatal(err)
}
},
}
cmd.SetOutput(out)
BindSignerCertOptions(options, cmd.Flags(), "")
return cmd
}
func (o CreateSignerCertOptions) 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.SerialFile) == 0 {
return errors.New("serial must be provided")
}
if len(o.Name) == 0 {
return errors.New("name must be provided")
}
return nil
}
func (o CreateSignerCertOptions) CreateSignerCert() (*crypto.CA, error) {
glog.V(2).Infof("Creating a signer cert with: %#v", o)
if o.Overwrite {
return crypto.MakeCA(o.CertFile, o.KeyFile, o.SerialFile, o.Name)
} else {
return crypto.EnsureCA(o.CertFile, o.KeyFile, o.SerialFile, o.Name)
}
}