2021-06-02 01:42:40 +02:00
|
|
|
//! Modification expressions and validation. This is how `ModifyEvents` store and
|
|
|
|
//! express the series of Modifications that should be applied. These are expressed
|
|
|
|
//! as "states" on what attribute-values should appear as within the `Entry`
|
|
|
|
|
2022-10-01 08:08:51 +02:00
|
|
|
use std::slice;
|
2019-03-12 06:20:08 +01:00
|
|
|
|
2024-02-27 10:25:02 +01:00
|
|
|
use kanidm_proto::internal::{
|
|
|
|
Modify as ProtoModify, ModifyList as ProtoModifyList, OperationError, SchemaError,
|
2022-10-01 08:08:51 +02:00
|
|
|
};
|
2024-02-27 10:25:02 +01:00
|
|
|
use kanidm_proto::v1::Entry as ProtoEntry;
|
2019-03-12 06:20:08 +01:00
|
|
|
// Should this be std?
|
2021-12-31 00:11:20 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-01 08:08:51 +02:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
use crate::schema::SchemaTransaction;
|
|
|
|
use crate::value::{PartialValue, Value};
|
2019-03-12 06:20:08 +01:00
|
|
|
|
2022-07-07 05:28:36 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-03-12 06:20:08 +01:00
|
|
|
pub struct ModifyValid;
|
2022-07-07 05:28:36 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-03-12 06:20:08 +01:00
|
|
|
pub struct ModifyInvalid;
|
|
|
|
|
2022-07-07 05:28:36 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2020-06-18 02:30:42 +02:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2019-01-31 05:29:14 +01:00
|
|
|
pub enum Modify {
|
|
|
|
// This value *should* exist.
|
2020-06-18 02:30:42 +02:00
|
|
|
// Clippy doesn't like value here, as value > pv. It could be an improvement to
|
|
|
|
// box here, but not sure. ... TODO and thought needed.
|
2024-09-09 02:53:10 +02:00
|
|
|
Present(Attribute, Value),
|
2019-01-31 05:29:14 +01:00
|
|
|
// This value *should not* exist.
|
2024-09-09 02:53:10 +02:00
|
|
|
Removed(Attribute, PartialValue),
|
2019-01-31 05:29:14 +01:00
|
|
|
// This attr *should not* exist.
|
2024-09-09 02:53:10 +02:00
|
|
|
Purged(Attribute),
|
2022-12-15 07:09:09 +01:00
|
|
|
// This attr and value must exist *in this state* for this change to proceed.
|
2023-09-16 04:11:06 +02:00
|
|
|
Assert(Attribute, PartialValue),
|
2019-01-31 05:29:14 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn m_pres(attr: Attribute, v: &Value) -> Modify {
|
2024-09-09 02:53:10 +02:00
|
|
|
Modify::Present(attr, v.clone())
|
2019-06-07 11:19:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn m_remove(attr: Attribute, v: &PartialValue) -> Modify {
|
2024-09-09 02:53:10 +02:00
|
|
|
Modify::Removed(attr, v.clone())
|
2019-06-07 11:19:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn m_purge(attr: Attribute) -> Modify {
|
2024-09-09 02:53:10 +02:00
|
|
|
Modify::Purged(attr)
|
2019-06-07 11:19:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn m_assert(attr: Attribute, v: &PartialValue) -> Modify {
|
|
|
|
Modify::Assert(attr, v.clone())
|
2022-12-15 07:09:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 07:15:48 +01:00
|
|
|
impl Modify {
|
2022-12-28 08:52:25 +01:00
|
|
|
pub fn from(
|
|
|
|
m: &ProtoModify,
|
|
|
|
qs: &mut QueryServerWriteTransaction,
|
|
|
|
) -> Result<Self, OperationError> {
|
2019-03-17 04:24:06 +01:00
|
|
|
Ok(match m {
|
2024-09-09 02:53:10 +02:00
|
|
|
ProtoModify::Present(a, v) => {
|
|
|
|
let a = Attribute::from(a.as_str());
|
|
|
|
let v = qs.clone_value(&a, v)?;
|
|
|
|
Modify::Present(a, v)
|
|
|
|
}
|
|
|
|
ProtoModify::Removed(a, v) => {
|
|
|
|
let a = Attribute::from(a.as_str());
|
|
|
|
let v = qs.clone_partialvalue(&a, v)?;
|
|
|
|
Modify::Removed(a, v)
|
|
|
|
}
|
|
|
|
ProtoModify::Purged(a) => Modify::Purged(Attribute::from(a.as_str())),
|
2019-03-17 04:24:06 +01:00
|
|
|
})
|
2019-02-22 07:15:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:28:36 +02:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2019-03-12 06:20:08 +01:00
|
|
|
pub struct ModifyList<VALID> {
|
2021-12-16 01:13:03 +01:00
|
|
|
// This is never read, it's just used for state machine enforcement.
|
|
|
|
#[allow(dead_code)]
|
2019-03-12 06:20:08 +01:00
|
|
|
valid: VALID,
|
|
|
|
// The order of this list matters. Each change must be done in order.
|
|
|
|
mods: Vec<Modify>,
|
2019-01-31 05:29:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 06:20:08 +01:00
|
|
|
impl<'a> IntoIterator for &'a ModifyList<ModifyValid> {
|
|
|
|
type IntoIter = slice::Iter<'a, Modify>;
|
2022-10-01 08:08:51 +02:00
|
|
|
type Item = &'a Modify;
|
2019-03-12 06:20:08 +01:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.mods.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModifyList<ModifyInvalid> {
|
2019-01-31 05:29:14 +01:00
|
|
|
pub fn new() -> Self {
|
2019-03-12 06:20:08 +01:00
|
|
|
ModifyList {
|
|
|
|
valid: ModifyInvalid,
|
2024-05-30 12:22:19 +02:00
|
|
|
mods: Vec::with_capacity(0),
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
2019-01-31 05:29:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_list(mods: Vec<Modify>) -> Self {
|
2019-03-12 06:20:08 +01:00
|
|
|
ModifyList {
|
|
|
|
valid: ModifyInvalid,
|
2020-01-09 11:07:14 +01:00
|
|
|
mods,
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
2019-01-31 05:29:14 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn new_purge_and_set(attr: Attribute, v: Value) -> Self {
|
2024-09-09 02:53:10 +02:00
|
|
|
Self::new_list(vec![m_purge(attr.clone()), Modify::Present(attr, v)])
|
2019-09-04 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn new_append(attr: Attribute, v: Value) -> Self {
|
2024-09-09 02:53:10 +02:00
|
|
|
Self::new_list(vec![Modify::Present(attr, v)])
|
2019-11-16 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn new_remove(attr: Attribute, pv: PartialValue) -> Self {
|
2024-09-09 02:53:10 +02:00
|
|
|
Self::new_list(vec![Modify::Removed(attr, pv)])
|
2021-10-07 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2023-09-16 04:11:06 +02:00
|
|
|
pub fn new_purge(attr: Attribute) -> Self {
|
2019-10-31 01:48:15 +01:00
|
|
|
Self::new_list(vec![m_purge(attr)])
|
|
|
|
}
|
|
|
|
|
2019-01-31 05:29:14 +01:00
|
|
|
pub fn push_mod(&mut self, modify: Modify) {
|
|
|
|
self.mods.push(modify)
|
|
|
|
}
|
2019-02-11 10:49:15 +01:00
|
|
|
|
2019-03-17 04:24:06 +01:00
|
|
|
pub fn from(
|
|
|
|
ml: &ProtoModifyList,
|
2022-12-28 08:52:25 +01:00
|
|
|
qs: &mut QueryServerWriteTransaction,
|
2019-03-17 04:24:06 +01:00
|
|
|
) -> Result<Self, OperationError> {
|
2019-02-22 07:15:48 +01:00
|
|
|
// For each ProtoModify, do a from.
|
2021-09-21 04:42:00 +02:00
|
|
|
let inner: Result<Vec<_>, _> = ml.mods.iter().map(|pm| Modify::from(pm, qs)).collect();
|
2019-03-17 04:24:06 +01:00
|
|
|
match inner {
|
|
|
|
Ok(m) => Ok(ModifyList {
|
|
|
|
valid: ModifyInvalid,
|
|
|
|
mods: m,
|
|
|
|
}),
|
|
|
|
Err(e) => Err(e),
|
2019-02-22 07:15:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-12 06:20:08 +01:00
|
|
|
|
2021-06-29 06:23:39 +02:00
|
|
|
pub fn from_patch(
|
|
|
|
pe: &ProtoEntry,
|
2022-12-28 08:52:25 +01:00
|
|
|
qs: &mut QueryServerWriteTransaction,
|
2021-06-29 06:23:39 +02:00
|
|
|
) -> Result<Self, OperationError> {
|
2024-05-30 12:22:19 +02:00
|
|
|
let mut mods = Vec::with_capacity(0);
|
2021-06-29 06:23:39 +02:00
|
|
|
|
|
|
|
pe.attrs.iter().try_for_each(|(attr, vals)| {
|
|
|
|
// Issue a purge to the attr.
|
2024-09-09 02:53:10 +02:00
|
|
|
let attr: Attribute = attr.as_str().into();
|
|
|
|
mods.push(m_purge(attr.clone()));
|
2021-06-29 06:23:39 +02:00
|
|
|
// Now if there are vals, push those too.
|
|
|
|
// For each value we want to now be present.
|
|
|
|
vals.iter().try_for_each(|val| {
|
2024-09-09 02:53:10 +02:00
|
|
|
qs.clone_value(&attr, val).map(|resolved_v| {
|
|
|
|
mods.push(Modify::Present(attr.clone(), resolved_v));
|
2021-06-29 06:23:39 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok(ModifyList {
|
|
|
|
valid: ModifyInvalid,
|
|
|
|
mods,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-12 06:40:25 +01:00
|
|
|
pub fn validate(
|
|
|
|
&self,
|
2019-09-06 05:05:27 +02:00
|
|
|
schema: &dyn SchemaTransaction,
|
2019-03-12 06:40:25 +01:00
|
|
|
) -> Result<ModifyList<ModifyValid>, SchemaError> {
|
2019-03-12 06:40:19 +01:00
|
|
|
let schema_attributes = schema.get_attributes();
|
2019-08-27 01:36:54 +02:00
|
|
|
/*
|
2019-03-12 06:40:19 +01:00
|
|
|
let schema_name = schema_attributes
|
2023-09-05 08:58:42 +02:00
|
|
|
.get(Attribute::Name.as_ref()")
|
2019-03-12 06:40:19 +01:00
|
|
|
.expect("Critical: Core schema corrupt or missing. To initiate a core transfer, please deposit substitute core in receptacle.");
|
2019-08-27 01:36:54 +02:00
|
|
|
*/
|
2019-03-12 06:40:19 +01:00
|
|
|
|
2022-10-29 11:07:54 +02:00
|
|
|
let res: Result<Vec<Modify>, _> = self
|
|
|
|
.mods
|
2020-01-09 11:07:14 +01:00
|
|
|
.iter()
|
2019-03-12 06:40:25 +01:00
|
|
|
.map(|m| match m {
|
2024-09-09 02:53:10 +02:00
|
|
|
Modify::Present(attr, value) => match schema_attributes.get(attr) {
|
2023-09-16 04:11:06 +02:00
|
|
|
Some(schema_a) => schema_a
|
2024-09-09 02:53:10 +02:00
|
|
|
.validate_value(attr, value)
|
|
|
|
.map(|_| Modify::Present(attr.clone(), value.clone())),
|
|
|
|
None => Err(SchemaError::InvalidAttribute(attr.to_string())),
|
|
|
|
},
|
|
|
|
Modify::Removed(attr, value) => match schema_attributes.get(attr) {
|
|
|
|
Some(schema_a) => schema_a
|
|
|
|
.validate_partialvalue(attr, value)
|
|
|
|
.map(|_| Modify::Removed(attr.clone(), value.clone())),
|
|
|
|
None => Err(SchemaError::InvalidAttribute(attr.to_string())),
|
|
|
|
},
|
|
|
|
Modify::Assert(attr, value) => match schema_attributes.get(attr) {
|
|
|
|
Some(schema_a) => schema_a
|
|
|
|
.validate_partialvalue(attr, value)
|
|
|
|
.map(|_| Modify::Assert(attr.clone(), value.clone())),
|
|
|
|
None => Err(SchemaError::InvalidAttribute(attr.to_string())),
|
|
|
|
},
|
|
|
|
Modify::Purged(attr) => match schema_attributes.get(attr) {
|
|
|
|
Some(_attr_name) => Ok(Modify::Purged(attr.clone())),
|
2023-09-16 04:11:06 +02:00
|
|
|
None => Err(SchemaError::InvalidAttribute(attr.to_string())),
|
|
|
|
},
|
2019-03-12 06:40:19 +01:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let valid_mods = match res {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2019-03-12 06:20:08 +01:00
|
|
|
|
|
|
|
// Return new ModifyList!
|
2019-03-12 06:40:19 +01:00
|
|
|
Ok(ModifyList {
|
|
|
|
valid: ModifyValid,
|
|
|
|
mods: valid_mods,
|
|
|
|
})
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
2019-06-07 11:19:09 +02:00
|
|
|
|
2023-07-31 04:20:52 +02:00
|
|
|
/// ⚠️ - Convert a modlist to be considered valid, bypassing schema.
|
|
|
|
/// This is a TEST ONLY method and will never be exposed in production.
|
2022-09-11 04:23:57 +02:00
|
|
|
#[cfg(test)]
|
2023-07-31 04:20:52 +02:00
|
|
|
pub(crate) fn into_valid(self) -> ModifyList<ModifyValid> {
|
2019-06-07 11:19:09 +02:00
|
|
|
ModifyList {
|
|
|
|
valid: ModifyValid,
|
|
|
|
mods: self.mods,
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModifyList<ModifyValid> {
|
2023-07-31 04:20:52 +02:00
|
|
|
/// ⚠️ - Create a new modlist that is considered valid, bypassing schema.
|
|
|
|
/// This is a TEST ONLY method and will never be exposed in production.
|
2019-03-12 06:20:08 +01:00
|
|
|
#[cfg(test)]
|
2023-07-31 04:20:52 +02:00
|
|
|
pub fn new_valid_list(mods: Vec<Modify>) -> Self {
|
2019-03-12 06:20:08 +01:00
|
|
|
ModifyList {
|
|
|
|
valid: ModifyValid,
|
2020-01-09 11:07:14 +01:00
|
|
|
mods,
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 11:19:09 +02:00
|
|
|
|
|
|
|
pub fn iter(&self) -> slice::Iter<Modify> {
|
|
|
|
self.mods.iter()
|
|
|
|
}
|
2019-03-12 06:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<VALID> ModifyList<VALID> {
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.mods.len()
|
|
|
|
}
|
2022-02-20 03:43:38 +01:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
2019-01-31 05:29:14 +01:00
|
|
|
}
|