feat(middleware): log more informations on authentification errors
Some checks failed
Cadoles/go-http-peering/pipeline/head There was a failure building this commit

This commit is contained in:
wpetit 2024-01-04 16:22:13 +01:00
parent e8e484a7ff
commit 20c4bef161
2 changed files with 30 additions and 31 deletions

View File

@ -3,12 +3,12 @@ package server
import (
"crypto/rsa"
"encoding/json"
"errors"
"net/http"
"time"
peering "forge.cadoles.com/Cadoles/go-http-peering"
peeringCrypto "forge.cadoles.com/Cadoles/go-http-peering/crypto"
"github.com/pkg/errors"
)
var (
@ -34,7 +34,7 @@ func AdvertiseHandler(store peering.Store, key *rsa.PublicKey, funcs ...OptionFu
serverClaims, err := assertServerToken(key, serverToken)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusUnauthorized)
return
}
@ -43,27 +43,26 @@ func AdvertiseHandler(store peering.Store, key *rsa.PublicKey, funcs ...OptionFu
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(advertising); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, ErrInvalidAdvertisingRequest)
return
}
if _, err := peeringCrypto.DecodePEMToPublicKey(advertising.PublicKey); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, ErrInvalidAdvertisingRequest)
return
}
peer, err := store.Get(serverClaims.PeerID)
if err == nil {
logger.Printf("[ERROR] %s", ErrPeerIDAlreadyInUse)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrPeerIDAlreadyInUse))
options.ErrorHandler(w, r, ErrPeerIDAlreadyInUse)
return
}
if err != peering.ErrPeerNotFound {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
@ -72,25 +71,25 @@ func AdvertiseHandler(store peering.Store, key *rsa.PublicKey, funcs ...OptionFu
peer, err = store.Create(serverClaims.PeerID, attrs)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
if err := store.UpdatePublicKey(peer.ID, advertising.PublicKey); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
@ -117,39 +116,39 @@ func UpdateHandler(store peering.Store, funcs ...OptionFunc) http.HandlerFunc {
peerID, err := GetPeerID(r)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
peer, err := store.Get(peerID)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
if peer == nil {
logger.Printf("[ERROR] %s", ErrUnauthorized)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrUnauthorized))
options.ErrorHandler(w, r, ErrUnauthorized)
return
}
if peer.Status == peering.StatusRejected {
logger.Printf("[ERROR] %s", ErrPeerRejected)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrPeerRejected))
options.ErrorHandler(w, r, ErrPeerRejected)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
attrs := filterAttributes(options.PeerAttributes, update.Attributes)
if err := store.UpdateAttributes(peer.ID, attrs); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
@ -176,32 +175,32 @@ func PingHandler(store peering.Store, funcs ...OptionFunc) http.HandlerFunc {
peerID, err := GetPeerID(r)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
peer, err := store.Get(peerID)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}
if peer == nil {
logger.Printf("[ERROR] %s", ErrUnauthorized)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrUnauthorized))
options.ErrorHandler(w, r, ErrUnauthorized)
return
}
if peer.Status == peering.StatusRejected {
logger.Printf("[ERROR] %s", ErrPeerRejected)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrPeerRejected))
options.ErrorHandler(w, r, ErrPeerRejected)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
options.ErrorHandler(w, r, err)
return
}

View File

@ -5,7 +5,6 @@ import (
"context"
"crypto/rsa"
"crypto/sha256"
"errors"
"io"
"io/ioutil"
"net/http"
@ -15,6 +14,7 @@ import (
peering "forge.cadoles.com/Cadoles/go-http-peering"
jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
)
const (
@ -51,7 +51,7 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
serverClaims, err := assertServerToken(key, serverToken)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusUnauthorized)
return
}
@ -64,13 +64,13 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
options.IgnoredClientTokenErrors...,
)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
switch err {
case peering.ErrPeerNotFound:
sendError(w, http.StatusUnauthorized)
case ErrNotPeered:
if err := store.UpdateLastContact(serverClaims.PeerID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusInternalServerError)
return
}
@ -78,7 +78,7 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
return
case ErrPeerRejected:
if err := store.UpdateLastContact(serverClaims.PeerID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusInternalServerError)
return
}
@ -92,19 +92,19 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
match, body, err := assertBodySum(r.Body, clientClaims.BodySum)
if err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusInternalServerError)
return
}
if !match {
logger.Printf("[ERROR] %s", ErrInvalidChecksum)
logger.Printf("[ERROR] %+v", errors.WithStack(ErrInvalidChecksum))
sendError(w, http.StatusBadRequest)
return
}
if err := store.UpdateLastContact(serverClaims.PeerID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
logger.Printf("[ERROR] %+v", errors.WithStack(err))
sendError(w, http.StatusInternalServerError)
return
}
@ -183,7 +183,7 @@ func assertClientToken(peerID peering.PeerID, store peering.Store, clientToken s
if ok {
for _, c := range ignoredValidationErrors {
if validationError.Errors&c != 0 {
logger.Printf("ignoring token validation error: '%s'", validationError.Inner)
logger.Printf("ignoring token validation error: '%+v'", errors.WithStack(validationError.Inner))
return getPeeringClaims(token)
}
}