This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
/
main.go
123 lines (105 loc) · 2.77 KB
/
main.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
/* {{{ Copyright 2015 Paul R. Tagliamonte <paultag@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. }}} */
package main
import (
"fmt"
"os"
"strings"
"pault.ag/go/config"
)
func Missing(paths ...string) bool {
for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
return true
}
}
return false
}
type MiniCA struct {
KeySize int `flag:"key-size" description:"Key Size"`
Org string `flag:"org" description:"Organization of the Cert"`
Type string `flag:"type" description:"Cert Type"`
CaCommonName string `flag:"ca-common-name" description:"Common Name of the CA Cert"`
CaCert string `flag:"ca-cert" description:"Path to the CA Cert"`
CaKey string `flag:"ca-key" description:"Path to the CA Key"`
CaKeySize int `flag:"ca-key-size" description:"CA Key Size"`
}
func main() {
conf := MiniCA{
Type: "auto",
Org: "Example Organization",
KeySize: 2048,
CaCert: "cacert.crt",
CaKey: "cakey.key",
CaKeySize: 2048,
}
flags, err := config.LoadFlags("minica", &conf)
if err != nil {
panic(err)
}
flags.Parse(os.Args[1:])
if len(flags.Args()) == 0 {
flags.Usage()
return
}
if Missing(conf.CaCert, conf.CaKey) {
if err := GenerateCACertificate(
conf.CaCert, conf.CaKey,
conf.Org, conf.CaCommonName,
conf.CaKeySize,
); err != nil {
panic(err)
}
}
var isClientCert bool
for _, cn := range flags.Args() {
switch conf.Type {
case "client":
isClientCert = true
case "server":
isClientCert = false
case "auto":
isClientCert = strings.Contains(cn, "@")
default:
fmt.Printf(`Unknown cert type!
Currently supported types:
client
server
auto (guess based on @ in CommonName)
`)
flags.Usage()
os.Exit(2)
}
newCrt := fmt.Sprintf("%s.crt", cn)
newKey := fmt.Sprintf("%s.key", cn)
fmt.Printf(`Creating a %s cert:
Client Cert: %t
Common Name: %s
Org: %s
Cert Flavor: %s
Output crt: %s
Output key: %s
`, conf.Type, isClientCert, cn, conf.Org, conf.Type, newCrt, newKey)
if err := GenerateCert(
[]string{cn},
newCrt, newKey, conf.CaCert, conf.CaKey,
conf.Org, cn,
conf.KeySize,
isClientCert,
); err != nil {
panic(err)
}
}
}
// vim: foldmethod=marker