From 8d2d472d1f3291e6b3a327d86c036d1de933fc0e Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Mon, 25 Nov 2024 16:23:52 +0200 Subject: [PATCH] Derive vm_name frm TLS name at registration Signed-off-by: Alexander V. Nikolaev --- src/admin/entry.rs | 10 ++++++---- src/admin/server.rs | 10 ++++++++-- src/utils/x509.rs | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/admin/entry.rs b/src/admin/entry.rs index c066dc9..e369624 100644 --- a/src/admin/entry.rs +++ b/src/admin/entry.rs @@ -92,9 +92,11 @@ impl RegistryEntry { } } -impl TryFrom for RegistryEntry { - type Error = anyhow::Error; - fn try_from(req: pb::RegistryRequest) -> Result { +impl RegistryEntry { + pub fn try_from_request( + req: pb::RegistryRequest, + vm_name: String, + ) -> Result { let ty = UnitType::try_from(req.r#type)?; let status = req .state @@ -114,7 +116,7 @@ impl TryFrom for RegistryEntry { r#type: ty, placement: Placement::Endpoint { endpoint, - vm: "bogus".into(), + vm: vm_name, }, }) } diff --git a/src/admin/server.rs b/src/admin/server.rs index b442a7c..0c1d49c 100644 --- a/src/admin/server.rs +++ b/src/admin/server.rs @@ -18,6 +18,7 @@ use crate::systemd_api::client::SystemDClient; use crate::types::*; use crate::utils::naming::*; use crate::utils::tonic::*; +use crate::utils::x509::SecurityInfo; use givc_client::endpoint::{EndpointConfig, TlsConfig}; use givc_common::query::*; @@ -360,10 +361,15 @@ impl pb::admin_service_server::AdminService for AdminService { &self, request: tonic::Request, ) -> std::result::Result, tonic::Status> { - let req = request.into_inner(); + let vm_name = request + .extensions() + .get::() + .map(move |si| si.hostname().unwrap_or("bogus, no hostname in cert".into())) + .unwrap_or("bogus: no TLS".into()); + let req = request.into_inner(); info!("Registering service {:?}", req); - let entry = RegistryEntry::try_from(req) + let entry = RegistryEntry::try_from_request(req, vm_name) .map_err(|e| Status::new(Code::InvalidArgument, format!("{e}")))?; let mut notify = None; diff --git a/src/utils/x509.rs b/src/utils/x509.rs index cb18c71..c6be6d7 100644 --- a/src/utils/x509.rs +++ b/src/utils/x509.rs @@ -33,8 +33,8 @@ impl SecurityInfo { !self.enabled || self.dns_names.iter().any(|hn| hostname == hn) } - pub fn hostname(self) -> Option { - self.dns_names.into_iter().next() + pub fn hostname(&self) -> Option { + self.dns_names.iter().next().map(ToOwned::to_owned) } }