2015-09-18 05:14:15 -04:00
|
|
|
/*
|
|
|
|
Copyright 2015 Home Office All rights reserved.
|
|
|
|
|
|
|
|
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 (
|
2017-05-24 07:41:16 -04:00
|
|
|
"fmt"
|
2015-09-18 05:14:15 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-06-21 13:33:49 -04:00
|
|
|
"sync"
|
2015-09-18 05:14:15 -04:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2017-05-24 07:41:16 -04:00
|
|
|
var (
|
|
|
|
prog = "vault-sidekick"
|
2017-09-19 05:08:24 -04:00
|
|
|
release = "v0.3.4"
|
2017-05-24 07:41:16 -04:00
|
|
|
gitsha = ""
|
2015-09-23 17:39:50 -04:00
|
|
|
)
|
|
|
|
|
2015-09-18 05:14:15 -04:00
|
|
|
func main() {
|
2017-05-24 07:41:16 -04:00
|
|
|
version := fmt.Sprintf("%s (git+sha %s)", release, gitsha)
|
2015-09-18 05:14:15 -04:00
|
|
|
// step: parse and validate the command line / environment options
|
2015-10-15 12:28:12 -04:00
|
|
|
if err := parseOptions(); err != nil {
|
2015-09-18 05:14:15 -04:00
|
|
|
showUsage("invalid options, %s", err)
|
|
|
|
}
|
2017-05-24 07:41:16 -04:00
|
|
|
if options.showVersion {
|
|
|
|
fmt.Printf("%s %s\n", prog, version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
glog.Infof("starting the %s, %s", prog, version)
|
2015-09-23 17:39:50 -04:00
|
|
|
|
2017-06-21 13:33:49 -04:00
|
|
|
if options.oneShot {
|
|
|
|
glog.Infof("running in one-shot mode")
|
|
|
|
}
|
|
|
|
|
2015-09-18 05:14:15 -04:00
|
|
|
// step: create a client to vault
|
2015-10-15 12:28:12 -04:00
|
|
|
vault, err := NewVaultService(options.vaultURL)
|
|
|
|
if err != nil {
|
2015-09-21 06:31:12 -04:00
|
|
|
showUsage("unable to create the vault client: %s", err)
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
2015-10-15 12:28:12 -04:00
|
|
|
// step: create a channel to receive events upon and add our resources for renewal
|
|
|
|
updates := make(chan VaultEvent, 10)
|
|
|
|
vault.AddListener(updates)
|
2015-09-18 05:14:15 -04:00
|
|
|
|
|
|
|
// step: setup the termination signals
|
|
|
|
signalChannel := make(chan os.Signal)
|
|
|
|
signal.Notify(signalChannel, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
|
2015-09-21 11:35:59 -04:00
|
|
|
// step: add each of the resources to the service processor
|
2015-09-18 05:14:15 -04:00
|
|
|
for _, rn := range options.resources.items {
|
2015-09-23 17:39:50 -04:00
|
|
|
if err := rn.IsValid(); err != nil {
|
2015-09-18 05:14:15 -04:00
|
|
|
showUsage("%s", err)
|
|
|
|
}
|
2015-09-23 17:39:50 -04:00
|
|
|
vault.Watch(rn)
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 13:33:49 -04:00
|
|
|
toProcess := options.resources.items
|
|
|
|
toProcessLock := &sync.Mutex{}
|
2017-07-17 11:34:25 -04:00
|
|
|
failedResource := false
|
2017-06-21 14:36:13 -04:00
|
|
|
if options.oneShot && len(toProcess) == 0 {
|
|
|
|
glog.Infof("nothing to retrieve from vault. exiting...")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-09-18 05:14:15 -04:00
|
|
|
// step: we simply wait for events i.e. secrets from vault and write them to the output directory
|
|
|
|
for {
|
|
|
|
select {
|
2015-09-23 17:39:50 -04:00
|
|
|
case evt := <-updates:
|
2015-10-15 12:28:12 -04:00
|
|
|
glog.V(10).Infof("recieved an update from the resource: %s", evt.Resource)
|
2016-03-16 08:55:16 -04:00
|
|
|
go func(r VaultEvent) {
|
2017-07-17 11:34:25 -04:00
|
|
|
toProcessLock.Lock()
|
|
|
|
defer toProcessLock.Unlock()
|
2017-07-17 19:41:11 -04:00
|
|
|
switch r.Type {
|
|
|
|
case EventTypeSuccess:
|
|
|
|
if err := processResource(evt.Resource, evt.Secret); err != nil {
|
|
|
|
glog.Errorf("failed to write out the update, error: %s", err)
|
|
|
|
}
|
|
|
|
if options.oneShot {
|
|
|
|
for i, r := range toProcess {
|
|
|
|
if evt.Resource == r {
|
|
|
|
toProcess = append(toProcess[:i], toProcess[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case EventTypeFailure:
|
|
|
|
if evt.Resource.maxRetries > 0 && evt.Resource.maxRetries < evt.Resource.retries {
|
|
|
|
for i, r := range toProcess {
|
|
|
|
if evt.Resource == r {
|
2017-07-17 11:34:25 -04:00
|
|
|
toProcess = append(toProcess[:i], toProcess[i+1:]...)
|
2017-07-17 19:41:11 -04:00
|
|
|
failedResource = true
|
2017-07-17 11:34:25 -04:00
|
|
|
}
|
2017-06-21 13:33:49 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 11:34:25 -04:00
|
|
|
}
|
|
|
|
if len(toProcess) == 0 {
|
|
|
|
glog.Infof("no resources left to process. exiting...")
|
|
|
|
if failedResource {
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
2017-06-21 13:33:49 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 08:55:16 -04:00
|
|
|
}(evt)
|
2015-09-18 05:14:15 -04:00
|
|
|
case <-signalChannel:
|
|
|
|
glog.Infof("recieved a termination signal, shutting down the service")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 12:58:52 -04:00
|
|
|
}
|