feat: desktop — connexion compte central + environnement prod/dev (#230)
Liaison d'appareil sur og/og-dev, validation du jeton draco et ouverture de la session de l'admin local. Sélecteur prod/dev (Configuration + premier lancement), admin local par défaut admin/chab30, sans impact sur le web Docker. Inclut BUG-126 (runtime python-embed/_pth + copie de backend/frontend + signature updater opt-in) et BUG-127 (retour /liaison même hôte).
This commit is contained in:
@@ -84,6 +84,11 @@ struct AppConfig {
|
||||
/// field existed) parseable instead of falling back to a full reset.
|
||||
#[serde(default)]
|
||||
wizard_done: bool,
|
||||
/// Environnement central choisi (#230) : `"prod"` (og.dracodev.net) ou
|
||||
/// `"dev"` (og-dev.dracodev.net). `#[serde(default)]` = prod pour les
|
||||
/// configs existantes ; normalisé à la lecture (`environment_key`).
|
||||
#[serde(default)]
|
||||
environment: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
@@ -97,10 +102,72 @@ impl Default for AppConfig {
|
||||
window_width: Some(1200.0),
|
||||
window_height: Some(800.0),
|
||||
wizard_done: false,
|
||||
environment: Some(ENV_PROD.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Environnement central (#230) ────────────────────────────────────────────
|
||||
// Le desktop peut viser la production (og.dracodev.net) ou le développement
|
||||
// (og-dev.dracodev.net). Le shell n'injecte que des variables d'environnement
|
||||
// dans le backend embarqué : l'application web Docker (qui lit son propre
|
||||
// `.env`) n'est jamais concernée.
|
||||
|
||||
const ENV_PROD: &str = "prod";
|
||||
const ENV_DEV: &str = "dev";
|
||||
|
||||
/// Normalise la valeur stockée : tout ce qui n'est pas `"dev"` vaut `"prod"`.
|
||||
fn environment_key(value: Option<&str>) -> &'static str {
|
||||
match value.map(|v| v.trim().to_ascii_lowercase()).as_deref() {
|
||||
Some(ENV_DEV) => ENV_DEV,
|
||||
_ => ENV_PROD,
|
||||
}
|
||||
}
|
||||
|
||||
fn config_environment(config: &AppConfig) -> &'static str {
|
||||
environment_key(config.environment.as_deref())
|
||||
}
|
||||
|
||||
/// URLs publiques (agent, site central ObsiGate) de l'environnement choisi.
|
||||
/// L'agent suffit à dériver les applications sœurs et le Hub en `-dev`
|
||||
/// (backend/draco_config.py, BUG-120) ; les deux sont posées explicitement.
|
||||
fn environment_urls(env: &str) -> (&'static str, &'static str) {
|
||||
if env == ENV_DEV {
|
||||
("https://agent-dev.dracodev.net", "https://og-dev.dracodev.net")
|
||||
} else {
|
||||
("https://agent.dracodev.net", "https://og.dracodev.net")
|
||||
}
|
||||
}
|
||||
|
||||
/// Mot de passe du compte admin local créé au tout premier lancement du
|
||||
/// desktop. Le backend n'écoute que sur 127.0.0.1 : ce compte est un secours
|
||||
/// local (aucun terminal pour lire le mot de passe aléatoire), pas une
|
||||
/// exposition réseau. Le compte central peut aussi déverrouiller cet admin.
|
||||
/// À changer depuis Réglages → Profil après la première connexion.
|
||||
pub const DEFAULT_DESKTOP_ADMIN_USER: &str = "admin";
|
||||
pub const DEFAULT_DESKTOP_ADMIN_PASSWORD: &str = "chab30";
|
||||
|
||||
/// Variables injectées dans le backend embarqué (jamais dans le web Docker).
|
||||
fn desktop_env_vars(env: &str) -> Vec<(&'static str, String)> {
|
||||
let (agent, obsigate) = environment_urls(env);
|
||||
vec![
|
||||
("OBSIGATE_DESKTOP", "1".to_string()),
|
||||
("OBSIGATE_DESKTOP_SSO", "1".to_string()),
|
||||
("OBSIGATE_DESKTOP_ENV", env.to_string()),
|
||||
("OBSIGATE_ADMIN_USER", DEFAULT_DESKTOP_ADMIN_USER.to_string()),
|
||||
("OBSIGATE_ADMIN_PASSWORD", DEFAULT_DESKTOP_ADMIN_PASSWORD.to_string()),
|
||||
("DRACO_AGENT_URL", agent.to_string()),
|
||||
("DRACO_OBSIGATE_URL", obsigate.to_string()),
|
||||
]
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
struct DesktopSettings {
|
||||
environment: String,
|
||||
agent_url: String,
|
||||
central_url: String,
|
||||
}
|
||||
|
||||
// ── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
fn app_data_dir() -> PathBuf {
|
||||
@@ -243,6 +310,11 @@ fn spawn_backend(exe_dir: &PathBuf) -> Result<Child, String> {
|
||||
cmd.env(format!("DIR_{}_PATH", n), &dir.path);
|
||||
}
|
||||
|
||||
// #230 : environnement central prod/dev (SSO, barre inter-apps, Hub).
|
||||
for (key, value) in desktop_env_vars(config_environment(&config)) {
|
||||
cmd.env(key, value);
|
||||
}
|
||||
|
||||
let port = pick_free_port();
|
||||
BACKEND_PORT.store(port, Ordering::Relaxed);
|
||||
|
||||
@@ -322,6 +394,42 @@ fn get_config() -> AppConfig {
|
||||
load_config()
|
||||
}
|
||||
|
||||
/// Environnement central + URLs résolues (interface Configuration / wizard).
|
||||
#[tauri::command]
|
||||
fn get_desktop_settings() -> DesktopSettings {
|
||||
let config = load_config();
|
||||
let env = config_environment(&config);
|
||||
let (agent, central) = environment_urls(env);
|
||||
DesktopSettings {
|
||||
environment: env.to_string(),
|
||||
agent_url: agent.to_string(),
|
||||
central_url: central.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Enregistre l'environnement central (#230). Le frontend redémarre ensuite
|
||||
/// le backend pour que les variables `DRACO_*` soient réinjectées.
|
||||
#[tauri::command]
|
||||
fn set_desktop_environment(environment: String) -> Result<DesktopSettings, String> {
|
||||
let normalized = environment_key(Some(&environment));
|
||||
if normalized != environment.trim().to_ascii_lowercase() {
|
||||
return Err(format!(
|
||||
"Environnement inconnu '{}' (attendu : prod ou dev)",
|
||||
environment
|
||||
));
|
||||
}
|
||||
let mut config = load_config();
|
||||
config.environment = Some(normalized.to_string());
|
||||
save_config(&config);
|
||||
let (agent, central) = environment_urls(normalized);
|
||||
info!("Desktop environment set to {}", normalized);
|
||||
Ok(DesktopSettings {
|
||||
environment: normalized.to_string(),
|
||||
agent_url: agent.to_string(),
|
||||
central_url: central.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn save_vault_path(path: String) -> Result<(), String> {
|
||||
let mut config = load_config();
|
||||
@@ -371,6 +479,26 @@ fn get_wizard_state() -> bool {
|
||||
load_config().wizard_done
|
||||
}
|
||||
|
||||
/// Ouvre l'URL d'approbation du central dans le navigateur système (#230).
|
||||
/// Seuls les hôtes `https://*.dracodev.net` sont acceptés.
|
||||
#[allow(deprecated)] // tauri-plugin-shell::open est déprécié au profit d'opener ;
|
||||
// le plugin shell est déjà embarqué et ACL-restreint.
|
||||
#[tauri::command]
|
||||
fn open_external_url(app: tauri::AppHandle, url: String) -> Result<bool, String> {
|
||||
let parsed = tauri::Url::parse(&url).map_err(|e| format!("URL invalide : {}", e))?;
|
||||
let host = parsed.host_str().unwrap_or("").to_ascii_lowercase();
|
||||
if parsed.scheme() != "https"
|
||||
|| !(host == "dracodev.net" || host.ends_with(".dracodev.net"))
|
||||
{
|
||||
return Err("URL externe refusée (https *.dracodev.net attendu)".to_string());
|
||||
}
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
app.shell()
|
||||
.open(parsed.to_string(), None)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn complete_wizard() -> Result<(), String> {
|
||||
let mut config = load_config();
|
||||
@@ -642,10 +770,13 @@ fn main() {
|
||||
get_backend_url,
|
||||
get_version,
|
||||
get_config,
|
||||
get_desktop_settings,
|
||||
set_desktop_environment,
|
||||
save_vault_path,
|
||||
get_vault_path,
|
||||
get_wizard_state,
|
||||
complete_wizard,
|
||||
open_external_url,
|
||||
pick_vault_folder,
|
||||
pick_folder,
|
||||
get_system_theme,
|
||||
@@ -1001,6 +1132,7 @@ mod tests {
|
||||
assert_eq!(c.window_width, Some(1200.0));
|
||||
assert_eq!(c.window_height, Some(800.0));
|
||||
assert!(!c.wizard_done);
|
||||
assert_eq!(c.environment.as_deref(), Some(ENV_PROD));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1017,6 +1149,7 @@ mod tests {
|
||||
window_width: Some(1400.0),
|
||||
window_height: Some(900.0),
|
||||
wizard_done: true,
|
||||
environment: Some(ENV_DEV.into()),
|
||||
};
|
||||
let loaded = roundtrip_config(&c);
|
||||
assert_eq!(loaded.vault_path, Some("/test/vault".into()));
|
||||
@@ -1026,6 +1159,7 @@ mod tests {
|
||||
assert_eq!(loaded.dirs.len(), 1);
|
||||
assert_eq!(loaded.window_x, Some(100.0));
|
||||
assert!(loaded.wizard_done);
|
||||
assert_eq!(loaded.environment.as_deref(), Some(ENV_DEV));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1054,6 +1188,55 @@ mod tests {
|
||||
assert!(roundtrip_config(&c).wizard_done);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_environment_normalization() {
|
||||
assert_eq!(environment_key(None), ENV_PROD);
|
||||
assert_eq!(environment_key(Some("prod")), ENV_PROD);
|
||||
assert_eq!(environment_key(Some(" DEV ")), ENV_DEV);
|
||||
assert_eq!(environment_key(Some("staging")), ENV_PROD);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_environment_urls() {
|
||||
assert_eq!(
|
||||
environment_urls(ENV_PROD),
|
||||
("https://agent.dracodev.net", "https://og.dracodev.net")
|
||||
);
|
||||
assert_eq!(
|
||||
environment_urls(ENV_DEV),
|
||||
("https://agent-dev.dracodev.net", "https://og-dev.dracodev.net")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_desktop_env_vars() {
|
||||
let vars = desktop_env_vars(ENV_DEV);
|
||||
let get = |k: &str| vars.iter().find(|(name, _)| *name == k).map(|(_, v)| v.as_str());
|
||||
assert_eq!(get("OBSIGATE_DESKTOP"), Some("1"));
|
||||
assert_eq!(get("OBSIGATE_DESKTOP_SSO"), Some("1"));
|
||||
assert_eq!(get("OBSIGATE_DESKTOP_ENV"), Some(ENV_DEV));
|
||||
assert_eq!(get("OBSIGATE_ADMIN_USER"), Some(DEFAULT_DESKTOP_ADMIN_USER));
|
||||
assert_eq!(get("OBSIGATE_ADMIN_PASSWORD"), Some(DEFAULT_DESKTOP_ADMIN_PASSWORD));
|
||||
assert_eq!(get("DRACO_AGENT_URL"), Some("https://agent-dev.dracodev.net"));
|
||||
assert_eq!(get("DRACO_OBSIGATE_URL"), Some("https://og-dev.dracodev.net"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_environment_missing_in_legacy_config_is_prod() {
|
||||
let json = r#"{
|
||||
"vault_path": null,
|
||||
"vaults": [],
|
||||
"dirs": [],
|
||||
"window_x": null,
|
||||
"window_y": null,
|
||||
"window_width": 1000.0,
|
||||
"window_height": 700.0,
|
||||
"wizard_done": true
|
||||
}"#;
|
||||
let c: AppConfig = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(config_environment(&c), ENV_PROD);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_empty_json() {
|
||||
// serde requires vaults/dirs fields — missing fields use unwrap_or_default
|
||||
|
||||
Reference in New Issue
Block a user