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
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{ClauseLabel, FactId, ObligationId, ParamKey, Presence, ReasonCode, TraceValue};
/// Disclosure shape for a denied grant.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DenyShape {
/// The denial may disclose that the protected resource exists.
Forbidden,
/// The denial should be presented as if the protected resource does not exist.
Hidden,
}
/// Permit or deny effect produced by evaluation.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Effect<O> {
/// Policy permitted with an outcome grade.
Permit(O),
/// Policy denied.
Deny,
}
/// Structured policy decision with obligations and a typed trace.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision<O> {
/// Permit or deny effect.
pub effect: Effect<O>,
/// Obligations attached to the decisive permit path.
pub obligations: Vec<ObligationId>,
/// Typed trace returned by pure evaluation.
pub trace: DecisionTrace<O>,
}
impl<O> Decision<O> {
/// Returns true when the decision is a permit.
#[must_use]
pub const fn is_permit(&self) -> bool {
matches!(self.effect, Effect::Permit(_))
}
}
impl<O: Serialize + Clone> Decision<O> {
/// Converts the typed trace into a durable, non-generic trace.
///
/// # Errors
///
/// Returns [`TraceError::Outcome`] when an outcome cannot be represented
/// as JSON.
pub fn to_trace(&self) -> Result<Trace, TraceError> {
let decisive = match &self.trace.decisive {
DecisiveClause::Permit {
granted,
satisfied,
label,
} => TraceClause::Permit {
granted: serde_json::to_value(granted).map_err(TraceError::Outcome)?,
satisfied: satisfied.clone(),
label: label.clone(),
},
DecisiveClause::Deny {
denied,
unsatisfied,
label,
reason,
shape,
} => TraceClause::Deny {
denied: denied
.as_ref()
.map(serde_json::to_value)
.transpose()
.map_err(TraceError::Outcome)?,
unsatisfied: unsatisfied.clone(),
label: label.clone(),
reason: reason.clone(),
shape: *shape,
},
};
Ok(Trace {
consulted: self.trace.consulted.clone(),
decisive,
})
}
/// Builds a stable denial reason for a denied grant.
///
/// # Errors
///
/// Returns [`TraceError`] when an outcome cannot be serialized or a
/// generated reason identifier fails validation.
pub fn denial_reason(&self) -> Result<Option<DenialReason>, TraceError> {
if !matches!(self.effect, Effect::Deny) {
return Ok(None);
}
let DecisiveClause::Deny {
denied,
unsatisfied,
label,
reason,
shape,
} = &self.trace.decisive
else {
return Ok(None);
};
let code = match (reason, label) {
(Some(reason), _) => reason.clone(),
(None, Some(label)) => ReasonCode::new(label.as_str()).map_err(TraceError::Identity)?,
(None, None) => return Ok(None),
};
let mut params = BTreeMap::new();
for (index, fact) in unsatisfied.iter().enumerate() {
let key = if index == 0 {
ParamKey::new("missing_fact").map_err(TraceError::Identity)?
} else {
ParamKey::new(format!("missing_fact_{index}")).map_err(TraceError::Identity)?
};
params.insert(key, ReasonValue::Fact(fact.clone()));
}
if let Some(outcome) = denied {
params.insert(
ParamKey::new("denied_outcome").map_err(TraceError::Identity)?,
ReasonValue::Outcome(serde_json::to_value(outcome).map_err(TraceError::Outcome)?),
);
}
Ok(Some(DenialReason {
code,
params,
shape: *shape,
}))
}
}
/// Typed trace produced by pure evaluation.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DecisionTrace<O> {
/// Facts read by the evaluator in first-read order.
pub consulted: Vec<(FactId, Presence)>,
/// Clause that fixed the decision effect.
pub decisive: DecisiveClause<O>,
}
/// Typed decisive clause.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DecisiveClause<O> {
/// Permit clause that fixed the decision.
Permit {
/// Granted outcome.
granted: O,
/// Facts that satisfied the deciding condition.
satisfied: Vec<FactId>,
/// Optional stable clause label.
label: Option<ClauseLabel>,
},
/// Deny clause that fixed the decision.
Deny {
/// Outcome requested by the denied grant, when one exists.
denied: Option<O>,
/// Facts that caused the deciding condition to fail.
unsatisfied: Vec<FactId>,
/// Optional stable clause label.
label: Option<ClauseLabel>,
/// Optional stable reason code.
reason: Option<ReasonCode>,
/// Disclosure shape for presentation.
shape: DenyShape,
},
}
/// Durable, non-generic decision trace.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Trace {
/// Facts read by the evaluator in first-read order.
pub consulted: Vec<(FactId, Presence)>,
/// Serialized decisive clause.
pub decisive: TraceClause,
}
/// Serialized decisive clause.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TraceClause {
/// Permit clause with a serialized outcome.
Permit {
/// Serialized granted outcome.
granted: TraceValue,
/// Facts that satisfied the deciding condition.
satisfied: Vec<FactId>,
/// Optional stable clause label.
label: Option<ClauseLabel>,
},
/// Deny clause with an optional serialized requested outcome.
Deny {
/// Serialized denied outcome, when one exists.
denied: Option<TraceValue>,
/// Facts that caused the deciding condition to fail.
unsatisfied: Vec<FactId>,
/// Optional stable clause label.
label: Option<ClauseLabel>,
/// Optional stable reason code.
reason: Option<ReasonCode>,
/// Disclosure shape for presentation.
shape: DenyShape,
},
}
/// Stable denial reason emitted by the core.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DenialReason {
/// Stable translation key.
pub code: ReasonCode,
/// Structured parameters for presentation.
pub params: BTreeMap<ParamKey, ReasonValue>,
/// Disclosure shape for presentation.
pub shape: DenyShape,
}
/// Structured denial-reason parameter value.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReasonValue {
/// String parameter.
Str(String),
/// Integer parameter.
Int(i64),
/// Fact identity parameter.
Fact(FactId),
/// Serialized outcome parameter.
Outcome(TraceValue),
}
/// Error produced while serializing trace or reason values.
#[derive(Debug, Error)]
pub enum TraceError {
/// Outcome serialization failed.
#[error("failed to serialize outcome for trace")]
Outcome(#[source] serde_json::Error),
/// Stable identity validation failed while building a reason.
#[error(transparent)]
Identity(#[from] crate::GatekeepError),
}