pgroles-core 0.10.0-alpha.1

Core manifest, diff, SQL rendering, and export primitives for pgroles
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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Effect-pair extraction and intersection.
//!
//! ADR-001 Decision 6 defines when an ephemeral overlay forces fresh review of
//! a candidate's plan: **iff the set of `(role, object)` pairs touched by the
//! active overlay's effects intersects the set touched by the candidate plan's
//! effects.** The comparison is deliberately narrower than "the change digest
//! moved" — overlays legitimately move the digest, and blanket invalidation
//! would make candidates unusable on any policy with continuous ephemeral
//! traffic.
//!
//! This module is the pure half of that rule. It takes typed effects
//! ([`Change`]) and membership overlay edges and projects both into the same
//! pair space, so the operator only has to ask whether two sets intersect.
//!
//! # The pair space
//!
//! * *role* is the resolved PostgreSQL role name — these are post-expansion
//!   effects, so profile/pattern expansion has already happened.
//! * *object* is [`EffectObject`], the fully-qualified target. Wildcards have
//!   already been expanded against objects observed under the lock by the time
//!   a [`Change`] exists, so nothing here re-expands them.
//! * Membership edges normalise to `(member, EffectObject::Role(role))`: the
//!   granted role is the *object* of the effect, and the member is the role
//!   whose access changes.
//! * Role-attribute-only effects (`LOGIN`, `CONNECTION LIMIT`, passwords,
//!   comments, drops) touch [`EffectObject::RoleAttributes`] — the "(role, ∅)"
//!   of the ADR.
//!
//! # Intersection
//!
//! Two pairs intersect only if their roles match, and then:
//!
//! * a schema-level object intersects any object *within* that schema, because
//!   a grant on the schema and a grant on a table in it can be the same access;
//! * `(role, ∅)` intersects any object of the same role — an attribute change
//!   is not scoped to an object, so it cannot be shown not to overlap. This is
//!   the conservative direction: the cost of a false intersection is one extra
//!   review round, the cost of a false miss is an unreviewed effect.
//! * everything else intersects only itself.
//!
//! Roles match when they are equal, and `PUBLIC` matches every role, because a
//! privilege held by PUBLIC is held by every role.

use std::collections::BTreeSet;

use crate::diff::Change;
use crate::manifest::ObjectType;
use crate::model::{DefaultPrivilegeScope, MembershipEdge, PUBLIC_ROLE};

/// The object half of an effect pair.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum EffectObject {
    /// The role itself, with no object — the ADR's `(role, ∅)`. Attribute
    /// changes, passwords, comments, creates and drops.
    RoleAttributes,
    /// A role treated as the object of a membership edge.
    Role(String),
    /// Every object in a schema. Also the projection of a schema-scoped effect
    /// with no named target (default privileges, schema-wide grants).
    Schema(String),
    /// One named object inside a schema.
    Relation {
        schema: String,
        name: String,
        object_type: ObjectType,
    },
    /// The database itself (`GRANT CONNECT ON DATABASE ...`).
    Database(String),
}

/// One `(role, object)` pair touched by an effect.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct EffectPair {
    pub role: String,
    pub object: EffectObject,
}

impl EffectPair {
    fn new(role: impl Into<String>, object: EffectObject) -> Self {
        Self {
            role: role.into(),
            object,
        }
    }

    /// Does this pair intersect `other` under the ADR-001 Decision 6 rule?
    pub fn intersects(&self, other: &EffectPair) -> bool {
        roles_intersect(&self.role, &other.role) && objects_intersect(&self.object, &other.object)
    }
}

/// A privilege held by PUBLIC is held by every role, so an effect on PUBLIC
/// reaches the same objects as an effect on any named role. `PUBLIC` is
/// reserved and can never name a real role, so the comparison is unambiguous.
fn roles_intersect(left: &str, right: &str) -> bool {
    left == right || left == PUBLIC_ROLE || right == PUBLIC_ROLE
}

fn objects_intersect(left: &EffectObject, right: &EffectObject) -> bool {
    use EffectObject::*;
    match (left, right) {
        // An attribute-only effect is not scoped to an object, so it cannot be
        // shown not to overlap anything else on the same role.
        (RoleAttributes, _) | (_, RoleAttributes) => true,
        (Role(a), Role(b)) => a == b,
        (Schema(a), Schema(b)) => a == b,
        (Schema(schema), Relation { schema: other, .. })
        | (Relation { schema: other, .. }, Schema(schema)) => schema == other,
        (
            Relation {
                schema: a_schema,
                name: a_name,
                object_type: a_type,
            },
            Relation {
                schema: b_schema,
                name: b_name,
                object_type: b_type,
            },
        ) => a_schema == b_schema && a_name == b_name && a_type == b_type,
        (Database(a), Database(b)) => a == b,
        _ => false,
    }
}

