forked from mastercactapus/letsencrypt-getcert
/
main.go
138 lines (126 loc) · 4.28 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"github.com/denkhaus/lecert/api"
"github.com/denkhaus/lecert/client"
"github.com/denkhaus/lecert/config"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
mainCmd = &cobra.Command{
Use: "lecert",
Short: "Simple utility for generating signed TLS certificates.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
v, _ := cmd.Flags().GetBool("verbose")
if v {
log.SetLevel(log.DebugLevel)
log.Debugln("verbose mode enabled")
}
},
}
signCmd = &cobra.Command{
Use: "sign <CSR files...>",
Short: "Fulfill existing CSR(s).",
Run: runSign,
}
verifyCmd = &cobra.Command{
Use: "verify <domains...>",
Short: "Verify existing certificate(s).",
Run: runVerify,
}
genCmd = &cobra.Command{
Use: "generate <domains...>",
Short: "Generate and sign new certificate(s).",
Run: runGen,
}
renewCmd = &cobra.Command{
Use: "renew <domains...>",
Short: "Renew existing certificate(s).",
Run: runRenew,
}
ensureCmd = &cobra.Command{
Use: "ensure <domains...>",
Short: "Create non existing certificate(s) or renew if necessary.",
Run: runEnsure,
}
)
func init() {
cobra.EnablePrefixMatching = true
mainCmd.PersistentFlags().Bool("standalone", false, "Standalone mode provides a buildin webserver for challenge handling listening on specified bindaddress.")
mainCmd.PersistentFlags().Bool("webroot", false, "Webroot mode uses the provided root-path for challenge handling.")
mainCmd.PersistentFlags().StringP("root-path", "r", "", "Path used by webroot mode.")
mainCmd.PersistentFlags().StringP("bind", "b", ":80", "Bind address. The binding address:port for the server. Note, port 80 on the domain(s) must be mapped to this address.")
mainCmd.PersistentFlags().StringP("acme-url", "u", client.AcmeURL, "ACME URL. URL to the ACME directory to use.")
mainCmd.PersistentFlags().StringP("output-dir", "d", ".", "Output directory. Certificates and keys will be stored here.")
mainCmd.PersistentFlags().StringP("account-key", "k", "acme.key", "ACME account key (PEM format). The account key to use with this CA. If it doesn't exist, one will be generated.")
mainCmd.PersistentFlags().Int("bits", 4096, "Bits for RSA key generation.")
mainCmd.PersistentFlags().Bool("chain", false, "Include full chain. If set, download and include all LE certificates in the chain.")
mainCmd.PersistentFlags().BoolP("verbose", "v", false, "Verbose mode. Logs extra messages for debugging.")
}
func createApi(cmd *cobra.Command) *api.Api {
log.Debug("create api")
c, err := config.NewFromCli(cmd)
if err != nil {
log.Fatalln(errors.Annotate(err, "new config"))
}
api, err := api.New(c)
if err != nil {
log.Fatalln(errors.Annotate(err, "new api"))
}
return api
}
func runRenew(cmd *cobra.Command, args []string) {
api := createApi(cmd)
log.Debug("process renew")
for _, domain := range args {
l := log.WithField("domain", domain)
if err := api.RenewCertificate(domain); err != nil {
l.Error(errors.Annotate(err, "renew certificate"))
}
}
}
func runEnsure(cmd *cobra.Command, args []string) {
api := createApi(cmd)
log.Debug("process ensure")
for _, domain := range args {
l := log.WithField("domain", domain)
if err := api.EnsureCertificate(domain); err != nil {
l.Error(errors.Annotate(err, "ensure certificate"))
}
}
}
func runGen(cmd *cobra.Command, args []string) {
api := createApi(cmd)
log.Debug("process generate")
for _, domain := range args {
l := log.WithField("domain", domain)
if err := api.GenerateCertificate(domain); err != nil {
l.Error(errors.Annotate(err, "renew certificate"))
}
}
}
func runVerify(cmd *cobra.Command, args []string) {
api := createApi(cmd)
log.Debug("process verify")
for _, domain := range args {
l := log.WithField("domain", domain)
if _, err := api.VerifyCertificate(domain); err != nil {
l.Error(errors.Annotate(err, "renew certificate"))
}
}
}
func runSign(cmd *cobra.Command, args []string) {
api := createApi(cmd)
log.Debug("process sign")
for _, domain := range args {
l := log.WithField("domain", domain)
if err := api.GenerateCertificate(domain); err != nil {
l.Error(errors.Annotate(err, "renew certificate"))
}
}
}
func main() {
mainCmd.AddCommand(genCmd, signCmd, verifyCmd, renewCmd, ensureCmd)
mainCmd.Execute()
}