-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple.rs
547 lines (491 loc) · 19.6 KB
/
simple.rs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Provides a simple authorizer that takes a `SimplePolicySetProvider` and a `SimpleEntityProvider`.
use std::sync::Arc;
use cedar_policy::{Entities, Request, Response};
#[cfg(feature = "partial-eval")]
use cedar_policy::{
PartialResponse, PartialResponse::Concrete, PartialResponse::Residual, ResidualResponse,
};
use derive_builder::Builder;
use thiserror::Error;
use tokio::join;
use tracing::{debug, error, event, info, instrument};
use tracing_core::Level;
use uuid::Uuid;
use crate::public::log::schema::OpenCyberSecurityFramework;
use crate::public::{log, EntityProviderError, SimpleEntityProvider};
use crate::public::{PolicySetProviderError, SimplePolicySetProvider};
/// The `AuthorizerConfig` provides customers the ability to build their own
/// simple authorizer.
#[derive(Default, Builder, Debug)]
#[builder(pattern = "owned")]
pub struct AuthorizerConfig<P, E> {
/// An atomic reference counter to a policy set provider
pub policy_set_provider: Arc<P>,
/// An atomic reference counter to an entity provider
pub entity_provider: Arc<E>,
/// An optional Logging Configuration
#[builder(setter(into, strip_option), default)]
pub log_config: Option<log::Config>,
}
/// `AuthorizerError` can be thrown when a provider fails to gather data.
#[derive(Error, Debug)]
pub enum AuthorizerError {
/// Thrown when the `SimplePolicySetProvider` fails to get a `PolicySet`.
#[error("The Policy Set Provider failed to get a policy set: {0}")]
PolicySetProviderError(#[source] PolicySetProviderError),
/// Thrown when the `SimpleEntityProvider` fails to get `Entities`.
#[error("The Entity provider failed to get the entities: {0}")]
EntityProviderError(#[source] EntityProviderError),
/// Handles catching generic errors within the `Authorizer`.
#[error("General error that can occur within the Authorizer: {0}")]
General(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
}
impl From<PolicySetProviderError> for AuthorizerError {
fn from(value: PolicySetProviderError) -> Self {
Self::PolicySetProviderError(value)
}
}
impl From<EntityProviderError> for AuthorizerError {
fn from(value: EntityProviderError) -> Self {
Self::EntityProviderError(value)
}
}
/// `Authorizer` that provides an `is_authorized` implementation
#[derive(Debug)]
pub struct Authorizer<P, E>
where
P: SimplePolicySetProvider + 'static,
E: SimpleEntityProvider + 'static,
{
/// the policy set provider -- provides the policies from a source location
policy_set_provider: Arc<P>,
/// the entity provider -- provides entities from on disk, a database look up etc.
entity_provider: Arc<E>,
/// Log config
log_config: log::Config,
}
impl<P, E> Authorizer<P, E>
where
P: SimplePolicySetProvider,
E: SimpleEntityProvider,
{
/// Constructor to create a `simple::Authorizer`
///
/// # Examples:
///
/// ```
/// use std::sync::Arc;
/// use cedar_local_agent::public::file::entity_provider::EntityProvider;
/// use cedar_local_agent::public::file::policy_set_provider::{ConfigBuilder, PolicySetProvider};
/// use cedar_local_agent::public::simple::{Authorizer, AuthorizerConfigBuilder};
///
/// let authorizer: Authorizer<PolicySetProvider, EntityProvider> = Authorizer::new(AuthorizerConfigBuilder::default()
/// .policy_set_provider(Arc::new(PolicySetProvider::new(
/// ConfigBuilder::default()
/// .policy_set_path("tests/data/sweets.cedar")
/// .build()
/// .unwrap())
/// .unwrap()))
/// .entity_provider(Arc::new(EntityProvider::default()))
/// .build()
/// .unwrap());
/// ```
#[instrument(skip(configuration))]
pub fn new(configuration: AuthorizerConfig<P, E>) -> Self {
let log_config = configuration.log_config.unwrap_or_default();
let entity_provider = configuration.entity_provider;
info!("Initialized Entity Provider");
let policy_set_provider = configuration.policy_set_provider;
info!("Initialized Policy Set Provider");
info!(
"Initialize Simple Authorizer: authorizer_id= {:?}",
log_config.requester
);
Self {
policy_set_provider,
entity_provider,
log_config,
}
}
/// Authorize
///
/// # Errors
///
/// This function can error if either the entity or policy set providers fails.
#[instrument(fields(request_id = %Uuid::new_v4(), authorizer_id = %self.log_config.requester), skip_all, err(Debug))]
pub async fn is_authorized(
&self,
request: &Request,
entities: &Entities,
) -> Result<Response, AuthorizerError> {
info!("Received request, running is_authorized...");
let entities_future = self.entity_provider.get_entities(request);
let policy_set_future = self.policy_set_provider.get_policy_set(request);
let (fetched_entities, policy_set) = join!(entities_future, policy_set_future);
let merged_entities = Entities::from_entities(
fetched_entities?
.as_ref()
.iter()
.chain(entities.iter())
.cloned(),
None,
)
.map_err(|e| AuthorizerError::General(Box::new(e)))?;
let response = cedar_policy::Authorizer::new().is_authorized(
request,
policy_set?.as_ref(),
&merged_entities,
);
info!("Fetched Authorization data from Policy Set Provider and Entity Provider");
info!("Generated OCSF log record.");
self.log(request, &response, entities);
info!(
"Is_authorized completed: response_decision={:?}",
response.decision()
);
debug!(
"This decision was reached because: response_diagnostics={:?}",
response.diagnostics()
);
Ok(response)
}
#[instrument(skip_all)]
fn log(&self, request: &Request, response: &Response, entities: &Entities) {
event!(target: "cedar::simple::authorizer", Level::INFO, "{}",
serde_json::to_string(
&OpenCyberSecurityFramework::create(
request,
response,
entities,
&self.log_config.field_set,
self.log_config.requester.as_str(),
)
.unwrap_or_else(|e| {
OpenCyberSecurityFramework::error(e.to_string(), self.log_config.requester.clone())
})
).unwrap_or_else(|_| "Failed to deserialize a known Open Cyber Security Framework string.".to_string()),
);
}
/// Authorize Partial
///
/// # Errors
///
/// This function can error if either the entity or policy set providers fails.
#[cfg(feature = "partial-eval")]
#[instrument(fields(request_id = %Uuid::new_v4(), authorizer_id = %self.log_config.requester), skip_all, err(Debug))]
pub async fn is_authorized_partial(
&self,
request: &Request,
entities: &Entities,
) -> Result<PartialResponse, AuthorizerError> {
info!("Received request, running is_authorized_partial...");
let entities_future = self.entity_provider.get_entities(request);
let policy_set_future = self.policy_set_provider.get_policy_set(request);
let (fetched_entities, policy_set) = join!(entities_future, policy_set_future);
let merged_entities = Entities::from_entities(
fetched_entities?
.as_ref()
.iter()
.chain(entities.iter())
.cloned(),
None,
)
.map_err(|e| AuthorizerError::General(Box::new(e)))?;
let partial_response = cedar_policy::Authorizer::new().is_authorized_partial(
request,
policy_set?.as_ref(),
&merged_entities.partial(),
);
info!("Fetched Authorization data from Policy Set Provider and Entity Provider");
// Skip logging for now
info!("Generated OCSF log record.");
match &partial_response {
Concrete(response) => self.log(request, response, entities),
Residual(residual_response) => self.log_residual(request, residual_response, entities),
};
info!(
"Is_authorized_partial completed: response_decision={}",
match &partial_response {
Concrete(response) => format!("{:?}", response.decision()),
Residual(residual_response) => format!("{:?}", residual_response.residuals()),
}
);
debug!(
"This decision was reached because: response_diagnostics={:?}",
match &partial_response {
Concrete(response) => response.diagnostics(),
Residual(residual_response) => residual_response.diagnostics(),
}
);
Ok(partial_response)
}
#[cfg(feature = "partial-eval")]
#[instrument(skip_all)]
fn log_residual(
&self,
request: &Request,
residual_response: &ResidualResponse,
entities: &Entities,
) {
event!(target: "cedar::simple::authorizer", Level::INFO, "{}",
serde_json::to_string(
&OpenCyberSecurityFramework::create_generic(
request,
residual_response.diagnostics(),
residual_response.residuals().policies()
.map(|policy| format!("{}", policy.id()))
.collect::<Vec<String>>()
.join(", ")
.as_str(),
String::from("Residuals"),
entities,
&self.log_config.field_set,
self.log_config.requester.as_str()
).unwrap_or_else(|e| {
OpenCyberSecurityFramework::error(e.to_string(), self.log_config.requester.clone())
})
).unwrap_or_else(|_| "Failed to deserialize a known Open Cyber Security Framework string.".to_string()),
);
}
}
#[cfg(test)]
mod test {
use std::fmt::Error;
use std::sync::Arc;
use async_trait::async_trait;
use cedar_policy::{Context, Entities, PolicySet, Request};
use cedar_policy_core::authorizer::Decision;
use crate::public::log::DEFAULT_REQUESTER_NAME;
use crate::public::simple::{Authorizer, AuthorizerConfigBuilder};
use crate::public::{
EntityProviderError, PolicySetProviderError, SimpleEntityProvider, SimplePolicySetProvider,
};
#[derive(Debug, Default)]
pub struct MockPolicySetProvider;
#[async_trait]
impl SimplePolicySetProvider for MockPolicySetProvider {
async fn get_policy_set(
&self,
_: &Request,
) -> Result<Arc<PolicySet>, PolicySetProviderError> {
Ok(Arc::new(PolicySet::new()))
}
}
#[derive(Debug, Default)]
pub struct MockEntityProvider;
#[async_trait]
impl SimpleEntityProvider for MockEntityProvider {
async fn get_entities(&self, _: &Request) -> Result<Arc<Entities>, EntityProviderError> {
Ok(Arc::new(Entities::empty()))
}
}
#[tokio::test]
async fn simple_authorizer_is_ok() {
let authorizer: Authorizer<MockPolicySetProvider, MockEntityProvider> = Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProvider))
.entity_provider(Arc::new(MockEntityProvider))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"10""#.parse().unwrap()),
Context::empty(),
None,
)
.unwrap(),
&Entities::empty(),
)
.await;
assert!(result.is_ok());
assert_eq!(result.unwrap().decision(), Decision::Deny);
assert_eq!(authorizer.log_config.requester, DEFAULT_REQUESTER_NAME);
assert!(!authorizer.log_config.field_set.principal);
}
#[derive(Debug, Default)]
pub struct MockEntityProviderError;
#[async_trait]
impl SimpleEntityProvider for MockEntityProviderError {
async fn get_entities(&self, _: &Request) -> Result<Arc<Entities>, EntityProviderError> {
Err(EntityProviderError::General(Box::<Error>::default()))
}
}
#[tokio::test]
async fn simple_authorizer_bad_entity_provider() {
let authorizer: Authorizer<MockPolicySetProvider, MockEntityProviderError> =
Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProvider))
.entity_provider(Arc::new(MockEntityProviderError))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"2""#.parse().unwrap()),
Context::empty(),
None,
)
.unwrap(),
&Entities::empty(),
)
.await;
assert!(result.is_err());
}
#[derive(Debug, Default)]
pub struct MockPolicySetProviderError;
#[async_trait]
impl SimplePolicySetProvider for MockPolicySetProviderError {
async fn get_policy_set(
&self,
_: &Request,
) -> Result<Arc<PolicySet>, PolicySetProviderError> {
Err(PolicySetProviderError::General(Box::<Error>::default()))
}
}
#[tokio::test]
async fn simple_authorizer_bad_policy_set_provider() {
let authorizer: Authorizer<MockPolicySetProviderError, MockEntityProvider> =
Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProviderError))
.entity_provider(Arc::new(MockEntityProvider))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized(
&Request::new(
Some(r#"User::"Mike""#.parse().unwrap()),
Some(r#"Action::"View""#.parse().unwrap()),
Some(r#"Box::"3""#.parse().unwrap()),
Context::empty(),
None,
)
.unwrap(),
&Entities::empty(),
)
.await;
assert!(result.is_err());
}
}
#[cfg(test)]
#[cfg(feature = "partial-eval")]
mod test_partial {
use std::sync::Arc;
use async_trait::async_trait;
use cedar_policy::{Context, Entities, PartialResponse, Policy, PolicySet, Request};
use cedar_policy_core::authorizer::Decision;
use cool_asserts::assert_matches;
use crate::public::log::DEFAULT_REQUESTER_NAME;
use crate::public::simple::test::{MockEntityProvider, MockPolicySetProvider};
use crate::public::simple::{Authorizer, AuthorizerConfigBuilder};
use crate::public::{PolicySetProviderError, SimplePolicySetProvider};
#[tokio::test]
async fn simple_authorizer_partial_no_resource_is_ok() {
let authorizer: Authorizer<MockPolicySetProvider, MockEntityProvider> = Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProvider))
.entity_provider(Arc::new(MockEntityProvider))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized_partial(
&Request::builder()
.principal(Some(r#"User::"Mike""#.parse().unwrap()))
.action(Some(r#"Action::"View""#.parse().unwrap()))
.context(Context::empty())
.build(),
&Entities::empty(),
)
.await;
assert_matches!(result, Ok(partial_response) =>
assert_matches!(partial_response, PartialResponse::Concrete(response) =>
assert_eq!(response.decision(), Decision::Deny)
)
);
assert_eq!(authorizer.log_config.requester, DEFAULT_REQUESTER_NAME);
assert!(!authorizer.log_config.field_set.principal);
}
#[tokio::test]
async fn simple_authorizer_partial_no_principal_is_ok() {
let authorizer: Authorizer<MockPolicySetProvider, MockEntityProvider> = Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProvider))
.entity_provider(Arc::new(MockEntityProvider))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized_partial(
&Request::builder()
.action(Some(r#"Action::"View""#.parse().unwrap()))
.resource(Some(r#"Box::"10""#.parse().unwrap()))
.context(Context::empty())
.build(),
&Entities::empty(),
)
.await;
assert_matches!(result, Ok(partial_response) =>
assert_matches!(partial_response, PartialResponse::Concrete(response) =>
assert_eq!(response.decision(), Decision::Deny)
)
);
assert_eq!(authorizer.log_config.requester, DEFAULT_REQUESTER_NAME);
assert!(!authorizer.log_config.field_set.principal);
}
#[derive(Debug, Default)]
pub struct MockPolicySetProviderPartial;
#[async_trait]
impl SimplePolicySetProvider for MockPolicySetProviderPartial {
async fn get_policy_set(
&self,
_: &Request,
) -> Result<Arc<PolicySet>, PolicySetProviderError> {
let policy = Policy::parse(
Some("test".into()),
r#"permit(principal == User::"Mike", action, resource == Box::"10");"#,
)
.expect("Failed to parse");
let mut policy_set = PolicySet::new();
policy_set.add(policy).expect("Failed to add");
Ok(Arc::new(policy_set))
}
}
#[tokio::test]
async fn simple_authorizer_partial_result() {
let authorizer: Authorizer<MockPolicySetProviderPartial, MockEntityProvider> =
Authorizer::new(
AuthorizerConfigBuilder::default()
.policy_set_provider(Arc::new(MockPolicySetProviderPartial))
.entity_provider(Arc::new(MockEntityProvider))
.build()
.unwrap(),
);
let result = authorizer
.is_authorized_partial(
&Request::builder()
.action(Some(r#"Action::"View""#.parse().unwrap()))
.resource(Some(r#"Box::"10""#.parse().unwrap()))
.context(Context::empty())
.build(),
&Entities::empty(),
)
.await;
assert_matches!(result, Ok(partial_response) =>
assert_matches!(partial_response, PartialResponse::Residual(residual_response) => {
assert_eq!(residual_response.residuals().policies().count(), 1);
})
);
assert_eq!(authorizer.log_config.requester, DEFAULT_REQUESTER_NAME);
assert!(!authorizer.log_config.field_set.principal);
}
}