[WIP] Encryption for secure values (#183)

This commit is contained in:
Gregory Schier
2025-04-15 07:18:26 -07:00
committed by GitHub
parent e114a85c39
commit 2e55a1bd6d
208 changed files with 4063 additions and 28698 deletions

View File

@@ -1,6 +1,4 @@
extern crate core;
#[cfg(target_os = "macos")]
extern crate objc;
use crate::encoding::read_response_body;
use crate::error::Error::GenericError;
use crate::grpc::metadata_to_map;
@@ -43,14 +41,15 @@ use yaak_plugins::events::{
BootResponse, CallHttpAuthenticationRequest, CallHttpRequestActionRequest, FilterResponse,
GetHttpAuthenticationConfigResponse, GetHttpAuthenticationSummaryResponse,
GetHttpRequestActionsResponse, GetTemplateFunctionsResponse, HttpHeader, InternalEvent,
InternalEventPayload, JsonPrimitive, RenderPurpose, WindowContext,
InternalEventPayload, JsonPrimitive, PluginWindowContext, RenderPurpose,
};
use yaak_plugins::manager::PluginManager;
use yaak_plugins::template_callback::PluginTemplateCallback;
use yaak_sse::sse::ServerSentEvent;
use yaak_templates::format::format_json;
use yaak_templates::{Parser, Tokens};
use yaak_templates::{Tokens, transform_args};
mod commands;
mod encoding;
mod error;
mod grpc;
@@ -59,8 +58,6 @@ mod http_request;
mod notifications;
mod plugin_events;
mod render;
#[cfg(target_os = "macos")]
mod tauri_plugin_mac_window;
mod updates;
mod uri_scheme;
mod window;
@@ -77,9 +74,9 @@ struct AppMetaData {
}
#[tauri::command]
async fn cmd_metadata(app_handle: AppHandle) -> Result<AppMetaData, ()> {
let app_data_dir = app_handle.path().app_data_dir().unwrap();
let app_log_dir = app_handle.path().app_log_dir().unwrap();
async fn cmd_metadata(app_handle: AppHandle) -> YaakResult<AppMetaData> {
let app_data_dir = app_handle.path().app_data_dir()?;
let app_log_dir = app_handle.path().app_log_dir()?;
Ok(AppMetaData {
is_dev: is_dev(),
version: app_handle.package_info().version.to_string(),
@@ -90,13 +87,18 @@ async fn cmd_metadata(app_handle: AppHandle) -> Result<AppMetaData, ()> {
}
#[tauri::command]
async fn cmd_parse_template(template: &str) -> YaakResult<Tokens> {
Ok(Parser::new(template).parse()?)
}
#[tauri::command]
async fn cmd_template_tokens_to_string(tokens: Tokens) -> Result<String, String> {
Ok(tokens.to_string())
async fn cmd_template_tokens_to_string<R: Runtime>(
window: WebviewWindow<R>,
app_handle: AppHandle<R>,
tokens: Tokens,
) -> YaakResult<String> {
let cb = PluginTemplateCallback::new(
&app_handle,
&PluginWindowContext::new(&window),
RenderPurpose::Preview,
);
let new_tokens = transform_args(tokens, &cb)?;
Ok(new_tokens.to_string())
}
#[tauri::command]
@@ -118,7 +120,7 @@ async fn cmd_render_template<R: Runtime>(
environment.as_ref(),
&PluginTemplateCallback::new(
&app_handle,
&WindowContext::from_window(&window),
&PluginWindowContext::new(&window),
RenderPurpose::Preview,
),
)
@@ -131,8 +133,8 @@ async fn cmd_dismiss_notification<R: Runtime>(
window: WebviewWindow<R>,
notification_id: &str,
yaak_notifier: State<'_, Mutex<YaakNotifier>>,
) -> Result<(), String> {
yaak_notifier.lock().await.seen(&window, notification_id).await
) -> YaakResult<()> {
Ok(yaak_notifier.lock().await.seen(&window, notification_id).await?)
}
#[tauri::command]
@@ -157,11 +159,11 @@ async fn cmd_grpc_reflect<R: Runtime>(
environment.as_ref(),
&PluginTemplateCallback::new(
&app_handle,
&WindowContext::from_window(&window),
&PluginWindowContext::new(&window),
RenderPurpose::Send,
),
)
.await?;
.await?;
let uri = safe_uri(&req.url);
@@ -200,7 +202,7 @@ async fn cmd_grpc_go<R: Runtime>(
environment.as_ref(),
&PluginTemplateCallback::new(
&app_handle,
&WindowContext::from_window(&window),
&PluginWindowContext::new(&window),
RenderPurpose::Send,
),
)
@@ -356,7 +358,7 @@ async fn cmd_grpc_go<R: Runtime>(
environment.as_ref(),
&PluginTemplateCallback::new(
&app_handle,
&WindowContext::from_window(&window),
&PluginWindowContext::new(&window),
RenderPurpose::Send,
),
)
@@ -425,7 +427,7 @@ async fn cmd_grpc_go<R: Runtime>(
environment.as_ref(),
&PluginTemplateCallback::new(
&app_handle,
&WindowContext::from_window(&window),
&PluginWindowContext::new(&window),
RenderPurpose::Send,
),
)
@@ -742,7 +744,7 @@ async fn cmd_send_ephemeral_request<R: Runtime>(
}
#[tauri::command]
async fn cmd_format_json(text: &str) -> Result<String, String> {
async fn cmd_format_json(text: &str) -> YaakResult<String> {
Ok(format_json(text, " "))
}
@@ -777,10 +779,10 @@ async fn cmd_filter_response<R: Runtime>(
}
#[tauri::command]
async fn cmd_get_sse_events(file_path: &str) -> Result<Vec<ServerSentEvent>, String> {
let body = fs::read(file_path).map_err(|e| e.to_string())?;
async fn cmd_get_sse_events(file_path: &str) -> YaakResult<Vec<ServerSentEvent>> {
let body = fs::read(file_path)?;
let mut event_parser = EventParser::new();
event_parser.process_bytes(body.into()).map_err(|e| e.to_string())?;
event_parser.process_bytes(body.into())?;
let mut events = Vec::new();
while let Some(e) = event_parser.get_event() {
@@ -897,27 +899,24 @@ async fn cmd_import_data<R: Runtime>(
async fn cmd_http_request_actions<R: Runtime>(
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
) -> Result<Vec<GetHttpRequestActionsResponse>, String> {
plugin_manager.get_http_request_actions(&window).await.map_err(|e| e.to_string())
) -> YaakResult<Vec<GetHttpRequestActionsResponse>> {
Ok(plugin_manager.get_http_request_actions(&window).await?)
}
#[tauri::command]
async fn cmd_template_functions<R: Runtime>(
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
) -> Result<Vec<GetTemplateFunctionsResponse>, String> {
plugin_manager.get_template_functions(&window).await.map_err(|e| e.to_string())
) -> YaakResult<Vec<GetTemplateFunctionsResponse>> {
Ok(plugin_manager.get_template_functions(&window).await?)
}
#[tauri::command]
async fn cmd_get_http_authentication_summaries<R: Runtime>(
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
) -> Result<Vec<GetHttpAuthenticationSummaryResponse>, String> {
let results = plugin_manager
.get_http_authentication_summaries(&window)
.await
.map_err(|e| e.to_string())?;
) -> YaakResult<Vec<GetHttpAuthenticationSummaryResponse>> {
let results = plugin_manager.get_http_authentication_summaries(&window).await?;
Ok(results.into_iter().map(|(_, a)| a).collect())
}
@@ -928,11 +927,10 @@ async fn cmd_get_http_authentication_config<R: Runtime>(
auth_name: &str,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
) -> Result<GetHttpAuthenticationConfigResponse, String> {
plugin_manager
) -> YaakResult<GetHttpAuthenticationConfigResponse> {
Ok(plugin_manager
.get_http_authentication_config(&window, auth_name, values, request_id)
.await
.map_err(|e| e.to_string())
.await?)
}
#[tauri::command]
@@ -940,8 +938,8 @@ async fn cmd_call_http_request_action<R: Runtime>(
window: WebviewWindow<R>,
req: CallHttpRequestActionRequest,
plugin_manager: State<'_, PluginManager>,
) -> Result<(), String> {
plugin_manager.call_http_request_action(&window, req).await.map_err(|e| e.to_string())
) -> YaakResult<()> {
Ok(plugin_manager.call_http_request_action(&window, req).await?)
}
#[tauri::command]
@@ -952,11 +950,10 @@ async fn cmd_call_http_authentication_action<R: Runtime>(
action_index: i32,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
) -> Result<(), String> {
plugin_manager
) -> YaakResult<()> {
Ok(plugin_manager
.call_http_authentication_action(&window, auth_name, action_index, values, request_id)
.await
.map_err(|e| e.to_string())
.await?)
}
#[tauri::command]
@@ -965,18 +962,20 @@ async fn cmd_curl_to_request<R: Runtime>(
command: &str,
plugin_manager: State<'_, PluginManager>,
workspace_id: &str,
) -> Result<HttpRequest, String> {
let import_result =
{ plugin_manager.import_data(&window, command).await.map_err(|e| e.to_string())? };
) -> YaakResult<HttpRequest> {
let import_result = plugin_manager.import_data(&window, command).await?;
import_result.resources.http_requests.get(0).ok_or("No curl command found".to_string()).map(
|r| {
Ok(import_result
.resources
.http_requests
.get(0)
.ok_or(GenericError("No curl command found".to_string()))
.map(|r| {
let mut request = r.clone();
request.workspace_id = workspace_id.into();
request.id = "".to_string();
request
},
)
})?)
}
#[tauri::command]
@@ -985,11 +984,9 @@ async fn cmd_export_data<R: Runtime>(
export_path: &str,
workspace_ids: Vec<&str>,
include_environments: bool,
) -> Result<(), String> {
) -> YaakResult<()> {
let export_data =
get_workspace_export_resources(&app_handle, workspace_ids, include_environments)
.await
.map_err(|e| e.to_string())?;
get_workspace_export_resources(&app_handle, workspace_ids, include_environments).await?;
let f = File::options()
.create(true)
.truncate(true)
@@ -998,7 +995,7 @@ async fn cmd_export_data<R: Runtime>(
.expect("Unable to create file");
serde_json::to_writer_pretty(&f, &export_data)
.map_err(|e| e.to_string())
.map_err(|e| GenericError(e.to_string()))
.expect("Failed to write");
f.sync_all().expect("Failed to sync");
@@ -1092,9 +1089,7 @@ async fn cmd_install_plugin<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Plugin> {
plugin_manager
.add_plugin_by_dir(&WindowContext::from_window(&window), &directory, true)
.await?;
plugin_manager.add_plugin_by_dir(&PluginWindowContext::new(&window), &directory, true).await?;
Ok(app_handle.db().upsert_plugin(
&Plugin {
@@ -1116,9 +1111,7 @@ async fn cmd_uninstall_plugin<R: Runtime>(
let plugin =
app_handle.db().delete_plugin_by_id(plugin_id, &UpdateSource::from_window(&window))?;
plugin_manager
.uninstall(&WindowContext::from_window(&window), plugin.directory.as_str())
.await?;
plugin_manager.uninstall(&PluginWindowContext::new(&window), plugin.directory.as_str()).await?;
Ok(plugin)
}
@@ -1149,11 +1142,8 @@ async fn cmd_reload_plugins<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
) -> Result<(), String> {
plugin_manager
.initialize_all_plugins(&app_handle, &WindowContext::from_window(&window))
.await
.map_err(|e| e.to_string())?;
) -> YaakResult<()> {
plugin_manager.initialize_all_plugins(&app_handle, &PluginWindowContext::new(&window)).await?;
Ok(())
}
@@ -1212,12 +1202,11 @@ async fn cmd_delete_all_http_responses<R: Runtime>(
#[tauri::command]
async fn cmd_get_workspace_meta<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
workspace_id: &str,
) -> YaakResult<WorkspaceMeta> {
let db = app_handle.db();
let workspace = db.get_workspace(workspace_id)?;
Ok(db.get_or_create_workspace_meta(&workspace.id, &UpdateSource::from_window(&window))?)
Ok(db.get_or_create_workspace_meta(&workspace.id)?)
}
#[tauri::command]
@@ -1227,13 +1216,13 @@ async fn cmd_new_child_window(
label: &str,
title: &str,
inner_size: (f64, f64),
) -> Result<(), String> {
) -> YaakResult<()> {
window::create_child_window(&parent_window, url, label, title, inner_size);
Ok(())
}
#[tauri::command]
async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> Result<(), String> {
async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> YaakResult<()> {
window::create_main_window(&app_handle, url);
Ok(())
}
@@ -1295,17 +1284,14 @@ pub fn run() {
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_fs::init())
.plugin(yaak_license::init())
.plugin(yaak_mac_window::init())
.plugin(yaak_models::init())
.plugin(yaak_plugins::init())
.plugin(yaak_crypto::init())
.plugin(yaak_git::init())
.plugin(yaak_ws::init())
.plugin(yaak_sync::init());
#[cfg(target_os = "macos")]
{
builder = builder.plugin(tauri_plugin_mac_window::init());
}
builder
.setup(|app| {
let app_data_dir = app.path().app_data_dir().unwrap();
@@ -1352,7 +1338,6 @@ pub fn run() {
cmd_metadata,
cmd_new_child_window,
cmd_new_main_window,
cmd_parse_template,
cmd_plugin_info,
cmd_reload_plugins,
cmd_render_template,
@@ -1362,6 +1347,12 @@ pub fn run() {
cmd_template_functions,
cmd_template_tokens_to_string,
cmd_uninstall_plugin,
//
//
// Migrated commands
crate::commands::cmd_decrypt_template,
crate::commands::cmd_secure_template,
crate::commands::cmd_show_workspace_key,
])
.register_uri_scheme_protocol("yaak", handle_uri_scheme)
.build(tauri::generate_context!())
@@ -1495,11 +1486,11 @@ async fn call_frontend<R: Runtime>(
fn get_window_from_window_context<R: Runtime>(
app_handle: &AppHandle<R>,
window_context: &WindowContext,
window_context: &PluginWindowContext,
) -> Option<WebviewWindow<R>> {
let label = match window_context {
WindowContext::Label { label } => label,
WindowContext::None => {
PluginWindowContext::Label { label, .. } => label,
PluginWindowContext::None => {
return app_handle.webview_windows().iter().next().map(|(_, w)| w.to_owned());
}
};