cf-gears-static-authz-plugin 0.2.0

AuthZ resolver plugin with static tenant-scoped policy for development and testing
Documentation
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
// Created: 2026-04-14 by Constructor Tech
use super::*;
use authz_resolver_sdk::pep::{ConstraintCompileError, IntoPropertyValue, compile_to_access_scope};
use authz_resolver_sdk::{Action, EvaluationRequestContext, Resource, Subject, TenantContext};
use std::collections::HashMap;
use toolkit_gts::gts_id;

/// Build a request whose PEP declares nothing it can constrain on -- the shape a
/// platform-global `ResourceType` (`from_static(.., &[])`) sends.
fn make_request(require_constraints: bool, tenant_id: Option<Uuid>) -> EvaluationRequest {
    let mut subject_properties = HashMap::new();
    subject_properties.insert(
        "tenant_id".to_owned(),
        serde_json::Value::String("22222222-2222-2222-2222-222222222222".to_owned()),
    );

    EvaluationRequest {
        subject: Subject {
            id: Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap(),
            subject_type: None,
            properties: subject_properties,
        },
        action: Action {
            name: "list".to_owned(),
        },
        resource: Resource {
            resource_type: gts_id!("cf.core.users.user.v1~").to_owned(),
            id: None,
            properties: HashMap::new(),
        },
        context: EvaluationRequestContext {
            tenant_context: tenant_id.map(|id| TenantContext {
                root_id: Some(id),
                ..TenantContext::default()
            }),
            token_scopes: vec!["*".to_owned()],
            require_constraints,
            capabilities: vec![],
            supported_properties: vec![],
            bearer_token: None,
        },
    }
}

/// Build a request whose PEP declares `OWNER_TENANT_ID` -- the shape any entity
/// declared with `tenant_col` sends, and the only shape the baseline clamp binds
/// against.
fn make_owner_tenant_request(
    require_constraints: bool,
    tenant_id: Option<Uuid>,
) -> EvaluationRequest {
    let mut req = make_request(require_constraints, tenant_id);
    req.context.supported_properties = vec![pep_properties::OWNER_TENANT_ID.to_owned()];
    req
}

/// Build a request that mirrors what an AM-style PEP sends:
/// `Capability::TenantHierarchy` advertised + `RESOURCE_ID` declared on
/// the supported-properties list, so the plugin should emit the
/// `InTenantSubtree(RESOURCE_ID, tid)` constraint alongside the
/// baseline `In(OWNER_TENANT_ID, [tid])`.
fn make_tenant_hierarchy_request(tenant_id: Uuid) -> EvaluationRequest {
    let mut req = make_request(true, Some(tenant_id));
    req.context.capabilities = vec![Capability::TenantHierarchy];
    req.context.supported_properties = vec![
        pep_properties::OWNER_TENANT_ID.to_owned(),
        pep_properties::RESOURCE_ID.to_owned(),
    ];
    req
}

#[test]
fn list_operation_with_tenant_context() {
    let tenant_id = Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap();
    let service = Service::new();
    let response = service.evaluate(&make_owner_tenant_request(true, Some(tenant_id)));

    assert!(response.decision);
    assert_eq!(response.context.constraints.len(), 1);

    let constraint = &response.context.constraints[0];
    assert_eq!(constraint.predicates.len(), 1);

    match &constraint.predicates[0] {
        Predicate::In(in_pred) => {
            assert_eq!(in_pred.property, pep_properties::OWNER_TENANT_ID);
            assert_eq!(in_pred.values, vec![tenant_id.into_filter_value()]);
        }
        other => panic!("Expected In predicate, got: {other:?}"),
    }
}

#[test]
fn group_selector_is_denied_instead_of_ignored() {
    let tenant_id = Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap();
    let service = Service::new();

    for property in ["group_ids", "ancestor_group_ids"] {
        let mut request = make_owner_tenant_request(true, Some(tenant_id));
        request.resource.properties.insert(
            property.to_owned(),
            serde_json::json!([Uuid::new_v4().to_string()]),
        );

        let response = service.evaluate(&request);
        assert!(
            !response.decision,
            "the static PDP must deny unsupported {property} rather than widen scope"
        );
    }
}

