Skip to content

Commit

Permalink
Use global logging (#92)
Browse files Browse the repository at this point in the history
* Use global logging

* Remove glog
  • Loading branch information
jaydhulia authored Sep 30, 2021
1 parent b00a109 commit 8d619aa
Show file tree
Hide file tree
Showing 33 changed files with 171 additions and 157 deletions.
10 changes: 6 additions & 4 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package cmd
import (
"os"

"github.com/netflix/weep/pkg/logging"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,7 +51,7 @@ MacOS:
`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
},
}
Expand All @@ -72,7 +74,7 @@ You will need to start a new shell for this setup to take effect.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
},
}
Expand All @@ -90,7 +92,7 @@ $ weep completion fish > ~/.config/fish/completions/weep.fish
`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
},
}
Expand Down Expand Up @@ -133,7 +135,7 @@ You will need to start a new shell for this setup to take effect.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Root().GenPowerShellCompletion(os.Stdout); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/credential_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"time"

"github.com/netflix/weep/pkg/logging"

"github.com/netflix/weep/pkg/aws"

"github.com/netflix/weep/pkg/creds"
Expand Down Expand Up @@ -129,7 +131,7 @@ func printCredentialProcess(credentials *aws.Credentials) {

b, err := json.Marshal(credentialProcessOutput)
if err != nil {
log.Error(err)
logging.Log.Error(err)
}
fmt.Printf(string(b))
}
3 changes: 2 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cmd

import (
"github.com/netflix/weep/pkg/logging"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
Expand All @@ -29,7 +30,7 @@ var docCommand = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
err := doc.GenMarkdownTree(rootCmd, "./docs/")
if err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
},
}
Expand Down
24 changes: 13 additions & 11 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strconv"
"time"

"github.com/netflix/weep/pkg/logging"

"github.com/netflix/weep/pkg/aws"

