|
| 1 | +use crate::bridge::envelope::PhysicalPlan; |
| 2 | +use crate::control::gateway::version_set::touched_collections; |
| 3 | +use crate::control::security::identity::{Permission, required_permission}; |
| 4 | + |
| 5 | +use super::AuthorizationRequirement; |
| 6 | +use super::order::requirement_order; |
| 7 | +use super::query::collect_query_requirements; |
| 8 | + |
| 9 | +/// Return every collection requirement for `plan`. |
| 10 | +/// |
| 11 | +/// The general plan permission applies to ordinary single-resource plans. The |
| 12 | +/// multi-resource cases below intentionally override it so sources require |
| 13 | +/// `Read` while targets require `Write`. Nested query inputs are traversed |
| 14 | +/// iteratively in addition to their named collection fields. |
| 15 | +pub fn plan_requirements(plan: &PhysicalPlan) -> Vec<AuthorizationRequirement> { |
| 16 | + let mut requirements = Vec::new(); |
| 17 | + collect_requirements(plan, &mut requirements); |
| 18 | + requirements.sort_by(requirement_order); |
| 19 | + requirements.dedup(); |
| 20 | + requirements |
| 21 | +} |
| 22 | + |
| 23 | +fn collect_requirements(plan: &PhysicalPlan, out: &mut Vec<AuthorizationRequirement>) { |
| 24 | + use nodedb_physical::physical_plan::{CrdtOp, DocumentOp, GraphOp, KvOp, MetaOp, SpatialOp}; |
| 25 | + |
| 26 | + let mut pending = vec![plan]; |
| 27 | + while let Some(plan) = pending.pop() { |
| 28 | + let initial_len = out.len(); |
| 29 | + match plan { |
| 30 | + PhysicalPlan::Document(DocumentOp::InsertSelect { |
| 31 | + target_collection, |
| 32 | + source_collection, |
| 33 | + .. |
| 34 | + }) |
| 35 | + | PhysicalPlan::Document(DocumentOp::UpdateFromJoin { |
| 36 | + target_collection, |
| 37 | + source_collection, |
| 38 | + .. |
| 39 | + }) |
| 40 | + | PhysicalPlan::Document(DocumentOp::Merge { |
| 41 | + target_collection, |
| 42 | + source_collection, |
| 43 | + .. |
| 44 | + }) => { |
| 45 | + out.push(AuthorizationRequirement::collection( |
| 46 | + target_collection, |
| 47 | + Permission::Write, |
| 48 | + )); |
| 49 | + out.push(AuthorizationRequirement::collection( |
| 50 | + source_collection, |
| 51 | + Permission::Read, |
| 52 | + )); |
| 53 | + } |
| 54 | + PhysicalPlan::Kv(KvOp::TransferItem { |
| 55 | + source_collection, |
| 56 | + dest_collection, |
| 57 | + .. |
| 58 | + }) => { |
| 59 | + out.push(AuthorizationRequirement::collection( |
| 60 | + source_collection, |
| 61 | + Permission::Read, |
| 62 | + )); |
| 63 | + out.push(AuthorizationRequirement::collection( |
| 64 | + dest_collection, |
| 65 | + Permission::Write, |
| 66 | + )); |
| 67 | + } |
| 68 | + PhysicalPlan::Query(_) | PhysicalPlan::Vector(_) => { |
| 69 | + if !collect_query_requirements(plan, &mut pending, out) { |
| 70 | + add_general_requirements(plan, out); |
| 71 | + } |
| 72 | + } |
| 73 | + PhysicalPlan::Spatial(SpatialOp::Insert { collection, .. }) |
| 74 | + | PhysicalPlan::Spatial(SpatialOp::Delete { collection, .. }) |
| 75 | + | PhysicalPlan::Crdt( |
| 76 | + CrdtOp::ImportSnapshot { collection, .. } |
| 77 | + | CrdtOp::SetConstraints { collection, .. } |
| 78 | + | CrdtOp::DropConstraints { collection, .. } |
| 79 | + | CrdtOp::ReadConstraints { collection, .. } |
| 80 | + | CrdtOp::GetVersionVector { collection, .. } |
| 81 | + | CrdtOp::ExportDelta { collection, .. } |
| 82 | + | CrdtOp::CompactAtVersion { collection, .. }, |
| 83 | + ) => add_collection_requirement(collection, required_permission(plan), out), |
| 84 | + PhysicalPlan::Graph(GraphOp::EdgePut { collection, .. }) |
| 85 | + | PhysicalPlan::Graph(GraphOp::EdgeDelete { collection, .. }) => { |
| 86 | + add_collection_requirement(collection, required_permission(plan), out); |
| 87 | + } |
| 88 | + PhysicalPlan::Graph(GraphOp::EdgePutBatch { edges }) |
| 89 | + | PhysicalPlan::Graph(GraphOp::EdgeDeleteBatch { edges }) => { |
| 90 | + let permission = required_permission(plan); |
| 91 | + for edge in edges { |
| 92 | + add_collection_requirement(&edge.collection, permission, out); |
| 93 | + } |
| 94 | + } |
| 95 | + PhysicalPlan::Meta( |
| 96 | + MetaOp::ConvertCollection { collection, .. } |
| 97 | + | MetaOp::EnforceTimeseriesRetention { collection, .. } |
| 98 | + | MetaOp::TemporalPurgeEdgeStore { collection, .. } |
| 99 | + | MetaOp::TemporalPurgeDocumentStrict { collection, .. } |
| 100 | + | MetaOp::TemporalPurgeColumnar { collection, .. } |
| 101 | + | MetaOp::TemporalPurgeCrdt { collection, .. } |
| 102 | + | MetaOp::QueryLastValues { collection } |
| 103 | + | MetaOp::QueryLastValue { collection, .. } |
| 104 | + | MetaOp::RebuildIndex { collection, .. }, |
| 105 | + ) => add_collection_requirement(collection, required_permission(plan), out), |
| 106 | + PhysicalPlan::Meta( |
| 107 | + MetaOp::UnregisterCollection { name, .. } |
| 108 | + | MetaOp::UnregisterMaterializedView { name, .. } |
| 109 | + | MetaOp::QueryCollectionSize { name, .. }, |
| 110 | + ) => add_collection_requirement(name, required_permission(plan), out), |
| 111 | + PhysicalPlan::Meta(MetaOp::RenameCollection { |
| 112 | + old_collection, |
| 113 | + new_collection, |
| 114 | + .. |
| 115 | + }) => { |
| 116 | + let permission = required_permission(plan); |
| 117 | + add_collection_requirement(old_collection, permission, out); |
| 118 | + add_collection_requirement(new_collection, permission, out); |
| 119 | + } |
| 120 | + PhysicalPlan::Meta(MetaOp::TransactionBatch { plans, .. }) |
| 121 | + | PhysicalPlan::Meta(MetaOp::ResolveTxn { plans, .. }) |
| 122 | + | PhysicalPlan::Meta(MetaOp::RecordCalvinWriteVersions { plans, .. }) |
| 123 | + | PhysicalPlan::Meta(MetaOp::CalvinExecuteStatic { plans, .. }) |
| 124 | + | PhysicalPlan::Meta(MetaOp::CalvinExecuteActive { plans, .. }) => { |
| 125 | + add_tenant_requirement(required_permission(plan), out); |
| 126 | + for nested in plans { |
| 127 | + pending.push(nested); |
| 128 | + } |
| 129 | + } |
| 130 | + PhysicalPlan::Meta(MetaOp::StageWrite { plan: nested }) => { |
| 131 | + add_tenant_requirement(required_permission(plan), out); |
| 132 | + pending.push(nested); |
| 133 | + } |
| 134 | + PhysicalPlan::Kv(KvOp::Transfer { collection, .. }) => { |
| 135 | + out.push(AuthorizationRequirement::collection( |
| 136 | + collection, |
| 137 | + Permission::Write, |
| 138 | + )); |
| 139 | + } |
| 140 | + PhysicalPlan::Kv( |
| 141 | + KvOp::Get { .. } |
| 142 | + | KvOp::Put { .. } |
| 143 | + | KvOp::Insert { .. } |
| 144 | + | KvOp::InsertIfAbsent { .. } |
| 145 | + | KvOp::InsertOnConflictUpdate { .. } |
| 146 | + | KvOp::Delete { .. } |
| 147 | + | KvOp::Scan { .. } |
| 148 | + | KvOp::Expire { .. } |
| 149 | + | KvOp::Persist { .. } |
| 150 | + | KvOp::GetTtl { .. } |
| 151 | + | KvOp::BatchGet { .. } |
| 152 | + | KvOp::BatchPut { .. } |
| 153 | + | KvOp::RegisterIndex { .. } |
| 154 | + | KvOp::DropIndex { .. } |
| 155 | + | KvOp::FieldGet { .. } |
| 156 | + | KvOp::FieldSet { .. } |
| 157 | + | KvOp::Truncate { .. } |
| 158 | + | KvOp::Incr { .. } |
| 159 | + | KvOp::IncrFloat { .. } |
| 160 | + | KvOp::Cas { .. } |
| 161 | + | KvOp::GetSet { .. } |
| 162 | + | KvOp::RegisterSortedIndex { .. } |
| 163 | + | KvOp::DropSortedIndex { .. } |
| 164 | + | KvOp::SortedIndexRank { .. } |
| 165 | + | KvOp::SortedIndexTopK { .. } |
| 166 | + | KvOp::SortedIndexRange { .. } |
| 167 | + | KvOp::SortedIndexCount { .. } |
| 168 | + | KvOp::SortedIndexScore { .. } |
| 169 | + | KvOp::MaterializeScan { .. }, |
| 170 | + ) => add_general_requirements(plan, out), |
| 171 | + PhysicalPlan::Document(_) |
| 172 | + | PhysicalPlan::Graph(_) |
| 173 | + | PhysicalPlan::Text(_) |
| 174 | + | PhysicalPlan::Columnar(_) |
| 175 | + | PhysicalPlan::Timeseries(_) |
| 176 | + | PhysicalPlan::Spatial(_) |
| 177 | + | PhysicalPlan::Crdt(_) |
| 178 | + | PhysicalPlan::Meta(_) |
| 179 | + | PhysicalPlan::Array(_) |
| 180 | + | PhysicalPlan::ClusterArray(_) => add_general_requirements(plan, out), |
| 181 | + } |
| 182 | + |
| 183 | + // A plan without a collection name is not public: it still needs the |
| 184 | + // permission implied by its physical operation at tenant scope. This also |
| 185 | + // covers provider-materialized and array-backed plans. |
| 186 | + if out.len() == initial_len { |
| 187 | + add_tenant_requirement(required_permission(plan), out); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +pub(super) fn add_general_requirements( |
| 193 | + plan: &PhysicalPlan, |
| 194 | + out: &mut Vec<AuthorizationRequirement>, |
| 195 | +) { |
| 196 | + let permission = required_permission(plan); |
| 197 | + for collection in touched_collections(plan) { |
| 198 | + out.push(AuthorizationRequirement::collection(collection, permission)); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +pub(super) fn add_collection_requirement( |
| 203 | + collection: &str, |
| 204 | + permission: Permission, |
| 205 | + out: &mut Vec<AuthorizationRequirement>, |
| 206 | +) { |
| 207 | + if !collection.is_empty() { |
| 208 | + out.push(AuthorizationRequirement::collection(collection, permission)); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +pub(super) fn add_tenant_requirement( |
| 213 | + permission: Permission, |
| 214 | + out: &mut Vec<AuthorizationRequirement>, |
| 215 | +) { |
| 216 | + out.push(AuthorizationRequirement::tenant(permission)); |
| 217 | +} |
| 218 | + |
| 219 | +pub(super) fn add_read(collection: &str, out: &mut Vec<AuthorizationRequirement>) { |
| 220 | + add_collection_requirement(collection, Permission::Read, out); |
| 221 | +} |
0 commit comments