summaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2022-07-10 11:05:51 -0400
committerCara Salter <cara@devcara.com>2022-07-10 11:05:51 -0400
commitd3279088e3a816db2c254b957159d5b697dc0f62 (patch)
tree764557ef69bdff984faad4885c8e00d6c9546107 /src/handlers
parent131dd5ab9df295b0bad320675b2307afcbf7d076 (diff)
downloadhomeworld-d3279088e3a816db2c254b957159d5b697dc0f62.tar.gz
homeworld-d3279088e3a816db2c254b957159d5b697dc0f62.zip
colonies: Fully support {meta, user}-data
cloud-init should be able to use this as a seed to properly set up the Planet.
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/colonies.rs55
-rw-r--r--src/handlers/ships.rs8
2 files changed, 56 insertions, 7 deletions
diff --git a/src/handlers/colonies.rs b/src/handlers/colonies.rs
index 37ec65a..cc9ee0e 100644
--- a/src/handlers/colonies.rs
+++ b/src/handlers/colonies.rs
@@ -1,15 +1,18 @@
use std::sync::Arc;
-use axum::{Extension, Json};
-use sqlx::query_as;
+use axum::{Extension, Json, extract::Path};
+use axum_auth::AuthBearer;
+use sqlx::{query_as, query};
use crate::{
- errors::{JsonResult, ServiceError, StringResult},
+ errors::{JsonResult, ServiceError, StringResult, NoneResult},
State,
};
use solarlib::colony::{User, UserData};
+use super::ships::check_bearer;
+
#[derive(Debug, Clone)]
pub struct DbUser {
pub id: i32,
@@ -18,6 +21,12 @@ pub struct DbUser {
pub ssh_authorized_keys: Vec<String>,
}
+#[derive(Debug)]
+struct DbMeta {
+ pub uuid: String,
+ pub hostname: String,
+}
+
impl From<DbUser> for User {
fn from(o: DbUser) -> Self {
Self {
@@ -44,3 +53,43 @@ pub async fn list_users(state: Extension<Arc<State>>) -> JsonResult<Json<UserDat
Ok(Json(data))
}
+
+pub async fn create_user(Json(new_user): Json<User>, state: Extension<Arc<State>>, AuthBearer(token): AuthBearer) -> NoneResult {
+ check_bearer(token)?;
+ let mut conn = state.conn.acquire().await?;
+
+ query!("INSERT INTO seed_users (name, groups, ssh_authorized_keys) VALUES ($1, $2, $3)", new_user.name, &new_user.groups, &new_user.ssh_authorized_keys)
+ .execute(&mut conn)
+ .await?;
+
+ Ok(())
+}
+
+pub async fn add_metadata(Path((uuid, hostname)): Path<(String, String)>, state: Extension<Arc<State>>, AuthBearer(token): AuthBearer) -> NoneResult {
+ check_bearer(token)?;
+ let mut conn = state.conn.acquire().await?;
+
+ query!("INSERT INTO planet_metadata (uuid, hostname) VALUES ($1, $2)", uuid, hostname).execute(&mut conn).await?;
+ Ok(())
+}
+
+pub async fn meta_data(Path(uuid): Path<String>, state: Extension<Arc<State>>) -> StringResult {
+ let mut conn = state.conn.acquire().await?;
+
+ let m = query_as!(DbMeta, "SELECT * FROM planet_metadata WHERE uuid=$1", uuid).fetch_one(&mut conn).await?;
+
+ Ok(format!("instance-id: {}\nlocal-hostname: {}", m.uuid, m.hostname))
+}
+
+pub async fn user_data(Path(_uuid): Path<String>, state: Extension<Arc<State>>) -> StringResult {
+ let mut conn = state.conn.acquire().await?;
+
+ let db_users = query_as!(DbUser, "SELECT * FROM seed_users").fetch_all(&mut conn).await?;
+
+ let users = db_users.into_iter().map(|u| u.into()).collect::<Vec<User>>();
+
+ let data = UserData { users };
+
+ Ok(format!("#cloud-config\n{}", serde_yaml::to_string(&data).unwrap()))
+
+}
diff --git a/src/handlers/ships.rs b/src/handlers/ships.rs
index 91944fc..3101e54 100644
--- a/src/handlers/ships.rs
+++ b/src/handlers/ships.rs
@@ -8,7 +8,7 @@ use sqlx::{query, query_as, Error as SqlxError};
use tracing::log::warn;
use crate::{
- errors::{JsonResult, ServiceError, StringResult},
+ errors::{JsonResult, ServiceError, StringResult, StrResult},
State,
};
@@ -31,7 +31,7 @@ pub async fn new(
Json(new_ship): Json<Ship>,
state: Extension<Arc<State>>,
AuthBearer(token): AuthBearer,
-) -> StringResult {
+) -> StrResult {
check_bearer(token)?;
let mut conn = state.conn.acquire().await?;
@@ -52,7 +52,7 @@ pub async fn delete(
Path(shasum): Path<Sha256>,
state: Extension<Arc<State>>,
AuthBearer(token): AuthBearer,
-) -> StringResult {
+) -> StrResult {
check_bearer(token)?;
let mut conn = state.conn.acquire().await?;
@@ -89,7 +89,7 @@ pub async fn get(
Ok(Json(db_ship))
}
-fn check_bearer(token: String) -> Result<(), ServiceError> {
+pub fn check_bearer(token: String) -> Result<(), ServiceError> {
let expected_token = match std::env::var("SHARED_KEY") {
Ok(t) => t,
Err(_) => {