fix: correction decodeJSON nil body + logs debug test-ssh
- helpers.go : corrige le cas r.Body == nil (panic → erreur explicite) - install.go : ajout logs étape par étape pour TestSSH (TCP, auth SSH) sans jamais logger le mot de passe (longueur uniquement) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f6b3761d53
commit
a1090db802
2 changed files with 16 additions and 2 deletions
|
|
@ -3,14 +3,15 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// decodeJSON décode le corps JSON d'une requête dans dest.
|
// decodeJSON décode le corps JSON d'une requête dans dest.
|
||||||
// Retourne une erreur si le corps est invalide ou manquant.
|
// Retourne une erreur si le corps est manquant ou invalide.
|
||||||
func decodeJSON(r *http.Request, dest any) error {
|
func decodeJSON(r *http.Request, dest any) error {
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
return json.NewDecoder(r.Body).Decode(dest)
|
return errors.New("corps de requête vide")
|
||||||
}
|
}
|
||||||
return json.NewDecoder(r.Body).Decode(dest)
|
return json.NewDecoder(r.Body).Decode(dest)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -55,32 +56,43 @@ func (h *InstallHandler) TestSSH(w http.ResponseWriter, r *http.Request) {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
if err := decodeJSON(r, &body); err != nil {
|
if err := decodeJSON(r, &body); err != nil {
|
||||||
|
log.Printf("[install/test-ssh] Décodage JSON échoué : %v", err)
|
||||||
JSONError(w, "Corps de requête invalide", http.StatusBadRequest)
|
JSONError(w, "Corps de requête invalide", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[install/test-ssh] Tentative — host=%s user=%s", body.Host, body.Username)
|
||||||
|
|
||||||
if body.Host == "" || body.Username == "" || body.Password == "" {
|
if body.Host == "" || body.Username == "" || body.Password == "" {
|
||||||
|
log.Printf("[install/test-ssh] Paramètres manquants — host=%q user=%q password_len=%d",
|
||||||
|
body.Host, body.Username, len(body.Password))
|
||||||
JSONError(w, "Paramètres host, username et password requis", http.StatusBadRequest)
|
JSONError(w, "Paramètres host, username et password requis", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valider le format host:port
|
// Valider le format host:port
|
||||||
if _, _, err := net.SplitHostPort(body.Host); err != nil {
|
if _, _, err := net.SplitHostPort(body.Host); err != nil {
|
||||||
|
log.Printf("[install/test-ssh] Format host invalide : %q — %v", body.Host, err)
|
||||||
JSONError(w, "Format host invalide (attendu: host:port)", http.StatusBadRequest)
|
JSONError(w, "Format host invalide (attendu: host:port)", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test de connectivité réseau d'abord
|
// Test de connectivité réseau d'abord
|
||||||
|
log.Printf("[install/test-ssh] Test connectivité TCP vers %s...", body.Host)
|
||||||
if err := auth.TestConnectivity(body.Host, 5*time.Second); err != nil {
|
if err := auth.TestConnectivity(body.Host, 5*time.Second); err != nil {
|
||||||
|
log.Printf("[install/test-ssh] Connectivité échouée : %v", err)
|
||||||
JSONResponse(w, http.StatusOK, map[string]any{
|
JSONResponse(w, http.StatusOK, map[string]any{
|
||||||
"success": false,
|
"success": false,
|
||||||
"error": fmt.Sprintf("Impossible de joindre %s : %v", body.Host, err),
|
"error": fmt.Sprintf("Impossible de joindre %s : %v", body.Host, err),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Printf("[install/test-ssh] Connectivité TCP OK")
|
||||||
|
|
||||||
// Test d'authentification SSH
|
// Test d'authentification SSH
|
||||||
|
log.Printf("[install/test-ssh] Test authentification SSH — user=%s", body.Username)
|
||||||
if err := auth.TestSSHAuth(body.Host, body.Username, body.Password); err != nil {
|
if err := auth.TestSSHAuth(body.Host, body.Username, body.Password); err != nil {
|
||||||
|
log.Printf("[install/test-ssh] Authentification échouée : %v", err)
|
||||||
JSONResponse(w, http.StatusOK, map[string]any{
|
JSONResponse(w, http.StatusOK, map[string]any{
|
||||||
"success": false,
|
"success": false,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
|
@ -88,6 +100,7 @@ func (h *InstallHandler) TestSSH(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[install/test-ssh] Succès — host=%s user=%s", body.Host, body.Username)
|
||||||
JSONResponse(w, http.StatusOK, map[string]any{
|
JSONResponse(w, http.StatusOK, map[string]any{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "Connexion SSH réussie",
|
"message": "Connexion SSH réussie",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue