proxmox-backup/src/json_schema.rs

490 lines
14 KiB
Rust
Raw Normal View History

2018-11-05 14:20:27 +00:00
use failure::*;
2018-11-03 14:10:21 +00:00
use std::collections::HashMap;
2018-11-05 14:20:27 +00:00
use serde_json::{json, Value};
2018-11-06 12:58:05 +00:00
use url::form_urlencoded;
2018-11-07 11:35:52 +00:00
use regex::Regex;
2018-11-01 12:05:45 +00:00
2018-11-03 14:10:21 +00:00
pub type PropertyMap = HashMap<&'static str, Jss>;
2018-10-31 09:42:14 +00:00
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub struct JssBoolean {
pub description: &'static str,
2018-11-06 12:10:10 +00:00
pub optional: bool,
2018-10-31 09:42:14 +00:00
pub default: Option<bool>,
}
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub struct JssInteger {
pub description: &'static str,
2018-11-06 12:10:10 +00:00
pub optional: bool,
pub minimum: Option<isize>,
pub maximum: Option<isize>,
pub default: Option<isize>,
2018-10-31 09:42:14 +00:00
}
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub struct JssString {
pub description: &'static str,
2018-11-06 12:10:10 +00:00
pub optional: bool,
2018-11-03 14:10:21 +00:00
pub default: Option<&'static str>,
2018-10-31 09:42:14 +00:00
pub min_length: Option<usize>,
pub max_length: Option<usize>,
2018-11-07 11:55:33 +00:00
pub format: ApiStringFormat,
2018-10-31 09:42:14 +00:00
}
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub struct JssArray {
pub description: &'static str,
2018-11-06 12:10:10 +00:00
pub optional: bool,
2018-11-03 14:10:21 +00:00
pub items: Box<Jss>,
2018-10-31 09:42:14 +00:00
}
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub struct JssObject {
pub description: &'static str,
2018-11-06 12:10:10 +00:00
pub optional: bool,
pub additional_properties: bool,
2018-11-05 14:20:27 +00:00
pub properties: HashMap<&'static str, Jss>,
2018-10-31 09:42:14 +00:00
}
#[derive(Debug)]
2018-11-03 14:10:21 +00:00
pub enum Jss {
2018-10-31 09:42:14 +00:00
Null,
2018-11-03 14:10:21 +00:00
Boolean(JssBoolean),
Integer(JssInteger),
String(JssString),
Object(JssObject),
Array(JssArray),
2018-10-31 09:42:14 +00:00
}
pub const DEFAULTBOOL: JssBoolean = JssBoolean {
2018-10-31 09:42:14 +00:00
description: "",
2018-11-06 12:10:10 +00:00
optional: false,
2018-10-31 09:42:14 +00:00
default: None,
};
#[macro_export]
macro_rules! Boolean {
($($name:ident => $e:expr),*) => {{
Jss::Boolean(JssBoolean { $($name: $e, )* ..DEFAULTBOOL})
}}
}
pub const DEFAULTINTEGER: JssInteger = JssInteger {
2018-10-31 09:42:14 +00:00
description: "",
2018-11-06 12:10:10 +00:00
optional: false,
2018-10-31 09:42:14 +00:00
default: None,
minimum: None,
maximum: None,
};
#[macro_export]
macro_rules! Integer {
($($name:ident => $e:expr),*) => {{
Jss::Integer(JssInteger { $($name: $e, )* ..DEFAULTINTEGER})
}}
}
pub const DEFAULTSTRING: JssString = JssString {
2018-10-31 09:42:14 +00:00
description: "",
2018-11-06 12:10:10 +00:00
optional: false,
2018-10-31 09:42:14 +00:00
default: None,
min_length: None,
max_length: None,
2018-11-07 11:55:33 +00:00
format: ApiStringFormat::None,
2018-10-31 09:42:14 +00:00
};
2018-11-07 11:55:33 +00:00
#[derive(Debug)]
pub enum ApiStringFormat {
None,
Pattern(Box<Regex>),
2018-11-07 11:55:33 +00:00
Complex(Box<Jss>),
}
2018-10-31 09:42:14 +00:00
#[macro_export]
macro_rules! ApiString {
($($name:ident => $e:expr),*) => {{
Jss::String(JssString { $($name: $e, )* ..DEFAULTSTRING})
}}
}
#[macro_export]
2018-11-03 14:10:21 +00:00
macro_rules! parameter {
2018-10-31 09:42:14 +00:00
($($name:ident => $e:expr),*) => {{
2018-11-03 14:10:21 +00:00
let inner = JssObject {
description: "",
2018-11-06 12:10:10 +00:00
optional: false,
additional_properties: false,
2018-11-03 14:10:21 +00:00
properties: {
let mut map = HashMap::<&'static str, Jss>::new();
$(
map.insert(stringify!($name), $e);
)*
2018-11-05 14:20:27 +00:00
map
2018-11-03 14:10:21 +00:00
}
};
2018-10-31 09:42:14 +00:00
2018-11-03 14:10:21 +00:00
Jss::Object(inner)
2018-10-31 09:42:14 +00:00
}}
}
2018-11-06 12:10:10 +00:00
fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> {
2018-10-31 09:42:14 +00:00
2018-11-06 12:10:10 +00:00
let value = match schema {
Jss::Null => {
2018-11-07 11:14:52 +00:00
bail!("internal error - found Null schema.");
2018-11-06 12:10:10 +00:00
}
Jss::Boolean(jss_boolean) => {
let res = match value_str.to_lowercase().as_str() {
"1" | "on" | "yes" | "true" => true,
"0" | "off" | "no" | "false" => false,
_ => bail!("Unable to parse boolean option."),
};
Value::Bool(res)
}
Jss::Integer(jss_integer) => {
let res: isize = value_str.parse()?;
2018-11-07 10:55:08 +00:00
if let Some(minimum) = jss_integer.minimum {
if res < minimum {
2018-11-07 11:11:09 +00:00
bail!("value must have a minimum value of {}", minimum);
2018-11-07 10:55:08 +00:00
}
}
if let Some(maximum) = jss_integer.maximum {
if res > maximum {
2018-11-07 11:11:09 +00:00
bail!("value must have a maximum value of {}", maximum);
2018-11-07 10:55:08 +00:00
}
}
2018-11-06 12:10:10 +00:00
Value::Number(res.into())
}
Jss::String(jss_string) => {
2018-11-07 11:11:09 +00:00
let res: String = value_str.into();
let char_count = res.chars().count();
if let Some(min_length) = jss_string.min_length {
if char_count < min_length {
bail!("value must be at least {} characters long", min_length);
}
}
if let Some(max_length) = jss_string.max_length {
if char_count > max_length {
bail!("value may only be {} characters long", max_length);
}
}
2018-11-07 11:55:33 +00:00
if let ApiStringFormat::Pattern(ref regex) = jss_string.format {
2018-11-07 11:35:52 +00:00
if !regex.is_match(&res) {
bail!("value does not match the regex pattern");
}
}
Value::String(res)
2018-11-06 12:10:10 +00:00
}
2018-11-07 11:14:52 +00:00
_ => bail!("unable to parse complex (sub) objects."),
2018-11-06 12:10:10 +00:00
};
Ok(value)
}
2018-11-06 13:18:13 +00:00
pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_required: bool) -> Result<Value, Vec<Error>> {
2018-11-05 14:20:27 +00:00
println!("QUERY Strings {:?}", data);
2018-11-06 12:10:10 +00:00
let mut params = json!({});
let mut errors: Vec<Error> = Vec::new();
match schema {
Jss::Object(JssObject { properties, additional_properties, .. }) => {
for (key, value) in data {
if let Some(prop_schema) = properties.get::<str>(key) {
match prop_schema {
Jss::Array(jss_array) => {
if params[key] == Value::Null {
params[key] = json!([]);
}
match params[key] {
Value::Array(ref mut array) => {
match parse_simple_value(value, &jss_array.items) {
Ok(res) => array.push(res),
2018-11-07 10:55:08 +00:00
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
2018-11-06 12:10:10 +00:00
}
}
2018-11-07 10:55:08 +00:00
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
2018-11-06 12:10:10 +00:00
}
}
_ => {
match parse_simple_value(value, prop_schema) {
Ok(res) => {
if params[key] == Value::Null {
params[key] = res;
} else {
2018-11-07 10:55:08 +00:00
errors.push(format_err!("parameter '{}': duplicate parameter.", key));
2018-11-06 12:10:10 +00:00
}
},
2018-11-07 10:55:08 +00:00
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
2018-11-06 12:10:10 +00:00
}
}
}
} else {
if *additional_properties {
match params[key] {
Value::Null => {
params[key] = Value::String(value.to_owned());
},
Value::String(ref old) => {
params[key] = Value::Array(
vec![Value::String(old.to_owned()), Value::String(value.to_owned())]);
}
Value::Array(ref mut array) => {
array.push(Value::String(value.to_string()));
}
2018-11-07 10:55:08 +00:00
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
2018-11-06 12:10:10 +00:00
}
} else {
2018-11-07 10:55:08 +00:00
errors.push(format_err!("parameter '{}': schema does not allow additional properties.", key));
2018-11-06 12:10:10 +00:00
}
}
}
2018-11-06 13:18:13 +00:00
2018-11-07 11:35:52 +00:00
if test_required && errors.len() == 0 {
2018-11-06 13:18:13 +00:00
for (name, prop_schema) in properties {
let optional = match prop_schema {
Jss::Boolean(jss_boolean) => jss_boolean.optional,
Jss::Integer(jss_integer) => jss_integer.optional,
Jss::String(jss_string) => jss_string.optional,
Jss::Array(jss_array) => jss_array.optional,
Jss::Object(jss_object) => jss_object.optional,
Jss::Null => true,
};
if optional == false && params[name] == Value::Null {
2018-11-07 10:55:08 +00:00
errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name));
2018-11-06 13:18:13 +00:00
}
}
}
2018-11-06 12:10:10 +00:00
}
_ => errors.push(format_err!("Got unexpected schema type in parse_parameter_strings.")),
}
if (errors.len() > 0) {
Err(errors)
} else {
Ok(params)
}
2018-11-05 14:20:27 +00:00
}
2018-11-06 13:18:13 +00:00
pub fn parse_query_string(query: &str, schema: &Jss, test_required: bool) -> Result<Value, Vec<Error>> {
2018-11-06 12:58:05 +00:00
let param_list: Vec<(String, String)> =
form_urlencoded::parse(query.as_bytes()).into_owned().collect();
2018-11-06 13:18:13 +00:00
parse_parameter_strings(&param_list, schema, test_required)
2018-11-06 12:58:05 +00:00
}
2018-11-03 14:10:21 +00:00
#[test]
fn test_shema1() {
let schema = Jss::Object(JssObject {
description: "TEST",
2018-11-06 12:58:05 +00:00
optional: false,
additional_properties: false,
2018-11-03 14:10:21 +00:00
properties: {
let map = HashMap::new();
2018-11-06 12:58:05 +00:00
map
2018-11-02 08:44:18 +00:00
}
2018-11-03 14:10:21 +00:00
});
println!("TEST Schema: {:?}", schema);
2018-11-02 08:44:18 +00:00
}
2018-11-03 08:08:01 +00:00
2018-11-07 11:11:09 +00:00
#[test]
fn test_query_string() {
let schema = parameter!{name => ApiString!{ optional => false }};
let res = parse_query_string("", &schema, true);
assert!(res.is_err());
let schema = parameter!{name => ApiString!{ optional => true }};
let res = parse_query_string("", &schema, true);
assert!(res.is_ok());
2018-11-07 11:35:52 +00:00
// TEST min_length and max_length
2018-11-07 11:11:09 +00:00
let schema = parameter!{name => ApiString!{
optional => false,
min_length => Some(5),
max_length => Some(10)
}};
let res = parse_query_string("name=abcd", &schema, true);
assert!(res.is_err());
let res = parse_query_string("name=abcde", &schema, true);
assert!(res.is_ok());
let res = parse_query_string("name=abcdefghijk", &schema, true);
assert!(res.is_err());
let res = parse_query_string("name=abcdefghij", &schema, true);
assert!(res.is_ok());
2018-11-07 11:35:52 +00:00
// TEST regex pattern
let schema = parameter!{name => ApiString!{
optional => false,
format => ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap()))
2018-11-07 11:35:52 +00:00
}};
2018-11-07 11:11:09 +00:00
2018-11-07 11:35:52 +00:00
let res = parse_query_string("name=abcd", &schema, true);
assert!(res.is_err());
2018-11-07 11:11:09 +00:00
2018-11-07 11:35:52 +00:00
let res = parse_query_string("name=ateststring", &schema, true);
assert!(res.is_ok());
let schema = parameter!{name => ApiString!{
optional => false,
format => ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap()))
2018-11-07 11:35:52 +00:00
}};
let res = parse_query_string("name=ateststring", &schema, true);
assert!(res.is_err());
let res = parse_query_string("name=test", &schema, true);
assert!(res.is_ok());
2018-11-07 11:11:09 +00:00
}
2018-11-07 10:55:08 +00:00
#[test]
fn test_query_integer() {
let schema = parameter!{count => Integer!{ optional => false }};
let res = parse_query_string("", &schema, true);
assert!(res.is_err());
let schema = parameter!{count => Integer!{
optional => true,
minimum => Some(-3),
maximum => Some(50)
}};
let res = parse_query_string("", &schema, true);
assert!(res.is_ok());
let res = parse_query_string("count=abc", &schema, false);
assert!(res.is_err());
let res = parse_query_string("count=30", &schema, false);
assert!(res.is_ok());
let res = parse_query_string("count=-1", &schema, false);
assert!(res.is_ok());
let res = parse_query_string("count=300", &schema, false);
assert!(res.is_err());
let res = parse_query_string("count=-30", &schema, false);
assert!(res.is_err());
let res = parse_query_string("count=50", &schema, false);
assert!(res.is_ok());
let res = parse_query_string("count=-3", &schema, false);
assert!(res.is_ok());
}
2018-11-06 12:58:05 +00:00
#[test]
fn test_query_boolean() {
let schema = parameter!{force => Boolean!{ optional => false }};
2018-11-06 13:18:13 +00:00
let res = parse_query_string("", &schema, true);
assert!(res.is_err());
2018-11-06 12:58:05 +00:00
let schema = parameter!{force => Boolean!{ optional => true }};
2018-11-06 13:18:13 +00:00
let res = parse_query_string("", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("a=b", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_err());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_err());
2018-11-07 10:55:08 +00:00
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=yes", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=1", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=On", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=TRUE", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=TREU", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_err());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=NO", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=0", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=off", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
2018-11-06 13:18:13 +00:00
let res = parse_query_string("force=False", &schema, true);
2018-11-06 12:58:05 +00:00
assert!(res.is_ok());
}
2018-11-03 14:10:21 +00:00
/*
2018-11-03 08:08:01 +00:00
#[test]
fn test_shema1() {
static PARAMETERS1: PropertyMap = propertymap!{
force => &Boolean!{
description => "Test for boolean options."
},
text1 => &ApiString!{
description => "A simple text string.",
min_length => Some(10),
max_length => Some(30)
},
count => &Integer!{
description => "A counter for everything.",
minimum => Some(0),
maximum => Some(10)
},
myarray1 => &Array!{
description => "Test Array of simple integers.",
items => &PVE_VMID
},
myarray2 => &Jss::Array(JssArray {
description: "Test Array of simple integers.",
optional: Some(false),
items: &Object!{description => "Empty Object."},
}),
myobject => &Object!{
description => "TEST Object.",
properties => &propertymap!{
vmid => &PVE_VMID,
loop => &Integer!{
description => "Totally useless thing.",
optional => Some(false)
}
}
},
emptyobject => &Object!{description => "Empty Object."}
};
for (k, v) in PARAMETERS1.entries {
println!("Parameter: {} Value: {:?}", k, v);
}
}
2018-11-03 14:10:21 +00:00
*/