-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
252 lines (220 loc) · 5.97 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright © 2023 Joshua Rich <joshua.rich@gmail.com>
*/
package cmd
import (
"io"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/joshuar/post-step-ca-renewal/internal/actions"
"github.com/joshuar/post-step-ca-renewal/internal/certs"
"github.com/joshuar/post-step-ca-renewal/internal/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultConfigFile = "/etc/post-step-ca-renewal/config.yml"
configFile string
debugFlag bool
profileFlag bool
)
var rootCmd = &cobra.Command{
Use: "post-step-ca-renewal",
Short: "Copy certs elsewhere when step-ca updates them.",
Long: `A daemon that watches for cert renewals from step-ca and then copies them elsewhere for use by other services.`,
PreRun: func(cmd *cobra.Command, args []string) {
if debugFlag {
log.SetLevel(log.DebugLevel)
log.Debug("debug logging enabled")
}
if profileFlag {
go func() {
log.Info(http.ListenAndServe("localhost:6060", nil))
}()
log.Info("profiling is enabled and available at localhost:6060")
}
},
Run: func(cmd *cobra.Command, args []string) {
certConfig := config.GetCertDetails()
allActions := config.GetActions()
watchCerts(allActions, certConfig)
},
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().StringVar(&configFile, "config", defaultConfigFile, "config file")
rootCmd.Flags().BoolVarP(&debugFlag, "debug", "d", false, "debug output (default is false)")
rootCmd.Flags().BoolVarP(&profileFlag, "profile", "p", false, "enable profiling (default is false)")
cobra.OnInitialize(initConfig)
}
func initConfig() {
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
viper.AddConfigPath(home + ".post-step-ca-renewal")
viper.AddConfigPath("/etc/post-step-ca-renewal/")
viper.AddConfigPath(".")
viper.SetConfigName("config")
}
if err := viper.ReadInConfig(); err == nil {
log.Infof("using config file: %s", viper.ConfigFileUsed())
} else {
log.Fatalf("error reading config file %s: %v", viper.ConfigFileUsed(), err)
}
}
func performActions(changed string, certConfig *certs.CertConfig, allActions *actions.AllActions) {
doWork := func(done <-chan interface{}, action *actions.Action, wg *sync.WaitGroup, result chan<- string) {
started := time.Now()
defer wg.Done()
if action.PreCommand != nil {
for _, cmd := range action.PreCommand {
log.Debugf("%s: running pre-command %s", action.Name, cmd)
run(cmd)
}
}
// switch filepath.Base(changed) {
// case filepath.Base(certConfig.Cert):
log.Debugf("%s - copying cert %s to %s", action.Name, certConfig.Cert, action.Cert)
_, err := copy(certConfig.Cert, action.Cert)
if err != nil {
log.Errorf("%s: failed to copy, %v", action.Name, err)
}
// case filepath.Base(certConfig.Key):
log.Debugf("%s: copying key %s to %s", action.Name, certConfig.Key, action.Key)
_, err = copy(certConfig.Key, action.Key)
if err != nil {
log.Errorf("%s: failed to copy, %v", action.Name, err)
}
// }
if action.FullChain != "" {
log.Debugf("%s: creating fullchain file %s", action.Name, action.FullChain)
_, err := concat(action.FullChain, certConfig.Cert, certConfig.CA)
if err != nil {
log.Errorf("%s: failed to create fullchain, %v", action.Name, err)
}
}
if action.PostCommand != nil {
for _, cmd := range action.PostCommand {
log.Debugf("%s: running post-command %s", action.Name, cmd)
run(cmd)
}
}
select {
case <-done:
case result <- action.Name:
}
took := time.Since(started)
log.Debugf("action %s took %v\n", action.Name, took)
}
done := make(chan interface{})
result := make(chan string)
var wg sync.WaitGroup
wg.Add(allActions.Count())
for i := 0; i < allActions.Count(); i++ {
go doWork(done, allActions.GetActionByIndex(i), &wg, result)
}
close(done)
wg.Wait()
}
func copy(src, dst string) (int64, error) {
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func concat(dst string, srcs ...string) (int64, error) {
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
var totalBytes int64
for _, s := range srcs {
source, err := os.Open(s)
if err != nil {
return 0, err
}
defer source.Close()
nBytes, err := io.Copy(destination, source)
if err != nil {
return nBytes, err
} else {
totalBytes += nBytes
}
}
return totalBytes, err
}
func run(command string) {
cmdArray := strings.Split(command, " ")
cmd := exec.Command(cmdArray[0], cmdArray[1:]...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("error running command %v: %s", err, string(out))
} else {
log.Debugf("%s\n", string(out))
}
}
func watchCerts(allActions *actions.AllActions, certs *certs.CertConfig) {
w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("creating a new watcher: %s", err)
}
defer w.Close()
go fileLoop(w, certs, allActions)
err = w.Add(certs.Cert)
if err != nil {
log.Errorf("%q: %s", certs.Cert, err)
} else {
log.Debugf("added watch for %s", certs.Cert)
}
// for _, p := range []string{certs.Cert, certs.Key} {
// err = w.Add(p)
// if err != nil {
// log.Errorf("%q: %s", p, err)
// } else {
// log.Debugf("added watch for %s", p)
// }
// }
<-make(chan struct{}) // Block forever
}
func fileLoop(w *fsnotify.Watcher, certs *certs.CertConfig, allActions *actions.AllActions) {
for {
select {
case err, ok := <-w.Errors:
if !ok {
return
}
log.Errorf("problem watching file: %s", err)
case e, ok := <-w.Events:
if !ok {
return
}
switch {
case e.Op.Has(fsnotify.Write):
performActions(e.Name, certs, allActions)
}
log.Debugf("observed event %s", e)
}
}
}