Skip to content

Commit

Permalink
feat: refactoring from lint results
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Stanley <[email protected]>
  • Loading branch information
lrstanley committed Sep 11, 2023
1 parent 2cf2daf commit 4a7b643
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
11 changes: 7 additions & 4 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/apex/log"
"github.com/hashicorp/vault/api"
)

func worker(ctx context.Context, wg *sync.WaitGroup, addr string) {
Expand Down Expand Up @@ -45,9 +46,9 @@ func worker(ctx context.Context, wg *sync.WaitGroup, addr string) {
status, err := client.Sys().SealStatus()
if err != nil {
errCount++
nerr := fmt.Errorf("checking seal status: %v", err)
nerr := fmt.Errorf("checking seal status: %w", err)

if err, ok := err.(net.Error); ok && err.Timeout() {
if err, ok := err.(net.Error); ok && err.Timeout() { //nolint:errorlint
// It's a network timeout. If it's the first network timeout,
// don't notify, just try again. This should help with network
// blips.
Expand Down Expand Up @@ -81,9 +82,11 @@ func worker(ctx context.Context, wg *sync.WaitGroup, addr string) {
"progress": status.Progress,
"total": status.T,
}).Info("using unseal token")
resp, err := client.Sys().Unseal(token)

var resp *api.SealStatusResponse
resp, err = client.Sys().Unseal(token)
if err != nil {
notify(fmt.Errorf("using unseal key %d on %v: %v", i+1, addr, err))
notify(fmt.Errorf("using unseal key %d on %v: %w", i+1, addr, err))
errCount++
continue
}
Expand Down
39 changes: 24 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ import (
yaml "gopkg.in/yaml.v2"
)

var (
const (
version = "master"
commit = "latest"
date = "-"

defaultCheckInterval = 30 * time.Second
defaultTimeout = 15 * time.Second
configRefreshInterval = 15 * time.Second
minimumNodes = 3
)

// Config is a combo of the flags passed to the cli and the configuration file (if used).
Expand Down Expand Up @@ -76,9 +81,7 @@ type Config struct {
}

var (
conf = &Config{
CheckInterval: 30 * time.Second,
}
conf = &Config{CheckInterval: defaultCheckInterval}

logger log.Interface
)
Expand All @@ -89,7 +92,7 @@ func newVault(addr string) (vault *vapi.Client) {
vconfig := vapi.DefaultConfig()
vconfig.Address = addr
vconfig.MaxRetries = 0
vconfig.Timeout = 15 * time.Second
vconfig.Timeout = defaultTimeout

if err = vconfig.ConfigureTLS(&vapi.TLSConfig{Insecure: conf.TLSSkipVerify}); err != nil {
logger.WithError(err).Fatal("error initializing tls config")
Expand All @@ -105,7 +108,8 @@ func newVault(addr string) (vault *vapi.Client) {
func main() {
var err error
if _, err = flags.Parse(conf); err != nil {
if FlagErr, ok := err.(*flags.Error); ok && FlagErr.Type == flags.ErrHelp {
var ferr *flags.Error
if errors.As(err, &ferr) && ferr.Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
Expand All @@ -127,7 +131,8 @@ func main() {
logWriters := []io.Writer{}

if conf.Log.Path != "" {
logFileWriter, err := os.OpenFile(conf.Log.Path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
var logFileWriter *os.File
logFileWriter, err = os.OpenFile(conf.Log.Path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening log file %q: %v", conf.Log.Path, err)
os.Exit(1)
Expand Down Expand Up @@ -156,7 +161,8 @@ func main() {
"version": version,
})

if err := readConfig(conf.ConfigPath); err != nil {
err = readConfig(conf.ConfigPath)
if err != nil {
logger.WithError(err).Fatal("error reading config")
}

Expand All @@ -174,9 +180,10 @@ func main() {
if conf.ConfigPath != "" {
go func() {
for {
time.Sleep(15 * time.Second)
time.Sleep(configRefreshInterval)

if err := readConfig(conf.ConfigPath); err != nil {
err = readConfig(conf.ConfigPath)
if err != nil {
logger.WithError(err).Fatal("error reading config")
}
}
Expand Down Expand Up @@ -210,12 +217,14 @@ func readConfig(path string) error {
return nil
}

b, err := os.ReadFile(path)
var b []byte
b, err = os.ReadFile(path)
if err != nil {
return err
}

if err := yaml.Unmarshal(b, conf); err != nil {
err = yaml.Unmarshal(b, conf)
if err != nil {
return err
}
}
Expand All @@ -228,12 +237,12 @@ func readConfig(path string) error {
conf.MaxCheckInterval = conf.CheckInterval * time.Duration(2)
}

if len(conf.Nodes) < 3 {
if len(conf.Nodes) < minimumNodes {
if !conf.AllowSingleNode {
return errors.New("not enough nodes in node list (must have at least 3!)")
return fmt.Errorf("not enough nodes in node list (must have at least %d)", minimumNodes)
}

logger.Warn("running with less than 3 nodes, this is not recommended")
logger.Warnf("running with less than %d nodes, this is not recommended", minimumNodes)
}

if len(conf.Tokens) < 1 {
Expand Down
11 changes: 9 additions & 2 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ func sendQueue() {
var err error

smtp := mail.NewDialer(conf.Email.Hostname, conf.Email.Port, conf.Email.Username, conf.Email.Password)
smtp.TLSConfig = &tls.Config{InsecureSkipVerify: conf.Email.TLSSkipVerify, ServerName: conf.Email.Hostname}
smtp.TLSConfig = &tls.Config{
InsecureSkipVerify: conf.Email.TLSSkipVerify, //nolint:gosec
ServerName: conf.Email.Hostname,
}

if conf.Email.MandatoryTLS {
smtp.StartTLSPolicy = mail.MandatoryStartTLS
}

smtp.LocalName, err = os.Hostname()
if err != nil {
smtp.LocalName = "localhost"
}

s, err := smtp.Dial()
if err != nil {
logger.WithError(err).WithFields(log.Fields{
Expand All @@ -115,7 +121,8 @@ func sendQueue() {
msg.SetHeader("CC", conf.Email.SendAddrs[1:]...)
}

if err := mail.Send(s, msg); err != nil {
err = mail.Send(s, msg)
if err != nil {
logger.WithError(err).Error("unable to send notification")
return
}
Expand Down

0 comments on commit 4a7b643

Please sign in to comment.