/// Project a default-privilege scope into the pair space.
fn default_privilege_object(scope: &DefaultPrivilegeScope) -> EffectObject {
    match scope {
        DefaultPrivilegeScope::Schema { schema } => EffectObject::Schema(schema.clone()),
        // A global rule applies in every schema the owner creates objects in,
        // including schemas no policy names, so nothing bounds it to a schema
        // and it must be treated as overlapping anything on the same role.
        DefaultPrivilegeScope::Global => EffectObject::RoleAttributes,
    }
}

/// Project a grant/revoke target into the pair space.
fn grant_object(object_type: ObjectType, schema: Option<&str>, name: Option<&str>) -> EffectObject {
    match object_type {
        // A schema-typed effect names the schema in either field depending on
        // how the manifest expressed it.
        ObjectType::Schema => match (name, schema) {
            (Some(name), _) => EffectObject::Schema(name.to_string()),
            (None, Some(schema)) => EffectObject::Schema(schema.to_string()),
            (None, None) => EffectObject::RoleAttributes,
        },
        ObjectType::Database => match name {
            Some(name) => EffectObject::Database(name.to_string()),
            // A database grant with no name is the connected database; no
            // better identity is available, so it can only match another
            // unnamed one.
            None => EffectObject::Database(String::new()),
        },
        object_type => match (schema, name) {
            (Some(schema), Some(name)) => EffectObject::Relation {
                schema: schema.to_string(),
                name: name.to_string(),
                object_type,
            },
            // No named target left after expansion means the whole schema.
            (Some(schema), None) => EffectObject::Schema(schema.to_string()),
            (None, Some(name)) => EffectObject::Relation {
                schema: String::new(),
                name: name.to_string(),
                object_type,
            },
            (None, None) => EffectObject::RoleAttributes,
        },
    }
}

/// Every `(role, object)` pair touched by one effect.
pub fn change_pairs(change: &Change) -> Vec<EffectPair> {
    match change {
        Change::CreateRole { name, .. }
        | Change::AlterRole { name, .. }
        | Change::SetComment { name, .. }
        | Change::SetPassword { name, .. }
        | Change::DropRole { name } => {
            vec![EffectPair::new(name, EffectObject::RoleAttributes)]
        }
        Change::DropOwned { role } | Change::TerminateSessions { role } => {
            vec![EffectPair::new(role, EffectObject::RoleAttributes)]
        }
        Change::ReassignOwned { from_role, to_role } => vec![
            EffectPair::new(from_role, EffectObject::RoleAttributes),
            EffectPair::new(to_role, EffectObject::RoleAttributes),
        ],
        Change::CreateSchema { name, owner } => owner
            .iter()
            .map(|owner| EffectPair::new(owner, EffectObject::Schema(name.clone())))
            .collect(),
        Change::AlterSchemaOwner { name, owner }
        | Change::EnsureSchemaOwnerPrivileges { name, owner, .. } => {
            vec![EffectPair::new(owner, EffectObject::Schema(name.clone()))]
        }
        Change::Grant {
            role,
            object_type,
            schema,
            name,
            ..
        }
        | Change::Revoke {
            role,
            object_type,
            schema,
            name,
            ..
        } => vec![EffectPair::new(
            role.as_str(),
            grant_object(*object_type, schema.as_deref(), name.as_deref()),
        )],
        // Default privileges change what the grantee will hold on objects the
        // owner creates. Both roles are touched: the grantee gains access, the
        // owner's future objects change.
        Change::SetDefaultPrivilege {
            owner,
            scope,
            grantee,
            ..
        }
        | Change::RevokeDefaultPrivilege {
            owner,
            scope,
            grantee,
            ..
        } => {
            let object = default_privilege_object(scope);
            vec![
                EffectPair::new(grantee.as_str(), object.clone()),
                EffectPair::new(owner, object),
            ]
        }
        Change::AddMember { role, member, .. } | Change::RemoveMember { role, member } => {
            vec![EffectPair::new(member, EffectObject::Role(role.clone()))]
        }
    }
}

/// Every pair touched by a set of effects.
pub fn effect_pairs(changes: &[Change]) -> BTreeSet<EffectPair> {
    changes.iter().flat_map(change_pairs).collect()
}

/// Every pair touched by a set of membership overlay edges.
///
/// The overlay the operator composes today is memberships only; keeping this a
/// separate entry point means a future overlay of another shape gets its own
/// projection rather than being squeezed through [`effect_pairs`].
pub fn membership_overlay_pairs<'a>(
    edges: impl IntoIterator<Item = &'a MembershipEdge>,
) -> BTreeSet<EffectPair> {
    edges
        .into_iter()
        .map(|edge| EffectPair::new(&edge.member, EffectObject::Role(edge.role.clone())))
        .collect()
}

