1 Commits

Author SHA1 Message Date
052c690509 feat(middleware): log more informations on authentification errors
Some checks failed
Cadoles/go-http-peering/pipeline/head There was a failure building this commit
2024-01-04 16:22:13 +01:00
3 changed files with 41 additions and 40 deletions

View File

@ -53,21 +53,21 @@ func DecodePEMEncryptedPrivateKey(key []byte, passphrase []byte) (*rsa.PrivateKe
} }
func EncodePrivateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, error) { func EncodePrivateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, error) {
if passphrase == nil {
return nil, errors.New("passphrase cannot be empty")
}
block := &pem.Block{ block := &pem.Block{
Type: "RSA PRIVATE KEY", Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key), Bytes: x509.MarshalPKCS1PrivateKey(key),
} }
if len(passphrase) != 0 { block, err := x509.EncryptPEMBlock(
encryptedBlock, err := x509.EncryptPEMBlock( rand.Reader, block.Type,
rand.Reader, block.Type, block.Bytes, passphrase, x509.PEMCipherAES256,
block.Bytes, passphrase, x509.PEMCipherAES256, )
) if err != nil {
if err != nil { return nil, errors.WithStack(err)
return nil, errors.WithStack(err)
}
block = encryptedBlock
} }
return pem.EncodeToMemory(block), nil return pem.EncodeToMemory(block), nil

View File

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

View File

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