"github.com/netflix/weep/pkg/creds"
Expand Down Expand Up @@ -61,7 +63,7 @@ func runFile(cmd *cobra.Command, args []string) error {
return err
}
if autoRefresh {
log.Infof("starting automatic file refresh for %s", role)
logging.Log.Infof("starting automatic file refresh for %s", role)
go fileRefresher(role, profileName, destination, noIpRestrict, assumeRole)
<-shutdown
}
Expand All @@ -86,18 +88,18 @@ func fileRefresher(role, profile, filename string, noIpRestrict bool, assumeRole
for {
select {
case _ = <-ticker.C:
log.Debug("checking credentials")
logging.Log.Debug("checking credentials")
expiring, err := isExpiring(filename, profile, 10)
if err != nil {
log.Errorf("error checking credential expiration: %v", err)
logging.Log.Errorf("error checking credential expiration: %v", err)
}
if expiring {
log.Info("credentials are expiring soon, refreshing...")
logging.Log.Info("credentials are expiring soon, refreshing...")
err = updateCredentialsFile(role, profile, filename, noIpRestrict, assumeRole)
if err != nil {
log.Errorf("error updating credentials: %v", err)
logging.Log.Errorf("error updating credentials: %v", err)
} else {
log.Info("credentials refreshed!")
logging.Log.Info("credentials refreshed!")
}
}
}
Expand All @@ -107,15 +109,15 @@ func fileRefresher(role, profile, filename string, noIpRestrict bool, assumeRole
func getDefaultCredentialsFile() string {
home, err := homedir.Dir()
if err != nil {
log.Fatal("couldn't get default directory")
logging.Log.Fatal("couldn't get default directory")
}
return path.Join(home, ".aws", "credentials")
}

func getDefaultAwsConfigFile() string {
home, err := homedir.Dir()
if err != nil {
log.Fatal("couldn't get default directory")
logging.Log.Fatal("couldn't get default directory")
}
return path.Join(home, ".aws", "config")
}
Expand Down Expand Up @@ -151,12 +153,12 @@ func isExpiring(filename, profile string, thresholdMinutes int) (bool, error) {
expirationTime := time.Unix(expirationInt, 0)
diff := time.Duration(thresholdMinutes) * time.Minute
timeUntilExpiration := expirationTime.Sub(time.Now()).Round(0)
log.Debugf("%s until expiration, refresh threshold is %s", timeUntilExpiration, diff)
logging.Log.Debugf("%s until expiration, refresh threshold is %s", timeUntilExpiration, diff)
if timeUntilExpiration < diff {
log.Debug("will refresh")
logging.Log.Debug("will refresh")
return true, nil
}
log.Debug("will not refresh")
logging.Log.Debug("will not refresh")
return false, nil
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"strings"

"github.com/netflix/weep/pkg/logging"

"github.com/netflix/weep/pkg/metadata"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -39,7 +41,7 @@ var infoCmd = &cobra.Command{
func marshalStruct(obj interface{}) []byte {
out, err := yaml.Marshal(obj)
if err != nil {
log.Errorf("failed to marshal struct: %v", err)
logging.Log.Errorf("failed to marshal struct: %v", err)
return nil
}
return out
Expand All @@ -65,7 +67,7 @@ func PrintWeepInfo(w io.Writer) error {

roles, err := roleList()
if err != nil {
log.Errorf("failed to retrieve role list from ConsoleMe: %v", err)
logging.Log.Errorf("failed to retrieve role list from ConsoleMe: %v", err)
} else {
_, _ = writer.Write([]byte(roles))
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var (
metadata.SetWeepMethod(cmd.CalledAs())
},
}
log = logging.GetLogger()
)

func init() {
Expand All @@ -57,13 +56,13 @@ func init() {
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level (debug, info, warn)")
rootCmd.PersistentFlags().StringVarP(&region, "region", "r", viper.GetString("aws.region"), "AWS region")
if err := viper.BindPFlag("log_level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
if err := viper.BindPFlag("log_file", rootCmd.PersistentFlags().Lookup("log-file")); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
if err := viper.BindPFlag("log_format", rootCmd.PersistentFlags().Lookup("log-format")); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
}

Expand All @@ -86,14 +85,14 @@ func Execute() error {

func initConfig() {
if err := config.InitConfig(cfgFile); err != nil {
log.Fatalf("failed to initialize config: %v", err)
logging.Log.Fatalf("failed to initialize config: %v", err)
}
}

// updateLoggingConfig overrides the default logging settings based on the config and CLI args
func updateLoggingConfig() {
err := logging.UpdateConfig(logLevel, logFormat, logFile)
if err != nil {
log.Errorf("failed to configure logger: %v", err)
logging.Log.Errorf("failed to configure logger: %v", err)
}
}
5 changes: 3 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cmd

import (
"github.com/netflix/weep/pkg/logging"
"github.com/netflix/weep/pkg/server"
"github.com/spf13/viper"

Expand All @@ -27,10 +28,10 @@ func init() {
serveCmd.PersistentFlags().StringVarP(&listenAddr, "listen-address", "a", viper.GetString("server.address"), "IP address for the ECS credential provider to listen on")
serveCmd.PersistentFlags().IntVarP(&listenPort, "port", "p", viper.GetInt("server.port"), "port for the ECS credential provider service to listen on")
if err := viper.BindPFlag("server.address", serveCmd.PersistentFlags().Lookup("listen-address")); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
if err := viper.BindPFlag("server.port", serveCmd.PersistentFlags().Lookup("port")); err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}
rootCmd.AddCommand(serveCmd)
}
Expand Down
22 changes: 12 additions & 10 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"os"

"github.com/netflix/weep/pkg/logging"

"github.com/spf13/viper"

"github.com/kardianos/service"
Expand Down Expand Up @@ -44,7 +46,7 @@ func runWeepServiceControl(cmd *cobra.Command, args []string) error {
}
cmd.Printf("successfully ran service %s\n", args[0])
}
log.Debug("sending done signal")
logging.Log.Debug("sending done signal")
done <- 0
return nil
}
Expand All @@ -58,13 +60,13 @@ func (p *program) Start(s service.Service) error {

func (p *program) run() {
var err error
log.Info("starting weep service!")
logging.Log.Info("starting weep service!")
exitCode := 0

flags := viper.GetStringSlice("service.args")
err = rootCmd.ParseFlags(flags)
if err != nil {
log.Errorf("could not parse flags: %v", err)
logging.Log.Errorf("could not parse flags: %v", err)
}

args := viper.GetStringSlice("service.args")
Expand All @@ -74,25 +76,25 @@ func (p *program) run() {
case "serve":
err = runWeepServer(nil, args)
if err != nil {
log.Error(err)
logging.Log.Error(err)
exitCode = 1
}
default:
log.Error("unknown command: ", command)
logging.Log.Error("unknown command: ", command)
exitCode = 1
}
log.Debug("sending done signal")
logging.Log.Debug("sending done signal")
done <- exitCode
}

func (p *program) Stop(s service.Service) error {
// Send an interrupt to the shutdown channel so everything will clean itself up
// This is seemingly only necessary on Windows, but it shouldn't hurt anything on other platforms.
log.Debug("got service stop, sending interrupt")
logging.Log.Debug("got service stop, sending interrupt")
shutdown <- os.Interrupt

// Wait for whatever is running to signal that it's done
log.Debug("waiting for done signal")
logging.Log.Debug("waiting for done signal")
<-done
return nil
}
Expand All @@ -118,13 +120,13 @@ func initService() {

weepService, err = service.New(svcProgram, svcConfig)
if err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}

errs := make(chan error, 5)
svcLogger, err = weepService.Logger(errs)
if err != nil {
log.Fatal(err)
logging.Log.Fatal(err)
}

go func() {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/aws/aws-sdk-go v1.40.39
github.com/bep/debounce v1.2.0
github.com/fsnotify/fsnotify v1.5.1
github.com/golang/glog v1.0.0
github.com/gorilla/mux v1.8.0
github.com/kardianos/service v1.2.0
github.com/lithammer/fuzzysearch v1.1.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
9 changes: 4 additions & 5 deletions pkg/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,29 @@ import (
"fmt"
"strings"

"github.com/netflix/weep/pkg/logging"

"github.com/spf13/viper"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/netflix/weep/pkg/logging"
)

var log = logging.GetLogger()

// getSessionName returns the AWS session name, or defaults to weep if we can't find one.
func getSessionName(session *sts.STS) string {
identity, err := session.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
log.Warnf("could not get user identity; defaulting to weep: %s", err)
logging.Log.Warnf("could not get user identity; defaulting to weep: %s", err)
return "weep"
}

// split identity.UserId on colon, which should give us a 2-element slice with the principal ID and session name
splitId := strings.Split(*identity.UserId, ":")
if len(splitId) < 2 {
log.Warnf("session name not found; defaulting to weep")
logging.Log.Warnf("session name not found; defaulting to weep")
return "weep"
}

Expand Down
Loading

0 comments on commit 8d619aa

Please sign in to comment.