Skip to content

Commit

Permalink
Merge pull request #6 from utilitywarehouse/refresh-no-signal
Browse files Browse the repository at this point in the history
cmd/refresh: make process optional
  • Loading branch information
Nicholas Jones authored Jan 5, 2021
2 parents 15de6bc + 04e7b61 commit e01e547
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
71 changes: 43 additions & 28 deletions cmd/refresh/refresher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,27 @@ type refresher struct {
extraTime time.Duration // Duration to be subtracted from the actual certificate expiration time
certExpTime time.Time // Actual certificate expiry time minus `extraTime`

maxSleepTime time.Duration // Maximum length of random sleep before sending signal
signal os.Signal
targetCommandName string
process *signalProcess
}

type signalProcess struct {
maxSleepTime time.Duration // Maximum length of random sleep before sending signal
signal os.Signal
commandName string
}

// NewRefresher returns a new instance of a Refresher or an error
// if an instance can't be created from provided context
func NewRefresher(c *cli.Context) (Refresher, error) {
conf := &refresher{
maxAttempts: c.Int("max-attempts"),

extraTime: c.Duration("extra-time"),
maxSleepTime: c.Duration("random-sleep"),
targetCommandName: c.String("target-proc-command"),
extraTime: c.Duration("extra-time"),
}

if conf.maxAttempts <= 0 {
return nil, errors.New(`"max-attempts" must be strictly larger than 0`)
}

if conf.maxSleepTime > conf.extraTime {
return nil, errMaxSleepTimeTooBig
}

switch sig := c.String("signal"); sig {
case "SIGHUP":
conf.signal = syscall.SIGHUP
case "SIGTERM":
conf.signal = syscall.SIGTERM
case "SIGINT":
conf.signal = syscall.SIGINT
default:
return nil, errors.Errorf(`"%s" is not an allowed signal`, sig)
}

cert, err := loadLocalCert(c)
if err != nil {
return nil, err
Expand All @@ -73,8 +59,32 @@ func NewRefresher(c *cli.Context) (Refresher, error) {
return nil, err
}

if _, err = getTargetProcess(conf.targetCommandName); err != nil {
return nil, err
if cmd := c.String("target-proc-command"); cmd != "" {
target := &signalProcess{
maxSleepTime: c.Duration("random-sleep"),
commandName: c.String("target-proc-command"),
}

if target.maxSleepTime > conf.extraTime {
return nil, errMaxSleepTimeTooBig
}

switch sig := c.String("signal"); sig {
case "SIGHUP":
target.signal = syscall.SIGHUP
case "SIGTERM":
target.signal = syscall.SIGTERM
case "SIGINT":
target.signal = syscall.SIGINT
default:
return nil, errors.Errorf(`"%s" is not an allowed signal`, sig)
}

if _, err = getTargetProcess(target.commandName); err != nil {
return nil, err
}

conf.process = target
}

return conf, nil
Expand Down Expand Up @@ -115,18 +125,23 @@ func (r *refresher) fetchCerts(c *cli.Context) error {
}

func (r *refresher) sendSignal() error {
if r.process == nil {
log.Info("no process to signal, skipping")
return nil
}

// Send signal to target process after random sleep
if r.maxSleepTime != 0 {
sleepTime := rand.Int63n(int64(r.maxSleepTime))
if r.process.maxSleepTime != 0 {
sleepTime := rand.Int63n(int64(r.process.maxSleepTime))
time.Sleep(time.Duration(sleepTime))
}

process, err := getTargetProcess(r.targetCommandName)
process, err := getTargetProcess(r.process.commandName)
if err != nil {
return err
}

if err := process.Signal(r.signal); err != nil {
if err := process.Signal(r.process.signal); err != nil {
return errors.Wrap(err, "failed sending signal")
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/utilitywarehouse/docker-cockroach-cfssl-certs

go 1.15

require (
github.com/cloudflare/cfssl v0.0.0-20180829183926-33259b0d2131
github.com/go-sql-driver/mysql v1.4.0 // indirect
Expand Down

0 comments on commit e01e547

Please sign in to comment.