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

Adjust log levels and add an option to truncate identifiers #37

Merged
merged 4 commits into from
Mar 1, 2024
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
4 changes: 2 additions & 2 deletions src/communicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ impl Communicator {
self.output.clear();
let indices = self.get_protocol_indices();

for i in 0..self.threshold as usize {
self.output.push(f(indices[i]));
for idx in indices {
self.output.push(f(idx));
}

self.clear_input();
Expand Down
39 changes: 21 additions & 18 deletions src/interfaces/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::proto::mpc_server::{Mpc, MpcServer};
use crate::proto::{KeyType, ProtocolType};
use crate::state::State;
use crate::tasks::{Task, TaskStatus};
use crate::{proto as msg, CA_CERT, CA_KEY};
use crate::{proto as msg, utils, CA_CERT, CA_KEY};

use std::pin::Pin;

Expand Down Expand Up @@ -88,7 +88,7 @@ impl Mpc for MPCService {
let group_id = request.group_id;
let name = request.name;
let data = request.data;
info!("SignRequest group_id={}", hex::encode(&group_id));
info!("SignRequest group_id={}", utils::hextrunc(&group_id));

let mut state = self.state.lock().await;
if let Some(task_id) = state.add_sign_task(&group_id, &name, &data) {
Expand All @@ -108,7 +108,7 @@ impl Mpc for MPCService {
let name = request.name;
let data = request.data;
let data_type = request.data_type;
info!("DecryptRequest group_id={}", hex::encode(&group_id));
info!("DecryptRequest group_id={}", utils::hextrunc(&group_id));

let mut state = self.state.lock().await;
if let Some(task_id) = state.add_decrypt_task(&group_id, &name, &data, &data_type) {
Expand All @@ -133,8 +133,8 @@ impl Mpc for MPCService {
};
debug!(
"TaskRequest task_id={} device_id={}",
hex::encode(task_id),
hex::encode(device_id.unwrap_or(&[]))
utils::hextrunc(task_id.as_bytes()),
utils::hextrunc(device_id.unwrap_or(&[]))
);

let state = self.state.lock().await;
Expand Down Expand Up @@ -164,10 +164,10 @@ impl Mpc for MPCService {
let task_id = Uuid::from_slice(&request.task).unwrap();
let data = request.data;
let attempt = request.attempt;
info!(
debug!(
"TaskUpdate task_id={} device_id={} attempt={}",
hex::encode(task_id),
hex::encode(&device_id),
utils::hextrunc(task_id.as_bytes()),
utils::hextrunc(&device_id),
attempt
);

Expand All @@ -191,7 +191,7 @@ impl Mpc for MPCService {
let device_id = request.device_id;
let device_str = device_id
.as_ref()
.map(hex::encode)
.map(utils::hextrunc)
.unwrap_or_else(|| "unknown".to_string());
debug!("TasksRequest device_id={}", device_str);

Expand Down Expand Up @@ -222,7 +222,7 @@ impl Mpc for MPCService {
let device_id = request.device_id;
let device_str = device_id
.as_ref()
.map(hex::encode)
.map(utils::hextrunc)
.unwrap_or_else(|| "unknown".to_string());
debug!("GroupsRequest device_id={}", device_str);

Expand Down Expand Up @@ -259,7 +259,10 @@ impl Mpc for MPCService {
info!(
"GroupRequest name={:?} device_ids={:?} threshold={}",
&name,
device_ids.iter().map(hex::encode).collect::<Vec<String>>(),
device_ids
.iter()
.map(utils::hextrunc)
.collect::<Vec<String>>(),
threshold
);

Expand Down Expand Up @@ -300,10 +303,10 @@ impl Mpc for MPCService {

let device_str = device_id
.as_ref()
.map(hex::encode)
.map(utils::hextrunc)
.unwrap_or_else(|| "unknown".to_string());
let message = request.into_inner().message;
info!("LogRequest device_id={} message={}", device_str, message);
debug!("LogRequest device_id={} message={}", device_str, message);

if device_id.is_some() {
self.state
Expand Down Expand Up @@ -335,8 +338,8 @@ impl Mpc for MPCService {

info!(
"TaskDecision task_id={} device_id={} accept={}",
hex::encode(task_id),
hex::encode(&device_id),
utils::hextrunc(task_id.as_bytes()),
utils::hextrunc(&device_id),
accept
);

Expand Down Expand Up @@ -366,10 +369,10 @@ impl Mpc for MPCService {

let task_id = request.into_inner().task_id;

info!(
debug!(
"TaskAcknowledgement task_id={} device_id={}",
hex::encode(&task_id),
hex::encode(&device_id)
utils::hextrunc(&task_id),
utils::hextrunc(&device_id)
);

let mut state = self.state.lock().await;
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces/timer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::get_timestamp;
use crate::state::State;
use crate::tasks::TaskStatus;
use crate::{get_timestamp, utils};

use log::info;
use log::debug;
use tokio::sync::MutexGuard;
use tokio::{sync::Mutex, time};
use tonic::codegen::Arc;
Expand All @@ -25,7 +25,7 @@ fn check_tasks(state: &mut MutexGuard<State>) {
&& task.is_approved()
&& timestamp - task.last_update() > 30
{
info!("Stale task detected task_id={:?}", hex::encode(task_id));
debug!("Stale task detected task_id={:?}", utils::hextrunc(task_id));
restarts.push(*task_id);
}
}
Expand All @@ -38,9 +38,9 @@ fn check_subscribers(state: &mut MutexGuard<State>) {
let mut remove = Vec::new();
for (device_id, tx) in state.get_subscribers() {
if tx.is_closed() {
info!(
debug!(
"Closed channel detected device_id={:?}",
hex::encode(device_id)
utils::hextrunc(device_id)
);
remove.push(device_id.clone());
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod interfaces;
mod protocols;
mod state;
mod tasks;
mod utils;

mod proto {
#![allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -53,7 +54,7 @@ const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
pub(self) struct Args {
struct Args {
#[clap(short, long, default_value_t = 1337)]
port: u16,

Expand Down
47 changes: 31 additions & 16 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use log::{error, warn};
use log::{debug, error, warn};
use uuid::Uuid;

use crate::device::Device;
Expand All @@ -12,7 +12,7 @@ use crate::tasks::group::GroupTask;
use crate::tasks::sign::SignTask;
use crate::tasks::sign_pdf::SignPDFTask;
use crate::tasks::{Task, TaskResult, TaskStatus};
use log::info;
use crate::utils;
use tokio::sync::mpsc::Sender;
use tonic::codegen::Arc;
use tonic::Status;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl State {
if self.devices.contains_key(identifier) {
warn!(
"Device identifier already registered {}",
hex::encode(identifier)
utils::hextrunc(identifier)
);
return false;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl State {
let mut device_list = Vec::new();
for device in devices {
if !self.devices.contains_key(device.as_slice()) {
warn!("Unknown Device ID {}", hex::encode(device));
warn!("Unknown Device ID {}", utils::hextrunc(device));
return None;
}
device_list.push(self.devices.get(device.as_slice()).unwrap().clone());
Expand All @@ -99,7 +99,7 @@ impl State {
if group.is_none() {
warn!(
"Signing requested from an unknown group group_id={}",
hex::encode(group_id)
utils::hextrunc(group_id)
);
return None;
}
Expand All @@ -118,7 +118,7 @@ impl State {
KeyType::Decrypt => {
warn!(
"Signing request made for decryption group group_id={}",
hex::encode(group_id)
utils::hextrunc(group_id)
);
return None;
}
Expand All @@ -142,7 +142,7 @@ impl State {
if group.is_none() {
warn!(
"Decryption requested from an unknown group group_id={}",
hex::encode(group_id)
utils::hextrunc(group_id)
);
return None;
}
Expand All @@ -158,7 +158,7 @@ impl State {
KeyType::SignPdf | KeyType::SignChallenge => {
warn!(
"Decryption request made for a signing group group_id={}",
hex::encode(group_id)
utils::hextrunc(group_id)
);
return None;
}
Expand Down Expand Up @@ -225,8 +225,8 @@ impl State {
if attempt != task.get_attempts() {
warn!(
"Stale update discarded task_id={} device_id={} attempt={}",
hex::encode(task_id),
hex::encode(device),
utils::hextrunc(task_id.as_bytes()),
utils::hextrunc(device),
attempt
);
return Err("Stale update".to_string());
Expand All @@ -251,8 +251,20 @@ impl State {
let change = task.decide(device, decision);
if change.is_some() {
self.send_updates(task_id);
if change.unwrap() {
log::info!(
"Task approved task_id={}",
utils::hextrunc(task_id.as_bytes())
);
} else {
log::info!(
"Task declined task_id={}",
utils::hextrunc(task_id.as_bytes())
);
}
return true;
}
change.is_some()
false
}

pub fn acknowledge_task(&mut self, task: &Uuid, device: &[u8]) {
Expand All @@ -268,7 +280,7 @@ impl State {
if let Some(device) = self.devices.get(device_id) {
device.activated();
} else {
error!("Unknown Device ID {}", hex::encode(device_id));
error!("Unknown Device ID {}", utils::hextrunc(device_id));
}
}

Expand Down Expand Up @@ -296,7 +308,10 @@ impl State {

pub fn remove_subscriber(&mut self, device_id: &Vec<u8>) {
self.subscribers.remove(device_id);
info!("Removing subscriber device_id={:?}", hex::encode(device_id));
debug!(
"Removing subscriber device_id={}",
utils::hextrunc(device_id)
);
}

pub fn get_subscribers(&self) -> &HashMap<Vec<u8>, Sender<Result<crate::proto::Task, Status>>> {
Expand All @@ -312,9 +327,9 @@ impl State {
let result = tx.try_send(Ok(format_task(task_id, task, Some(device_id), None)));

if result.is_err() {
info!(
"Closed channel detected device_id={}",
hex::encode(device_id)
debug!(
"Closed channel detected device_id={}",
utils::hextrunc(&device_id[..4])
);
remove.push(device_id.to_vec());
}
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::communicator::Communicator;
use crate::device::Device;
use crate::get_timestamp;
use crate::group::Group;
use crate::proto::{DecryptRequest, ProtocolType, TaskType};
use crate::protocols::elgamal::ElgamalDecrypt;
use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::info;
use meesign_crypto::proto::{Message as _, ProtocolMessage};
use prost::Message as _;
Expand Down Expand Up @@ -68,7 +68,7 @@ impl DecryptTask {

info!(
"Data decrypted by group_id={}",
hex::encode(self.group.identifier())
utils::hextrunc(self.group.identifier())
);

self.result = Some(Ok(decrypted));
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/group.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::communicator::Communicator;
use crate::device::Device;
use crate::get_timestamp;
use crate::group::Group;
use crate::proto::{KeyType, ProtocolType, TaskType};
use crate::protocols::elgamal::ElgamalGroup;
use crate::protocols::frost::FROSTGroup;
use crate::protocols::gg18::GG18Group;
use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::{info, warn};
use meesign_crypto::proto::{Message as _, ProtocolMessage};
use prost::Message as _;
Expand Down Expand Up @@ -120,10 +120,10 @@ impl GroupTask {

info!(
"Group established group_id={} devices={:?}",
hex::encode(&identifier),
utils::hextrunc(&identifier),
self.devices
.iter()
.map(|device| hex::encode(device.identifier()))
.map(|device| utils::hextrunc(device.identifier()))
.collect::<Vec<_>>()
);

Expand Down
4 changes: 2 additions & 2 deletions src/tasks/sign.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::communicator::Communicator;
use crate::device::Device;
use crate::get_timestamp;
use crate::group::Group;
use crate::proto::{ProtocolType, SignRequest, TaskType};
use crate::protocols::frost::FROSTSign;
use crate::protocols::gg18::GG18Sign;
use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::{info, warn};
use meesign_crypto::proto::{Message as _, ProtocolMessage};
use prost::Message as _;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl SignTask {

info!(
"Signature created by group_id={}",
hex::encode(self.group.identifier())
utils::hextrunc(self.group.identifier())
);

self.result = Some(Ok(signature));
Expand Down
Loading
Loading