#[test]
fn list_operation_without_tenant_falls_back_to_subject_properties() {
    let service = Service::new();
    let response = service.evaluate(&make_owner_tenant_request(true, None));

    // Falls back to subject.properties["tenant_id"]
    assert!(response.decision);
    assert_eq!(response.context.constraints.len(), 1);

    match &response.context.constraints[0].predicates[0] {
        Predicate::In(in_pred) => {
            assert_eq!(
                in_pred.values,
                vec![
                    Uuid::parse_str("22222222-2222-2222-2222-222222222222")
                        .unwrap()
                        .into_filter_value()
                ]
            );
        }
        other => panic!("Expected In predicate, got: {other:?}"),
    }
}

#[test]
fn nil_tenant_is_denied() {
    let service = Service::new();
    let response = service.evaluate(&make_request(true, Some(Uuid::default())));

    assert!(!response.decision);
    assert!(response.context.constraints.is_empty());
}

#[test]
fn missing_tenant_context_and_subject_property_is_denied() {
    let request = EvaluationRequest {
        subject: Subject {
            id: Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap(),
            subject_type: None,
            properties: HashMap::new(), // no tenant_id property
        },
        action: Action {
            name: "list".to_owned(),
        },
        resource: Resource {
            resource_type: gts_id!("cf.core.users.user.v1~").to_owned(),
            id: None,
            properties: HashMap::new(),
        },
        context: EvaluationRequestContext {
            tenant_context: None,
            token_scopes: vec!["*".to_owned()],
            require_constraints: true,
            capabilities: vec![],
            supported_properties: vec![],
            bearer_token: None,
        },
    };

    let service = Service::new();
    let response = service.evaluate(&request);

    assert!(!response.decision);
    assert!(response.context.constraints.is_empty());
}

#[test]
fn no_bindable_property_emits_no_constraints() {
    // The clamp cannot bind, so it must not be emitted: as the only constraint it
    // failed the whole set (`AllConstraintsFailed`, a deny). Empty either way --
    // the compiler owns what that means, see the composition tests below.
    let tenant_id = Uuid::parse_str("66666666-6666-6666-6666-666666666666").unwrap();
    let service = Service::new();

    for require_constraints in [false, true] {
        let response = service.evaluate(&make_request(require_constraints, Some(tenant_id)));

        assert!(
            response.decision,
            "require_constraints: {require_constraints}"
        );
        assert!(
            response.context.constraints.is_empty(),
            "require_constraints: {require_constraints}"
        );
    }
}

#[test]
fn empty_constraint_set_compiles_to_allow_all_when_constraints_are_not_required() {
    // Composition guard over the real compiler: a PEP that declares nothing gets
    // the `allow_all` a decision-only caller asked for, not a deny.
    let tenant_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap();
    let service = Service::new();
    let response = service.evaluate(&make_request(false, Some(tenant_id)));

    let scope = compile_to_access_scope(&response, false, &[]).expect("compiles");

    assert!(scope.is_unconstrained());
}

