vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit 5710703c5057048e52006bb26783aa636e6c96d0
parent 1322b876e9113cc265e5a2b30f990c0d8ede716e
Author: Daniel GarcĂ­a <dani-garcia@users.noreply.github.com>
Date:   Sun,  2 Jun 2019 00:08:52 +0200

Make sure the backup option only appears when using sqlite

Diffstat:
Msrc/api/admin.rs | 15+++++++++++++--
Msrc/static/templates/admin/page.hbs | 5++++-
2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/api/admin.rs b/src/api/admin.rs @@ -1,4 +1,5 @@ use serde_json::Value; +use std::process::Command; use rocket::http::{Cookie, Cookies, SameSite}; use rocket::request::{self, FlashMessage, Form, FromRequest, Request}; @@ -9,7 +10,7 @@ use rocket_contrib::json::Json; use crate::api::{ApiResult, EmptyResult, JsonResult}; use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp}; use crate::config::ConfigBuilder; -use crate::db::{models::*, DbConn, backup_database}; +use crate::db::{backup_database, models::*, DbConn}; use crate::error::Error; use crate::mail; use crate::CONFIG; @@ -34,6 +35,10 @@ pub fn routes() -> Vec<Route> { ] } +lazy_static! { + static ref CAN_BACKUP: bool = cfg!(feature = "sqlite") && Command::new("sqlite").arg("-version").status().is_ok(); +} + #[get("/")] fn admin_disabled() -> &'static str { "The admin panel is disabled, please configure the 'ADMIN_TOKEN' variable to enable it" @@ -102,6 +107,7 @@ struct AdminTemplateData { version: Option<&'static str>, users: Vec<Value>, config: Value, + can_backup: bool, } impl AdminTemplateData { @@ -111,6 +117,7 @@ impl AdminTemplateData { version: VERSION, users, config: CONFIG.prepare_json(), + can_backup: *CAN_BACKUP, } } @@ -207,7 +214,11 @@ fn delete_config(_token: AdminToken) -> EmptyResult { #[post("/config/backup_db")] fn backup_db(_token: AdminToken) -> EmptyResult { - backup_database() + if *CAN_BACKUP { + backup_database() + } else { + err!("Can't back up current DB (either it's not SQLite or the 'sqlite' binary is not present)"); + } } pub struct AdminToken {} diff --git a/src/static/templates/admin/page.hbs b/src/static/templates/admin/page.hbs @@ -157,9 +157,11 @@ </div> </div> + + {{#if can_backup}} <div class="card bg-light mb-3"> <div class="card-header"><button type="button" class="btn btn-link collapsed" data-toggle="collapse" - data-target="#g_database">Database</button></div> + data-target="#g_database">Backup Database</button></div> <div id="g_database" class="card-body collapse" data-parent="#config-form"> <div class="small mb-3"> NOTE: A local installation of sqlite3 is required for this section to work. @@ -167,6 +169,7 @@ <button type="button" class="btn btn-primary" onclick="backupDatabase();">Backup Database</button> </div> </div> + {{/if}} <button type="submit" class="btn btn-primary">Save</button> <button type="button" class="btn btn-danger float-right" onclick="deleteConf();">Reset defaults</button>