Skip to content

Commit

Permalink
Some minimal security
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBLCKLPTN committed Oct 24, 2024
1 parent 47f5728 commit 4a48290
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Plugins/Alice-Database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ chrono = "0.4.38"
env_logger = "0.11.5"
log = "0.4.22"
prost = "0.13.3"
rand = "0.8.5"
ring = "0.17.8"
serde = { version = "1.0.213", features = ["derive"] }
serde_json = "1.0.132"
simplelog = "0.12.2"
Expand Down
26 changes: 26 additions & 0 deletions Plugins/Alice-Database/proto/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message Instance {
message CreateInstanceRequest {
string engine_type = 1;
string root_path = 2;
Token token = 3;
}

message CreateInstanceResponse {
Expand All @@ -19,6 +20,7 @@ message CreateInstanceResponse {

message GetInstanceRequest {
string instance_name = 1;
Token token = 2;
}

message GetInstanceResponse {
Expand All @@ -27,14 +29,38 @@ message GetInstanceResponse {

message GetAllInstancesRequest {
string message = 1;
Token token = 2;
}

message GetAllInstancesResponse {
repeated Instance instances = 1;
}

message SignInRequest {
string secret_key = 1;
string app_name = 2;
}

message SingInResponse {
string access = 1;
}

message SignUpRequest {
string app_name = 1;
}

message SignUpResponse {
string secret_key = 1;
}

message Token {
string secret_key = 1;
}

service InstanceService {
rpc CreateInstance(CreateInstanceRequest) returns (CreateInstanceResponse);
rpc GetInstance(GetInstanceRequest) returns (GetInstanceResponse);
rpc GetAllInstances(GetAllInstancesRequest) returns (GetAllInstancesResponse);
//rpc SignIn(SignInRequest) returns (SingInResponse);
rpc SignUp(SignUpRequest) returns (SignUpResponse);
}
28 changes: 23 additions & 5 deletions Plugins/Alice-Database/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::Engines;
use uuid::Uuid;
use std::path::PathBuf;
use crate::JSONEngine;
use std::collections::HashMap;
use ring::{rand::{SecureRandom, SystemRandom}, hmac};
use rand::rngs::OsRng;


#[derive(Debug, Clone)]
pub struct Instance {
Expand All @@ -12,22 +16,26 @@ pub struct Instance {
#[derive(Debug, Clone, Default)]
pub struct InstanceManager {
pub name: String,
pub instances: Vec<Instance>
pub instances: Vec<Instance>,
pub root_path: PathBuf,
pub authenticated_apps: HashMap<String, String>,
}


impl InstanceManager {
pub fn new() -> Self {
pub fn new(root_path: &PathBuf) -> Self {
let name = Uuid::new_v4().to_string();

let mut instances: Vec<Instance> = vec![];
Self {name, instances}
let mut authenticated_apps: HashMap<String, String> = HashMap::new();
Self {name, instances, root_path: root_path.to_owned(), authenticated_apps}
}

pub fn create_instance(&mut self, engine_type: &str, root_path: &PathBuf) -> String {
pub fn create_instance(&mut self, engine_type: &str) -> String {
let instance_name: String = Uuid::new_v4().to_string();

let mut engine = match engine_type {
"json_engine" => Engines::JSONEngine(JSONEngine::new(&root_path)),
"json_engine" => Engines::JSONEngine(JSONEngine::new(&self.root_path)),
_ => panic!("Engine not found"),
};
let mut instance = Instance {engine, name: instance_name.clone()};
Expand All @@ -49,4 +57,14 @@ impl InstanceManager {
}
None
}

pub fn sign_up(&mut self, app_name: String) -> String {
let key = Uuid::new_v4().to_string();
&self.authenticated_apps.insert(app_name, key.clone());
return key;
}

pub fn get_all_apps(&self) {
println!("{:#?}", self.authenticated_apps);
}
}
1 change: 1 addition & 0 deletions Plugins/Alice-Database/src/json_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl JSONEngine {
JSONEngine { collections }
}


/// Retrieve a mutable reference to a collection by its name.
///
/// # Parameters
Expand Down
43 changes: 36 additions & 7 deletions Plugins/Alice-Database/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ use std::sync::{Arc, Mutex};
pub mod instance_g {
tonic::include_proto!("instance");
}
pub use instance_g::{CreateInstanceRequest, CreateInstanceResponse,
GetInstanceRequest, GetInstanceResponse, GetAllInstancesRequest, GetAllInstancesResponse};
pub use instance_g::{
CreateInstanceRequest, CreateInstanceResponse,
GetInstanceRequest, GetInstanceResponse,
GetAllInstancesRequest, GetAllInstancesResponse,
//SignInRequest, SignInResponse,
SignUpRequest, SignUpResponse,
Token,

};

use crate::instance_g::instance_service_server::InstanceServiceServer;
use crate::instance_g::instance_service_server::InstanceService;
Expand Down Expand Up @@ -102,10 +109,9 @@ impl InstanceService for MyInstanceManager {
request: Request<CreateInstanceRequest>,
) -> Result<Response<CreateInstanceResponse>, Status> {
let inner = request.into_inner();
let engine_type = inner.engine_type;
let root_path = PathBuf::from(inner.root_path); // assuming root_path is a string path
let engine_type = inner.engine_type; // assuming root_path is a string path
let mut manager = self.instances.lock().unwrap();
let id = manager.create_instance(&engine_type,&root_path);
let id = manager.create_instance(&engine_type);

let response = CreateInstanceResponse { instance: id };
Ok(Response::new(response))
Expand Down Expand Up @@ -142,20 +148,43 @@ impl InstanceService for MyInstanceManager {
Ok(Response::new(response))

}

async fn sign_up(
&self,
request: Request<SignUpRequest>,
) -> Result<Response<SignUpResponse>, Status> {
let inner = request.into_inner();
let mut manager = self.instances.lock().unwrap();
let mut key: String = String::new();
if !manager.authenticated_apps.contains_key(&inner.app_name) {
key = manager.sign_up(inner.app_name);
} else {
key = "whoops...".to_string();
}
let response = SignUpResponse { secret_key: key };

manager.get_all_apps();
Ok(Response::new(response))
}
}


// Main function to start the gRPC server
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
print_ascii();
let root_path = match prepare() {
Ok(k) => k,
_ => panic!("Errors in prepare function.")
};
let instance_manager = MyInstanceManager {
instances: Arc::new(Mutex::new(InstanceManager::new())),
instances: Arc::new(Mutex::new(InstanceManager::new(&root_path))),
};

println!("Starting gRPC server...");
Server::builder()
.add_service(InstanceServiceServer::new(instance_manager))
.serve("[::1]:50051".parse()?)
.serve("[::1]:50052".parse()?)
.await?;

Ok(())
Expand Down

0 comments on commit 4a48290

Please sign in to comment.