1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use super::QueryServerWriteTransaction;
use crate::prelude::*;
use crate::server::Plugins;
use hashbrown::HashMap;
pub type ModSetValid = HashMap<Uuid, ModifyList<ModifyValid>>;
pub struct BatchModifyEvent {
pub ident: Identity,
pub modset: ModSetValid,
}
impl<'a> QueryServerWriteTransaction<'a> {
#[instrument(level = "debug", skip_all)]
pub fn batch_modify(&mut self, me: &BatchModifyEvent) -> Result<(), OperationError> {
if !me.ident.is_internal() {
security_info!(name = %me.ident, "batch modify initiator");
}
if me.modset.is_empty() {
request_error!("empty modify request");
return Err(OperationError::EmptyRequest);
}
let filter_or = me
.modset
.keys()
.copied()
.map(|u| f_eq("uuid", PartialValue::Uuid(u)))
.collect();
let filter = filter_all!(f_or(filter_or))
.validate(self.get_schema())
.map_err(OperationError::SchemaViolation)?;
let pre_candidates = self
.impersonate_search_valid(filter.clone(), filter.clone(), &me.ident)
.map_err(|e| {
admin_error!("error in pre-candidate selection {:?}", e);
e
})?;
if pre_candidates.is_empty() {
if me.ident.is_internal() {
trace!("no candidates match filter ... continuing {:?}", filter);
return Ok(());
} else {
request_error!("no candidates match modset request, failure {:?}", filter);
return Err(OperationError::NoMatchingEntries);
}
};
if pre_candidates.len() != me.modset.len() {
error!("Inconsistent modify, some uuids were not found in request.");
return Err(OperationError::MissingEntries);
}
trace!("pre_candidates -> {:?}", pre_candidates);
trace!("modset -> {:?}", me.modset);
let access = self.get_accesscontrols();
let op_allow = access
.batch_modify_allow_operation(me, &pre_candidates)
.map_err(|e| {
admin_error!("Unable to check batch modify access {:?}", e);
e
})?;
if !op_allow {
return Err(OperationError::AccessDenied);
}
let mut candidates = pre_candidates
.iter()
.map(|er| {
let u = er.get_uuid();
let mut ent_mut = er.as_ref().clone().invalidate(self.cid.clone());
me.modset
.get(&u)
.ok_or_else(|| {
error!("No entry for uuid {} was found, aborting", u);
OperationError::NoMatchingEntries
})
.and_then(|modlist| {
ent_mut
.apply_modlist(modlist)
.map(|()| ent_mut)
.map_err(|e| {
error!("Modification failed for {}", u);
e
})
})
})
.collect::<Result<Vec<EntryInvalidCommitted>, _>>()?;
if std::iter::zip(
pre_candidates
.iter()
.map(|e| e.mask_recycled_ts().is_none()),
candidates.iter().map(|e| e.mask_recycled_ts().is_none()),
)
.any(|(a, b)| a != b)
{
admin_warn!("Refusing to apply modifications that are attempting to bypass replication state machine.");
return Err(OperationError::AccessDenied);
}
Plugins::run_pre_batch_modify(self, &mut candidates, me).map_err(|e| {
admin_error!("Pre-Modify operation failed (plugin), {:?}", e);
e
})?;
let norm_cand = candidates
.into_iter()
.map(|entry| {
entry
.validate(&self.schema)
.map_err(|e| {
admin_error!("Schema Violation in validation of modify_pre_apply {:?}", e);
OperationError::SchemaViolation(e)
})
.map(|entry| entry.seal(&self.schema))
})
.collect::<Result<Vec<EntrySealedCommitted>, _>>()?;
self.be_txn
.modify(&self.cid, &pre_candidates, &norm_cand)
.map_err(|e| {
admin_error!("Modify operation failed (backend), {:?}", e);
e
})?;
Plugins::run_post_batch_modify(self, &pre_candidates, &norm_cand, me).map_err(|e| {
admin_error!("Post-Modify operation failed (plugin), {:?}", e);
e
})?;
if !self.changed_schema {
self.changed_schema = norm_cand
.iter()
.chain(pre_candidates.iter().map(|e| e.as_ref()))
.any(|e| {
e.attribute_equality("class", &PVCLASS_CLASSTYPE)
|| e.attribute_equality("class", &PVCLASS_ATTRIBUTETYPE)
});
}
if !self.changed_acp {
self.changed_acp = norm_cand
.iter()
.chain(pre_candidates.iter().map(|e| e.as_ref()))
.any(|e| e.attribute_equality("class", &PVCLASS_ACP));
}
if !self.changed_oauth2 {
self.changed_oauth2 = norm_cand
.iter()
.chain(pre_candidates.iter().map(|e| e.as_ref()))
.any(|e| e.attribute_equality("class", &PVCLASS_OAUTH2_RS));
}
if !self.changed_domain {
self.changed_domain = norm_cand
.iter()
.chain(pre_candidates.iter().map(|e| e.as_ref()))
.any(|e| e.attribute_equality("uuid", &PVUUID_DOMAIN_INFO));
}
self.changed_uuid.extend(
norm_cand
.iter()
.map(|e| e.get_uuid())
.chain(pre_candidates.iter().map(|e| e.get_uuid())),
);
trace!(
schema_reload = ?self.changed_schema,
acp_reload = ?self.changed_acp,
oauth2_reload = ?self.changed_oauth2,
domain_reload = ?self.changed_domain,
);
if me.ident.is_internal() {
trace!("Modify operation success");
} else {
admin_info!("Modify operation success");
}
Ok(())
}
pub fn internal_batch_modify(
&mut self,
mods_iter: impl Iterator<Item = (Uuid, ModifyList<ModifyInvalid>)>,
) -> Result<(), OperationError> {
let modset = mods_iter
.map(|(u, ml)| {
ml.validate(self.get_schema())
.map(|modlist| (u, modlist))
.map_err(OperationError::SchemaViolation)
})
.collect::<Result<ModSetValid, _>>()?;
let bme = BatchModifyEvent {
ident: Identity::from_internal(),
modset,
};
self.batch_modify(&bme)
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[qs_test]
async fn test_batch_modify_basic(server: &QueryServer) {
let mut server_txn = server.write(duration_from_epoch_now()).await;
let uuid_a = Uuid::new_v4();
let uuid_b = Uuid::new_v4();
assert!(server_txn
.internal_create(vec![
entry_init!(
("class", Value::new_class("object")),
("uuid", Value::Uuid(uuid_a))
),
entry_init!(
("class", Value::new_class("object")),
("uuid", Value::Uuid(uuid_b))
),
])
.is_ok());
assert!(server_txn
.internal_batch_modify(
[
(
uuid_a,
ModifyList::new_append("description", Value::Utf8("a".into()))
),
(
uuid_b,
ModifyList::new_append("description", Value::Utf8("b".into()))
),
]
.into_iter()
)
.is_ok());
let ent_a = server_txn
.internal_search_uuid(uuid_a)
.expect("Failed to get entry.");
let ent_b = server_txn
.internal_search_uuid(uuid_b)
.expect("Failed to get entry.");
assert!(ent_a.get_ava_single_utf8("description") == Some("a"));
assert!(ent_b.get_ava_single_utf8("description") == Some("b"));
}
}