SOGOMS v1.0.3 - Admin UI, Cron, Config reload
Phase 13 : sogoms-cron
- Jobs planifiés avec schedule cron standard
- Types: query_email, http, service
- Actions: list, trigger, status
Phase 16 : Réorganisation config/apps/{app}/
- Tous les fichiers d'une app dans un seul dossier
- Migration prokov vers nouvelle structure
Phase 17 : sogoms-admin
- Interface web d'administration (Go templates + htmx)
- Auth sessions cookies signées HMAC-SHA256
- Rôles super_admin / app_admin avec permissions
Phase 19 : Création d'app via Admin UI
- Formulaire création app avec config DB/auth
- Bouton "Scanner la base" : introspection + schema.yaml
- Rechargement automatique sogoway via SIGHUP
Infrastructure :
- sogoctl : socket de contrôle /run/sogoctl.sock
- sogoway : reload config sur SIGHUP sans restart
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
105
internal/admin/audit.go
Normal file
105
internal/admin/audit.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"sogoms.com/internal/protocol"
|
||||
)
|
||||
|
||||
// AuditEvent représente un type d'événement audit.
|
||||
type AuditEvent string
|
||||
|
||||
// Événements audit.
|
||||
const (
|
||||
AuditLoginSuccess AuditEvent = "login_success"
|
||||
AuditLoginFailed AuditEvent = "login_failed"
|
||||
AuditLogout AuditEvent = "logout"
|
||||
AuditSessionExpired AuditEvent = "session_expired"
|
||||
AuditActionPerformed AuditEvent = "action_performed"
|
||||
AuditPermissionDenied AuditEvent = "permission_denied"
|
||||
)
|
||||
|
||||
// AuditLogger enregistre les événements admin vers sogoms-logs.
|
||||
type AuditLogger struct {
|
||||
logsPool *protocol.Pool
|
||||
appID string // "admin" pour les logs admin
|
||||
}
|
||||
|
||||
// NewAuditLogger crée un nouveau logger d'audit.
|
||||
func NewAuditLogger(logsPool *protocol.Pool) *AuditLogger {
|
||||
return &AuditLogger{
|
||||
logsPool: logsPool,
|
||||
appID: "admin",
|
||||
}
|
||||
}
|
||||
|
||||
// Log enregistre un événement d'audit (non-bloquant).
|
||||
func (a *AuditLogger) Log(event AuditEvent, username string, data map[string]any) {
|
||||
if a.logsPool == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
data = make(map[string]any)
|
||||
}
|
||||
data["username"] = username
|
||||
data["event"] = string(event)
|
||||
data["timestamp"] = time.Now().Format(time.RFC3339)
|
||||
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req := protocol.NewRequest("log_event", map[string]any{
|
||||
"app_id": a.appID,
|
||||
"event_type": "audit_" + string(event),
|
||||
"data": data,
|
||||
})
|
||||
a.logsPool.Call(ctx, req)
|
||||
}()
|
||||
}
|
||||
|
||||
// LogLogin enregistre une tentative de connexion.
|
||||
func (a *AuditLogger) LogLogin(success bool, username, ip, userAgent string, reason string) {
|
||||
event := AuditLoginSuccess
|
||||
if !success {
|
||||
event = AuditLoginFailed
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"ip": ip,
|
||||
"user_agent": userAgent,
|
||||
}
|
||||
if reason != "" {
|
||||
data["reason"] = reason
|
||||
}
|
||||
|
||||
a.Log(event, username, data)
|
||||
}
|
||||
|
||||
// LogLogout enregistre une déconnexion.
|
||||
func (a *AuditLogger) LogLogout(username, ip string) {
|
||||
a.Log(AuditLogout, username, map[string]any{
|
||||
"ip": ip,
|
||||
})
|
||||
}
|
||||
|
||||
// LogAction enregistre une action effectuée.
|
||||
func (a *AuditLogger) LogAction(username, action, appID string, details map[string]any) {
|
||||
data := map[string]any{
|
||||
"action": action,
|
||||
"app_id": appID,
|
||||
"details": details,
|
||||
}
|
||||
a.Log(AuditActionPerformed, username, data)
|
||||
}
|
||||
|
||||
// LogPermissionDenied enregistre un refus de permission.
|
||||
func (a *AuditLogger) LogPermissionDenied(username, action, appID, permission string) {
|
||||
a.Log(AuditPermissionDenied, username, map[string]any{
|
||||
"action": action,
|
||||
"app_id": appID,
|
||||
"permission": permission,
|
||||
})
|
||||
}
|
||||
112
internal/admin/config.go
Normal file
112
internal/admin/config.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// Package admin gère la configuration et les permissions de l'interface d'administration.
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// AdminConfig représente la configuration complète de l'admin.
|
||||
type AdminConfig struct {
|
||||
Session SessionConfig `yaml:"session"`
|
||||
RateLimit RateLimitConfig `yaml:"rate_limit"`
|
||||
Users []AdminUser `yaml:"users"`
|
||||
}
|
||||
|
||||
// SessionConfig configure les sessions.
|
||||
type SessionConfig struct {
|
||||
SecretFile string `yaml:"secret_file"`
|
||||
MaxAge int `yaml:"max_age"` // secondes
|
||||
CookieName string `yaml:"cookie_name"`
|
||||
Secret string `yaml:"-"` // chargé depuis fichier
|
||||
}
|
||||
|
||||
// RateLimitConfig configure le rate limiting.
|
||||
type RateLimitConfig struct {
|
||||
LoginMax int `yaml:"login_max"`
|
||||
LoginWindow int `yaml:"login_window"` // secondes
|
||||
}
|
||||
|
||||
// AdminUser représente un utilisateur admin.
|
||||
type AdminUser struct {
|
||||
Username string `yaml:"username"`
|
||||
PasswordHash string `yaml:"password_hash"`
|
||||
Role string `yaml:"role"`
|
||||
Email string `yaml:"email"`
|
||||
Apps []string `yaml:"apps,omitempty"` // pour app_admin
|
||||
Permissions []string `yaml:"permissions,omitempty"` // pour app_admin
|
||||
}
|
||||
|
||||
// IsSuperAdmin retourne true si l'utilisateur est super_admin.
|
||||
func (u *AdminUser) IsSuperAdmin() bool {
|
||||
return u.Role == "super_admin"
|
||||
}
|
||||
|
||||
// LoadAdminConfig charge la configuration admin depuis un fichier YAML.
|
||||
func LoadAdminConfig(path string) (*AdminConfig, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read admin config: %w", err)
|
||||
}
|
||||
|
||||
var cfg AdminConfig
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("parse admin config: %w", err)
|
||||
}
|
||||
|
||||
// Valeurs par défaut
|
||||
if cfg.Session.MaxAge == 0 {
|
||||
cfg.Session.MaxAge = 3600 // 1 heure
|
||||
}
|
||||
if cfg.Session.CookieName == "" {
|
||||
cfg.Session.CookieName = "sogoms_admin_sid"
|
||||
}
|
||||
if cfg.RateLimit.LoginMax == 0 {
|
||||
cfg.RateLimit.LoginMax = 5
|
||||
}
|
||||
if cfg.RateLimit.LoginWindow == 0 {
|
||||
cfg.RateLimit.LoginWindow = 60
|
||||
}
|
||||
|
||||
// Charger le secret de session depuis le fichier
|
||||
if cfg.Session.SecretFile != "" {
|
||||
secretData, err := os.ReadFile(cfg.Session.SecretFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read session secret: %w", err)
|
||||
}
|
||||
cfg.Session.Secret = strings.TrimSpace(string(secretData))
|
||||
}
|
||||
|
||||
// Valider
|
||||
if len(cfg.Users) == 0 {
|
||||
return nil, fmt.Errorf("no users defined")
|
||||
}
|
||||
if cfg.Session.Secret == "" {
|
||||
return nil, fmt.Errorf("session secret is required")
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// GetUser retourne un utilisateur par son username.
|
||||
func (cfg *AdminConfig) GetUser(username string) *AdminUser {
|
||||
for i := range cfg.Users {
|
||||
if cfg.Users[i].Username == username {
|
||||
return &cfg.Users[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByEmail retourne un utilisateur par son email.
|
||||
func (cfg *AdminConfig) GetUserByEmail(email string) *AdminUser {
|
||||
for i := range cfg.Users {
|
||||
if cfg.Users[i].Email == email {
|
||||
return &cfg.Users[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
131
internal/admin/permissions.go
Normal file
131
internal/admin/permissions.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package admin
|
||||
|
||||
// Permission représente une permission granulaire.
|
||||
type Permission string
|
||||
|
||||
// Permissions disponibles.
|
||||
const (
|
||||
PermSchemaRead Permission = "schema:read"
|
||||
PermSchemaWrite Permission = "schema:write"
|
||||
PermSchemaUpload Permission = "schema:upload"
|
||||
|
||||
PermQueriesRead Permission = "queries:read"
|
||||
PermQueriesWrite Permission = "queries:write"
|
||||
|
||||
PermEmailsRead Permission = "emails:read"
|
||||
PermEmailsWrite Permission = "emails:write"
|
||||
|
||||
PermCronRead Permission = "cron:read"
|
||||
PermCronTrigger Permission = "cron:trigger"
|
||||
PermCronWrite Permission = "cron:write"
|
||||
|
||||
PermLogsRead Permission = "logs:read"
|
||||
|
||||
PermDBIntrospect Permission = "db:introspect"
|
||||
|
||||
PermAll Permission = "*"
|
||||
)
|
||||
|
||||
// PermissionChecker vérifie les droits d'un utilisateur.
|
||||
type PermissionChecker struct {
|
||||
config *AdminConfig
|
||||
}
|
||||
|
||||
// NewPermissionChecker crée un nouveau vérificateur de permissions.
|
||||
func NewPermissionChecker(config *AdminConfig) *PermissionChecker {
|
||||
return &PermissionChecker{config: config}
|
||||
}
|
||||
|
||||
// HasPermission vérifie si l'utilisateur a une permission pour une app.
|
||||
func (pc *PermissionChecker) HasPermission(user *AdminUser, appID string, perm Permission) bool {
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Super admin a toutes les permissions
|
||||
if user.IsSuperAdmin() {
|
||||
return true
|
||||
}
|
||||
|
||||
// Vérifier l'accès à l'app
|
||||
if !pc.CanAccessApp(user, appID) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Vérifier la permission
|
||||
for _, p := range user.Permissions {
|
||||
if Permission(p) == PermAll || Permission(p) == perm {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CanAccessApp vérifie si l'utilisateur peut accéder à une app.
|
||||
func (pc *PermissionChecker) CanAccessApp(user *AdminUser, appID string) bool {
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Super admin a accès à tout
|
||||
if user.IsSuperAdmin() {
|
||||
return true
|
||||
}
|
||||
|
||||
// App admin : vérifier la liste des apps autorisées
|
||||
for _, app := range user.Apps {
|
||||
if app == appID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// GetAccessibleApps retourne les apps accessibles par l'utilisateur.
|
||||
func (pc *PermissionChecker) GetAccessibleApps(user *AdminUser, allApps []string) []string {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Super admin voit tout
|
||||
if user.IsSuperAdmin() {
|
||||
return allApps
|
||||
}
|
||||
|
||||
// App admin : filtrer sur ses apps autorisées
|
||||
var accessible []string
|
||||
for _, app := range allApps {
|
||||
if pc.CanAccessApp(user, app) {
|
||||
accessible = append(accessible, app)
|
||||
}
|
||||
}
|
||||
return accessible
|
||||
}
|
||||
|
||||
// GetUserPermissions retourne les permissions effectives d'un utilisateur.
|
||||
func (pc *PermissionChecker) GetUserPermissions(user *AdminUser) []Permission {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Super admin a toutes les permissions
|
||||
if user.IsSuperAdmin() {
|
||||
return []Permission{
|
||||
PermSchemaRead, PermSchemaWrite, PermSchemaUpload,
|
||||
PermQueriesRead, PermQueriesWrite,
|
||||
PermEmailsRead, PermEmailsWrite,
|
||||
PermCronRead, PermCronTrigger, PermCronWrite,
|
||||
PermLogsRead,
|
||||
PermDBIntrospect,
|
||||
}
|
||||
}
|
||||
|
||||
// Convertir les permissions string en Permission
|
||||
perms := make([]Permission, 0, len(user.Permissions))
|
||||
for _, p := range user.Permissions {
|
||||
perms = append(perms, Permission(p))
|
||||
}
|
||||
return perms
|
||||
}
|
||||
Reference in New Issue
Block a user