-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathroot.go
123 lines (105 loc) · 3.43 KB
/
root.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
package cli
import (
"fmt"
"os"
"sort"
"strings"
"github.com/cloudflare/certmgr/cert"
"github.com/cloudflare/certmgr/metrics"
"github.com/cloudflare/certmgr/mgr"
"github.com/cloudflare/certmgr/svcmgr"
"github.com/cloudflare/cfssl/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var logLevel string
var debug, sync bool
var manager struct {
Dir string
ServiceManager string
Before string
}
func newManager() (*mgr.Manager, error) {
return mgr.New(
viper.GetString("dir"),
viper.GetString("default_remote"),
viper.GetString("svcmgr"),
viper.GetString("before"),
viper.GetString("interval"),
)
}
func root(cmd *cobra.Command, args []string) {
mgr, err := newManager()
if err != nil {
log.Fatalf("certmgr: %s", err)
}
err = mgr.Load()
if err != nil {
log.Fatalf("certmgr: %s", err)
}
// bit of a hack- metrics should instead see the mgr
// so changes in certs count are properly reflected.
certs := []*cert.Spec{}
for _, x := range mgr.Certs {
certs = append(certs, x.Spec)
}
metrics.Start(
viper.GetString("metrics_address"),
viper.GetString("metrics_port"),
viper.GetString("index_extra_html"),
certs,
)
mgr.Server(sync)
}
var RootCmd = &cobra.Command{
Use: "certmgr",
Short: "Manage TLS certificates for multiple services",
Long: ``,
Run: root,
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file (default is /etc/certmgr/certmgr.yaml)")
RootCmd.PersistentFlags().StringVarP(&manager.Dir, "dir", "d", "", "either the directory containing certificate specs, or the path to the spec file you wish to operate on")
backends := []string{}
for backend := range svcmgr.SupportedBackends {
backends = append(backends, backend)
}
sort.Strings(backends)
RootCmd.PersistentFlags().StringVarP(&manager.ServiceManager, "svcmgr", "m", "", fmt.Sprintf("service manager, must be one of: %s", strings.Join(backends, ", ")))
RootCmd.PersistentFlags().StringVarP(&manager.Before, "before", "t", "", "how long before certificates expire to start renewing (in the form Nh)")
RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug mode")
RootCmd.Flags().BoolVarP(&sync, "sync", "s", false, "the first certificate check should be synchronous")
RootCmd.Flags().MarkHidden("sync")
viper.BindPFlag("dir", RootCmd.PersistentFlags().Lookup("dir"))
viper.BindPFlag("svcmgr", RootCmd.PersistentFlags().Lookup("svcmgr"))
viper.BindPFlag("before", RootCmd.PersistentFlags().Lookup("before"))
viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug"))
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName("certmgr") // name of config file (without extension)
viper.AddConfigPath("/etc/certmgr") // adding home directory as first search path
}
viper.SetEnvPrefix("CERTMGR")
viper.AutomaticEnv() // read in environment variables that match
viper.SetDefault("index_extra_html", "")
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Info("certmgr: loading from config file ", viper.ConfigFileUsed())
}
if debug {
log.Level = log.LevelDebug
log.Debug("debug mode enabled")
}
}