Switch the license server to use tracing rather than the old log system.

This commit is contained in:
Herbert Wolverson
2023-08-12 15:30:50 +00:00
parent b177a93177
commit edabebf552
4 changed files with 49 additions and 28 deletions

4
src/rust/Cargo.lock generated
View File

@@ -1875,13 +1875,13 @@ name = "license_server"
version = "0.1.0"
dependencies = [
"anyhow",
"env_logger",
"log",
"lts_client",
"once_cell",
"pgdb",
"serde",
"tokio",
"tracing",
"tracing-subscriber 0.3.17",
]
[[package]]

View File

@@ -6,8 +6,8 @@ edition = "2021"
[dependencies]
tokio = { version = "1.25.0", features = ["full"] }
anyhow = "1"
env_logger = "0"
log = "0"
tracing = "0.1"
tracing-subscriber = { version = "0.3" }
serde = { version = "1.0", features = ["derive"] }
lts_client = { path = "../lts_client" }
pgdb = { path = "../pgdb" }

View File

@@ -1,13 +1,34 @@
use tracing_subscriber::fmt::format::FmtSpan;
mod server;
mod pki;
fn set_console_logging() -> anyhow::Result<()> {
// install global collector configured based on RUST_LOG env var.
let subscriber = tracing_subscriber::fmt()
// Use a more compact, abbreviated log format
.compact()
// Display source code file paths
.with_file(true)
// Display source code line numbers
.with_line_number(true)
// Display the thread ID an event was recorded on
.with_thread_ids(true)
// Don't display the event's target (module path)
.with_target(false)
// Include per-span timings
.with_span_events(FmtSpan::CLOSE)
// Build the subscriber
.finish();
// Set the subscriber as the default
tracing::subscriber::set_global_default(subscriber)?;
Ok(())
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Start the logger
env_logger::init_from_env(
env_logger::Env::default()
.filter_or(env_logger::DEFAULT_FILTER_ENV, "warn"),
);
set_console_logging().unwrap();
let _ = server::start().await;
Ok(())

View File

@@ -10,37 +10,37 @@ use crate::pki::LIBREQOS_KEYPAIR;
pub async fn start() -> anyhow::Result<()> {
let listener = TcpListener::bind(":::9126").await?;
log::info!("Listening on :::9126");
tracing::info!("Listening on :::9126");
let pool = pgdb::get_connection_pool(10).await;
if pool.is_err() {
log::error!("Unable to connect to the database");
log::error!("{pool:?}");
tracing::error!("Unable to connect to the database");
tracing::error!("{pool:?}");
return Err(anyhow::Error::msg("Unable to connect to the database"));
}
let pool = pool.unwrap();
loop {
let (mut socket, address) = listener.accept().await?;
log::info!("Connection from {address:?}");
tracing::info!("Connection from {address:?}");
let pool = pool.clone();
spawn(async move {
let mut buf = vec![0u8; 10240];
if let Ok(bytes) = socket.read(&mut buf).await {
log::info!("Received {bytes} bytes from {address:?}");
tracing::info!("Received {bytes} bytes from {address:?}");
match decode(&buf, address, pool).await {
Err(e) => log::error!("{e:?}"),
Err(e) => tracing::error!("{e:?}"),
Ok(reply) => {
let bytes = build_reply(&reply);
match bytes {
Ok(bytes) => {
log::info!("Submitting {} bytes to network", bytes.len());
tracing::info!("Submitting {} bytes to network", bytes.len());
if let Err(e) = socket.write_all(&bytes).await {
log::error!("Write error: {e:?}");
tracing::error!("Write error: {e:?}");
}
}
Err(e) => {
log::error!("{e:?}");
tracing::error!("{e:?}");
}
}
}
@@ -60,7 +60,7 @@ async fn decode(
let version = u16::from_be_bytes(*version_buf);
let size_buf = &buf[2..2 + U64SIZE].try_into()?;
let size = u64::from_be_bytes(*size_buf);
log::info!("Received a version {version} payload of serialized size {size} from {address:?}");
tracing::info!("Received a version {version} payload of serialized size {size} from {address:?}");
match version {
1 => {
@@ -71,7 +71,7 @@ async fn decode(
Ok(license)
}
_ => {
log::error!("Unknown version of statistics: {version}, dumped {size} bytes");
tracing::error!("Unknown version of statistics: {version}, dumped {size} bytes");
Err(anyhow::Error::msg("Version error"))
}
}
@@ -84,9 +84,9 @@ async fn check_license(
) -> anyhow::Result<LicenseReply> {
match request {
LicenseRequest::LicenseCheck { key } => {
log::info!("Checking license from {address:?}, key: {key}");
tracing::info!("Checking license from {address:?}, key: {key}");
if key == "test" {
log::info!("License is valid");
tracing::info!("License is valid");
Ok(LicenseReply::Valid {
expiry: 0, // Temporary value
stats_host: "127.0.0.1:9127".to_string(), // Also temporary
@@ -94,23 +94,23 @@ async fn check_license(
} else {
match pgdb::get_stats_host_for_key(pool, key).await {
Ok(host) => {
log::info!("License is valid");
tracing::info!("License is valid");
return Ok(LicenseReply::Valid {
expiry: 0, // Temporary value
stats_host: host,
});
}
Err(e) => {
log::warn!("Unable to get stats host for key: {e:?}");
tracing::warn!("Unable to get stats host for key: {e:?}");
}
}
log::info!("License is denied");
tracing::info!("License is denied");
Ok(LicenseReply::Denied)
}
}
LicenseRequest::KeyExchange { node_id, node_name, license_key, public_key } => {
log::info!("Public key exchange requested by {node_id}");
tracing::info!("Public key exchange requested by {node_id}");
// Check if the node_id / license key combination exists
// If it does, update it to the current last-seen and the new public key
@@ -118,7 +118,7 @@ async fn check_license(
let public_key = lts_client::cbor::to_vec(&public_key).unwrap();
let result = pgdb::insert_or_update_node_public_key(pool, node_id, node_name, license_key, &public_key).await;
if result.is_err() {
log::warn!("Unable to insert or update node public key: {result:?}");
tracing::warn!("Unable to insert or update node public key: {result:?}");
return Err(anyhow::Error::msg("Unable to insert or update node public key"));
}
@@ -132,8 +132,8 @@ fn build_reply(reply: &LicenseReply) -> anyhow::Result<Vec<u8>> {
let mut result = Vec::new();
let payload = lts_client::cbor::to_vec(reply);
if let Err(e) = payload {
log::warn!("Unable to serialize statistics. Not sending them.");
log::warn!("{e:?}");
tracing::warn!("Unable to serialize statistics. Not sending them.");
tracing::warn!("{e:?}");
return Err(anyhow::Error::msg("Unable to serialize"));
}
let payload = payload.unwrap();