Skip to content

Commit

Permalink
wip - add secure strings to profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Aug 8, 2024
1 parent a5ba7ad commit 277baad
Show file tree
Hide file tree
Showing 13 changed files with 1,159 additions and 522 deletions.
4 changes: 4 additions & 0 deletions lumni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ rayon = { version = "1.10" }
crossbeam-channel = { version = "0.5" }
globset = { version = "0.4" }
uuid = { version = "1.10.0", features = ["v4"] }
ring = "0.17"
rsa = { version = "0.9" }
base64 = "0.22"
dirs = "5"

# CLI
env_logger = { version = "0.9", optional = true }
Expand Down
91 changes: 91 additions & 0 deletions lumni/src/apps/api/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::error::Error;
use std::fmt;

use base64::engine::general_purpose;
use base64::Engine as _;
use ring::aead;
use ring::rand::{SecureRandom, SystemRandom};
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
use rusqlite::Error as SqliteError;
use tokio::task::JoinError;

Expand Down Expand Up @@ -39,6 +44,7 @@ pub enum ApplicationError {
DatabaseError(String),
NotImplemented(String),
NotReady(String),
EncryptionError(EncryptionError),
CustomError(Box<dyn Error + Send + Sync>),
}

Expand Down Expand Up @@ -134,6 +140,9 @@ impl fmt::Display for ApplicationError {
write!(f, "NotImplemented: {}", s)
}
ApplicationError::NotReady(s) => write!(f, "NotReady: {}", s),
ApplicationError::EncryptionError(e) => {
write!(f, "EncryptionError: {}", e)
}
ApplicationError::CustomError(e) => write!(f, "{}", e),
}
}
Expand Down Expand Up @@ -204,3 +213,85 @@ impl From<std::string::String> for LumniError {
LumniError::Any(error.to_owned())
}
}

#[derive(Debug)]
pub enum EncryptionError {
RsaError(rsa::Error),
RingError(String),
Base64Error(base64::DecodeError),
Utf8Error(std::string::FromUtf8Error),
SpkiError(rsa::pkcs8::spki::Error),
Pkcs8Error(rsa::pkcs8::Error),
Other(Box<dyn Error + Send + Sync>),
}

impl fmt::Display for EncryptionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
EncryptionError::RsaError(e) => write!(f, "RSA error: {}", e),
EncryptionError::RingError(e) => {
write!(f, "Ring encryption error: {}", e)
}
EncryptionError::Base64Error(e) => {
write!(f, "Base64 decoding error: {}", e)
}
EncryptionError::Utf8Error(e) => {
write!(f, "UTF-8 conversion error: {}", e)
}
EncryptionError::SpkiError(e) => write!(f, "SPKI error: {}", e),
EncryptionError::Pkcs8Error(e) => write!(f, "PKCS8 error: {}", e),
EncryptionError::Other(e) => write!(f, "Other error: {}", e),
}
}
}

impl Error for EncryptionError {}

impl From<rsa::Error> for EncryptionError {
fn from(err: rsa::Error) -> EncryptionError {
EncryptionError::RsaError(err)
}
}

impl From<ring::error::Unspecified> for EncryptionError {
fn from(_: ring::error::Unspecified) -> EncryptionError {
EncryptionError::RingError("Unspecified Ring error".to_string())
}
}

impl From<base64::DecodeError> for EncryptionError {
fn from(err: base64::DecodeError) -> EncryptionError {
EncryptionError::Base64Error(err)
}
}

impl From<std::string::FromUtf8Error> for EncryptionError {
fn from(err: std::string::FromUtf8Error) -> EncryptionError {
EncryptionError::Utf8Error(err)
}
}

impl From<rsa::pkcs8::spki::Error> for EncryptionError {
fn from(err: rsa::pkcs8::spki::Error) -> EncryptionError {
EncryptionError::SpkiError(err)
}
}

impl From<rsa::pkcs8::Error> for EncryptionError {
fn from(err: rsa::pkcs8::Error) -> EncryptionError {
EncryptionError::Pkcs8Error(err)
}
}

impl From<Box<dyn Error + Send + Sync>> for EncryptionError {
fn from(err: Box<dyn Error + Send + Sync>) -> EncryptionError {
EncryptionError::Other(err)
}
}

// Implement From<EncryptionError> for ApplicationError
impl From<EncryptionError> for ApplicationError {
fn from(err: EncryptionError) -> ApplicationError {
ApplicationError::CustomError(Box::new(err))
}
}
50 changes: 46 additions & 4 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::collections::VecDeque;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use lumni::api::error::{ApplicationError, EncryptionError};
use rusqlite::{params, Error as SqliteError, Transaction};

use crate::external as lumni;

pub struct DatabaseConnector {
connection: rusqlite::Connection,
operation_queue: Arc<Mutex<VecDeque<String>>>,
Expand Down Expand Up @@ -138,11 +141,12 @@ impl DatabaseConnector {

pub fn process_queue_with_result<T>(
&mut self,
result_handler: impl FnOnce(&Transaction) -> Result<T, SqliteError>,
) -> Result<T, SqliteError> {
result_handler: impl FnOnce(
&Transaction,
) -> Result<T, DatabaseOperationError>,
) -> Result<T, DatabaseOperationError> {
let mut queue = self.operation_queue.lock().unwrap();
let tx = self.connection.transaction()?;

while let Some(sql) = queue.pop_front() {
if sql.trim().to_lowercase().starts_with("select") {
// For SELECT statements, use query
Expand All @@ -153,8 +157,46 @@ impl DatabaseConnector {
}
}
let result = result_handler(&tx)?;

tx.commit()?;
Ok(result)
}
}

#[derive(Debug)]
pub enum DatabaseOperationError {
SqliteError(SqliteError),
ApplicationError(ApplicationError),
}

// implement display
impl std::fmt::Display for DatabaseOperationError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
DatabaseOperationError::SqliteError(e) => write!(f, "{}", e),
DatabaseOperationError::ApplicationError(e) => write!(f, "{}", e),
}
}
}

impl From<SqliteError> for DatabaseOperationError {
fn from(error: SqliteError) -> Self {
DatabaseOperationError::SqliteError(error)
}
}

impl From<ApplicationError> for DatabaseOperationError {
fn from(error: ApplicationError) -> Self {
DatabaseOperationError::ApplicationError(error)
}
}

impl From<DatabaseOperationError> for ApplicationError {
fn from(error: DatabaseOperationError) -> Self {
match error {
DatabaseOperationError::SqliteError(e) => {
ApplicationError::DatabaseError(e.to_string())
}
DatabaseOperationError::ApplicationError(e) => e,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ impl ConversationDbHandler {
pub async fn permanent_delete_conversation(
&mut self,
conversation_id: Option<ConversationId>,
) -> Result<(), SqliteError> {
) -> Result<(), DatabaseOperationError> {
let target_conversation_id = conversation_id.or(self.conversation_id);

if let Some(id) = target_conversation_id {
Expand Down Expand Up @@ -44,7 +44,9 @@ impl ConversationDbHandler {

result
} else {
Err(SqliteError::QueryReturnedNoRows)
Err(DatabaseOperationError::SqliteError(
SqliteError::QueryReturnedNoRows,
))
}
}
}
Loading

0 comments on commit 277baad

Please sign in to comment.