From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 19479B3B06 for ; Wed, 29 Nov 2023 14:21:02 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E44B77640 for ; Wed, 29 Nov 2023 14:20:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 29 Nov 2023 14:20:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id F33D940B46 for ; Wed, 29 Nov 2023 14:20:30 +0100 (CET) From: Markus Frank To: pbs-devel@lists.proxmox.com Date: Wed, 29 Nov 2023 14:20:18 +0100 Message-Id: <20231129132018.117868-1-m.frank@proxmox.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.034 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox-backup v2] cli: add interactive confirmation for block device wipe X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Nov 2023 13:21:02 -0000 If stdin is a TTY, an interactive prompt is added to confirm the deletion of a block device, ensuring user verification before proceeding. Signed-off-by: Markus Frank --- v2: * changed stdin_isatty() to std::io::stdin().is_terminal() * removed unnecessary eprint * do not loop if answer is not either y/Y or n/N * moved err inline in bail src/bin/proxmox_backup_manager/disk.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/bin/proxmox_backup_manager/disk.rs b/src/bin/proxmox_backup_manager/disk.rs index 7a292098..e0056f6c 100644 --- a/src/bin/proxmox_backup_manager/disk.rs +++ b/src/bin/proxmox_backup_manager/disk.rs @@ -3,6 +3,7 @@ use serde_json::Value; use proxmox_router::{cli::*, ApiHandler, RpcEnvironment}; use proxmox_schema::api; +use std::io::{IsTerminal, Write}; use pbs_api_types::{ ZfsCompressionType, ZfsRaidLevel, BLOCKDEVICE_DISK_AND_PARTITION_NAME_SCHEMA, @@ -152,6 +153,26 @@ async fn initialize_disk( async fn wipe_disk(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result { param["node"] = "localhost".into(); + // If we're on a TTY, query the user + if std::io::stdin().is_terminal() { + loop { + eprintln!("You are about to wipe block device {}.", param["disk"]); + eprint!("Are you sure you want to continue? (y/N): "); + let _ = std::io::stdout().flush(); + use std::io::{BufRead, BufReader}; + let mut line = String::new(); + match BufReader::new(std::io::stdin()).read_line(&mut line) { + Ok(_) => { + match line.trim() { + "y" | "Y" => break, + _ => bail!("Aborting."), + } + } + Err(err) => bail!("Failed to read line - {err}."), + } + } + } + let info = &api2::node::disks::API_METHOD_WIPE_DISK; let result = match info.handler { ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, -- 2.39.2