Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/api-db/src/machine_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ pub async fn find_by_mac_address(
find_by(txn, ObjectColumnFilter::One(MacAddressColumn, &macaddr)).await
}

/// This function returns only an IP for efficiency, we don't need to fetch/deserialize the entire
/// MachineInterfaceSnapshot
pub async fn lookup_bmc_ip_by_mac_address(
db: impl DbReader<'_>,
mac_address: MacAddress,
) -> DatabaseResult<Vec<IpAddr>> {
let query = r"SELECT mia.address FROM machine_interfaces mi
INNER JOIN machine_interface_addresses mia ON (mia.interface_id = mi.id)
WHERE mi.mac_address = $1";
sqlx::query_scalar(query)
.bind(mac_address)
.fetch_all(db)
.await
.map_err(|e| DatabaseError::query(query, e))
}

pub async fn find_by_ip(
txn: impl DbReader<'_>,
ip: IpAddr,
Expand Down
7 changes: 7 additions & 0 deletions crates/api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,13 @@ impl Forge for Api {
templates,
}))
}

async fn find_bmc_ips(
&self,
request: Request<::rpc::forge::FindBmcIpsRequest>,
) -> Result<Response<::rpc::forge::BmcIpList>, Status> {
crate::handlers::machine_interface::find_bmc_ips(self, request).await
}
}

fn ipxe_template_scope_to_proto(
Expand Down
1 change: 1 addition & 0 deletions crates/api/src/auth/internal_rbac_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ impl InternalRBACRules {
vec![SiteAgent, ForgeAdminCLI],
);
x.perm("FindMacAddressByBmcIp", vec![SiteAgent, BmcProxy]);
x.perm("FindBmcIps", vec![ForgeAdminCLI, BmcProxy]);
x.perm("BmcCredentialStatus", vec![ForgeAdminCLI, SiteAgent]);
x.perm(
"GetMachineValidationExternalConfigs",
Expand Down
70 changes: 70 additions & 0 deletions crates/api/src/handlers/machine_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::net::IpAddr;
use std::str::FromStr;

use ::rpc::forge as rpc;
use db::WithTransaction;
use futures_util::FutureExt;
use itertools::Itertools;
use model::machine_interface::InterfaceType;
use tonic::{Request, Response, Status};
Expand Down Expand Up @@ -145,3 +147,71 @@ pub(crate) async fn find_mac_address_by_bmc_ip(
mac_address: interface.mac_address.to_string(),
}))
}

pub(crate) async fn find_bmc_ips(
api: &Api,
request: Request<rpc::FindBmcIpsRequest>,
) -> Result<Response<rpc::BmcIpList>, Status> {
use rpc::find_bmc_ips_request::LookupBy;

log_request_data(&request);

let req = request.into_inner();

let bmc_ips = match req.lookup_by {
Some(LookupBy::MacAddress(mac_address)) => {
db::machine_interface::lookup_bmc_ip_by_mac_address(
&api.database_connection,
mac_address.parse().map_err(|e| {
CarbideError::InvalidArgument(format!("Invalid MAC address: {e}"))
})?,
)
.await?
}
Some(LookupBy::Serial(serial)) => {
// Get the machine ID for this serial
let machine_ids =
db::machine_topology::find_by_serial(&api.database_connection, &serial).await?;
if machine_ids.len() > 1 {
tracing::warn!(
serial,
"Multiple machines match serial number, cannot resolve to BMC IP"
);
return Ok(Response::new(rpc::BmcIpList::default()));
}
let Some(machine_id) = machine_ids.into_iter().next() else {
return Ok(Response::new(rpc::BmcIpList::default()));
};

// Get the machine topology for this machine
let Some(machine_topology) = api
.with_txn(|txn| {
async move {
db::machine_topology::find_latest_by_machine_ids(txn, &[machine_id]).await
}
.boxed()
})
.await??
.into_values()
.next()
else {
return Ok(Response::new(rpc::BmcIpList::default()));
};

// Get the BMC IP out of the machine topology
let bmc_ip = match machine_topology.topology.bmc_info.ip.map(|ip| ip.parse()) {
Some(Ok(ip)) => ip,
None | Some(Err(_)) => {
return Ok(Response::new(rpc::BmcIpList::default()));
}
};

vec![bmc_ip]
}
None => return Err(CarbideError::MissingArgument("lookup_by").into()),
};

Ok(Response::new(rpc::BmcIpList {
bmc_ips: bmc_ips.into_iter().map(|ip| ip.to_string()).collect(),
}))
}
2 changes: 2 additions & 0 deletions crates/bmc-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ hyper-timeout = { workspace = true }
hyper-util = { workspace = true }
hyper = { workspace = true, features = ["full"] }
lazy_static = { workspace = true }
mac_address = { workspace = true }
opentelemetry = { workspace = true, features = ["logs"] }
opentelemetry-otlp = { workspace = true, features = ["grpc-tonic"] }
opentelemetry-prometheus.workspace = true
Expand All @@ -74,6 +75,7 @@ tokio-rustls = { workspace = true }
tokio-stream = { workspace = true }
tokio-util = { workspace = true }
tokio = { workspace = true }
tonic = { workspace = true }
tower = { workspace = true }
tower-http = { features = [
"add-extension",
Expand Down
Loading
Loading