feat: initialisation complète du CORE ProxmoxPanel
Backend Go 1.23+ : - API REST + WebSocket (chi, gorilla/websocket) - Authentification PAM via SSH + JWT RS256 - Chiffrement AES-256-GCM pour secrets SQLite - Pool SSH, client Proxmox REST, hub WebSocket pub/sub - Système de modules compilés à initialisation conditionnelle - Audit log, migrations SQLite versionnées Frontend Vue 3 + Vite + TypeScript : - Thème Neumorphism sombre/clair (CSS custom properties) - Wizard d'installation, Dashboard drag-drop, Terminal xterm.js - Toutes les vues CORE + stubs modules optionnels - i18n EN/FR (vue-i18n v11) Infrastructure : - Docker multi-stage (Go → alpine, Node → nginx) - docker-compose.yml, .gitattributes, LICENSE MIT, README Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
5dbcb1df07
66 changed files with 10370 additions and 0 deletions
110
backend/internal/crypto/aes.go
Normal file
110
backend/internal/crypto/aes.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// Package crypto fournit le chiffrement/déchiffrement AES-256-GCM
|
||||
// pour protéger les secrets stockés en base SQLite (tokens API, credentials SSH, etc.)
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Encryptor gère le chiffrement/déchiffrement avec une clé AES-256.
|
||||
type Encryptor struct {
|
||||
key []byte
|
||||
}
|
||||
|
||||
// NewEncryptor crée un Encryptor depuis une clé maître stockée sur disque.
|
||||
// Si la clé n'existe pas, elle est générée aléatoirement et sauvegardée.
|
||||
func NewEncryptor(dataDir string) (*Encryptor, error) {
|
||||
keyPath := filepath.Join(dataDir, "master.key")
|
||||
|
||||
var masterSecret []byte
|
||||
|
||||
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
|
||||
// Générer un secret maître de 64 octets aléatoires
|
||||
masterSecret = make([]byte, 64)
|
||||
if _, err := io.ReadFull(rand.Reader, masterSecret); err != nil {
|
||||
return nil, fmt.Errorf("génération clé maître : %w", err)
|
||||
}
|
||||
|
||||
// Sauvegarder avec permissions restreintes (lecture propriétaire uniquement)
|
||||
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||
return nil, fmt.Errorf("création répertoire : %w", err)
|
||||
}
|
||||
if err := os.WriteFile(keyPath, masterSecret, 0600); err != nil {
|
||||
return nil, fmt.Errorf("sauvegarde clé maître : %w", err)
|
||||
}
|
||||
} else {
|
||||
// Lire la clé existante
|
||||
masterSecret, err = os.ReadFile(keyPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lecture clé maître : %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Dériver une clé AES-256 depuis le secret maître via SHA-256
|
||||
hash := sha256.Sum256(masterSecret)
|
||||
return &Encryptor{key: hash[:]}, nil
|
||||
}
|
||||
|
||||
// Encrypt chiffre une valeur en clair et retourne une chaîne base64.
|
||||
// Format : base64(nonce || ciphertext || tag)
|
||||
func (e *Encryptor) Encrypt(plaintext string) (string, error) {
|
||||
block, err := aes.NewCipher(e.key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("création cipher AES : %w", err)
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("création GCM : %w", err)
|
||||
}
|
||||
|
||||
// Générer un nonce aléatoire (12 octets pour GCM)
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", fmt.Errorf("génération nonce : %w", err)
|
||||
}
|
||||
|
||||
// Chiffrer : Seal(nonce, nonce, plaintext, nil) → nonce||ciphertext||tag
|
||||
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// Decrypt déchiffre une valeur chiffrée par Encrypt.
|
||||
func (e *Encryptor) Decrypt(encoded string) (string, error) {
|
||||
data, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("décodage base64 : %w", err)
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(e.key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("création cipher AES : %w", err)
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("création GCM : %w", err)
|
||||
}
|
||||
|
||||
nonceSize := gcm.NonceSize()
|
||||
if len(data) < nonceSize {
|
||||
return "", errors.New("données chiffrées trop courtes")
|
||||
}
|
||||
|
||||
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
|
||||
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("déchiffrement : %w", err)
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue