forked from eliquious/kappa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-ca.go
146 lines (123 loc) · 3.89 KB
/
init-ca.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
package commands
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"os"
"path"
"strings"
log "github.com/mgutz/logxi/v1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/subsilent/kappa/auth"
)
// InitCACmd is the kappa root command.
var InitCACmd = &cobra.Command{
Use: "init-ca",
Short: "init-ca creates a new certificate authority",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
// Create logger
writer := log.NewConcurrentWriter(os.Stdout)
logger := log.NewLogger(writer, "init-ca")
err := InitializeConfig(writer)
if err != nil {
return
}
// Setup directory structure
if err := auth.CreatePkiDirectories(logger, "."); err != nil {
return
}
// Create file paths
pki := path.Join(".", "pki")
crtFile := path.Join(pki, "ca.crt")
privFile := path.Join(pki, "private", "ca.key")
// Verify it is ok to delete files if they exist
if !viper.GetBool("ForceOverwrite") {
var files []string
for _, filename := range []string{privFile, crtFile} {
if _, err := os.Stat(filename); err == nil {
files = append(files, filename)
}
}
if len(files) > 0 {
var input string
fmt.Println("This operation will overwrite these existing files:")
for _, file := range files {
fmt.Println("\t", file)
}
fmt.Print("Are you sure you want to overwrite these files (yN)? ")
fmt.Scanln(&input)
if !strings.Contains(strings.ToLower(input), "y") {
fmt.Println("New certificate was not created.")
return
}
}
}
// generate private key
privatekey, err := rsa.GenerateKey(rand.Reader, viper.GetInt("Bits"))
if err != nil {
logger.Warn("Error generating private key")
return
}
// Create CA
cert, err := auth.CreateCertificateAuthority(logger, privatekey,
viper.GetInt("Years"), viper.GetString("Organization"),
viper.GetString("Country"), viper.GetString("Hosts"))
if err != nil {
logger.Warn("Error creating CA", "err", err.Error())
return
}
// Save cert
auth.SaveCertificate(logger, cert, crtFile)
// Save private key
auth.SavePrivateKey(logger, privatekey, privFile)
},
}
// Pointer to InitCACmd used in initialization
var initCmd *cobra.Command
// Command line args
var (
KeyBits int
Years int
Organization string
Country string
Hosts string
)
func init() {
InitCACmd.PersistentFlags().IntVarP(&KeyBits, "bits", "", 4096, "Number of bits in key")
InitCACmd.PersistentFlags().IntVarP(&Years, "years", "", 10, "Number of years until the CA certificate expires")
InitCACmd.PersistentFlags().StringVarP(&Organization, "organization", "", "kappa-ca", "Organization for CA")
InitCACmd.PersistentFlags().StringVarP(&Country, "country", "", "USA", "Country of origin for CA")
InitCACmd.PersistentFlags().StringVarP(&Hosts, "hosts", "", "127.0.0.1", "Comma delimited list of IPs or domains")
InitCACmd.PersistentFlags().BoolVarP(&ForceOverwrite, "overwrite", "", false, "Overwrite replaces existing certs")
initCmd = InitCACmd
}
// InitializeCertAuthConfig sets up the command line options for creating a CA
func InitializeCertAuthConfig(logger log.Logger) error {
viper.SetDefault("Bits", "4096")
viper.SetDefault("Years", "10")
viper.SetDefault("Organization", "kappa-ca")
viper.SetDefault("Country", "USA")
if initCmd.PersistentFlags().Lookup("bits").Changed {
logger.Info("", "Bits", KeyBits)
viper.Set("Bits", KeyBits)
}
if initCmd.PersistentFlags().Lookup("years").Changed {
logger.Info("", "Years", Years)
viper.Set("Years", Years)
}
if initCmd.PersistentFlags().Lookup("organization").Changed {
logger.Info("", "Organization", Organization)
viper.Set("Organization", Organization)
}
if initCmd.PersistentFlags().Lookup("country").Changed {
logger.Info("", "Country", Country)
viper.Set("Country", Country)
}
if initCmd.PersistentFlags().Lookup("hosts").Changed {
logger.Info("", "Hosts", Hosts)
viper.Set("Hosts", Hosts)
}
return nil
}