Can get VM caps
This commit is contained in:
@ -7,6 +7,7 @@ use std::io::ErrorKind;
|
||||
|
||||
pub mod auth_controller;
|
||||
pub mod server_controller;
|
||||
pub mod vm_controller;
|
||||
|
||||
/// Custom error to ease controller writing
|
||||
#[derive(Debug)]
|
||||
|
48
remote_backend/src/controllers/vm_controller.rs
Normal file
48
remote_backend/src/controllers/vm_controller.rs
Normal file
@ -0,0 +1,48 @@
|
||||
//! # Virtual machines routes controller
|
||||
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::virtweb_client;
|
||||
use crate::virtweb_client::VMUuid;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
pub struct VMInfoAndCaps {
|
||||
uiid: VMUuid,
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
can_get_state: bool,
|
||||
can_start: bool,
|
||||
can_shutdown: bool,
|
||||
can_kill: bool,
|
||||
can_reset: bool,
|
||||
can_suspend: bool,
|
||||
can_resume: bool,
|
||||
can_screenshot: bool,
|
||||
}
|
||||
|
||||
/// Get the list of VMs that can be controlled by VirtWeb remote
|
||||
pub async fn list() -> HttpResult {
|
||||
let rights = virtweb_client::get_token_info().await?;
|
||||
|
||||
let mut res = vec![];
|
||||
|
||||
for v in rights.list_vm() {
|
||||
let vm_info = virtweb_client::get_vm_info(v).await?;
|
||||
|
||||
res.push(VMInfoAndCaps {
|
||||
uiid: vm_info.uuid,
|
||||
name: vm_info.name,
|
||||
description: vm_info.description.clone(),
|
||||
can_get_state: rights.is_route_allowed("GET", &v.route_state()),
|
||||
can_start: rights.is_route_allowed("GET", &v.route_start()),
|
||||
can_shutdown: rights.is_route_allowed("GET", &v.route_shutdown()),
|
||||
can_kill: rights.is_route_allowed("GET", &v.route_kill()),
|
||||
can_reset: rights.is_route_allowed("GET", &v.route_reset()),
|
||||
can_suspend: rights.is_route_allowed("GET", &v.route_suspend()),
|
||||
can_resume: rights.is_route_allowed("GET", &v.route_resume()),
|
||||
can_screenshot: rights.is_route_allowed("GET", &v.route_screenshot()),
|
||||
})
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(res))
|
||||
}
|
@ -11,7 +11,7 @@ use actix_web::{web, App, HttpServer};
|
||||
use light_openid::basic_state_manager::BasicStateManager;
|
||||
use remote_backend::app_config::AppConfig;
|
||||
use remote_backend::constants;
|
||||
use remote_backend::controllers::{auth_controller, server_controller};
|
||||
use remote_backend::controllers::{auth_controller, server_controller, vm_controller};
|
||||
use remote_backend::middlewares::auth_middleware::AuthChecker;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -79,6 +79,7 @@ async fn main() -> std::io::Result<()> {
|
||||
"/api/auth/sign_out",
|
||||
web::get().to(auth_controller::sign_out),
|
||||
)
|
||||
.route("/api/vm/list", web::get().to(vm_controller::list))
|
||||
})
|
||||
.bind(&AppConfig::get().listen_address)?
|
||||
.run()
|
||||
|
@ -1,8 +1,10 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::utils::time;
|
||||
use lazy_regex::regex;
|
||||
use std::fmt::Display;
|
||||
use std::str::FromStr;
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
use uuid::{Error, Uuid};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum VirtWebClientError {
|
||||
@ -10,6 +12,53 @@ pub enum VirtWebClientError {
|
||||
InvalidStatusCode(u16),
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct VMUuid(Uuid);
|
||||
|
||||
impl VMUuid {
|
||||
pub fn route_info(&self) -> String {
|
||||
format!("/api/vm/{}", self.0)
|
||||
}
|
||||
|
||||
pub fn route_state(&self) -> String {
|
||||
format!("/api/vm/{}/state", self.0)
|
||||
}
|
||||
|
||||
pub fn route_start(&self) -> String {
|
||||
format!("/api/vm/{}/start", self.0)
|
||||
}
|
||||
|
||||
pub fn route_shutdown(&self) -> String {
|
||||
format!("/api/vm/{}/shutdown", self.0)
|
||||
}
|
||||
pub fn route_kill(&self) -> String {
|
||||
format!("/api/vm/{}/kill", self.0)
|
||||
}
|
||||
|
||||
pub fn route_reset(&self) -> String {
|
||||
format!("/api/vm/{}/reset", self.0)
|
||||
}
|
||||
pub fn route_suspend(&self) -> String {
|
||||
format!("/api/vm/{}/suspend", self.0)
|
||||
}
|
||||
|
||||
pub fn route_resume(&self) -> String {
|
||||
format!("/api/vm/{}/resume", self.0)
|
||||
}
|
||||
|
||||
pub fn route_screenshot(&self) -> String {
|
||||
format!("/api/vm/{}/screenshot", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for VMUuid {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(VMUuid(Uuid::from_str(s)?))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct TokenClaims {
|
||||
pub sub: String,
|
||||
@ -20,6 +69,13 @@ pub struct TokenClaims {
|
||||
pub nonce: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Debug)]
|
||||
pub struct VMInfo {
|
||||
pub uuid: VMUuid,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Debug)]
|
||||
pub struct TokenRight {
|
||||
verb: String,
|
||||
@ -29,10 +85,49 @@ pub struct TokenRight {
|
||||
pub type TokenRights = Vec<TokenRight>;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TokenInfo {
|
||||
pub struct TokenInfo {
|
||||
rights: TokenRights,
|
||||
}
|
||||
|
||||
impl TokenInfo {
|
||||
/// Check whether a route is allowed or not
|
||||
pub fn is_route_allowed(&self, verb: &str, route: &str) -> bool {
|
||||
let search_route_split = route.split('/').collect::<Vec<_>>();
|
||||
|
||||
for r in &self.rights {
|
||||
if r.verb != verb {
|
||||
continue;
|
||||
}
|
||||
|
||||
let curr_route_split = r.path.split('/').collect::<Vec<_>>();
|
||||
|
||||
if search_route_split.len() != curr_route_split.len() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if curr_route_split
|
||||
.iter()
|
||||
.zip(search_route_split.iter())
|
||||
.all(|(curr, search)| curr == &"*" || curr == search)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// List the virtual machines with access
|
||||
pub fn list_vm(&self) -> Vec<VMUuid> {
|
||||
self.rights
|
||||
.iter()
|
||||
.filter(|r| r.verb == "GET")
|
||||
.filter(|r| regex!("^/api/vm/[^/]+$").is_match(&r.path))
|
||||
.map(|r| VMUuid::from_str(r.path.rsplit_once('/').unwrap().1).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform a request on the API
|
||||
async fn request<D: Display, E: serde::de::DeserializeOwned>(uri: D) -> anyhow::Result<E> {
|
||||
let url = format!("{}{}", AppConfig::get().virtweb_base_url, uri);
|
||||
@ -62,10 +157,17 @@ async fn request<D: Display, E: serde::de::DeserializeOwned>(uri: D) -> anyhow::
|
||||
Ok(res.json().await?)
|
||||
}
|
||||
|
||||
/// Get current token rights
|
||||
pub async fn get_token_rights() -> anyhow::Result<TokenRights> {
|
||||
/// Get current token information
|
||||
pub async fn get_token_info() -> anyhow::Result<TokenInfo> {
|
||||
let res: TokenInfo =
|
||||
request(format!("/api/token/{}", AppConfig::get().virtweb_token_id)).await?;
|
||||
|
||||
Ok(res.rights)
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Get a vm information
|
||||
pub async fn get_vm_info(id: VMUuid) -> anyhow::Result<VMInfo> {
|
||||
let res: VMInfo = request(format!("/api/vm/{}", id.0)).await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
Reference in New Issue
Block a user