Skip to content

Commit

Permalink
fix: check for error
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Jul 10, 2023
1 parent bf2f82d commit a075efc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
25 changes: 16 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
"errors"
"flag"
"fmt"
"os"
"os/signal"
"os/user"
"path"
"strings"
"syscall"
"time"

"github.com/rs/zerolog/log"
"github.com/soerenschneider/acmevault/internal"
"github.com/soerenschneider/acmevault/internal/config"
Expand All @@ -12,13 +20,6 @@ import (
"github.com/soerenschneider/acmevault/internal/server/acme"
"github.com/soerenschneider/acmevault/pkg/certstorage"
"github.com/soerenschneider/acmevault/pkg/certstorage/vault"
"os"
"os/signal"
"os/user"
"path"
"strings"
"syscall"
"time"
)

func main() {
Expand Down Expand Up @@ -120,11 +121,17 @@ func Run(acmeVault *server.AcmeVaultServer, storage certstorage.CertStorage, con
syscall.SIGTERM,
syscall.SIGQUIT)

acmeVault.CheckCerts()
err = acmeVault.CheckCerts()
if err != nil {
log.Error().Err(err).Msg("error checking certs")
}
for {
select {
case <-ticker.C:
acmeVault.CheckCerts()
err = acmeVault.CheckCerts()
if err != nil {
log.Error().Err(err).Msg("error checking certs")
}
case <-done:
log.Info().Msg("Received signal, quitting")
storage.Logout()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
github.com/prometheus/common v0.44.0
github.com/rs/zerolog v1.29.1
github.com/stretchr/testify v1.8.4
go.uber.org/multierr v1.11.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,8 @@ go.opentelemetry.io/proto/otlp v0.15.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/ratelimit v0.2.0/go.mod h1:YYBV4e4naJvhpitQrWJu1vCpgB7CboMe0qhltKt6mUg=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
6 changes: 5 additions & 1 deletion internal/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package config
import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"net/url"
"os"
"strings"

"github.com/rs/zerolog/log"
)

type VaultConfig struct {
Expand Down Expand Up @@ -119,6 +120,9 @@ func (conf *VaultConfig) Validate() error {

func isFileWritable(fileName string) bool {
file, err := os.OpenFile(fileName, os.O_WRONLY, 0600)
if err != nil {
return false
}
defer file.Close()
if err != nil {
if os.IsPermission(err) {
Expand Down
16 changes: 10 additions & 6 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package server
import (
"errors"
"fmt"

"github.com/rs/zerolog/log"
"github.com/soerenschneider/acmevault/internal/config"
"github.com/soerenschneider/acmevault/internal/metrics"
"github.com/soerenschneider/acmevault/internal/server/acme"
"github.com/soerenschneider/acmevault/pkg/certstorage"
"go.uber.org/multierr"
)

type AcmeVaultServer struct {
Expand Down Expand Up @@ -36,16 +38,18 @@ func NewAcmeVaultServer(domains []config.AcmeServerDomains, acmeClient acme.Acme
}, nil
}

func (c *AcmeVaultServer) CheckCerts() {
c.certStorage.Authenticate()
func (c *AcmeVaultServer) CheckCerts() error {
err := c.certStorage.Authenticate()
if err != nil {
return err
}

metrics.ServerLatestIterationTimestamp.SetToCurrentTime()
for _, domain := range c.domains {
err := c.obtainAndHandleCert(domain)
if err != nil {
log.Error().Msgf("error while handling received certificate: %v", err)
}
err = multierr.Append(err, c.obtainAndHandleCert(domain))
}
c.certStorage.Logout()
return err
}

func (c *AcmeVaultServer) obtainAndHandleCert(domain config.AcmeServerDomains) error {
Expand Down

0 comments on commit a075efc

Please sign in to comment.