Skip to content

Commit

Permalink
Fix misleading log - refactoring
Browse files Browse the repository at this point in the history
### Desc

modify function
certificate_info_default -> subject_from_cert_opt
subject_from_cert, subject_from_cert_verbose

### Refenece

#800
  • Loading branch information
henry0715-dev authored Aug 20, 2024
1 parent 26d7ee1 commit 301611d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use x509_parser::nom::AsBytes;

use crate::publish::send_direct_stream;
use crate::server::{
certificate_info, config_server, extract_cert_from_conn, Certs, SERVER_CONNNECTION_DELAY,
SERVER_ENDPOINT_DELAY,
config_server, extract_cert_from_conn, subject_from_cert_verbose, Certs,
SERVER_CONNNECTION_DELAY, SERVER_ENDPOINT_DELAY,
};
use crate::storage::{Database, RawEventStore, StorageKey};
use crate::{
Expand Down Expand Up @@ -165,7 +165,7 @@ async fn handle_connection(
}
};

let (agent, source) = certificate_info(&extract_cert_from_conn(&connection)?)?;
let (agent, source) = subject_from_cert_verbose(&extract_cert_from_conn(&connection)?)?;
let rep = agent.contains("reproduce");

if !rep {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use tracing_subscriber::{

use crate::{
graphql::{status::TEMP_TOML_POST_FIX, NodeName},
server::{certificate_info_default, Certs, SERVER_REBOOT_DELAY},
server::{subject_from_cert, Certs, SERVER_REBOOT_DELAY},
storage::migrate_data_dir,
};

Expand Down Expand Up @@ -160,7 +160,7 @@ async fn main() -> Result<()> {
let ack_transmission_cnt = new_ack_transmission_count(settings.ack_transmission);

let schema = graphql::schema(
NodeName(certificate_info_default(&cert, false)?.1),
NodeName(subject_from_cert(&cert)?.1),
database.clone(),
pcap_sources.clone(),
ingest_sources.clone(),
Expand Down
8 changes: 4 additions & 4 deletions src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::{
TomlPeers, CONFIG_GRAPHQL_SRV_ADDR, CONFIG_PUBLISH_SRV_ADDR,
},
server::{
certificate_info, certificate_info_default, config_client, config_server,
extract_cert_from_conn, Certs, SERVER_CONNNECTION_DELAY, SERVER_ENDPOINT_DELAY,
config_client, config_server, extract_cert_from_conn, subject_from_cert,
subject_from_cert_verbose, Certs, SERVER_CONNNECTION_DELAY, SERVER_ENDPOINT_DELAY,
},
IngestSources,
};
Expand Down Expand Up @@ -111,7 +111,7 @@ pub struct Peer {

impl Peer {
pub fn new(local_address: SocketAddr, certs: &Arc<Certs>) -> Result<Self> {
let (_, local_host_name) = certificate_info_default(certs.certs.as_slice(), false)?;
let (_, local_host_name) = subject_from_cert(certs.certs.as_slice())?;

let server_config =
config_server(certs).expect("server configuration error with cert, key or root");
Expand Down Expand Up @@ -668,7 +668,7 @@ async fn check_for_duplicate_connections(
peer_conn: Arc<RwLock<HashMap<String, Connection>>>,
) -> Result<(String, String)> {
let remote_addr = connection.remote_address().ip().to_string();
let (_, remote_host_name) = certificate_info(&extract_cert_from_conn(connection)?)?;
let (_, remote_host_name) = subject_from_cert_verbose(&extract_cert_from_conn(connection)?)?;
if peer_conn.read().await.contains_key(&remote_host_name) {
connection.close(
quinn::VarInt::from_u32(0),
Expand Down
4 changes: 2 additions & 2 deletions src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::graphql::TIMESTAMP_SIZE;
use crate::ingest::{implement::EventFilter, NetworkKey};
use crate::peer::{PeerIdents, Peers};
use crate::server::{
certificate_info, config_client, config_server, extract_cert_from_conn, Certs,
config_client, config_server, extract_cert_from_conn, subject_from_cert_verbose, Certs,
SERVER_CONNNECTION_DELAY, SERVER_ENDPOINT_DELAY,
};
use crate::storage::{Database, Direction, RawEventStore, StorageKey};
Expand Down Expand Up @@ -161,7 +161,7 @@ async fn handle_connection(
bail!("{e}")
}
};
let (_, source) = certificate_info(&extract_cert_from_conn(&connection)?)?;
let (_, source) = subject_from_cert_verbose(&extract_cert_from_conn(&connection)?)?;

tokio::spawn({
let certs = certs.clone();
Expand Down
14 changes: 9 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ pub fn extract_cert_from_conn(connection: &Connection) -> Result<Vec<Certificate
Ok(cert_info)
}

pub fn certificate_info(cert_info: &[CertificateDer]) -> Result<(String, String)> {
certificate_info_default(cert_info, true)
pub fn subject_from_cert(cert_info: &[CertificateDer]) -> Result<(String, String)> {
subject_from_cert_opt(cert_info, false)
}

pub fn certificate_info_default(
pub fn subject_from_cert_verbose(cert_info: &[CertificateDer]) -> Result<(String, String)> {
subject_from_cert_opt(cert_info, true)
}

pub fn subject_from_cert_opt(
cert_info: &[CertificateDer],
print_client_name: bool,
logging: bool,
) -> Result<(String, String)> {
let Some(cert) = cert_info.first() else {
bail!("no certificate in identity");
Expand All @@ -92,7 +96,7 @@ pub fn certificate_info_default(
.and_then(|cn| cn.as_str().ok())
.context("the subject of the certificate is not valid")?;
if subject.contains('@') {
if print_client_name {
if logging {
info!("Connected client name : {subject}");
}
let parsed = subject.split('@').collect::<Vec<&str>>();
Expand Down

0 comments on commit 301611d

Please sign in to comment.