omyacid: ssh pubkeys

This commit is contained in:
snow flurry 2024-08-14 21:52:25 -07:00
parent 04f4d625a6
commit 29fc84e949
4 changed files with 37 additions and 3 deletions

View file

@ -179,7 +179,12 @@ impl Nazrin for MockServer {
id: i32,
) -> Result<Vec<u8>, String> {
let db = self.db.read().await;
let Some(inst) = db.instances.get(id as usize).and_then(|o| o.as_ref()) else {
let Some(inst) = db
.instances
.iter()
.find(|i| i.as_ref().map(|i| i.id == id).is_some())
.and_then(|o| o.as_ref())
else {
return Err("No such instance".to_owned());
};
Ok(db.ci_userdatas.get(&inst.name).cloned().unwrap_or_default())

View file

@ -8,6 +8,7 @@ use anyhow::Result;
use moka::future::Cache;
use nzr_api::config::Config;
use nzr_api::model::Instance;
use nzr_api::model::SshPubkey;
use nzr_api::InstanceQuery;
use nzr_api::NazrinClient;
use tokio::net::UnixStream;
@ -55,6 +56,17 @@ impl Context {
}
}
pub async fn get_sshkeys(&self) -> Result<Vec<SshPubkey>> {
// TODO: do we cache SSH keys? I don't like the idea of it
let ssh_keys = self
.api_client
.get_ssh_pubkeys(nzr_api::default_ctx())
.await
.context("RPC Error")?
.map_err(|e| anyhow::anyhow!("Couldn't get SSH keys"))?;
Ok(ssh_keys)
}
// Internal function to hydrate the instance metadata, if needed
async fn get_instmeta(&self, addr: Ipv4Addr) -> Result<Option<InstanceMeta>> {
if let Some(meta) = self.host_cache.get(&addr).await {

View file

@ -18,17 +18,31 @@ use axum::{
};
use model::Metadata;
use nzr_api::config::Config;
use tracing::instrument;
#[instrument(skip(ctx))]
async fn get_meta_data(
State(ctx): State<ctx::Context>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
) -> Result<String, StatusCode> {
tracing::info!("Handling /meta-data");
if let IpAddr::V4(ip) = addr.ip() {
let ssh_pubkeys: Vec<String> = ctx
.get_sshkeys()
.await
.map_err(|e| {
tracing::error!("Couldn't get SSH keys: {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?
.into_iter()
.map(|k| k.to_string())
.collect();
match ctx.get_instance(ip).await {
Ok(Some(inst)) => {
let meta = Metadata {
inst_name: &inst.name,
ssh_pubkeys: Vec::new(), // TODO
// XXX: this is very silly imo
ssh_pubkeys: ssh_pubkeys.iter().collect(),
username: Some(ctx.cfg().cloud.admin_user.as_ref()),
};
@ -51,10 +65,12 @@ async fn get_meta_data(
}
}
#[instrument(skip(ctx))]
async fn get_user_data(
State(ctx): State<ctx::Context>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
) -> Result<Vec<u8>, StatusCode> {
tracing::info!("Handling /user-data");
if let IpAddr::V4(ip) = addr.ip() {
match ctx.get_inst_userdata(ip).await {
Ok(Some(data)) => Ok(data),

View file

@ -1,4 +1,4 @@
use std::net::SocketAddr;
use std::net::{Ipv4Addr, SocketAddr};
use axum::extract::{ConnectInfo, State};
use nzr_api::{
@ -10,6 +10,7 @@ use crate::ctx;
#[tokio::test]
async fn get_metadata() {
tracing_subscriber::fmt().init();
let (mut client, _server) = mock::spawn_c2s().await;
let inst = client