Skip to content

Commit

Permalink
make EncryptedFs singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Sep 22, 2024
1 parent 0f22544 commit e57ebee
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 312 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build_and_tests_reusable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ jobs:
-A clippy::significant_drop_tightening \
-A clippy::redundant_closure \
-A clippy::missing_errors_doc \
-A clippy::type_complexity \
-A clippy::too_long_first_doc_paragraph
-A clippy::type_complexity
shell: bash

- name: doc
Expand Down Expand Up @@ -92,8 +91,7 @@ jobs:
-A clippy::significant_drop_tightening \
-A clippy::redundant_closure \
-A clippy::missing_errors_doc \
-A clippy::type_complexity \
-A clippy::too_long_first_doc_paragraph
-A clippy::type_complexity
shell: bash

- name: java-bridge doc
Expand Down
5 changes: 3 additions & 2 deletions examples/encryptedfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Result;
use shush_rs::SecretString;

use rencfs::crypto::Cipher;
use rencfs::encryptedfs::write_all_string_to_fs;
use rencfs::encryptedfs::{write_all_string_to_fs, FsError};
use rencfs::encryptedfs::{CreateFileAttr, EncryptedFs, FileType, PasswordProvider};

const ROOT_INODE: u64 = 1;
Expand All @@ -27,13 +27,14 @@ async fn main() -> Result<()> {
let data_dir = Path::new("/tmp/rencfs_data_test").to_path_buf();
let _ = fs::remove_dir_all(data_dir.to_str().unwrap());
let cipher = Cipher::ChaCha20Poly1305;
let fs = EncryptedFs::new(
EncryptedFs::initialize(
data_dir.clone(),
Box::new(PasswordProviderImpl {}),
cipher,
false,
)
.await?;
let fs = EncryptedFs::instance().ok_or(FsError::Other("not initialized"))?;

let file1 = SecretString::from_str("file1").unwrap();
let (fh, attr) = fs
Expand Down
46 changes: 35 additions & 11 deletions examples/file_layer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use rencfs::crypto::Cipher;
use shush_rs::SecretString;
use std::io::SeekFrom;
use std::path::Path;
use std::str::FromStr;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};

use crate::rencfs::fs::OpenOptions;
// use tokio::fs::OpenOptions;

mod rencfs;
use rencfs::crypto::fs::OpenOptions;
use rencfs::encryptedfs::{EncryptedFs, PasswordProvider};

static ROOT_CIPHER_FS_DATA_DIR: &str = "/tmp/rencfs/file_layer/fs_cipher";
static FILENAME: &str = "test1";

#[tokio::main]
async fn main() {
async fn main() -> anyhow::Result<()> {
init_fs().await?;

cleanup().await;
let file_path = Path::new(FILENAME).to_path_buf();

Expand All @@ -27,8 +30,8 @@ async fn main() {
// // add static flags
// let opts = opts.read(true).write(true);
// // add dynamic flags
// let opts = _add_create(opts, &path);
// let mut file = opts.open(&path).await.unwrap();
// let opts = _add_create(opts, &file_path);
// let mut file = opts.open(&file_path).await.unwrap();

file.write_all(b"test42").await.unwrap();
file.flush().await.unwrap();
Expand All @@ -41,11 +44,24 @@ async fn main() {

let mut buf = String::new();
let len = file.read_to_string(&mut buf).await.unwrap();
println!("{len} {buf}")
println!("{len} {buf}");

Ok(())
}

async fn init_fs() -> anyhow::Result<()> {
EncryptedFs::initialize(
Path::new(ROOT_CIPHER_FS_DATA_DIR).to_path_buf(),
Box::new(PasswordProviderImpl {}),
Cipher::ChaCha20Poly1305,
false,
)
.await?;
Ok(())
}

fn _add_create<'a>(opts: &'a mut OpenOptions, x: &Path) -> &'a mut OpenOptions {
if !x.to_path_buf().exists() {
fn _add_create<'a>(opts: &'a mut OpenOptions, path: &Path) -> &'a mut OpenOptions {
if !path.to_path_buf().exists() {
opts.create(true);
}
opts
Expand All @@ -55,8 +71,16 @@ async fn cleanup() {
// todo: ignore if we delete first time when not present
let _ = tokio::fs::remove_dir_all(Path::new(ROOT_CIPHER_FS_DATA_DIR)).await;

// todo: seems we need to abstract also Path because exists() goes against local FS
// // todo: seems we need to abstract also Path because exists() goes against local FS
// if file_path.exists() {
// fs::remove_file(&file_path).await.unwrap();
// }
}

struct PasswordProviderImpl {}

impl PasswordProvider for PasswordProviderImpl {
fn get_password(&self) -> Option<SecretString> {
Some(SecretString::from_str("pass42").unwrap())
}
}
Loading

0 comments on commit e57ebee

Please sign in to comment.