Skip to content
This repository has been archived by the owner on Aug 21, 2021. It is now read-only.

Commit

Permalink
Replace zap by logrus
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Uiterwijk <patrick@puiterwijk.org>
  • Loading branch information
puiterwijk committed Jul 13, 2018
1 parent cb6a7ac commit 5be4286
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 796 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ before_script:
script: script:
- git config --global protocol.ext.allow always - git config --global protocol.ext.allow always
- ./build.sh - ./build.sh
- go vet ./...
- go test ./... - go test ./...
44 changes: 20 additions & 24 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
version = "~3.3.7" version = "~3.3.7"


[[constraint]] [[constraint]]
name = "go.uber.org/zap" name = "github.com/sirupsen/logrus"
version = "^1.8.0" version = "^1.0.5"


[[constraint]] [[constraint]]
name = "github.com/pkg/errors" name = "github.com/pkg/errors"
Expand Down
6 changes: 0 additions & 6 deletions client/bridge.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ func bridge(r *http.Request) {
"request", r, "request", r,
) )
defer resp.Body.Close() defer resp.Body.Close()
logger.Debugw(
"Server response received",
"url", r.URL,
"request", r,
"statuscode", resp.StatusCode,
)
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
exitWithError("Repository does not exist") exitWithError("Repository does not exist")
} }
Expand Down
95 changes: 17 additions & 78 deletions client/root.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"strings" "strings"
"syscall" "syscall"


"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap"


"repospanner.org/repospanner/server/service" "repospanner.org/repospanner/server/service"
) )


var ( var (
logger *zap.SugaredLogger
username string username string
) )


Expand All @@ -27,10 +24,6 @@ func exitWithError(errmsg string, extra ...interface{}) {
// Send a plain (non-sidebanded) git packet. // Send a plain (non-sidebanded) git packet.
// The only moment that this would be wrong // The only moment that this would be wrong
service.SendPacketWithFlush(os.Stdout, []byte("ERR "+errmsg)) service.SendPacketWithFlush(os.Stdout, []byte("ERR "+errmsg))

if logger != nil {
logger.Infow(errmsg, extra...)
}
os.Exit(1) os.Exit(1)
} }


Expand Down Expand Up @@ -83,10 +76,7 @@ func checkConfigured(options ...string) {
for _, opt := range options { for _, opt := range options {
if !viper.IsSet(opt) { if !viper.IsSet(opt) {
missing = true missing = true
logger.Errorw( fmt.Fprintln(os.Stderr, "Required option not configured:", opt)
"Required option not configured",
"option", opt,
)
} }
} }
if missing { if missing {
Expand All @@ -95,7 +85,22 @@ func checkConfigured(options ...string) {
} }


func ExecuteClient() { func ExecuteClient() {
logger = initLogging() username = os.Getenv("USER")
cfgFile := os.Getenv("REPOCLIENT_CONFIG")
if username == "" {
exitWithError("Unable to determine username")
os.Exit(1)
}
viper.SetConfigName("client_config")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("/etc/repospanner")
}
if err := viper.ReadInConfig(); err != nil {
exitWithError("Unable to read config file")
os.Exit(1)
}


// Just call this to make sure we abort loudly early on if the user has no access // Just call this to make sure we abort loudly early on if the user has no access
getCertAndKey() getCertAndKey()
Expand All @@ -121,11 +126,6 @@ func ExecuteClient() {
exitWithError("Invalid call arguments", "len", len(os.Args)) exitWithError("Invalid call arguments", "len", len(os.Args))
} }


logger = logger.With(
"command", command,
"repo", repo,
)

if command != "receive-pack" && command != "upload-pack" { if command != "receive-pack" && command != "upload-pack" {
exitWithError("Invalid call") exitWithError("Invalid call")
} }
Expand All @@ -144,7 +144,6 @@ func ExecuteClient() {


isdone, r := shouldClose(os.Stdin) isdone, r := shouldClose(os.Stdin)
if isdone { if isdone {
logger.Debug("Client sent flush, terminating")
os.Exit(0) os.Exit(0)
} }


Expand All @@ -167,10 +166,6 @@ func shouldClose(r io.ReadCloser) (bool, io.ReadCloser) {
if n != 4 { if n != 4 {
exitWithError("Not enough bytes read to determine close status") exitWithError("Not enough bytes read to determine close status")
} }
logger.Debugw(
"PossibleCloser read",
"buffer", string(buf),
)
buffer := bytes.NewBuffer(buf) buffer := bytes.NewBuffer(buf)
combined := &splitReadCloser{ combined := &splitReadCloser{
r: io.MultiReader(buffer, r), r: io.MultiReader(buffer, r),
Expand Down Expand Up @@ -202,59 +197,3 @@ func getCertAndKey() (string, string) {
exitWithError("User does not have access to this client") exitWithError("User does not have access to this client")
return "", "" return "", ""
} }

func initLogging() *zap.SugaredLogger {
username = os.Getenv("USER")
client := os.Getenv("SSH_CLIENT")
logdebug := os.Getenv("REPOCLIENT_LOG_DEBUG") != ""
cfgFile := os.Getenv("REPOCLIENT_CONFIG")

if username == "" {
exitWithError("Unable to determine username")
os.Exit(1)
}

viper.SetConfigName("client_config")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("/etc/repospanner")
}

if err := viper.ReadInConfig(); err != nil {
exitWithError("Unable to read config file")
os.Exit(1)
}

var logconfig zap.Config
if logdebug || viper.GetBool("log.debug") {
logconfig = zap.NewDevelopmentConfig()
} else {
logconfig = zap.NewProductionConfig()
}
destdir := viper.GetString("log.destdir")
if destdir == "" {
exitWithError("Config file incomplete")
os.Exit(1)
}
destfile, err := ioutil.TempFile(destdir, "log_")
if err != nil {
exitWithError("Error opening log file")
os.Exit(1)
}
logconfig.OutputPaths = []string{
destfile.Name(),
}

rlogger, err := logconfig.Build()
if err != nil {
exitWithError("Error preparing logging")
os.Exit(1)
}
logger := rlogger.Sugar().With(
"username", username,
"client", client,
"args", os.Args,
)
return logger
}
19 changes: 9 additions & 10 deletions server/service/authutils.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"


"go.uber.org/zap" "github.com/sirupsen/logrus"

"repospanner.org/repospanner/server/constants" "repospanner.org/repospanner/server/constants"
) )


Expand Down Expand Up @@ -48,17 +47,17 @@ func (cfg *Service) getPermissionInfo(cstate *tls.ConnectionState) permissionInf
return info return info
} }


func (cfg *Service) addPermToLogger(perminfo permissionInfo, log *zap.SugaredLogger) *zap.SugaredLogger { func (cfg *Service) addPermToLogger(perminfo permissionInfo, log *logrus.Entry) *logrus.Entry {
log = log.With( log = log.WithField(
"authed", perminfo.Authenticated, "authed", perminfo.Authenticated,
) )
if perminfo.Authenticated { if perminfo.Authenticated {
log = log.With( log = log.WithFields(logrus.Fields{
"username", perminfo.Username, "username": perminfo.Username,
"user-permissions", perminfo.permissions, "user-permissions": perminfo.permissions,
"user-regions", perminfo.regions, "user-regions": perminfo.regions,
"user-repos", perminfo.repos, "user-repos": perminfo.repos,
) })
} }


return log return log
Expand Down
Loading

0 comments on commit 5be4286

Please sign in to comment.