2021-09-21 05:58:50 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
use serde_json::{json, Value};
|
2019-01-26 14:08:02 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_router::{RpcEnvironment, RpcEnvironmentType};
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2021-09-21 05:58:50 +00:00
|
|
|
use crate::ApiConfig;
|
|
|
|
|
2019-04-06 07:17:25 +00:00
|
|
|
/// Encapsulates information about the runtime environment
|
2019-01-26 14:08:02 +00:00
|
|
|
pub struct RestEnvironment {
|
2019-01-27 09:33:42 +00:00
|
|
|
env_type: RpcEnvironmentType,
|
2020-05-18 07:57:35 +00:00
|
|
|
result_attributes: Value,
|
2020-10-23 11:33:21 +00:00
|
|
|
auth_id: Option<String>,
|
2021-09-21 05:58:50 +00:00
|
|
|
client_ip: Option<SocketAddr>,
|
|
|
|
api: Arc<ApiConfig>,
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RestEnvironment {
|
2021-09-21 05:58:50 +00:00
|
|
|
pub fn new(env_type: RpcEnvironmentType, api: Arc<ApiConfig>) -> Self {
|
2019-01-27 09:33:42 +00:00
|
|
|
Self {
|
2020-05-18 07:57:35 +00:00
|
|
|
result_attributes: json!({}),
|
2020-10-23 11:33:21 +00:00
|
|
|
auth_id: None,
|
2020-10-15 15:43:42 +00:00
|
|
|
client_ip: None,
|
2019-01-27 09:33:42 +00:00
|
|
|
env_type,
|
2021-09-21 05:58:50 +00:00
|
|
|
api,
|
2019-01-27 09:33:42 +00:00
|
|
|
}
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|
2021-09-21 05:58:50 +00:00
|
|
|
|
|
|
|
pub fn api_config(&self) -> &ApiConfig {
|
|
|
|
&self.api
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_auth(&self, auth_id: &str) {
|
|
|
|
let msg = format!("successful auth for user '{}'", auth_id);
|
2021-11-10 16:05:09 +00:00
|
|
|
log::debug!("{}", msg); // avoid noisy syslog, admins can already check the auth log
|
2021-09-21 05:58:50 +00:00
|
|
|
if let Some(auth_logger) = self.api.get_auth_log() {
|
|
|
|
auth_logger.lock().unwrap().log(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_failed_auth(&self, failed_auth_id: Option<String>, msg: &str) {
|
|
|
|
let msg = match (self.client_ip, failed_auth_id) {
|
|
|
|
(Some(peer), Some(user)) => {
|
|
|
|
format!("authentication failure; rhost={} user={} msg={}", peer, user, msg)
|
|
|
|
}
|
|
|
|
(Some(peer), None) => {
|
|
|
|
format!("authentication failure; rhost={} msg={}", peer, msg)
|
|
|
|
}
|
|
|
|
(None, Some(user)) => {
|
|
|
|
format!("authentication failure; rhost=unknown user={} msg={}", user, msg)
|
|
|
|
}
|
|
|
|
(None, None) => {
|
|
|
|
format!("authentication failure; rhost=unknown msg={}", msg)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
log::error!("{}", msg);
|
|
|
|
if let Some(auth_logger) = self.api.get_auth_log() {
|
|
|
|
auth_logger.lock().unwrap().log(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcEnvironment for RestEnvironment {
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
fn result_attrib_mut (&mut self) -> &mut Value {
|
|
|
|
&mut self.result_attributes
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
fn result_attrib(&self) -> &Value {
|
|
|
|
&self.result_attributes
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|
2019-01-27 09:33:42 +00:00
|
|
|
|
|
|
|
fn env_type(&self) -> RpcEnvironmentType {
|
|
|
|
self.env_type
|
|
|
|
}
|
2019-01-27 09:42:45 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
fn set_auth_id(&mut self, auth_id: Option<String>) {
|
|
|
|
self.auth_id = auth_id;
|
2019-01-27 09:42:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
fn get_auth_id(&self) -> Option<String> {
|
|
|
|
self.auth_id.clone()
|
2019-01-27 09:42:45 +00:00
|
|
|
}
|
2020-10-15 15:43:42 +00:00
|
|
|
|
2021-09-21 05:58:50 +00:00
|
|
|
fn set_client_ip(&mut self, client_ip: Option<SocketAddr>) {
|
2020-10-15 15:43:42 +00:00
|
|
|
self.client_ip = client_ip;
|
|
|
|
}
|
|
|
|
|
2021-09-21 05:58:50 +00:00
|
|
|
fn get_client_ip(&self) -> Option<SocketAddr> {
|
2021-01-15 13:38:27 +00:00
|
|
|
self.client_ip
|
2020-10-15 15:43:42 +00:00
|
|
|
}
|
2019-01-26 14:08:02 +00:00
|
|
|
}
|