mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
fix export/import self-key roundtrip
This commit is contained in:
@@ -103,6 +103,14 @@ class FFIEventTracker:
|
|||||||
if rex.search(ev.data2):
|
if rex.search(ev.data2):
|
||||||
return ev
|
return ev
|
||||||
|
|
||||||
|
def get_info_regex_groups(self, regex, check_error=True):
|
||||||
|
rex = re.compile(regex)
|
||||||
|
while 1:
|
||||||
|
ev = self.get_matching("DC_EVENT_INFO", check_error=check_error)
|
||||||
|
m = rex.match(ev.data2)
|
||||||
|
if m is not None:
|
||||||
|
return m.groups()
|
||||||
|
|
||||||
def ensure_event_not_queued(self, event_name_regex):
|
def ensure_event_not_queued(self, event_name_regex):
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
rex = re.compile("(?:{}).*".format(event_name_regex))
|
rex = re.compile("(?:{}).*".format(event_name_regex))
|
||||||
|
|||||||
@@ -503,6 +503,9 @@ def lp():
|
|||||||
def step(self, msg):
|
def step(self, msg):
|
||||||
print("-" * 5, "step " + msg, "-" * 5)
|
print("-" * 5, "step " + msg, "-" * 5)
|
||||||
|
|
||||||
|
def indent(self, msg):
|
||||||
|
print(" " + msg)
|
||||||
|
|
||||||
return Printer()
|
return Printer()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -668,7 +668,7 @@ class TestOnlineAccount:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_export_import_self_keys(self, acfactory, tmpdir):
|
def test_export_import_self_keys(self, acfactory, tmpdir, lp):
|
||||||
ac1, ac2 = acfactory.get_two_online_accounts()
|
ac1, ac2 = acfactory.get_two_online_accounts()
|
||||||
|
|
||||||
dir = tmpdir.mkdir("exportdir")
|
dir = tmpdir.mkdir("exportdir")
|
||||||
@@ -676,8 +676,17 @@ class TestOnlineAccount:
|
|||||||
assert len(export_files) == 2
|
assert len(export_files) == 2
|
||||||
for x in export_files:
|
for x in export_files:
|
||||||
assert x.startswith(dir.strpath)
|
assert x.startswith(dir.strpath)
|
||||||
|
key_id, = ac1._evtracker.get_info_regex_groups(r".*xporting.*KeyId\((.*)\).*")
|
||||||
ac1._evtracker.consume_events()
|
ac1._evtracker.consume_events()
|
||||||
|
|
||||||
|
lp.sec("exported keys (private and public)")
|
||||||
|
for name in os.listdir(dir.strpath):
|
||||||
|
lp.indent(dir.strpath + os.sep + name)
|
||||||
|
lp.sec("importing into existing account")
|
||||||
ac2.import_self_keys(dir.strpath)
|
ac2.import_self_keys(dir.strpath)
|
||||||
|
key_id2, = ac2._evtracker.get_info_regex_groups(
|
||||||
|
r".*stored.*KeyId\((.*)\).*", check_error=False)
|
||||||
|
assert key_id2 == key_id
|
||||||
|
|
||||||
def test_one_account_send_bcc_setting(self, acfactory, lp):
|
def test_one_account_send_bcc_setting(self, acfactory, lp):
|
||||||
ac1 = acfactory.get_online_configuring_account()
|
ac1 = acfactory.get_online_configuring_account()
|
||||||
|
|||||||
31
src/imex.rs
31
src/imex.rs
@@ -31,6 +31,7 @@ use crate::param::*;
|
|||||||
use crate::pgp;
|
use crate::pgp;
|
||||||
use crate::sql::{self, Sql};
|
use crate::sql::{self, Sql};
|
||||||
use crate::stock::StockMessage;
|
use crate::stock::StockMessage;
|
||||||
|
use ::pgp::types::KeyTrait;
|
||||||
use async_tar::Archive;
|
use async_tar::Archive;
|
||||||
|
|
||||||
// Name of the database file in the backup.
|
// Name of the database file in the backup.
|
||||||
@@ -413,6 +414,8 @@ async fn set_self_key(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
info!(context, "stored self key: {:?}", keypair.secret.key_id());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,14 +447,16 @@ async fn imex_inner(
|
|||||||
what: ImexMode,
|
what: ImexMode,
|
||||||
param: Option<impl AsRef<Path>>,
|
param: Option<impl AsRef<Path>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
ensure!(param.is_some(), "No Import/export dir/file given.");
|
let path = if let Some(ref p) = param {
|
||||||
|
info!(context, "Import/export dir: {}", p.as_ref().display());
|
||||||
|
p
|
||||||
|
} else {
|
||||||
|
return Err(format_err!("Imex: Param was None"));
|
||||||
|
};
|
||||||
|
|
||||||
info!(context, "Import/export process started.");
|
|
||||||
context.emit_event(EventType::ImexProgress(10));
|
context.emit_event(EventType::ImexProgress(10));
|
||||||
|
|
||||||
ensure!(context.sql.is_open().await, "Database not opened.");
|
ensure!(context.sql.is_open().await, "Database not opened.");
|
||||||
|
|
||||||
let path = param.ok_or_else(|| format_err!("Imex: Param was None"))?;
|
|
||||||
if what == ImexMode::ExportBackup || what == ImexMode::ExportSelfKeys {
|
if what == ImexMode::ExportBackup || what == ImexMode::ExportSelfKeys {
|
||||||
// before we export anything, make sure the private key exists
|
// before we export anything, make sure the private key exists
|
||||||
if e2ee::ensure_secret_key_exists(context).await.is_err() {
|
if e2ee::ensure_secret_key_exists(context).await.is_err() {
|
||||||
@@ -875,6 +880,12 @@ async fn import_self_keys(context: &Context, dir: impl AsRef<Path>) -> Result<()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
info!(
|
||||||
|
context,
|
||||||
|
"considering key file: {}",
|
||||||
|
path_plus_name.display()
|
||||||
|
);
|
||||||
|
|
||||||
match dc_read_file(context, &path_plus_name).await {
|
match dc_read_file(context, &path_plus_name).await {
|
||||||
Ok(buf) => {
|
Ok(buf) => {
|
||||||
let armored = std::string::String::from_utf8_lossy(&buf);
|
let armored = std::string::String::from_utf8_lossy(&buf);
|
||||||
@@ -972,7 +983,12 @@ where
|
|||||||
let id = id.map_or("default".into(), |i| i.to_string());
|
let id = id.map_or("default".into(), |i| i.to_string());
|
||||||
dir.as_ref().join(format!("{}-key-{}.asc", kind, &id))
|
dir.as_ref().join(format!("{}-key-{}.asc", kind, &id))
|
||||||
};
|
};
|
||||||
info!(context, "Exporting key {}", file_name.display());
|
info!(
|
||||||
|
context,
|
||||||
|
"Exporting key {:?} to {}",
|
||||||
|
key.key_id(),
|
||||||
|
file_name.display()
|
||||||
|
);
|
||||||
dc_delete_file(context, &file_name).await;
|
dc_delete_file(context, &file_name).await;
|
||||||
|
|
||||||
let content = key.to_asc(None).into_bytes();
|
let content = key.to_asc(None).into_bytes();
|
||||||
@@ -1078,8 +1094,9 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.is_ok());
|
.is_ok());
|
||||||
|
|
||||||
let blobdir = context.ctx.get_blobdir().to_str().unwrap();
|
let context2 = TestContext::new().await;
|
||||||
assert!(imex(&context.ctx, ImexMode::ImportSelfKeys, Some(blobdir))
|
context2.configure_alice().await;
|
||||||
|
assert!(imex(&context2.ctx, ImexMode::ImportSelfKeys, Some(blobdir))
|
||||||
.await
|
.await
|
||||||
.is_ok());
|
.is_ok());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user