feat: v1.2.0, add feature encrypt with tiny-encrypt

This commit is contained in:
2024-12-15 17:45:54 +08:00
parent 27e107770e
commit 5333dcb91a
4 changed files with 2522 additions and 484 deletions

View File

@@ -15,7 +15,9 @@ use std::{
path::Path,
fs::{ self, File },
};
use rust_util::{ XResult, util_time::* };
use std::path::PathBuf;
use rust_util::{XResult, util_time::*, util_msg::*};
use tiny_encrypt::CmdEncrypt;
use oss_util::*;
use config_util::*;
#[cfg(feature = "use_sequoia_openpgp")]
@@ -105,12 +107,10 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret);
let backup_count = config_item.get_safe_backup_count();
let meta_file_name = &format!("{}/ossbackupd_meta_{}_{}.json", &oss_backupd_config.get_prefix(), &oss_backupd_config.get_host(), &config_item.get_file_name());
let new_file = format!("{}/{}", path, config_item.make_oss_key(oss_backupd_config, "zip.gpg"));
if options.verbose {
debugging!("Backup count: {}", backup_count);
debugging!("Meta file name: {}", meta_file_name);
debugging!("New backup file: {}", new_file);
}
let secs = get_current_secs();
@@ -159,6 +159,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
}
let temp_encrypted_file;
let temp_new_file;
if let Some(encrypt_pubkey_file) = encrypt_pubkey_file {
#[cfg(feature = "use_sequoia_openpgp")]
let enc_file_by_pgp = || {
@@ -196,13 +197,42 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
return Ok(());
}
temp_encrypted_file = temp_pgp_file;
temp_new_file = format!("{}/{}", path, config_item.make_oss_key(oss_backupd_config, "zip.gpg"));
} else if let Some(tiny_encrypt_config) = tiny_encrypt_config {
// TODO ...
panic!("TODO to be implemented");
let config = match tiny_encrypt::TinyEncryptConfig::load(&tiny_encrypt_config.config) {
Ok(config) => config,
Err(e) => return Err(format!("Load tiny-encrypt config failed: {}", e)),
};
let envelops = match config.find_envelops(&tiny_encrypt_config.profile, &None) {
Ok(envelops) => envelops,
Err(e) => return Err(format!("Load tiny-encrypt config and filter failed: {}", e)),
};
let cmd_encrypt = CmdEncrypt{
comment: None,
encrypted_comment: None,
profile: None,
key_filter: None,
compress: false,
compress_level: None,
remove_file: false,
create: false,
disable_compress_meta: false,
pem_output: false,
encryption_algorithm: None,
paths: vec![],
};
let path_in = PathBuf::from(temp_zip_file);
if let Err(e) = tiny_encrypt::encrypt_single_file_out(&path_in, temp_tiny_encrypt_file, &envelops, &cmd_encrypt) {
return Err(format!("Encrypt with tiny-encrypt failed: {}", e));
}
temp_encrypted_file = temp_tiny_encrypt_file;
temp_new_file = format!("{}/{}", path, config_item.make_oss_key(oss_backupd_config, "zip.tinyenc"));
} else {
return Err("Config encrypt_pubkey_file or tiny_encrypt MUST config one".to_string());
}
if options.verbose {
debugging!("New backup file: {}", temp_new_file);
}
let file_temp_encrypted_file = match File::open(temp_encrypted_file) {
Ok(f) => f, Err(e) => {
@@ -215,7 +245,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
if options.verbose {
debugging!("Upload file: {}", temp_encrypted_file);
}
if let Err(e) = oss_client.put_file(bucket, &new_file, oss_util::DEFAULT_URL_VALID_IN_SECS, file_temp_encrypted_file) {
if let Err(e) = oss_client.put_file(bucket, &temp_new_file, oss_util::DEFAULT_URL_VALID_IN_SECS, file_temp_encrypted_file) {
failure!("Error in encrypt file: {}, at item index: {}", e, item_index);
remove_temp_files();
return Ok(());
@@ -224,7 +254,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
if options.verbose {
debugging!("Processing meta file: {}", meta_file_name);
}
match process_oss_files(options, &oss_client, bucket, path, meta_file_name, &new_file, backup_count) {
match process_oss_files(options, &oss_client, bucket, path, meta_file_name, &temp_new_file, backup_count) {
Err(e) => failure!("Error: {}, at item index: {}", e, item_index),
Ok(_) => success!("Success, at item index: {}", item_index),
};

View File

@@ -52,12 +52,13 @@ pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
let zip_fn = get_file_name(target_path);
zip.start_file(zip_fn, options)?;
copy_io_with_head(&mut File::open(target_path)?, &mut zip, -1, "Compressing")?;
let mut print_status_context = PrintStatusContext::default();
copy_io_with_head(&mut File::open(target_path)?, &mut zip, -1, "Compressing", &mut print_status_context)?;
zip.finish()?;
} else {
let mut_zip = RefCell::new(zip);
walk_dir(target_path, &|p, e| {
walk_dir(target_path, &|p, e| {
warning!("Compress {} failed: {}", &p.display(), &e);
}, &|f| {
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
@@ -66,8 +67,11 @@ pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
match m_zip.start_file(&file_name, options) {
Ok(_) => match File::open(f) {
Err(e) => warning!("Compress {} failed: {}", &f.display(), e),
Ok(mut ff) => if let Err(e) = copy_io_with_head(&mut ff, &mut *m_zip, -1, &format!("Compressing {}", &file_name)) {
warning!("Compress {} failed: {}", &f.display(), e)
Ok(mut ff) => {
let mut print_status_context = PrintStatusContext::default();
if let Err(e) = copy_io_with_head(&mut ff, &mut *m_zip, -1, &format!("Compressing {}", &file_name), &mut print_status_context) {
warning!("Compress {} failed: {}", &f.display(), e)
}
}
},
Err(e) => warning!("Compress {} failed: {}", &f.display(), e),