Skip to content

Commit

Permalink
Derive vm_name frm TLS name at registration
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander V. Nikolaev <[email protected]>
  • Loading branch information
avnik committed Nov 25, 2024
1 parent 25b23d3 commit 8d2d472
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/admin/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ impl RegistryEntry {
}
}

impl TryFrom<pb::RegistryRequest> for RegistryEntry {
type Error = anyhow::Error;
fn try_from(req: pb::RegistryRequest) -> Result<Self, Self::Error> {
impl RegistryEntry {
pub fn try_from_request(
req: pb::RegistryRequest,
vm_name: String,
) -> Result<Self, anyhow::Error> {
let ty = UnitType::try_from(req.r#type)?;
let status = req
.state
Expand All @@ -114,7 +116,7 @@ impl TryFrom<pb::RegistryRequest> for RegistryEntry {
r#type: ty,
placement: Placement::Endpoint {
endpoint,
vm: "bogus".into(),
vm: vm_name,
},
})
}
Expand Down
10 changes: 8 additions & 2 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -360,10 +361,15 @@ impl pb::admin_service_server::AdminService for AdminService {
&self,
request: tonic::Request<RegistryRequest>,
) -> std::result::Result<tonic::Response<pb::RegistryResponse>, tonic::Status> {
let req = request.into_inner();
let vm_name = request
.extensions()
.get::<SecurityInfo>()
.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;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl SecurityInfo {
!self.enabled || self.dns_names.iter().any(|hn| hostname == hn)
}

pub fn hostname(self) -> Option<String> {
self.dns_names.into_iter().next()
pub fn hostname(&self) -> Option<String> {
self.dns_names.iter().next().map(ToOwned::to_owned)
}
}

Expand Down

0 comments on commit 8d2d472

Please sign in to comment.