mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
gosec severity=medium passes, all severity=low errors are from unhandled errors, we have 107 of them. tbh it doesn't look worth it to me, but maybe there are a few assholes even itchier than mine out there. medium has some good stuff in it, and of course high makes sense if we're gonna do this at all. this adds some nosec annotations for some things like sql sprintfs where we know it's clean (we're constructing the strings with variables in them). fixed up other spots where we were sprinting without need. some stuff like filepath.Clean when opening a file from a variable, and file permissions, easy stuff... I can't get the CI build to shut up, but I can locally get it to be pretty quiet about imports and it just outputs the gosec output. fortunately, it still works as expected even when it's noisy. I got it to shut up by unsetting some of the go mod flags locally, but that doesn't seem to quite do it in circle, printed the env out and don't see them, so idk... i give up, this works closes #1303
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package common
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func SetLogFormat(format string) {
|
|
if format != "text" && format != "json" {
|
|
logrus.WithFields(logrus.Fields{"format": format}).Warn("Unknown log format specified, using text. Possible options are json and text.")
|
|
}
|
|
|
|
if format == "json" {
|
|
logrus.SetFormatter(&logrus.JSONFormatter{TimestampFormat: time.RFC3339Nano})
|
|
} else {
|
|
// show full timestamps
|
|
formatter := &logrus.TextFormatter{
|
|
FullTimestamp: true,
|
|
}
|
|
logrus.SetFormatter(formatter)
|
|
}
|
|
}
|
|
|
|
func SetLogLevel(ll string) {
|
|
if ll == "" {
|
|
ll = "info"
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{"level": ll}).Info("Setting log level to")
|
|
logLevel, err := logrus.ParseLevel(ll)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{"level": ll}).Warn("Could not parse log level, setting to INFO")
|
|
logLevel = logrus.InfoLevel
|
|
}
|
|
logrus.SetLevel(logLevel)
|
|
|
|
// this effectively just adds more gin log goodies
|
|
gin.SetMode(gin.ReleaseMode)
|
|
if logLevel == logrus.DebugLevel {
|
|
gin.SetMode(gin.DebugMode)
|
|
}
|
|
}
|
|
|
|
func SetLogDest(to, prefix string) {
|
|
logrus.SetOutput(os.Stderr) // in case logrus changes their mind...
|
|
if to == "stderr" {
|
|
return
|
|
}
|
|
|
|
// possible schemes: { udp, tcp, file }
|
|
// file url must contain only a path, syslog must contain only a host[:port]
|
|
// expect: [scheme://][host][:port][/path]
|
|
// default scheme to udp:// if none given
|
|
|
|
parsed, err := url.Parse(to)
|
|
if parsed.Host == "" && parsed.Path == "" {
|
|
logrus.WithFields(logrus.Fields{"to": to}).Warn("No scheme on logging url, adding udp://")
|
|
// this happens when no scheme like udp:// is present
|
|
to = "udp://" + to
|
|
parsed, err = url.Parse(to)
|
|
}
|
|
if err != nil {
|
|
logrus.WithError(err).WithFields(logrus.Fields{"to": to}).Error("could not parse logging URI, defaulting to stderr")
|
|
return
|
|
}
|
|
|
|
// File URL must contain only `url.Path`. Syslog location must contain only `url.Host`
|
|
if (parsed.Host == "" && parsed.Path == "") || (parsed.Host != "" && parsed.Path != "") {
|
|
logrus.WithFields(logrus.Fields{"to": to, "uri": parsed}).Error("invalid logging location, defaulting to stderr")
|
|
return
|
|
}
|
|
|
|
switch parsed.Scheme {
|
|
case "udp", "tcp":
|
|
err = NewSyslogHook(parsed, prefix)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{"uri": parsed, "to": to}).WithError(err).Error("unable to connect to syslog, defaulting to stderr")
|
|
return
|
|
}
|
|
case "file":
|
|
f, err := os.OpenFile(parsed.Path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
|
|
if err != nil {
|
|
logrus.WithError(err).WithFields(logrus.Fields{"to": to, "path": parsed.Path}).Error("cannot open file, defaulting to stderr")
|
|
return
|
|
}
|
|
logrus.SetOutput(f)
|
|
default:
|
|
logrus.WithFields(logrus.Fields{"scheme": parsed.Scheme, "to": to}).Error("unknown logging location scheme, defaulting to stderr")
|
|
}
|
|
}
|
|
|
|
// MaskPassword returns a stringified URL without its password visible
|
|
func MaskPassword(u *url.URL) string {
|
|
if u.User != nil {
|
|
p, set := u.User.Password()
|
|
if set {
|
|
return strings.Replace(u.String(), p+"@", "***@", 1)
|
|
}
|
|
}
|
|
return u.String()
|
|
}
|