Skip to content
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

add dependency import rules and format code #260

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions craft/src/vault/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use clap::{arg, Args, Subcommand};

use super::{
use crate::vault::{
crypt::{decrypt_blob, encrypt_blob, generate_key_full},
init_rv_core,
pgp_key::{delete_key, list_keys},
Expand Down Expand Up @@ -42,6 +42,13 @@ enum VaultMode {
},
}

/// Handles different modes for interacting with the Rusty Vault.
///
/// It initializes the Rusty Vault Core and performs operations based on the specified mode.
///
/// # Arguments
///
/// * `args` - A VaultArgs enum representing different modes of operation.
pub fn handle(args: VaultArgs) {
let (core, token) = init_rv_core();
// Match the mode with different functions
Expand Down Expand Up @@ -79,7 +86,7 @@ pub fn handle(args: VaultArgs) {
#[cfg(test)]
mod tests {

use std::sync::{RwLock, Arc};
use std::sync::{Arc, RwLock};

use rusty_vault::core::Core;

Expand All @@ -91,14 +98,14 @@ mod tests {

// Define a test function for generate-key-full mode
// #[test]
fn test_generate_key_full(core: Arc<RwLock<Core>>, token : &str) {
fn test_generate_key_full(core: Arc<RwLock<Core>>, token: &str) {
// generate a full key
let _ = generate_key_full("Craft <[email protected]>", "secret/craft", core, token);
}

// Define a test function for encrypt mode
// #[test]
fn test_encrypt(core: Arc<RwLock<Core>>, token : &str) {
fn test_encrypt(core: Arc<RwLock<Core>>, token: &str) {
// generate key to crypt
let _ = generate_key_full("User2 <[email protected]>", "secret/sci", core, token).unwrap();
// Create and run a new process to execute the encrypt_blob function
Expand Down Expand Up @@ -131,7 +138,7 @@ mod tests {

// Define a test function for decrypt mode
// #[test]
fn test_decrypt(core: Arc<RwLock<Core>>, token : &str) {
fn test_decrypt(core: Arc<RwLock<Core>>, token: &str) {
// Generate a key pair for testing
let _ = generate_key_full(
"User3 <[email protected]>",
Expand Down Expand Up @@ -205,16 +212,21 @@ mod tests {

// Define a test function for list-keys mode
// #[test]
fn test_list_keys(core: Arc<RwLock<Core>>, token : &str) {
fn test_list_keys(core: Arc<RwLock<Core>>, token: &str) {
let actual = list_keys("secret/", core, token).unwrap();
assert!(!actual.is_empty());
// Check if the output contains the expected key information
}

// Define a test function for delete-key mode
// #[test]
fn test_delete_key(core: Arc<RwLock<Core>>, token : &str) {
let _ = generate_key_full("Delete <[email protected]>", "secret/delete", core.clone(), token);
fn test_delete_key(core: Arc<RwLock<Core>>, token: &str) {
let _ = generate_key_full(
"Delete <[email protected]>",
"secret/delete",
core.clone(),
token,
);
let _ = delete_key("secret/delete", core.clone(), token);
}

Expand Down
8 changes: 4 additions & 4 deletions craft/src/vault/crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use std::{
sync::{Arc, RwLock},
};

use anyhow::Ok;
use pgp_key::{decrypt_message, encrypt_message, generate_key_pair};
use pgp_key::{
decrypt_message, delete_key, encrypt_message, generate_key_pair, list_keys, KeyPair,
};
use rusty_vault::{
core::Core,
logical::{Operation, Request},
};
use serde_json::json;

use crate::vault::pgp_key::{self, delete_key, list_keys, KeyPair};
use crate::vault::pgp_key;

// the trait and impl for KeyPair is a preparation for crate Tongsuo.
// a trait for key
Expand Down Expand Up @@ -58,7 +59,6 @@ impl Key for KeyPair {
}
}


// Generate full key with pubkey, seckey, primary id.
// Arguments: primary_id, as &str, it should be written as "User <[email protected]>"; key_name, git-craft will keep ur key file as key_namepub.asc
pub fn generate_key_full(
Expand Down
19 changes: 14 additions & 5 deletions craft/src/vault/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
pub mod command;
pub mod crypt;
pub mod pgp_key;

use std::{
fs,
path::Path,
Expand All @@ -15,8 +11,22 @@ use rusty_vault::{
storage::{barrier_aes_gcm, physical},
};

pub mod command;
pub mod crypt;
pub mod pgp_key;

pub const WORK_DIR_PATH_DEFAULT: &str = "/tmp/.mega/rusty_vault";

/// Initializes the Rusty Vault Core.
///
/// This function prepares the necessary configuration and initializes the Rusty Vault Core
/// based on the provided configuration or creates a default configuration if none exists.
/// It sets up storage, backend, and initializes encryption barriers required by the core.
/// If already initialized, it retrieves the root token and secret shares; otherwise, it initializes
/// the core, generates secret shares, and saves them securely.
///
/// # Returns
/// Returns a tuple containing an Arc of the RwLock guarding the initialized Core and the root token.
pub fn init_rv_core() -> (Arc<RwLock<Core>>, String) {
let path = Path::new(WORK_DIR_PATH_DEFAULT);
let config_path = path.join("config.hcl");
Expand Down Expand Up @@ -100,4 +110,3 @@ pub fn init_rv_core() -> (Arc<RwLock<Core>>, String) {
}
(Arc::clone(&c), token)
}

46 changes: 20 additions & 26 deletions craft/src/vault/pgp_key.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use anyhow::{Context, Result};
//!
//!
//!
//!
//!
use std::{
io::Cursor,
sync::{Arc, RwLock},
};

use anyhow::{Context, Result};
use pgp::{
composed,
composed::signed_key::*,
crypto::{self, sym::SymmetricKeyAlgorithm},
types::SecretKeyTrait,
Deserializable, Message,
crypto::sym::SymmetricKeyAlgorithm, types::SecretKeyTrait, Deserializable, KeyType, Message,
SecretKeyParamsBuilder, SignedPublicKey, SignedSecretKey,
};
use rand::prelude::*;
use rusty_vault::{
core::Core,
logical::{Operation, Request},
};
use smallvec::*;
use std::{
io::Cursor,
sync::{Arc, RwLock},
};

pub struct KeyPair {
pub secret_key: pgp::SignedSecretKey,
Expand All @@ -28,13 +30,13 @@ pub struct KeyPair {
// Return: KeyPair, it has a signed secret key and a signed public key
pub fn generate_key_pair(primary_user_id: &str) -> Result<KeyPair, anyhow::Error> {
// Set key_params with primary user id, Rsa with 2048 bites, symmetric algorithms key prefer to use is AES with 256 bit
let mut key_params = composed::key::SecretKeyParamsBuilder::default();
let mut key_params = SecretKeyParamsBuilder::default();
key_params
.key_type(composed::KeyType::Rsa(2048))
.key_type(KeyType::Rsa(2048))
.can_create_certificates(false)
.can_sign(true)
.primary_user_id(primary_user_id.into())
.preferred_symmetric_algorithms(smallvec![crypto::sym::SymmetricKeyAlgorithm::AES256]);
.preferred_symmetric_algorithms(smallvec![SymmetricKeyAlgorithm::AES256]);

// build a new SecretKeyParams
let secret_key_params = key_params
Expand Down Expand Up @@ -73,15 +75,11 @@ pub fn generate_key_pair(primary_user_id: &str) -> Result<KeyPair, anyhow::Error
pub fn encrypt(msg: &str, pubkey_str: &str) -> Result<String, anyhow::Error> {
let (pubkey, _) = SignedPublicKey::from_string(pubkey_str)?;
// Requires a file name as the first arg, in this case I pass "none", as it's not used
let msg = composed::message::Message::new_literal("none", msg);
let msg = Message::new_literal("none", msg);
// Encrypt
let mut rng = StdRng::from_entropy();

let new_msg = msg.encrypt_to_keys(
&mut rng,
crypto::sym::SymmetricKeyAlgorithm::AES128,
&[&pubkey],
)?;
let new_msg = msg.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[&pubkey])?;
Ok(new_msg.to_armored_string(None)?)
}

Expand All @@ -90,8 +88,8 @@ pub fn encrypt(msg: &str, pubkey_str: &str) -> Result<String, anyhow::Error> {
pub fn decrypt(armored: &str, seckey: &SignedSecretKey) -> Result<String, anyhow::Error> {
// Get encrypted contents
let buf = Cursor::new(armored);
let (msg, _) = composed::message::Message::from_armor_single(buf)
.context("Failed to convert &str to armored message")?;
let (msg, _) =
Message::from_armor_single(buf).context("Failed to convert &str to armored message")?;
// Set a decryptor
let (decryptor, _) = msg
.decrypt(|| String::from(""), &[seckey])
Expand All @@ -110,7 +108,6 @@ pub fn decrypt(armored: &str, seckey: &SignedSecretKey) -> Result<String, anyhow

// Encrypt message from file, and write it to a MGS_FILE waiting for decrypt
// Arguments: message, read from file; public key file path
#[allow(unused)]
pub fn encrypt_message(msg: &str, pubkey: &str) -> Result<String> {
let (pubkey, _) = SignedPublicKey::from_string(pubkey)?;
// Requires a file name as the first arg, in this case I pass "none", as it's not used typically, it's just meta data
Expand All @@ -124,7 +121,6 @@ pub fn encrypt_message(msg: &str, pubkey: &str) -> Result<String> {
// Convert data from OpenPGP Message to String
// Arguments: msg, OpenPGP Message; pk, a signed public key
// Return: string
#[allow(unused)]
pub fn generate_armored_string(msg: Message, pk: SignedPublicKey) -> Result<String> {
let mut rng = StdRng::from_entropy();
// encrypt the message
Expand All @@ -135,7 +131,6 @@ pub fn generate_armored_string(msg: Message, pk: SignedPublicKey) -> Result<Stri

// Decrypt message from file
// Arguments: armored, encrypted message;v seckey_file, secret key file path
#[allow(unused)]
pub fn decrypt_message(armored: &str, seckey: &str) -> Result<String, anyhow::Error> {
let (seckey, _) = SignedSecretKey::from_string(seckey)?;
// get encrypted message
Expand All @@ -158,9 +153,8 @@ pub fn decrypt_message(armored: &str, seckey: &str) -> Result<String, anyhow::Er
}

// List keys and show their fingerprint, key id
// Argument: key_path, key file path, I use a default file path in main.rs
// Argument: key_path, key file path.
// Return: public key and its name, secret key and its name
#[allow(unused)]
pub fn list_keys(key_path: &str, core: Arc<RwLock<Core>>, token: &str) -> Result<Vec<String>> {
let core = core.read().unwrap();
let mut req = Request::new(key_path);
Expand Down
54 changes: 53 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,56 @@

## Architect

![Mega Architect](images/mega-architect.png)
![Mega Architect](images/mega-architect.png)


## Rust Dependency Import Order Guide

This guide outlines the recommended order for importing dependencies in Rust projects.

#### 1. Rust Standard Library

Import dependencies from the Rust standard library.

#### 2. Third-Party Crates
Import dependencies from third-party crates.

#### 3. Other Modules in Workspace
Import dependencies from other modules within the project workspace.

#### 4. Within Modules
Import functions and structs from within modules.


Example:
```rust

// 1. Rust Standard Library
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

// 2. Third-Party Crates
use async_trait::async_trait;
use bytes::{BufMut, Bytes, BytesMut};
use russh::server::{self, Auth, Msg, Session};
use russh::{Channel, ChannelId};
use russh_keys::key;
use tokio::io::{AsyncReadExt, BufReader};

// 3. Other Modules in Workspace
use storage::driver::database::storage::ObjectStorage;

// 4. Other Files in the Same Module
use crate::protocol::pack::{self};
use crate::protocol::ServiceType;
use crate::protocol::{PackProtocol, Protocol};
```


### Additional Notes:
- Always group imports with an empty line between different sections for better readability.
- Alphabetize imports within each section to maintain consistency.
- Avoid using extern crate syntax for Rust 2018 edition and later; prefer using use with crates.
- Do not use `super::` and `self::` in imports. It can lead to ambiguity and hinder code readability. Instead, use crate to reference the current crate's modules.
5 changes: 2 additions & 3 deletions gateway/src/api_service/obj_service.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::collections::HashMap;

use std::sync::Arc;

use axum::body::Full;
use axum::response::{IntoResponse, Json};
use axum::{http::StatusCode, response::Response};
use hyper::body::Bytes;

use storage::driver::database::storage::ObjectStorage;
use git::internal::object::commit::Commit;
use git::internal::object::tree::Tree;
use git::internal::object::ObjectT;
use hyper::body::Bytes;
use storage::driver::database::storage::ObjectStorage;

use crate::model::object_detail::{BlobObjects, Directories, Item};
use crate::model::query::DirectoryQuery;
Expand Down
12 changes: 4 additions & 8 deletions gateway/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@
//!
//!
//!

use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
use std::{net::SocketAddr, sync::Arc};

use anyhow::Result;
use axum::extract::{Query, State};
use axum::response::Response;
use axum::routing::get;
use axum::{Router, Server};

use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};

use anyhow::Result;
use clap::Args;
use hyper::{Body, Request, StatusCode, Uri};
use regex::Regex;
use serde::Deserialize;
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};

use common::enums::DataSource;
use git::lfs::lfs_structs::LockListQuery;
use git::lfs::{self, LfsConfig};
use git::protocol::{http, ServiceType};
use git::protocol::{PackProtocol, Protocol};

use storage::driver::database;
use storage::driver::database::storage::ObjectStorage;

Expand Down Expand Up @@ -266,7 +262,7 @@ mod api_routers {
},
};

use super::AppState;
use crate::AppState;

pub fn routers<S>(state: AppState) -> Router<S> {
Router::new()
Expand Down
2 changes: 1 addition & 1 deletion gateway/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod object_detail;
pub mod query;
pub mod query;
Loading