-
Notifications
You must be signed in to change notification settings - Fork 484
Enable encryption for attached databases #2155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Example of showing using an encrypted local database with libsql. It also shows how to | ||
| // attach another encrypted database. The example expects a local `world.db` encrypted database | ||
| // to be present in the same directory. | ||
|
|
||
| use libsql::{params, Builder}; | ||
| use libsql::{Cipher, EncryptionConfig}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| tracing_subscriber::fmt::init(); | ||
|
|
||
| // The local database path where the data will be stored. | ||
| let db_path = std::env::var("LIBSQL_DB_PATH").unwrap(); | ||
| // The encryption key for the database. | ||
| let encryption_key = std::env::var("LIBSQL_ENCRYPTION_KEY").unwrap_or("s3cR3t".to_string()); | ||
|
|
||
| let mut db_builder = Builder::new_local(db_path); | ||
|
|
||
| db_builder = db_builder.encryption_config(EncryptionConfig { | ||
| cipher: Cipher::Aes256Cbc, | ||
| encryption_key: encryption_key.into(), | ||
| }); | ||
|
|
||
| let db = db_builder.build().await.unwrap(); | ||
| let conn = db.connect().unwrap(); | ||
| conn.execute( | ||
| "CREATE TABLE IF NOT EXISTS guest_book_entries (text TEXT)", | ||
| (), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // let's attach another encrypted database and print its contents | ||
| conn.execute("ATTACH DATABASE 'world.db' AS world KEY s3cR3t", ()) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let mut attached_results = conn | ||
| .query("SELECT * FROM world.guest_book_entries", ()) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| println!("attached database guest book entries:"); | ||
| while let Some(row) = attached_results.next().await.unwrap() { | ||
| let text: String = row.get(0).unwrap(); | ||
| println!(" {}", text); | ||
| } | ||
|
|
||
| let mut input = String::new(); | ||
| println!("Please write your entry to the guestbook:"); | ||
| match std::io::stdin().read_line(&mut input) { | ||
| Ok(_) => { | ||
| println!("You entered: {}", input); | ||
| let params = params![input.as_str()]; | ||
| conn.execute("INSERT INTO guest_book_entries (text) VALUES (?)", params) | ||
| .await | ||
| .unwrap(); | ||
| } | ||
| Err(error) => { | ||
| eprintln!("Error reading input: {}", error); | ||
| } | ||
| } | ||
| let mut results = conn | ||
| .query("SELECT * FROM guest_book_entries", ()) | ||
| .await | ||
| .unwrap(); | ||
| println!("Guest book entries:"); | ||
| while let Some(row) = results.next().await.unwrap() { | ||
| let text: String = row.get(0).unwrap(); | ||
| println!(" {}", text); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| use libsql::{params, Builder}; | ||
| use libsql_sys::{Cipher, EncryptionConfig}; | ||
|
|
||
| #[tokio::test] | ||
| #[cfg(feature = "encryption")] | ||
| async fn test_encryption() { | ||
| let tempdir = std::env::temp_dir(); | ||
| let encrypted_path = tempdir.join("encrypted.db"); | ||
| let base_path = tempdir.join("base.db"); | ||
|
|
||
| // lets create an encrypted database | ||
| { | ||
| let mut db_builder = Builder::new_local(&encrypted_path); | ||
| db_builder = db_builder.encryption_config(EncryptionConfig { | ||
| cipher: Cipher::Aes256Cbc, | ||
| encryption_key: "s3cR3t".into(), | ||
| }); | ||
| let db = db_builder.build().await.unwrap(); | ||
|
|
||
| let conn = db.connect().unwrap(); | ||
| conn.execute("CREATE TABLE IF NOT EXISTS messages (text TEXT)", ()) | ||
| .await | ||
| .unwrap(); | ||
| let params = params!["the only winning move is not to play"]; | ||
| conn.execute("INSERT INTO messages (text) VALUES (?)", params) | ||
| .await | ||
| .unwrap(); | ||
| } | ||
|
|
||
| // lets test encryption with ATTACH | ||
| { | ||
| let db = Builder::new_local(&base_path).build().await.unwrap(); | ||
| let conn = db.connect().unwrap(); | ||
| let attach_stmt = format!( | ||
| "ATTACH DATABASE '{}' AS encrypted KEY 's3cR3t'", | ||
| tempdir.join("encrypted.db").display() | ||
| ); | ||
| conn.execute(&attach_stmt, ()).await.unwrap(); | ||
| let mut attached_results = conn | ||
| .query("SELECT * FROM encrypted.messages", ()) | ||
| .await | ||
| .unwrap(); | ||
| let row = attached_results.next().await.unwrap().unwrap(); | ||
| let text: String = row.get(0).unwrap(); | ||
| assert_eq!(text, "the only winning move is not to play"); | ||
| } | ||
|
|
||
| { | ||
| let _ = std::fs::remove_file(&encrypted_path); | ||
| let _ = std::fs::remove_file(&base_path); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.