feat(sync): encrypt snippet sync snapshots
This commit is contained in:
21 files changed
+1412
-124
No files matched your search
@@ -757,6 +757,9 @@ async fn main() {
|
||||
.route("/cloud-sync/snippet/token-status", post(routes::cloud_sync::snippet_token_status))
|
||||
.route("/cloud-sync/snippet/save-token", post(routes::cloud_sync::save_snippet_saved_token))
|
||||
.route("/cloud-sync/snippet/forget-token", post(routes::cloud_sync::forget_snippet_saved_token))
|
||||
.route("/cloud-sync/snippet/settings", post(routes::cloud_sync::snippet_sync_settings))
|
||||
.route("/cloud-sync/snippet/save-id", post(routes::cloud_sync::save_snippet_sync_id))
|
||||
.route("/cloud-sync/snippet/retry-legacy-cleanup", post(routes::cloud_sync::retry_snippet_legacy_cleanup))
|
||||
.route("/cloud-sync/snippet/upload", post(routes::cloud_sync::snippet_sync_upload))
|
||||
.route("/cloud-sync/snippet/download", post(routes::cloud_sync::snippet_sync_download));
|
||||
|
||||
|
||||
@@ -3,13 +3,16 @@ use std::sync::Arc;
|
||||
use axum::extract::State;
|
||||
use axum::Json;
|
||||
use dbx_core::cloud_sync::{
|
||||
apply_sync_snapshot, build_sync_snapshot_with_saved_secrets, forget_snippet_token, forget_webdav_password,
|
||||
apply_sync_snapshot, build_sync_snapshot, build_sync_snapshot_with_saved_secrets, finalize_snippet_migration,
|
||||
forget_snippet_token, forget_webdav_password,
|
||||
forget_webdav_sync_secrets_passphrase as core_forget_webdav_sync_secrets_passphrase, resolve_snippet_token,
|
||||
resolve_webdav_password, resolve_webdav_sync_secrets_passphrase, save_snippet_token, save_webdav_password,
|
||||
resolve_webdav_password, resolve_webdav_sync_secrets_passphrase, retry_pending_snippet_cleanup,
|
||||
save_snippet_sync_id as core_save_snippet_sync_id, save_snippet_token, save_webdav_password,
|
||||
save_webdav_sync_secrets_preference as core_save_webdav_sync_secrets_preference, snippet_saved_token_status,
|
||||
webdav_saved_password_status, webdav_sync_secrets_status as core_webdav_sync_secrets_status, ApplySnapshotOptions,
|
||||
ApplySnapshotSummary, SnippetSyncClient, SnippetSyncConfig, SnippetSyncSummary, SnippetTokenStatus, WebDavClient,
|
||||
WebDavConfig, WebDavPasswordStatus, WebDavSyncSecretsStatus, WebDavSyncSummary,
|
||||
snippet_sync_settings as core_snippet_sync_settings, webdav_saved_password_status,
|
||||
webdav_sync_secrets_status as core_webdav_sync_secrets_status, ApplySnapshotOptions, ApplySnapshotSummary,
|
||||
SnippetProvider, SnippetSyncClient, SnippetSyncConfig, SnippetSyncSettings, SnippetSyncSummary, SnippetTokenStatus,
|
||||
WebDavClient, WebDavConfig, WebDavPasswordStatus, WebDavSyncSecretsStatus, WebDavSyncSummary,
|
||||
};
|
||||
use dbx_core::storage::DesktopSettings;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -88,6 +91,9 @@ pub struct SaveSnippetTokenRequest {
|
||||
pub struct SnippetUploadRequest {
|
||||
pub config: SnippetSyncConfig,
|
||||
pub editor_settings: Option<serde_json::Value>,
|
||||
pub snippet_passphrase: Option<String>,
|
||||
#[serde(default)]
|
||||
pub include_secrets: bool,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
}
|
||||
|
||||
@@ -95,9 +101,25 @@ pub struct SnippetUploadRequest {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SnippetDownloadRequest {
|
||||
pub config: SnippetSyncConfig,
|
||||
pub snippet_passphrase: Option<String>,
|
||||
#[serde(default)]
|
||||
pub restore_secrets: bool,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SnippetSyncSettingsRequest {
|
||||
pub provider: SnippetProvider,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SaveSnippetSyncIdRequest {
|
||||
pub provider: SnippetProvider,
|
||||
pub snippet_id: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn webdav_sync_test(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<WebDavConfigRequest>,
|
||||
@@ -182,7 +204,10 @@ pub async fn webdav_sync_download(
|
||||
let apply_summary = apply_sync_snapshot(
|
||||
&state.app.storage,
|
||||
&snapshot,
|
||||
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
|
||||
ApplySnapshotOptions {
|
||||
secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()),
|
||||
restore_secrets: true,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
@@ -226,20 +251,60 @@ pub async fn forget_snippet_saved_token(
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn snippet_sync_settings(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<SnippetSyncSettingsRequest>,
|
||||
) -> Result<Json<SnippetSyncSettings>, AppError> {
|
||||
core_snippet_sync_settings(&state.app.storage, req.provider).await.map(Json).map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn save_snippet_sync_id(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<SaveSnippetSyncIdRequest>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
core_save_snippet_sync_id(&state.app.storage, req.provider, req.snippet_id.as_deref())
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn retry_snippet_legacy_cleanup(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<SnippetConfigRequest>,
|
||||
) -> Result<Json<SnippetSyncSettings>, AppError> {
|
||||
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
|
||||
let provider = req.config.provider;
|
||||
let client = SnippetSyncClient::new(req.config);
|
||||
retry_pending_snippet_cleanup(&state.app.storage, provider, &client).await.map(Json).map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn snippet_sync_upload(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<SnippetUploadRequest>,
|
||||
) -> Result<Json<SnippetSyncSummary>, AppError> {
|
||||
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
|
||||
let snapshot = build_sync_snapshot_with_saved_secrets(
|
||||
&state.app.storage,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
req.editor_settings,
|
||||
req.secrets_passphrase.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
SnippetSyncClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
|
||||
let secrets_passphrase = if req.include_secrets {
|
||||
Some(
|
||||
req.secrets_passphrase
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| AppError::from("A sync password is required when including synced secrets."))?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let snapshot =
|
||||
build_sync_snapshot(&state.app.storage, env!("CARGO_PKG_VERSION"), req.editor_settings, secrets_passphrase)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
let client = SnippetSyncClient::new(req.config);
|
||||
let mut summary = client
|
||||
.put_snapshot(&snapshot, req.snippet_passphrase.as_deref(), secrets_passphrase)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
finalize_snippet_migration(&state.app.storage, &client, &mut summary).await.map_err(AppError::from)?;
|
||||
Ok(Json(summary))
|
||||
}
|
||||
|
||||
pub async fn snippet_sync_download(
|
||||
@@ -247,17 +312,17 @@ pub async fn snippet_sync_download(
|
||||
Json(mut req): Json<SnippetDownloadRequest>,
|
||||
) -> Result<Json<SnippetDownloadResult>, AppError> {
|
||||
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
|
||||
let (snapshot, summary) = SnippetSyncClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
|
||||
let explicit_passphrase = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
|
||||
let saved_passphrase = if explicit_passphrase.is_some() {
|
||||
None
|
||||
} else {
|
||||
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
|
||||
};
|
||||
let (snapshot, summary) = SnippetSyncClient::new(req.config)
|
||||
.get_snapshot(req.snippet_passphrase.as_deref())
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
let apply_summary = apply_sync_snapshot(
|
||||
&state.app.storage,
|
||||
&snapshot,
|
||||
ApplySnapshotOptions { secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()) },
|
||||
ApplySnapshotOptions {
|
||||
secrets_passphrase: req.secrets_passphrase.as_deref(),
|
||||
restore_secrets: req.restore_secrets,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
@@ -268,3 +333,40 @@ pub async fn snippet_sync_download(
|
||||
apply_summary,
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::Json;
|
||||
use dbx_core::cloud_sync::SnippetProvider;
|
||||
use dbx_core::connection::AppState;
|
||||
use dbx_core::storage::Storage;
|
||||
|
||||
use crate::state::WebState;
|
||||
|
||||
#[tokio::test]
|
||||
async fn snippet_settings_surfaces_pending_cleanup_after_restart() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-web-snippet-cleanup-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let db = dir.join("storage.db");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
storage.save_snippet_migration_state("github", "replacement-id", "legacy-id", "content-hash").await.unwrap();
|
||||
drop(storage);
|
||||
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
let app = Arc::new(AppState::new_with_plugin_dir(storage, dir.join("plugins")));
|
||||
let state = Arc::new(WebState::for_tests(app, dir.clone()));
|
||||
let Json(settings) = super::snippet_sync_settings(
|
||||
State(state),
|
||||
Json(super::SnippetSyncSettingsRequest { provider: SnippetProvider::GitHub }),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(settings.snippet_id.as_deref(), Some("replacement-id"));
|
||||
assert_eq!(settings.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user