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
//! Policy evaluation engine
#![allow(dead_code)]
use super::model::*;
use crate::core::api::types::RunRequest;
use anyhow::Result;
use glob::Pattern;
use std::collections::HashMap;
/// Policy evaluation engine
pub struct PolicyEngine {
policy: PolicySet,
compiled_patterns: HashMap<String, Pattern>,
}
impl PolicyEngine {
/// Create a new policy engine with the given policy set
pub fn new(policy: PolicySet) -> Result<Self> {
let mut compiled_patterns = HashMap::new();
// Pre-compile all patterns for efficiency
for rule in &policy.rules {
let pattern = Pattern::new(&rule.pattern)?;
compiled_patterns.insert(rule.name.clone(), pattern);
}
Ok(Self {
policy,
compiled_patterns,
})
}
/// Evaluate a request against the policy
pub fn evaluate(
&self,
request: &RunRequest,
url: &str,
context: &EvaluationContext,
) -> PolicyDecision {
// SECURITY: Check authentication requirement FIRST
// This prevents bypassing auth requirements through rule matching
if self.policy.defaults.require_auth && request.auth_profile.is_none() {
// Check if any rule explicitly allows unauthenticated access
let has_explicit_unauth_allow = self.policy.rules.iter().any(|rule| {
if let Some(pattern) = self.compiled_patterns.get(&rule.name) {
if pattern.matches(url) {
// Check if this rule has conditions that explicitly don't require auth
if let Some(_conditions) = &rule.conditions {
// If conditions exist but don't check for auth, this rule doesn't override
return false;
}
// Rule matches and has no auth conditions - check if it allows this operation
if let Some(allow) = &rule.allow {
return self.matches_action(allow, request, context);
}
}
}
false
});
// If no rule explicitly allows unauthenticated access, deny
if !has_explicit_unauth_allow {
return PolicyDecision::Deny {
rule: "authentication".to_string(),
reason: "Authentication required - no rule allows unauthenticated access"
.to_string(),
audit: Some(AuditConfig {
level: self.policy.defaults.audit_level.clone(),
include_body: false,
include_response: false,
}),
};
}
}
// Check each rule in order
for rule in &self.policy.rules {
if let Some(pattern) = self.compiled_patterns.get(&rule.name) {
if pattern.matches(url) {
// Check conditions
if let Some(conditions) = &rule.conditions {
if !self.check_conditions(conditions, request, context) {
continue;
}
}
// Check deny first (deny takes precedence)
if let Some(deny) = &rule.deny {
if self.matches_action(deny, request, context) {
return PolicyDecision::Deny {
rule: rule.name.clone(),
reason: rule.explain.clone().unwrap_or_else(|| {
format!("Operation denied by rule: {}", rule.name)
}),
audit: rule.audit.clone(),
};
}
}
// Check allow - but only if auth requirements are met
if let Some(allow) = &rule.allow {
if self.matches_action(allow, request, context) {
// Double-check auth requirement for allow decisions
if self.policy.defaults.require_auth && request.auth_profile.is_none() {
// This shouldn't happen due to earlier check, but defense in depth
return PolicyDecision::Deny {
rule: rule.name.clone(),
reason: "Authentication required for this operation"
.to_string(),
audit: rule.audit.clone(),
};
}
return PolicyDecision::Allow {
rule: rule.name.clone(),
audit: rule.audit.clone(),
};
}
}
}
}
}
// No matching rule - apply defaults
self.apply_defaults(request, context)
}
/// Check if all conditions are met
fn check_conditions(
&self,
conditions: &[PolicyCondition],
request: &RunRequest,
context: &EvaluationContext,
) -> bool {
conditions.iter().all(|condition| {
// Check auth profile
if let Some(required_profile) = &condition.auth_profile {
if let Some(auth_profile) = &request.auth_profile {
if auth_profile != required_profile {
return false;
}
} else {
return false;
}
}
// Check time window
if let Some(time_window) = &condition.time_window {
if !self.check_time_window(time_window, context) {
return false;
}
}
// Check source IP
if let Some(required_ip) = &condition.source_ip {
if let Some(source_ip) = &context.source_ip {
if !self.matches_ip_pattern(source_ip, required_ip) {
return false;
}
} else {
return false;
}
}
// Check environment
if let Some(required_env) = &condition.environment {
if let Some(env) = &request.env {
if env != required_env {
return false;
}
} else {
return false;
}
}
true
})
}
/// Check if action matches the request
fn matches_action(
&self,
action: &PolicyAction,
request: &RunRequest,
context: &EvaluationContext,
) -> bool {
// Check "all" flag
if let Some(true) = action.all {
return true;
}
// Check operations
if let Some(operations) = &action.operations {
let matches = operations.iter().any(|op_pattern| {
if let Ok(pattern) = Pattern::new(op_pattern) {
pattern.matches(&request.operation_id)
} else {
op_pattern == &request.operation_id
}
});
if !matches {
return false;
}
}
// Check methods
if let Some(methods) = &action.methods {
if let Some(method) = &context.method {
if !methods.iter().any(|m| m.eq_ignore_ascii_case(method)) {
return false;
}
}
}
// Check tags
if let Some(required_tags) = &action.tags {
if let Some(operation_tags) = &context.tags {
let has_required_tag = required_tags
.iter()
.any(|required| operation_tags.contains(required));
if !has_required_tag {
return false;
}
} else if !required_tags.is_empty() {
return false;
}
}
true
}
/// Apply default policy when no rules match
fn apply_defaults(&self, request: &RunRequest, context: &EvaluationContext) -> PolicyDecision {
// Check if authentication is required
if self.policy.defaults.require_auth && request.auth_profile.is_none() {
return PolicyDecision::Deny {
rule: "default".to_string(),
reason: "Authentication required by default policy".to_string(),
audit: Some(AuditConfig {
level: self.policy.defaults.audit_level.clone(),
include_body: false,
include_response: false,
}),
};
}
// Check if method is allowed by default
if let Some(method) = &context.method {
if !self
.policy
.defaults
.allow_methods
.iter()
.any(|m| m.eq_ignore_ascii_case(method))
{
return PolicyDecision::Deny {
rule: "default".to_string(),
reason: format!("Method {} not allowed by default policy", method),
audit: Some(AuditConfig {
level: self.policy.defaults.audit_level.clone(),
include_body: false,
include_response: false,
}),
};
}
}
// Default deny for safety
PolicyDecision::Deny {
rule: "default".to_string(),
reason: "No matching allow rule found".to_string(),
audit: Some(AuditConfig {
level: self.policy.defaults.audit_level.clone(),
include_body: false,
include_response: false,
}),
}
}
/// Check time window constraint
fn check_time_window(&self, time_window: &str, _context: &EvaluationContext) -> bool {
match time_window {
"business_hours" => {
// TODO: Implement business hours check
true
}
"weekdays" => {
// TODO: Implement weekdays check
true
}
_ => false,
}
}
/// Check if IP matches pattern (supports CIDR notation)
fn matches_ip_pattern(&self, ip: &str, pattern: &str) -> bool {
// TODO: Implement proper IP matching with CIDR support
ip == pattern
}
}
/// Evaluation context provides additional information for policy decisions
#[derive(Debug, Clone)]
pub struct EvaluationContext {
/// HTTP method of the operation
pub method: Option<String>,
/// Tags associated with the operation
pub tags: Option<Vec<String>>,
/// Source IP address
pub source_ip: Option<String>,
/// Current timestamp
pub timestamp: chrono::DateTime<chrono::Utc>,
}
impl Default for EvaluationContext {
fn default() -> Self {
Self {
method: None,
tags: None,
source_ip: None,
timestamp: chrono::Utc::now(),
}
}
}
/// Result of policy evaluation
#[derive(Debug, Clone)]
pub enum PolicyDecision {
Allow {
rule: String,
audit: Option<AuditConfig>,
},
Deny {
rule: String,
reason: String,
audit: Option<AuditConfig>,
},
}
impl PolicyDecision {
/// Check if the decision is to allow the operation
pub fn is_allowed(&self) -> bool {
matches!(self, PolicyDecision::Allow { .. })
}
/// Get the rule name that made the decision
pub fn rule_name(&self) -> &str {
match self {
PolicyDecision::Allow { rule, .. } => rule,
PolicyDecision::Deny { rule, .. } => rule,
}
}
/// Get the audit configuration for this decision
pub fn audit_config(&self) -> Option<&AuditConfig> {
match self {
PolicyDecision::Allow { audit, .. } => audit.as_ref(),
PolicyDecision::Deny { audit, .. } => audit.as_ref(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_policy() -> PolicySet {
PolicySet {
version: "1.0".to_string(),
metadata: None,
defaults: PolicyDefaults {
allow_methods: vec!["GET".to_string()],
deny_external_refs: true,
require_auth: true,
audit_level: "basic".to_string(),
},
rules: vec![PolicyRule {
name: "readonly".to_string(),
description: Some("Allow read-only operations".to_string()),
pattern: "*".to_string(),
conditions: None,
allow: Some(PolicyAction {
methods: Some(vec!["GET".to_string()]),
operations: Some(vec!["get*".to_string(), "list*".to_string()]),
all: None,
tags: None,
}),
deny: None,
audit: None,
explain: None,
}],
}
}
#[test]
fn test_allow_readonly_operation() {
let policy = create_test_policy();
let engine = PolicyEngine::new(policy).unwrap();
let request = RunRequest {
operation_id: "getUser".to_string(),
parameters: None,
body: None,
spec_path: None,
env: None,
auth_profile: Some("default".to_string()),
};
let context = EvaluationContext {
method: Some("GET".to_string()),
..Default::default()
};
let decision = engine.evaluate(&request, "https://api.example.com/users/123", &context);
assert!(decision.is_allowed());
assert_eq!(decision.rule_name(), "readonly");
}
#[test]
fn test_deny_write_operation() {
let policy = create_test_policy();
let engine = PolicyEngine::new(policy).unwrap();
let request = RunRequest {
operation_id: "createUser".to_string(),
parameters: None,
body: None,
spec_path: None,
env: None,
auth_profile: Some("default".to_string()),
};
let context = EvaluationContext {
method: Some("POST".to_string()),
..Default::default()
};
let decision = engine.evaluate(&request, "https://api.example.com/users", &context);
assert!(!decision.is_allowed());
}
#[test]
fn test_deny_no_auth() {
let policy = create_test_policy();
let engine = PolicyEngine::new(policy).unwrap();
let request = RunRequest {
operation_id: "getUser".to_string(),
parameters: None,
body: None,
spec_path: None,
env: None,
auth_profile: None, // No auth
};
let context = EvaluationContext {
method: Some("GET".to_string()),
..Default::default()
};
let decision = engine.evaluate(&request, "https://api.example.com/users/123", &context);
assert!(!decision.is_allowed());
if let PolicyDecision::Deny { reason, .. } = decision {
assert!(reason.contains("Authentication required"));
}
}
}