#[test]
fn empty_constraint_set_is_denied_by_the_pep_compiler_when_constraints_are_required() {
    // Who denies when constraints were required but none could be built: the
    // compiler (`compiler.rs` Step 1), which every gear maps to a 403. That is why
    // this service does not repeat the check.
    let tenant_id = Uuid::parse_str("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap();
    let service = Service::new();
    let response = service.evaluate(&make_request(true, Some(tenant_id)));

    let err = compile_to_access_scope(&response, true, &[]).expect_err("fails closed");

    assert!(
        matches!(err, ConstraintCompileError::ConstraintsRequiredButAbsent),
        "expected ConstraintsRequiredButAbsent, got: {err:?}"
    );
}

#[test]
fn require_constraints_false_still_clamps_when_owner_tenant_id_binds() {
    // Security regression guard. `require_constraints: false` alone must not
    // drop the clamp. A PEP that declares OWNER_TENANT_ID can bind the clamp, so
    // the service must deliver the tenant narrowing. Some callers read a row with
    // an unscoped prefetch, then use the constrained scope to force a scoped
    // re-read. An `allow_all` scope turns that path into a cross-tenant read.
    let tenant_id = Uuid::parse_str("77777777-7777-7777-7777-777777777777").unwrap();
    let request = make_owner_tenant_request(false, Some(tenant_id));

    let service = Service::new();
    let response = service.evaluate(&request);

    assert!(response.decision);
    assert_eq!(response.context.constraints.len(), 1);
    match &response.context.constraints[0].predicates[0] {
        Predicate::In(in_pred) => {
            assert_eq!(in_pred.property, pep_properties::OWNER_TENANT_ID);
            assert_eq!(in_pred.values, vec![tenant_id.into_filter_value()]);
        }
        other => panic!("Expected In predicate, got: {other:?}"),
    }
}

#[test]
fn require_constraints_false_still_clamps_when_only_the_subtree_predicate_binds() {
    // Security regression guard for the `no_tenant, resource_col = "..."` shape.
    // OWNER_TENANT_ID does not bind here, but the subtree predicate binds with
    // `Capability::TenantHierarchy` and RESOURCE_ID. The service must emit that
    // predicate, and not an unconstrained permit.
    let tenant_id = Uuid::parse_str("88888888-8888-8888-8888-888888888888").unwrap();
    let mut request = make_request(false, Some(tenant_id));
    request.context.capabilities = vec![Capability::TenantHierarchy];
    request.context.supported_properties = vec![pep_properties::RESOURCE_ID.to_owned()];

    let service = Service::new();
    let response = service.evaluate(&request);

    assert!(response.decision);
    // No baseline clamp: the compiler would have failed it anyway. The subtree
    // predicate carries the narrowing on its own.
    assert_eq!(response.context.constraints.len(), 1);
    match &response.context.constraints[0].predicates[0] {
        Predicate::InTenantSubtree(sub_pred) => {
            assert_eq!(sub_pred.property, pep_properties::RESOURCE_ID);
            assert_eq!(sub_pred.root_tenant_id, tenant_id.into_filter_value());
        }
        other => panic!("Expected InTenantSubtree predicate, got: {other:?}"),
    }

    // Composition guard: dropping the dead baseline must not widen the compiled
    // scope to `allow_all`.
    let scope = compile_to_access_scope(&response, false, &[pep_properties::RESOURCE_ID])
        .expect("compiles");
    assert!(!scope.is_unconstrained());
    assert_eq!(scope.constraints().len(), 1);
}

#[test]
fn require_constraints_false_nil_tenant_is_still_denied() {
    // Security regression guard. The tenant denials run before any constraint is
    // built, so a permissive constraint set cannot bypass the nil-tenant deny.
    let service = Service::new();
    let response = service.evaluate(&make_request(false, Some(Uuid::default())));

    assert!(!response.decision);
    assert!(response.context.constraints.is_empty());
}

#[test]
fn require_constraints_false_unresolvable_tenant_is_still_denied() {
    // Security regression guard. The tenant denials run before any constraint is
    // built, so no resolvable tenant is a deny even when the PEP declares nothing.
    let request = EvaluationRequest {
        subject: Subject {
            id: Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap(),
            subject_type: None,
            properties: HashMap::new(), // no tenant_id property
        },
        action: Action {
            name: "list".to_owned(),
        },
        resource: Resource {
            resource_type: gts_id!("cf.core.users.user.v1~").to_owned(),
            id: None,
            properties: HashMap::new(),
        },
        context: EvaluationRequestContext {
            tenant_context: None,
            token_scopes: vec!["*".to_owned()],
            require_constraints: false,
            capabilities: vec![],
            supported_properties: vec![],
            bearer_token: None,
        },
    };

    let service = Service::new();
    let response = service.evaluate(&request);

    assert!(!response.decision);
    assert!(response.context.constraints.is_empty());
}

#[test]
fn tenant_hierarchy_capability_emits_in_tenant_subtree_for_both_supported_properties() {
    let tenant_id = Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap();
    let service = Service::new();
    let response = service.evaluate(&make_tenant_hierarchy_request(tenant_id));

    assert!(response.decision);

    // Three parallel constraints OR-ed by the PEP compiler:
    //   1. legacy `In(OWNER_TENANT_ID)` clamp (binds via `tenant_col`)
    //   2. `InTenantSubtree(OWNER_TENANT_ID, tid)` (binds via `tenant_col`
    //      against entities that opt-in via `Capability::TenantHierarchy`
    //      so children of the caller's tenant become visible)
    //   3. `InTenantSubtree(RESOURCE_ID, tid)` (binds via `resource_col`
    //      against `no_tenant` entities like AM's `tenants`)
    //
    // The SecureORM compiler drops the predicates whose property doesn't
    // resolve on the entity, so each entity shape ends up with only the
    // constraints that actually bind.
    assert_eq!(response.context.constraints.len(), 3);

    match &response.context.constraints[0].predicates[0] {
        Predicate::In(in_pred) => {
            assert_eq!(in_pred.property, pep_properties::OWNER_TENANT_ID);
            assert_eq!(in_pred.values, vec![tenant_id.into_filter_value()]);
        }
        other => panic!("Expected In predicate, got: {other:?}"),
    }

    match &response.context.constraints[1].predicates[0] {
        Predicate::InTenantSubtree(sub_pred) => {
            assert_eq!(sub_pred.property, pep_properties::OWNER_TENANT_ID);
            assert_eq!(sub_pred.root_tenant_id, tenant_id.into_filter_value());
        }
        other => panic!("Expected InTenantSubtree predicate, got: {other:?}"),
    }

    match &response.context.constraints[2].predicates[0] {
        Predicate::InTenantSubtree(sub_pred) => {
            assert_eq!(sub_pred.property, pep_properties::RESOURCE_ID);
            assert_eq!(sub_pred.root_tenant_id, tenant_id.into_filter_value());
        }
        other => panic!("Expected InTenantSubtree predicate, got: {other:?}"),
    }
}

#[test]
fn tenant_hierarchy_capability_only_emits_for_declared_supported_properties() {
    let tenant_id = Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap();
    let mut request = make_request(true, Some(tenant_id));
    request.context.capabilities = vec![Capability::TenantHierarchy];
    // RESOURCE_ID is intentionally omitted -- the PEP did not declare it
    // as a constraint property, so the plugin must NOT emit a predicate
    // bound to it (the secure-extension would have no column to bind).
    request.context.supported_properties = vec![pep_properties::OWNER_TENANT_ID.to_owned()];

    let service = Service::new();
    let response = service.evaluate(&request);

    assert!(response.decision);
    // Two constraints: the baseline In(OWNER_TENANT_ID) plus a single
    // InTenantSubtree(OWNER_TENANT_ID) since that's the only property
    // the PEP declared. No InTenantSubtree(RESOURCE_ID) is emitted.
    assert_eq!(response.context.constraints.len(), 2);
    match &response.context.constraints[0].predicates[0] {
        Predicate::In(in_pred) => {
            assert_eq!(in_pred.property, pep_properties::OWNER_TENANT_ID);
        }
        other => panic!("Expected In predicate, got: {other:?}"),
    }
    match &response.context.constraints[1].predicates[0] {
        Predicate::InTenantSubtree(sub_pred) => {
            assert_eq!(sub_pred.property, pep_properties::OWNER_TENANT_ID);
        }
        other => panic!("Expected InTenantSubtree predicate, got: {other:?}"),
    }
}