mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-23 00:08:15 -07:00
pkg/log: create wrapper around logrus
This commit is contained in:
+26
-18
@@ -19,11 +19,11 @@ import (
|
||||
jc "github.com/SermoDigital/jose/crypto"
|
||||
"github.com/SermoDigital/jose/jws"
|
||||
"github.com/SermoDigital/jose/jwt"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/mendsley/gojwk"
|
||||
|
||||
"github.com/chihaya/chihaya/bittorrent"
|
||||
"github.com/chihaya/chihaya/middleware"
|
||||
"github.com/chihaya/chihaya/pkg/log"
|
||||
"github.com/chihaya/chihaya/pkg/stop"
|
||||
)
|
||||
|
||||
@@ -44,6 +44,16 @@ type Config struct {
|
||||
JWKUpdateInterval time.Duration `yaml:"jwk_set_update_interval"`
|
||||
}
|
||||
|
||||
// LogFields implements log.Fielder for a Config.
|
||||
func (cfg Config) LogFields() log.Fields {
|
||||
return log.Fields{
|
||||
"issuer": cfg.Issuer,
|
||||
"audience": cfg.Audience,
|
||||
"JWKSetURL": cfg.JWKSetURL,
|
||||
"JWKUpdateInterval": cfg.JWKUpdateInterval,
|
||||
}
|
||||
}
|
||||
|
||||
type hook struct {
|
||||
cfg Config
|
||||
publicKeys map[string]crypto.PublicKey
|
||||
@@ -52,7 +62,7 @@ type hook struct {
|
||||
|
||||
// NewHook returns an instance of the JWT middleware.
|
||||
func NewHook(cfg Config) (middleware.Hook, error) {
|
||||
log.Debugf("creating new JWT middleware with config: %#v", cfg)
|
||||
log.Debug("creating new JWT middleware", cfg)
|
||||
h := &hook{
|
||||
cfg: cfg,
|
||||
publicKeys: map[string]crypto.PublicKey{},
|
||||
@@ -83,7 +93,7 @@ func NewHook(cfg Config) (middleware.Hook, error) {
|
||||
func (h *hook) updateKeys() error {
|
||||
resp, err := http.Get(h.cfg.JWKSetURL)
|
||||
if err != nil {
|
||||
log.Errorln("failed to fetch JWK Set: " + err.Error())
|
||||
log.Error("failed to fetch JWK Set", log.Err(err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -91,7 +101,7 @@ func (h *hook) updateKeys() error {
|
||||
err = json.NewDecoder(resp.Body).Decode(&parsedJWKs)
|
||||
if err != nil {
|
||||
resp.Body.Close()
|
||||
log.Errorln("failed to decode JWK JSON: " + err.Error())
|
||||
log.Error("failed to decode JWK JSON", log.Err(err))
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
@@ -100,7 +110,7 @@ func (h *hook) updateKeys() error {
|
||||
for _, parsedJWK := range parsedJWKs.Keys {
|
||||
publicKey, err := parsedJWK.DecodePublicKey()
|
||||
if err != nil {
|
||||
log.Errorln("failed to decode JWK into public key: " + err.Error())
|
||||
log.Error("failed to decode JWK into public key", log.Err(err))
|
||||
return err
|
||||
}
|
||||
keys[parsedJWK.Kid] = publicKey
|
||||
@@ -156,55 +166,53 @@ func validateJWT(ih bittorrent.InfoHash, jwtBytes []byte, cfgIss, cfgAud string,
|
||||
|
||||
claims := parsedJWT.Claims()
|
||||
if iss, ok := claims.Issuer(); !ok || iss != cfgIss {
|
||||
log.WithFields(log.Fields{
|
||||
log.Debug("unequal or missing issuer when validating JWT", log.Fields{
|
||||
"exists": ok,
|
||||
"claim": iss,
|
||||
"config": cfgIss,
|
||||
}).Debugln("unequal or missing issuer when validating JWT")
|
||||
})
|
||||
return jwt.ErrInvalidISSClaim
|
||||
}
|
||||
|
||||
if auds, ok := claims.Audience(); !ok || !in(cfgAud, auds) {
|
||||
log.WithFields(log.Fields{
|
||||
log.Debug("unequal or missing audience when validating JWT", log.Fields{
|
||||
"exists": ok,
|
||||
"claim": strings.Join(auds, ","),
|
||||
"config": cfgAud,
|
||||
}).Debugln("unequal or missing audience when validating JWT")
|
||||
})
|
||||
return jwt.ErrInvalidAUDClaim
|
||||
}
|
||||
|
||||
ihHex := hex.EncodeToString(ih[:])
|
||||
if ihClaim, ok := claims.Get("infohash").(string); !ok || ihClaim != ihHex {
|
||||
log.WithFields(log.Fields{
|
||||
log.Debug("unequal or missing infohash when validating JWT", log.Fields{
|
||||
"exists": ok,
|
||||
"claim": ihClaim,
|
||||
"request": ihHex,
|
||||
}).Debugln("unequal or missing infohash when validating JWT")
|
||||
})
|
||||
return errors.New("claim \"infohash\" is invalid")
|
||||
}
|
||||
|
||||
parsedJWS := parsedJWT.(jws.JWS)
|
||||
kid, ok := parsedJWS.Protected().Get("kid").(string)
|
||||
if !ok {
|
||||
log.WithFields(log.Fields{
|
||||
log.Debug("missing kid when validating JWT", log.Fields{
|
||||
"exists": ok,
|
||||
"claim": kid,
|
||||
}).Debugln("missing kid when validating JWT")
|
||||
})
|
||||
return errors.New("invalid kid")
|
||||
}
|
||||
publicKey, ok := publicKeys[kid]
|
||||
if !ok {
|
||||
log.WithFields(log.Fields{
|
||||
log.Debug("missing public key forkid when validating JWT", log.Fields{
|
||||
"kid": kid,
|
||||
}).Debugln("missing public key for kid when validating JWT")
|
||||
})
|
||||
return errors.New("signed by unknown kid")
|
||||
}
|
||||
|
||||
err = parsedJWS.Verify(publicKey, jc.SigningMethodRS256)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err,
|
||||
}).Debugln("failed to verify signature of JWT")
|
||||
log.Debug("failed to verify signature of JWT", log.Err(err))
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/chihaya/chihaya/bittorrent"
|
||||
"github.com/chihaya/chihaya/frontend"
|
||||
"github.com/chihaya/chihaya/pkg/log"
|
||||
"github.com/chihaya/chihaya/pkg/stop"
|
||||
"github.com/chihaya/chihaya/storage"
|
||||
)
|
||||
@@ -61,7 +61,7 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
|
||||
}
|
||||
}
|
||||
|
||||
log.WithFields(resp.LogFields()).Debug("generated announce response")
|
||||
log.Debug("generated announce response", resp)
|
||||
return ctx, resp, nil
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque
|
||||
var err error
|
||||
for _, h := range l.postHooks {
|
||||
if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||
log.Errorln("chihaya: post-announce hooks failed:", err.Error())
|
||||
log.Error("post-announce hooks failed", log.Err(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest)
|
||||
}
|
||||
}
|
||||
|
||||
log.WithFields(resp.LogFields()).Debug("generated scrape response")
|
||||
log.Debug("generated scrape response", resp)
|
||||
return ctx, resp, nil
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest,
|
||||
var err error
|
||||
for _, h := range l.postHooks {
|
||||
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
||||
log.Errorln("chihaya: post-scrape hooks failed:", err.Error())
|
||||
log.Error("post-scrape hooks failed", log.Err(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user