/// The pairs present in both sets, under the intersection rule above.
///
/// Returns the *left* set's members, because the caller reports which of the
/// candidate's own effects a reviewer must look at again.
pub fn intersecting_pairs(
    left: &BTreeSet<EffectPair>,
    right: &BTreeSet<EffectPair>,
) -> BTreeSet<EffectPair> {
    left.iter()
        .filter(|pair| right.iter().any(|other| pair.intersects(other)))
        .cloned()
        .collect()
}

/// Whether any pair in `left` intersects any pair in `right`.
pub fn pairs_intersect(left: &BTreeSet<EffectPair>, right: &BTreeSet<EffectPair>) -> bool {
    left.iter()
        .any(|pair| right.iter().any(|other| pair.intersects(other)))
}

/// A short human-readable rendering of a pair, for conditions and Events.
pub fn describe_pair(pair: &EffectPair) -> String {
    match &pair.object {
        EffectObject::RoleAttributes => format!("{} (role attributes)", pair.role),
        EffectObject::Role(role) => format!("{} -> {role} (membership)", pair.role),
        EffectObject::Schema(schema) => format!("{} on schema {schema}", pair.role),
        EffectObject::Relation {
            schema,
            name,
            object_type,
        } => format!("{} on {object_type} {schema}.{name}", pair.role),
        EffectObject::Database(name) => format!("{} on database {name}", pair.role),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::manifest::Privilege;
    use crate::model::Grantee;

    fn grant(
        role: &str,
        object_type: ObjectType,
        schema: Option<&str>,
        name: Option<&str>,
    ) -> Change {
        Change::Grant {
            role: Grantee::parse(role),
            privileges: BTreeSet::from([Privilege::Select]),
            object_type,
            schema: schema.map(str::to_string),
            name: name.map(str::to_string),
        }
    }

    fn member_edge(role: &str, member: &str) -> MembershipEdge {
        MembershipEdge {
            role: role.to_string(),
            member: member.to_string(),
            inherit: true,
            admin: false,
        }
    }

    #[test]
    fn membership_edges_normalize_to_member_and_role_as_object() {
        let pairs = membership_overlay_pairs(&[member_edge("app_rw", "alice")]);
        assert_eq!(
            pairs,
            BTreeSet::from([EffectPair::new(
                "alice",
                EffectObject::Role("app_rw".to_string())
            )])
        );

        // The diff's own membership change must land on the same pair, or the
        // overlay and the candidate could never be compared.
        assert_eq!(
            effect_pairs(&[Change::AddMember {
                role: "app_rw".to_string(),
                member: "alice".to_string(),
                inherit: true,
                admin: false,
            }]),
            pairs
        );
    }

    #[test]
    fn a_schema_level_effect_intersects_any_object_in_that_schema() {
        let schema_level = effect_pairs(&[grant("app_rw", ObjectType::Schema, None, Some("app"))]);
        let table_level = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("app"),
            Some("orders"),
        )]);

        assert!(pairs_intersect(&schema_level, &table_level));
        assert!(pairs_intersect(&table_level, &schema_level));

        let other_schema = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("billing"),
            Some("invoices"),
        )]);
        assert!(!pairs_intersect(&schema_level, &other_schema));
    }

    #[test]
    fn attribute_only_effects_match_the_same_role_only() {
        let attributes = effect_pairs(&[Change::AlterRole {
            name: "alice".to_string(),
            attributes: Vec::new(),
        }]);

        let same_role = membership_overlay_pairs(&[member_edge("app_rw", "alice")]);
        let other_role = membership_overlay_pairs(&[member_edge("app_rw", "bob")]);

        assert!(pairs_intersect(&attributes, &same_role));
        assert!(!pairs_intersect(&attributes, &other_role));
    }

    #[test]
    fn distinct_objects_on_the_same_role_do_not_intersect() {
        let orders = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("app"),
            Some("orders"),
        )]);
        let invoices = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("app"),
            Some("invoices"),
        )]);
        assert!(!pairs_intersect(&orders, &invoices));
    }

    #[test]
    fn the_same_object_under_different_types_does_not_intersect() {
        let table = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("app"),
            Some("orders"),
        )]);
        let sequence = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Sequence,
            Some("app"),
            Some("orders"),
        )]);
        assert!(!pairs_intersect(&table, &sequence));
    }

    #[test]
    fn different_roles_never_intersect() {
        let alice = effect_pairs(&[grant(
            "alice",
            ObjectType::Table,
            Some("app"),
            Some("orders"),
        )]);
        let bob = effect_pairs(&[grant("bob", ObjectType::Table, Some("app"), Some("orders"))]);
        assert!(!pairs_intersect(&alice, &bob));
    }

    #[test]
    fn revoke_and_grant_of_the_same_access_land_on_the_same_pair() {
        let granted = effect_pairs(&[grant(
            "app_rw",
            ObjectType::Table,
            Some("app"),
            Some("orders"),
        )]);
        let revoked = effect_pairs(&[Change::Revoke {
            role: Grantee::parse("app_rw"),
            privileges: BTreeSet::from([Privilege::Select]),
            object_type: ObjectType::Table,
            schema: Some("app".to_string()),
            name: Some("orders".to_string()),
        }]);
        assert_eq!(granted, revoked);
    }

    #[test]
    fn default_privileges_touch_both_grantee_and_owner_at_schema_level() {
        let pairs = effect_pairs(&[Change::SetDefaultPrivilege {
            owner: "app_owner".to_string(),
            scope: DefaultPrivilegeScope::Schema {
                schema: "app".to_string(),
            },
            on_type: ObjectType::Table,
            grantee: Grantee::parse("app_ro"),
            privileges: BTreeSet::from([Privilege::Select]),
        }]);
        assert_eq!(
            pairs,
            BTreeSet::from([
                EffectPair::new("app_owner", EffectObject::Schema("app".to_string())),
                EffectPair::new("app_ro", EffectObject::Schema("app".to_string())),
            ])
        );
    }

    #[test]
    fn a_public_effect_overlaps_every_named_role_on_the_same_object() {
        let object = EffectObject::Relation {
            schema: "app".to_string(),
            name: "orders".to_string(),
            object_type: ObjectType::Table,
        };
        let public = EffectPair::new(PUBLIC_ROLE, object.clone());
        let named = EffectPair::new("alice", object);
        assert!(public.intersects(&named));
        assert!(named.intersects(&public));
    }

    #[test]
    fn a_public_effect_still_respects_object_scope() {
        let public = EffectPair::new(PUBLIC_ROLE, EffectObject::Schema("app".to_string()));
        let elsewhere = EffectPair::new("alice", EffectObject::Schema("other".to_string()));
        assert!(!public.intersects(&elsewhere));
    }

    #[test]
    fn a_global_default_privilege_is_not_bounded_to_any_schema() {
        let pairs = effect_pairs(&[Change::SetDefaultPrivilege {
            owner: "app_owner".to_string(),
            scope: DefaultPrivilegeScope::Global,
            on_type: ObjectType::Table,
            grantee: Grantee::parse("app_ro"),
            privileges: BTreeSet::from([Privilege::Select]),
        }]);
        assert_eq!(
            pairs,
            BTreeSet::from([
                EffectPair::new("app_owner", EffectObject::RoleAttributes),
                EffectPair::new("app_ro", EffectObject::RoleAttributes),
            ])
        );
        // It must overlap a schema-scoped effect on the same role, since the
        // global rule reaches every schema.
        assert!(
            EffectPair::new("app_ro", EffectObject::RoleAttributes).intersects(&EffectPair::new(
                "app_ro",
                EffectObject::Schema("anything".to_string())
            ))
        );
    }

    #[test]
    fn a_non_overlapping_overlay_leaves_the_candidate_alone() {
        // The case the ADR exists to protect: continuous ephemeral traffic on
        // unrelated roles must not force a candidate back through review.
        let overlay = membership_overlay_pairs(&[member_edge("oncall_admin", "carol")]);
        let candidate = effect_pairs(&[
            grant(
                "reporting_reader",
                ObjectType::Table,
                Some("app"),
                Some("orders"),
            ),
            Change::CreateRole {
                name: "reporting_reader".to_string(),
                state: Default::default(),
            },
        ]);
        assert!(!pairs_intersect(&candidate, &overlay));
        assert!(intersecting_pairs(&candidate, &overlay).is_empty());
    }

    #[test]
    fn intersecting_pairs_reports_the_left_sides_own_effects() {
        let overlay = membership_overlay_pairs(&[member_edge("app_rw", "alice")]);
        let candidate = effect_pairs(&[
            Change::RemoveMember {
                role: "app_rw".to_string(),
                member: "alice".to_string(),
            },
            grant("bob", ObjectType::Table, Some("app"), Some("orders")),
        ]);
        let overlapping = intersecting_pairs(&candidate, &overlay);
        assert_eq!(
            overlapping,
            BTreeSet::from([EffectPair::new(
                "alice",
                EffectObject::Role("app_rw".to_string())
            )])
        );
        assert_eq!(
            describe_pair(overlapping.iter().next().unwrap()),
            "alice -> app_rw (membership)"
        );
    }

    #[test]
    fn database_scoped_effects_do_not_reach_into_schemas() {
        let database = effect_pairs(&[grant("app_rw", ObjectType::Database, None, Some("orders"))]);
        let schema = effect_pairs(&[grant("app_rw", ObjectType::Schema, None, Some("app"))]);
        assert!(!pairs_intersect(&database, &schema));
        assert!(pairs_intersect(&database, &database.clone()));
    }
}