Skip to content

Commit

Permalink
Merge pull request #141 from coveooss/feature/sentry-logger
Browse files Browse the repository at this point in the history
Log exceptions to Sentry if configured properly
  • Loading branch information
dblanchette committed Nov 15, 2021
2 parents 469c9a1 + a791a40 commit a427b55
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 36 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Valid levels are `debug`, `info`, `warning` and `error`.

![example](https://raw.githubusercontent.com/coveooss/credentials-sync/main/example.png)

## Monitoring with Sentry

To send errors to Sentry, set the following environment variables:
- `SENTRY_DSN`
- `SENTRY_RELEASE`
- `SENTRY_ENVIRONMENT`

## Configuration file

A configuration file must be given to the application. Its path can either be a local path or a S3 path
Expand Down
5 changes: 3 additions & 2 deletions cli/credentialslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package cli
import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/coveooss/credentials-sync/logger"

"github.com/spf13/cobra"
)

Expand All @@ -16,7 +17,7 @@ var listCredentialsCmd = &cobra.Command{
Short: "Resolves and lists all configured credentials",
Run: func(cmd *cobra.Command, args []string) {
if err := configuration.Sources.ValidateConfiguration(); err != nil {
log.Fatalf("The sources section of the config file is invalid: %v", err)
logger.Log.Fatalf("The sources section of the config file is invalid: %v", err)
}
allCredentials, err := configuration.Sources.Credentials()
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"

"github.com/coveooss/credentials-sync/credentials"
"github.com/coveooss/credentials-sync/logger"
"github.com/coveooss/credentials-sync/sync"
"github.com/coveooss/credentials-sync/targets"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -41,11 +42,11 @@ var rootCmd = &cobra.Command{
fileContent []byte
)

level, err := log.ParseLevel(viper.GetString("log-level"))
level, err := logrus.ParseLevel(viper.GetString("log-level"))
if err != nil {
return fmt.Errorf("Invalid log level: %s", err)
}
log.SetLevel(level)
logger.Log.SetLevel(level)

if configurationFile == "" {
return fmt.Errorf("A configuration file must be defined")
Expand Down Expand Up @@ -109,15 +110,15 @@ var rootCmd = &cobra.Command{
}

func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
logger.Log.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})

viper.AutomaticEnv()
viper.SetEnvPrefix("sync")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
rootCmd.PersistentFlags().StringP("config", "c", "", "configuration file")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))

rootCmd.PersistentFlags().StringP("log-level", "l", log.InfoLevel.String(), `"debug", "info", "warning" or "error"`)
rootCmd.PersistentFlags().StringP("log-level", "l", logrus.InfoLevel.String(), `"debug", "info", "warning" or "error"`)
viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))

initListCredentials()
Expand All @@ -129,6 +130,7 @@ func Execute(commit string, date string, version string) {
rootCmd.Version = fmt.Sprintf("%s %s (%s)", version, commit, date)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
logger.Log.Error(err)
os.Exit(1)
}
}
8 changes: 4 additions & 4 deletions cli/sync.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cli

import (
log "github.com/sirupsen/logrus"
"github.com/coveooss/credentials-sync/logger"
"github.com/spf13/cobra"
)

Expand All @@ -10,13 +10,13 @@ var syncCmd = &cobra.Command{
Short: "Fetches credentials and syncs them to targets",
Run: func(cmd *cobra.Command, args []string) {
if err := configuration.Sources.ValidateConfiguration(); err != nil {
log.Fatalf("The sources section of the config file is invalid: %v", err)
logger.Log.Fatalf("The sources section of the config file is invalid: %v", err)
}
if err := configuration.Targets.ValidateConfiguration(); err != nil {
log.Fatalf("The targets section of the config file is invalid: %v", err)
logger.Log.Fatalf("The targets section of the config file is invalid: %v", err)
}
if err := configuration.Sync(); err != nil {
log.Fatalf("The synchronization process failed: %v", err)
logger.Log.Fatalf("The synchronization process failed: %v", err)
}
},
}
4 changes: 2 additions & 2 deletions cli/targetslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cli

import (
"fmt"
"github.com/coveooss/credentials-sync/logger"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -12,7 +12,7 @@ var listTargetsCmd = &cobra.Command{
Short: "Resolves and lists all configured targets",
Run: func(cmd *cobra.Command, args []string) {
if err := configuration.Targets.ValidateConfiguration(); err != nil {
log.Fatalf("The targets section of the config file is invalid: %v", err)
logger.Log.Fatalf("The targets section of the config file is invalid: %v", err)
}
for _, target := range configuration.Targets.AllTargets() {
fmt.Println(target.ToString())
Expand Down
9 changes: 4 additions & 5 deletions cli/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cli

import (
log "github.com/sirupsen/logrus"

"github.com/coveooss/credentials-sync/logger"
"github.com/spf13/cobra"
)

Expand All @@ -11,11 +10,11 @@ var validateCmd = &cobra.Command{
Short: "Parses and validates the given configuration",
Run: func(cmd *cobra.Command, args []string) {
if err := configuration.Sources.ValidateConfiguration(); err != nil {
log.Fatalf("The sources section of the config file is invalid: %v", err)
logger.Log.Fatalf("The sources section of the config file is invalid: %v", err)
}
if err := configuration.Targets.ValidateConfiguration(); err != nil {
log.Fatalf("The targets section of the config file is invalid: %v", err)
logger.Log.Fatalf("The targets section of the config file is invalid: %v", err)
}
log.Info("The config file is valid!")
logger.Log.Info("The config file is valid!")
},
}
4 changes: 2 additions & 2 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package credentials
import (
"fmt"

"github.com/coveooss/credentials-sync/logger"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
)

// Credentials defines the methods that can be called by all types of credentials
Expand Down Expand Up @@ -106,7 +106,7 @@ func (credBase *Base) ShouldSync(targetName string, targetTags map[string]string
return true
}
} else {
log.Warningf("%s ignored. Its value should either be a string or a list of string", key)
logger.Log.Warningf("%s ignored. Its value should either be a string or a list of string", key)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions credentials/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"sort"

"github.com/coveooss/credentials-sync/logger"
"github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -110,10 +110,10 @@ func getCredentialsFromBytes(byteArray []byte) ([]Credentials, error) {
}

if !success {
log.Warning("Failed to get credential from data using all known formats (details below)")
logger.Log.Warning("Failed to get credential from data using all known formats (details below)")
for _, err := range errors {
if err != nil {
log.Warning(err)
logger.Log.Warning(err)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ go 1.12
require (
github.com/aws/aws-sdk-go v1.36.28
github.com/bndr/gojenkins v1.0.1
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/evalphobia/logrus_sentry v0.8.2
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/getsentry/sentry-go v0.11.0
github.com/golang/mock v1.4.4
github.com/hashicorp/go-multierror v1.1.0
github.com/mitchellh/mapstructure v1.4.1
Expand Down
Loading

0 comments on commit a427b55

Please sign in to comment.