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
use std::convert::{From, Infallible};
use thiserror::Error;
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum Token {
#[error("internal error")]
InternalError,
#[error("error deserializing or verifying the token")]
Format(Format),
#[error("multiple blocks declare the same symbols")]
SymbolTableOverlap,
#[error("tried to append a block to a sealed token")]
AppendOnSealed,
#[error("tried to seal an already sealed token")]
AlreadySealed,
#[error("authorization failed")]
FailedLogic(Logic),
#[error("error generating Datalog")]
Language(LanguageError),
#[error("Reached Datalog execution limits")]
RunLimit(RunLimit),
#[error("Cannot convert from Term: {0}")]
ConversionError(String),
#[error("Cannot decode base64 token: {0}")]
Base64(Base64Error),
}
impl From<Infallible> for Token {
fn from(_: Infallible) -> Self {
unreachable!()
}
}
impl From<Format> for Token {
fn from(e: Format) -> Self {
Token::Format(e)
}
}
impl From<Logic> for Token {
fn from(e: Logic) -> Self {
Token::FailedLogic(e)
}
}
impl<'a> From<crate::parser::Error<'a>> for Token {
fn from(e: crate::parser::Error<'a>) -> Self {
Token::Language(LanguageError::ParseError(e.into()))
}
}
impl<'a> From<Vec<crate::parser::Error<'a>>> for Token {
fn from(e: Vec<crate::parser::Error<'a>>) -> Self {
Token::Language(LanguageError::ParseError(e.into()))
}
}
impl From<base64::DecodeError> for Token {
fn from(e: base64::DecodeError) -> Self {
let err = match e {
base64::DecodeError::InvalidByte(offset, byte) => {
Base64Error::InvalidByte(offset, byte)
}
base64::DecodeError::InvalidLength => Base64Error::InvalidLength,
base64::DecodeError::InvalidLastSymbol(offset, byte) => {
Base64Error::InvalidLastSymbol(offset, byte)
}
};
Token::Base64(err)
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum Base64Error {
InvalidByte(usize, u8),
InvalidLength,
InvalidLastSymbol(usize, u8),
}
impl std::fmt::Display for Base64Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Base64Error::InvalidByte(index, byte) => {
write!(f, "Invalid byte {}, offset {}.", byte, index)
}
Base64Error::InvalidLength => write!(f, "Encoded text cannot have a 6-bit remainder."),
Base64Error::InvalidLastSymbol(index, byte) => {
write!(f, "Invalid last symbol {}, offset {}.", byte, index)
}
}
}
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum Format {
#[error("failed verifying the signature")]
Signature(Signature),
#[error("failed verifying the signature of a sealed token")]
SealedSignature,
#[error("the token does not provide intermediate public keys")]
EmptyKeys,
#[error("the root public key was not recognized")]
UnknownPublicKey,
#[error("could not deserialize the wrapper object")]
DeserializationError(String),
#[error("could not serialize the wrapper object")]
SerializationError(String),
#[error("could not deserialize the block")]
BlockDeserializationError(String),
#[error("could not serialize the block")]
BlockSerializationError(String),
#[error("Block format version is higher than supported")]
Version {
maximum: u32,
minimum: u32,
actual: u32,
},
#[error("invalid key size")]
InvalidKeySize(usize),
#[error("invalid signature size")]
InvalidSignatureSize(usize),
#[error("invalid key")]
InvalidKey(String),
#[error("could not deserialize signature")]
SignatureDeserializationError(String),
#[error("could not deserialize the block signature")]
BlockSignatureDeserializationError(String),
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum Signature {
#[error("could not parse the signature elements")]
InvalidFormat,
#[error("the signature did not match")]
InvalidSignature(String),
#[error("could not sign")]
InvalidSignatureGeneration(String),
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum Logic {
#[error("a rule provided by a block is generating facts with the authority or ambient tag, or has head variables not used in its body")]
InvalidBlockRule(u32, String),
#[error("authorization failed")]
Unauthorized {
policy: MatchedPolicy,
checks: Vec<FailedCheck>,
},
#[error("the authorizer already contains a token")]
AuthorizerNotEmpty,
#[error("no matching policy was found")]
NoMatchingPolicy {
checks: Vec<FailedCheck>,
},
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum MatchedPolicy {
#[error("an allow policy matched")]
Allow(usize),
#[error("a deny policy matched")]
Deny(usize),
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum FailedCheck {
#[error("a check failed in a block")]
Block(FailedBlockCheck),
#[error("a check provided by the authorizer failed")]
Authorizer(FailedAuthorizerCheck),
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub struct FailedBlockCheck {
pub block_id: u32,
pub check_id: u32,
pub rule: String,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub struct FailedAuthorizerCheck {
pub check_id: u32,
pub rule: String,
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum RunLimit {
#[error("too many facts generated")]
TooManyFacts,
#[error("too many engine iterations")]
TooManyIterations,
#[error("spent too much time verifying")]
Timeout,
}
#[derive(Error, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub enum LanguageError {
#[error("datalog parsing error")]
ParseError(ParseErrors),
#[error("facts must not contain unbound parameters")]
Builder { invalid_parameters: Vec<String> },
#[error("cannot set value for an unknown parameter")]
UnknownParameter(String),
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub struct ParseErrors {
pub errors: Vec<ParseError>,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-error", derive(serde::Serialize, serde::Deserialize))]
pub struct ParseError {
pub input: String,
pub message: Option<String>,
}
impl<'a> From<crate::parser::Error<'a>> for ParseError {
fn from(e: crate::parser::Error<'a>) -> Self {
ParseError {
input: e.input.to_string(),
message: e.message,
}
}
}
impl<'a> From<crate::parser::Error<'a>> for ParseErrors {
fn from(error: crate::parser::Error<'a>) -> Self {
ParseErrors {
errors: vec![error.into()],
}
}
}
impl<'a> From<Vec<crate::parser::Error<'a>>> for ParseErrors {
fn from(errors: Vec<crate::parser::Error<'a>>) -> Self {
ParseErrors {
errors: errors.into_iter().map(|e| e.into()).collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_format_strings() {
assert_eq!(
format!("{}", Token::ConversionError("test".to_owned())),
"Cannot convert from Term: test"
);
assert_eq!(
format!("{}", Token::Base64(Base64Error::InvalidLength)),
"Cannot decode base64 token: Encoded text cannot have a 6-bit remainder."
);
}
}