@feltdb/core 0.15.0 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/dist/agent-state.d.ts +20 -0
  2. package/dist/agent-state.d.ts.map +1 -1
  3. package/dist/agent-state.js +36 -0
  4. package/dist/build-identity.json +12 -4
  5. package/dist/cli/commands.js +187 -4
  6. package/dist/cli/publish-engine.js +116 -16
  7. package/dist/collection.d.ts.map +1 -1
  8. package/dist/collection.js +20 -2
  9. package/dist/create/build-identity.json +12 -4
  10. package/dist/create/managed-account.js +93 -35
  11. package/dist/create/package-versions.js +1 -1
  12. package/dist/create/server-source/Cargo.lock +6 -509
  13. package/dist/create/server-source/Cargo.toml +2 -13
  14. package/dist/create/server-source/crates/feltdb-client/Cargo.toml +1 -1
  15. package/dist/create/server-source/crates/feltdb-server/Cargo.toml +1 -1
  16. package/dist/create/server-source/crates/feltdb-server/src/main.rs +356 -24
  17. package/dist/create/server-source/crates/feltdb-server/tests/rust_client.rs +10 -1
  18. package/dist/create/server-source/crates/feltdb-wasm/Cargo.toml +1 -1
  19. package/dist/db.d.ts +11 -1
  20. package/dist/db.d.ts.map +1 -1
  21. package/dist/db.js +15 -2
  22. package/dist/error-codes.d.ts +5 -0
  23. package/dist/error-codes.d.ts.map +1 -1
  24. package/dist/error-codes.js +9 -1
  25. package/dist/http-db.d.ts +16 -0
  26. package/dist/http-db.d.ts.map +1 -1
  27. package/dist/http-db.js +36 -6
  28. package/dist/managed-recovery.d.ts +6 -0
  29. package/dist/managed-recovery.d.ts.map +1 -1
  30. package/dist/managed-recovery.js +9 -0
  31. package/dist/studio-app/assets/{feltdb_wasm-BjM1ylL3.js → feltdb_wasm-BUIxCuTR.js} +1 -1
  32. package/dist/studio-app/assets/feltdb_wasm_bg-C3D6y6DK.wasm +0 -0
  33. package/dist/studio-app/assets/index-BhXYAhgS.js +37 -0
  34. package/dist/studio-app/index.html +1 -1
  35. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  36. package/dist/wasm/package.json +1 -1
  37. package/dist/workload.d.ts +55 -3
  38. package/dist/workload.d.ts.map +1 -1
  39. package/dist/workload.js +23 -0
  40. package/dist/workspace/workspace-connection.d.ts.map +1 -1
  41. package/dist/workspace/workspace-connection.js +14 -3
  42. package/package.json +1 -1
  43. package/dist/create/server-source/crates/feltdb/Cargo.lock +0 -175
  44. package/dist/create/server-source/crates/feltdb/Cargo.toml +0 -102
  45. package/dist/create/server-source/crates/feltdb/LICENSE +0 -21
  46. package/dist/create/server-source/crates/feltdb/README.md +0 -75
  47. package/dist/create/server-source/crates/feltdb/benches/baselines/gate-13-redux.json +0 -370
  48. package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +0 -589
  49. package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +0 -247
  50. package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +0 -440
  51. package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +0 -275
  52. package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +0 -381
  53. package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +0 -467
  54. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +0 -489
  55. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +0 -281
  56. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +0 -606
  57. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +0 -531
  58. package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +0 -462
  59. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +0 -505
  60. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +0 -484
  61. package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +0 -758
  62. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +0 -421
  63. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +0 -329
  64. package/dist/create/server-source/crates/feltdb/src/acceptance_tests.rs +0 -1705
  65. package/dist/create/server-source/crates/feltdb/src/acquisition.rs +0 -286
  66. package/dist/create/server-source/crates/feltdb/src/admission.rs +0 -185
  67. package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +0 -507
  68. package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +0 -539
  69. package/dist/create/server-source/crates/feltdb/src/agent_state.rs +0 -320
  70. package/dist/create/server-source/crates/feltdb/src/agent_transition_contract.rs +0 -1545
  71. package/dist/create/server-source/crates/feltdb/src/analytics.rs +0 -521
  72. package/dist/create/server-source/crates/feltdb/src/application.rs +0 -2674
  73. package/dist/create/server-source/crates/feltdb/src/application_runtime.rs +0 -1070
  74. package/dist/create/server-source/crates/feltdb/src/authority.rs +0 -479
  75. package/dist/create/server-source/crates/feltdb/src/authority_failover.rs +0 -847
  76. package/dist/create/server-source/crates/feltdb/src/authorization.rs +0 -1030
  77. package/dist/create/server-source/crates/feltdb/src/authorization_security_tests.rs +0 -1122
  78. package/dist/create/server-source/crates/feltdb/src/bin/feltdb-authority-client.rs +0 -32
  79. package/dist/create/server-source/crates/feltdb/src/bin/feltdb-authority.rs +0 -19
  80. package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +0 -1474
  81. package/dist/create/server-source/crates/feltdb/src/capabilities/mod.rs +0 -8
  82. package/dist/create/server-source/crates/feltdb/src/capabilities/search.rs +0 -418
  83. package/dist/create/server-source/crates/feltdb/src/capabilities/vector.rs +0 -482
  84. package/dist/create/server-source/crates/feltdb/src/capability.rs +0 -903
  85. package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +0 -263
  86. package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +0 -77
  87. package/dist/create/server-source/crates/feltdb/src/causal_backlog_bound.rs +0 -496
  88. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +0 -2574
  89. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +0 -212
  90. package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +0 -422
  91. package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +0 -445
  92. package/dist/create/server-source/crates/feltdb/src/content_distribution.rs +0 -465
  93. package/dist/create/server-source/crates/feltdb/src/convergence.rs +0 -634
  94. package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +0 -427
  95. package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +0 -376
  96. package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +0 -365
  97. package/dist/create/server-source/crates/feltdb/src/cron.rs +0 -294
  98. package/dist/create/server-source/crates/feltdb/src/dedup_bound_investigation.rs +0 -483
  99. package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +0 -477
  100. package/dist/create/server-source/crates/feltdb/src/distributed_tests.rs +0 -1003
  101. package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +0 -1334
  102. package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +0 -368
  103. package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +0 -231
  104. package/dist/create/server-source/crates/feltdb/src/durable_operation_identity.rs +0 -482
  105. package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +0 -203
  106. package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +0 -317
  107. package/dist/create/server-source/crates/feltdb/src/equality_index.rs +0 -595
  108. package/dist/create/server-source/crates/feltdb/src/execution.rs +0 -426
  109. package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +0 -214
  110. package/dist/create/server-source/crates/feltdb/src/indexing.rs +0 -776
  111. package/dist/create/server-source/crates/feltdb/src/lib.rs +0 -7365
  112. package/dist/create/server-source/crates/feltdb/src/managed_cas_tests.rs +0 -234
  113. package/dist/create/server-source/crates/feltdb/src/materialization.rs +0 -184
  114. package/dist/create/server-source/crates/feltdb/src/metrics.rs +0 -266
  115. package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +0 -310
  116. package/dist/create/server-source/crates/feltdb/src/multi_operation_transaction.rs +0 -736
  117. package/dist/create/server-source/crates/feltdb/src/observability.rs +0 -379
  118. package/dist/create/server-source/crates/feltdb/src/operation.rs +0 -290
  119. package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +0 -439
  120. package/dist/create/server-source/crates/feltdb/src/operation_log.rs +0 -349
  121. package/dist/create/server-source/crates/feltdb/src/p1_application_atomicity.rs +0 -332
  122. package/dist/create/server-source/crates/feltdb/src/p1_atomicity_acceptance.rs +0 -718
  123. package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +0 -487
  124. package/dist/create/server-source/crates/feltdb/src/peer_registry.rs +0 -166
  125. package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +0 -289
  126. package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +0 -566
  127. package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +0 -3391
  128. package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +0 -203
  129. package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +0 -261
  130. package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +0 -502
  131. package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +0 -216
  132. package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +0 -303
  133. package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +0 -323
  134. package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +0 -531
  135. package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +0 -406
  136. package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +0 -436
  137. package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +0 -387
  138. package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +0 -378
  139. package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +0 -359
  140. package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +0 -372
  141. package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +0 -404
  142. package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +0 -376
  143. package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +0 -461
  144. package/dist/create/server-source/crates/feltdb/src/policy_evaluation.rs +0 -2125
  145. package/dist/create/server-source/crates/feltdb/src/production_api.rs +0 -418
  146. package/dist/create/server-source/crates/feltdb/src/provenance.rs +0 -207
  147. package/dist/create/server-source/crates/feltdb/src/query_execution_diagnostics.rs +0 -126
  148. package/dist/create/server-source/crates/feltdb/src/query_performance.rs +0 -218
  149. package/dist/create/server-source/crates/feltdb/src/read_basis.rs +0 -257
  150. package/dist/create/server-source/crates/feltdb/src/references.rs +0 -404
  151. package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +0 -401
  152. package/dist/create/server-source/crates/feltdb/src/replica_acknowledgements.rs +0 -501
  153. package/dist/create/server-source/crates/feltdb/src/replica_membership.rs +0 -680
  154. package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +0 -163
  155. package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +0 -133
  156. package/dist/create/server-source/crates/feltdb/src/routing.rs +0 -409
  157. package/dist/create/server-source/crates/feltdb/src/semantic_decision.rs +0 -2029
  158. package/dist/create/server-source/crates/feltdb/src/semantic_query.rs +0 -599
  159. package/dist/create/server-source/crates/feltdb/src/sharding.rs +0 -549
  160. package/dist/create/server-source/crates/feltdb/src/state_conflict_contract.rs +0 -516
  161. package/dist/create/server-source/crates/feltdb/src/state_contract.rs +0 -6003
  162. package/dist/create/server-source/crates/feltdb/src/state_diff_contract.rs +0 -222
  163. package/dist/create/server-source/crates/feltdb/src/state_facade.rs +0 -320
  164. package/dist/create/server-source/crates/feltdb/src/state_hash.rs +0 -291
  165. package/dist/create/server-source/crates/feltdb/src/state_model.rs +0 -4158
  166. package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +0 -379
  167. package/dist/create/server-source/crates/feltdb/src/state_trigger.rs +0 -673
  168. package/dist/create/server-source/crates/feltdb/src/storage.rs +0 -273
  169. package/dist/create/server-source/crates/feltdb/src/submission.rs +0 -471
  170. package/dist/create/server-source/crates/feltdb/src/sync.rs +0 -495
  171. package/dist/create/server-source/crates/feltdb/src/sync_contract.rs +0 -822
  172. package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +0 -433
  173. package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +0 -462
  174. package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +0 -965
  175. package/dist/create/server-source/crates/feltdb/src/transaction_preconditions.rs +0 -1120
  176. package/dist/create/server-source/crates/feltdb/src/transactions.rs +0 -643
  177. package/dist/create/server-source/crates/feltdb/src/trigger.rs +0 -390
  178. package/dist/create/server-source/crates/feltdb/src/trigger_contract.rs +0 -749
  179. package/dist/create/server-source/crates/feltdb/src/versioned_object_substrate.rs +0 -708
  180. package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +0 -1269
  181. package/dist/create/server-source/crates/feltdb/src/workflow.rs +0 -713
  182. package/dist/create/server-source/crates/feltdb/src/workflow_acceptance_tests.rs +0 -510
  183. package/dist/create/server-source/crates/feltdb/src/workflow_integration.rs +0 -354
  184. package/dist/create/server-source/crates/feltdb/src/workflow_runtime.rs +0 -594
  185. package/dist/create/server-source/crates/feltdb/src/workload.rs +0 -4222
  186. package/dist/create/server-source/crates/feltdb/src/workload_diagnostics.rs +0 -456
  187. package/dist/create/server-source/crates/feltdb/tests/agent_state_native.rs +0 -1188
  188. package/dist/create/server-source/crates/feltdb/tests/agent_transition_contract_conformance.rs +0 -2884
  189. package/dist/create/server-source/crates/feltdb/tests/authority_process.rs +0 -297
  190. package/dist/create/server-source/crates/feltdb/tests/authority_transaction_identity.rs +0 -534
  191. package/dist/create/server-source/crates/feltdb/tests/bounded_read_contract.rs +0 -132
  192. package/dist/create/server-source/crates/feltdb/tests/branching_evidence.rs +0 -299
  193. package/dist/create/server-source/crates/feltdb/tests/common/mod.rs +0 -96
  194. package/dist/create/server-source/crates/feltdb/tests/compaction_stall_contract.rs +0 -272
  195. package/dist/create/server-source/crates/feltdb/tests/crash_durability_contract.rs +0 -467
  196. package/dist/create/server-source/crates/feltdb/tests/current_revision_authority_evidence.rs +0 -309
  197. package/dist/create/server-source/crates/feltdb/tests/durable_backup_contract.rs +0 -445
  198. package/dist/create/server-source/crates/feltdb/tests/durable_corruption_contract.rs +0 -518
  199. package/dist/create/server-source/crates/feltdb/tests/durable_format_compatibility.rs +0 -392
  200. package/dist/create/server-source/crates/feltdb/tests/feltdb_state_boundary_tests.rs +0 -850
  201. package/dist/create/server-source/crates/feltdb/tests/fixtures/agent_transition_corpus.json +0 -23340
  202. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_conflict_contract_corpus.json +0 -1916
  203. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_diff_contract_corpus.json +0 -1878
  204. package/dist/create/server-source/crates/feltdb/tests/fixtures/trigger_contract_corpus.json +0 -1862
  205. package/dist/create/server-source/crates/feltdb/tests/generalized_version_control_evidence.rs +0 -123
  206. package/dist/create/server-source/crates/feltdb/tests/generalized_version_control_substrate_completion.rs +0 -299
  207. package/dist/create/server-source/crates/feltdb/tests/local_process_ownership.rs +0 -69
  208. package/dist/create/server-source/crates/feltdb/tests/managed_incident_regression.rs +0 -191
  209. package/dist/create/server-source/crates/feltdb/tests/operational_health_contract.rs +0 -278
  210. package/dist/create/server-source/crates/feltdb/tests/pr34_query_collection.rs +0 -234
  211. package/dist/create/server-source/crates/feltdb/tests/pr35_equality_index.rs +0 -947
  212. package/dist/create/server-source/crates/feltdb/tests/pr7_self_authorization_proof.rs +0 -404
  213. package/dist/create/server-source/crates/feltdb/tests/pr8_vocabulary_assessment.rs +0 -916
  214. package/dist/create/server-source/crates/feltdb/tests/pr9_phase2_boundary_tests.rs +0 -1045
  215. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3a_path_a_tests.rs +0 -347
  216. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_authorized_mutations.rs +0 -361
  217. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_role_based_authorization.rs +0 -330
  218. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_simple_auth_delete.rs +0 -93
  219. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_team_delete_role_authorization.rs +0 -622
  220. package/dist/create/server-source/crates/feltdb/tests/pr9_teams_role_based_access.rs +0 -517
  221. package/dist/create/server-source/crates/feltdb/tests/production_certification.rs +0 -1153
  222. package/dist/create/server-source/crates/feltdb/tests/production_contract.rs +0 -771
  223. package/dist/create/server-source/crates/feltdb/tests/production_readiness_contract.rs +0 -1383
  224. package/dist/create/server-source/crates/feltdb/tests/reconciliation_application.rs +0 -868
  225. package/dist/create/server-source/crates/feltdb/tests/reconciliation_wire_format_evidence.rs +0 -221
  226. package/dist/create/server-source/crates/feltdb/tests/replicated_history_contract.rs +0 -417
  227. package/dist/create/server-source/crates/feltdb/tests/resource_scoped_revisions.rs +0 -338
  228. package/dist/create/server-source/crates/feltdb/tests/revision_identity_contract.rs +0 -1039
  229. package/dist/create/server-source/crates/feltdb/tests/revision_model_decision.rs +0 -739
  230. package/dist/create/server-source/crates/feltdb/tests/revision_retention_boundary_evidence.rs +0 -452
  231. package/dist/create/server-source/crates/feltdb/tests/saas_authorization_integration.rs +0 -81
  232. package/dist/create/server-source/crates/feltdb/tests/saas_invitation_lifecycle.rs +0 -438
  233. package/dist/create/server-source/crates/feltdb/tests/semantic_decision.rs +0 -1165
  234. package/dist/create/server-source/crates/feltdb/tests/semantic_query.rs +0 -767
  235. package/dist/create/server-source/crates/feltdb/tests/semantic_query_contract.rs +0 -363
  236. package/dist/create/server-source/crates/feltdb/tests/state_conflict_contract_conformance.rs +0 -1799
  237. package/dist/create/server-source/crates/feltdb/tests/state_diff_contract_conformance.rs +0 -1316
  238. package/dist/create/server-source/crates/feltdb/tests/state_model_integration.rs +0 -358
  239. package/dist/create/server-source/crates/feltdb/tests/state_persistence_integration.rs +0 -365
  240. package/dist/create/server-source/crates/feltdb/tests/state_ref_authority.rs +0 -918
  241. package/dist/create/server-source/crates/feltdb/tests/state_store_boundary_evidence.rs +0 -299
  242. package/dist/create/server-source/crates/feltdb/tests/storage_independent_versioned_object_substrate.rs +0 -328
  243. package/dist/create/server-source/crates/feltdb/tests/sync_divergence_evidence.rs +0 -255
  244. package/dist/create/server-source/crates/feltdb/tests/three_way_input_boundary_evidence.rs +0 -249
  245. package/dist/create/server-source/crates/feltdb/tests/trigger_contract_conformance.rs +0 -994
  246. package/dist/create/server-source/crates/feltdb/tests/workload_envelope_contract.rs +0 -484
  247. package/dist/studio-app/assets/feltdb_wasm_bg-TseJn2Ax.wasm +0 -0
  248. package/dist/studio-app/assets/index-1wd0uKua.js +0 -30
@@ -1,1030 +0,0 @@
1
- //! Canonical resource authorization and portable signed grants.
2
- //!
3
- //! This module is deliberately independent of HTTP and Studio. Rust owns every
4
- //! security decision; other surfaces serialize and consume these contracts.
5
-
6
- use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
7
- use serde::{Deserialize, Serialize};
8
- use sha2::{Digest, Sha256};
9
- use std::collections::{BTreeMap, BTreeSet};
10
- use std::fs;
11
- use std::path::{Path, PathBuf};
12
- use std::time::{SystemTime, UNIX_EPOCH};
13
-
14
- pub const AUTHORIZATION_CONTRACT_VERSION: u32 = 1;
15
- pub const POLICY_VERSION: &str = "feltdb.resource-authorization.v1";
16
-
17
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
18
- #[serde(tag = "kind", content = "id", rename_all = "snake_case")]
19
- pub enum Subject {
20
- Human(String),
21
- Service(String),
22
- Workload(String),
23
- Agent(String),
24
- Connection(String),
25
- Worker(String),
26
- }
27
- impl Subject {
28
- pub fn id(&self) -> &str {
29
- match self {
30
- Self::Human(v)
31
- | Self::Service(v)
32
- | Self::Workload(v)
33
- | Self::Agent(v)
34
- | Self::Connection(v)
35
- | Self::Worker(v) => v,
36
- }
37
- }
38
- pub fn kind(&self) -> &'static str {
39
- match self {
40
- Self::Human(_) => "human",
41
- Self::Service(_) => "service",
42
- Self::Workload(_) => "workload",
43
- Self::Agent(_) => "agent",
44
- Self::Connection(_) => "connection",
45
- Self::Worker(_) => "worker",
46
- }
47
- }
48
- }
49
-
50
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
51
- pub struct Resource {
52
- pub uri: String,
53
- #[serde(default)]
54
- pub attributes: BTreeMap<String, String>,
55
- }
56
- impl Resource {
57
- pub fn new(application_id: &str, path: &str) -> Result<Self, AuthorizationError> {
58
- let uri = format!("flow://{}/{}", application_id, path.trim_start_matches('/'));
59
- validate_resource_uri(&uri, application_id)?;
60
- Ok(Self {
61
- uri,
62
- attributes: BTreeMap::new(),
63
- })
64
- }
65
- }
66
-
67
- #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
68
- #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
69
- pub enum ResourceScopeKind {
70
- Application,
71
- Collection,
72
- Resource,
73
- ResourcePrefix,
74
- Workload,
75
- Connection,
76
- Artifact,
77
- }
78
-
79
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
80
- pub struct ResourceScope {
81
- pub kind: ResourceScopeKind,
82
- pub uri: String,
83
- }
84
- impl ResourceScope {
85
- pub fn matches(&self, resource: &Resource) -> bool {
86
- match self.kind {
87
- ResourceScopeKind::Application => {
88
- resource.uri == self.uri
89
- || resource
90
- .uri
91
- .starts_with(&(self.uri.trim_end_matches('/').to_owned() + "/"))
92
- }
93
- ResourceScopeKind::ResourcePrefix => {
94
- let prefix = self.uri.trim_end_matches('*').trim_end_matches('/');
95
- resource.uri == prefix || resource.uri.starts_with(&(prefix.to_owned() + "/"))
96
- }
97
- _ => resource.uri == self.uri,
98
- }
99
- }
100
- pub fn is_subset_of(&self, parent: &Self) -> bool {
101
- parent.matches(&Resource {
102
- uri: self.uri.trim_end_matches('*').trim_end_matches('/').into(),
103
- attributes: BTreeMap::new(),
104
- }) && (!matches!(self.kind, ResourceScopeKind::Application)
105
- || matches!(parent.kind, ResourceScopeKind::Application))
106
- }
107
- }
108
-
109
- #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
110
- pub struct AuthorizationContext {
111
- pub environment: Option<String>,
112
- pub execution_id: Option<String>,
113
- pub workload_id: Option<String>,
114
- pub worker_id: Option<String>,
115
- pub connection_id: Option<String>,
116
- pub timestamp: i64,
117
- #[serde(default)]
118
- pub attributes: BTreeMap<String, String>,
119
- }
120
- impl AuthorizationContext {
121
- pub fn now() -> Self {
122
- Self {
123
- timestamp: unix_seconds(),
124
- ..Default::default()
125
- }
126
- }
127
- }
128
-
129
- #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
130
- pub struct GrantConstraints {
131
- #[serde(default)]
132
- pub environments: BTreeSet<String>,
133
- pub worker_class: Option<String>,
134
- pub network: Option<String>,
135
- pub workload_id: Option<String>,
136
- pub connection_enabled: Option<bool>,
137
- pub minimum_schema_version: Option<u64>,
138
- #[serde(default)]
139
- pub subject_attributes: BTreeMap<String, String>,
140
- #[serde(default)]
141
- pub resource_attributes: BTreeMap<String, String>,
142
- #[serde(default)]
143
- pub context_attributes: BTreeMap<String, String>,
144
- }
145
- impl GrantConstraints {
146
- fn permits(
147
- &self,
148
- subject: &Subject,
149
- resource: &Resource,
150
- context: &AuthorizationContext,
151
- ) -> bool {
152
- (self.environments.is_empty()
153
- || context
154
- .environment
155
- .as_ref()
156
- .is_some_and(|v| self.environments.contains(v)))
157
- && self
158
- .workload_id
159
- .as_ref()
160
- .is_none_or(|v| context.workload_id.as_ref() == Some(v))
161
- && self
162
- .worker_class
163
- .as_ref()
164
- .is_none_or(|v| context.attributes.get("worker_class") == Some(v))
165
- && self
166
- .network
167
- .as_ref()
168
- .is_none_or(|v| context.attributes.get("network") == Some(v))
169
- && self.connection_enabled.is_none_or(|v| {
170
- context
171
- .attributes
172
- .get("connection_enabled")
173
- .map(|x| x == "true")
174
- == Some(v)
175
- })
176
- && self.minimum_schema_version.is_none_or(|v| {
177
- context
178
- .attributes
179
- .get("schema_version")
180
- .and_then(|x| x.parse().ok())
181
- .is_some_and(|x: u64| x >= v)
182
- })
183
- && self.subject_attributes.iter().all(|(k, v)| {
184
- (k == "type" && subject.kind() == v)
185
- || context.attributes.get(&format!("subject.{k}")) == Some(v)
186
- })
187
- && self
188
- .resource_attributes
189
- .iter()
190
- .all(|(k, v)| resource.attributes.get(k) == Some(v))
191
- && self
192
- .context_attributes
193
- .iter()
194
- .all(|(k, v)| context.attributes.get(k) == Some(v))
195
- }
196
- fn is_subset_of(&self, parent: &Self) -> bool {
197
- (parent.environments.is_empty() || self.environments.is_subset(&parent.environments))
198
- && parent
199
- .worker_class
200
- .as_ref()
201
- .is_none_or(|v| self.worker_class.as_ref() == Some(v))
202
- && parent
203
- .network
204
- .as_ref()
205
- .is_none_or(|v| self.network.as_ref() == Some(v))
206
- && parent
207
- .workload_id
208
- .as_ref()
209
- .is_none_or(|v| self.workload_id.as_ref() == Some(v))
210
- && parent
211
- .connection_enabled
212
- .is_none_or(|v| self.connection_enabled == Some(v))
213
- && parent
214
- .minimum_schema_version
215
- .is_none_or(|v| self.minimum_schema_version.is_some_and(|child| child >= v))
216
- && parent
217
- .subject_attributes
218
- .iter()
219
- .all(|(k, v)| self.subject_attributes.get(k) == Some(v))
220
- && parent
221
- .resource_attributes
222
- .iter()
223
- .all(|(k, v)| self.resource_attributes.get(k) == Some(v))
224
- && parent
225
- .context_attributes
226
- .iter()
227
- .all(|(k, v)| self.context_attributes.get(k) == Some(v))
228
- }
229
- }
230
-
231
- #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
232
- #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
233
- pub enum GrantStatus {
234
- Active,
235
- Revoked,
236
- Expired,
237
- Superseded,
238
- }
239
-
240
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
241
- pub struct Grant {
242
- pub contract_version: u32,
243
- pub id: String,
244
- pub issuer: Subject,
245
- pub subject: Subject,
246
- pub tenant_id: String,
247
- pub application_id: String,
248
- pub capabilities: BTreeSet<String>,
249
- pub resources: Vec<ResourceScope>,
250
- #[serde(default)]
251
- pub constraints: GrantConstraints,
252
- pub issued_at: i64,
253
- pub expires_at: i64,
254
- pub parent_grant: Option<String>,
255
- pub revision: u64,
256
- pub status: GrantStatus,
257
- }
258
- impl Grant {
259
- pub fn is_subset_of(&self, parent: &Grant) -> bool {
260
- self.tenant_id == parent.tenant_id
261
- && self.application_id == parent.application_id
262
- && self.issued_at >= parent.issued_at
263
- && self.expires_at <= parent.expires_at
264
- && self.capabilities.is_subset(&parent.capabilities)
265
- && self.resources.iter().all(|child| {
266
- parent
267
- .resources
268
- .iter()
269
- .any(|scope| child.is_subset_of(scope))
270
- })
271
- && self.constraints.is_subset_of(&parent.constraints)
272
- }
273
- pub fn canonical_bytes(&self) -> Result<Vec<u8>, AuthorizationError> {
274
- serde_json::to_vec(self)
275
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))
276
- }
277
- pub fn digest(&self) -> Result<String, AuthorizationError> {
278
- Ok(hex(&Sha256::digest(self.canonical_bytes()?)))
279
- }
280
- }
281
-
282
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
283
- pub struct SignedGrant {
284
- pub algorithm: String,
285
- pub key_id: String,
286
- pub grant: Grant,
287
- pub digest: String,
288
- pub signature: String,
289
- }
290
-
291
- #[derive(Clone)]
292
- pub struct GrantSigner {
293
- key_id: String,
294
- signing: Option<SigningKey>,
295
- verifying: VerifyingKey,
296
- }
297
- impl GrantSigner {
298
- pub fn new(
299
- key_id: impl Into<String>,
300
- key: impl Into<Vec<u8>>,
301
- ) -> Result<Self, AuthorizationError> {
302
- let key = key.into();
303
- if key.len() != 32 {
304
- return Err(AuthorizationError::new(
305
- "GRANT_INVALID",
306
- "Ed25519 signing key must be 32 bytes",
307
- ));
308
- }
309
- let bytes: [u8; 32] = key
310
- .try_into()
311
- .map_err(|_| AuthorizationError::new("GRANT_INVALID", "invalid signing key"))?;
312
- let signing = SigningKey::from_bytes(&bytes);
313
- let verifying = signing.verifying_key();
314
- Ok(Self {
315
- key_id: key_id.into(),
316
- signing: Some(signing),
317
- verifying,
318
- })
319
- }
320
- pub fn verifier(
321
- key_id: impl Into<String>,
322
- public_key: &[u8],
323
- ) -> Result<Self, AuthorizationError> {
324
- let bytes: [u8; 32] = public_key.try_into().map_err(|_| {
325
- AuthorizationError::new("GRANT_INVALID", "Ed25519 public key must be 32 bytes")
326
- })?;
327
- let verifying = VerifyingKey::from_bytes(&bytes)
328
- .map_err(|_| AuthorizationError::new("GRANT_INVALID", "invalid Ed25519 public key"))?;
329
- Ok(Self {
330
- key_id: key_id.into(),
331
- signing: None,
332
- verifying,
333
- })
334
- }
335
- pub fn verifier_hex(
336
- key_id: impl Into<String>,
337
- public_key: &str,
338
- ) -> Result<Self, AuthorizationError> {
339
- Self::verifier(key_id, &decode_hex(public_key)?)
340
- }
341
- pub fn public_key_hex(&self) -> String {
342
- hex(self.verifying.as_bytes())
343
- }
344
- pub fn sign(&self, grant: Grant) -> Result<SignedGrant, AuthorizationError> {
345
- let bytes = grant.canonical_bytes()?;
346
- let digest = hex(&Sha256::digest(&bytes));
347
- let signing = self.signing.as_ref().ok_or_else(|| {
348
- AuthorizationError::new("GRANT_INVALID", "verification-only key cannot sign grants")
349
- })?;
350
- Ok(SignedGrant {
351
- algorithm: "ED25519-V1".into(),
352
- key_id: self.key_id.clone(),
353
- signature: hex(&signing.sign(&bytes).to_bytes()),
354
- digest,
355
- grant,
356
- })
357
- }
358
- pub fn verify_signature(&self, signed: &SignedGrant) -> Result<(), AuthorizationError> {
359
- if signed.algorithm != "ED25519-V1" || signed.key_id != self.key_id {
360
- return Err(AuthorizationError::new(
361
- "GRANT_INVALID",
362
- "unsupported signer",
363
- ));
364
- }
365
- let bytes = signed.grant.canonical_bytes()?;
366
- let expected_digest = hex(&Sha256::digest(&bytes));
367
- let signature = Signature::from_slice(&decode_hex(&signed.signature)?)
368
- .map_err(|_| AuthorizationError::new("GRANT_INVALID", "invalid Ed25519 signature"))?;
369
- if self.verifying.verify(&bytes, &signature).is_err() || expected_digest != signed.digest {
370
- return Err(AuthorizationError::new(
371
- "GRANT_INVALID",
372
- "grant signature or digest is invalid",
373
- ));
374
- }
375
- Ok(())
376
- }
377
- }
378
-
379
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
380
- pub struct AuthorizationRequest {
381
- pub subject: Subject,
382
- pub tenant_id: String,
383
- pub application_id: String,
384
- pub resource: Resource,
385
- pub capability: String,
386
- #[serde(default)]
387
- pub context: AuthorizationContext,
388
- pub grant_id: Option<String>,
389
- #[serde(default)]
390
- pub subject_attributes: BTreeMap<String, String>,
391
- }
392
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
393
- pub struct AuthorizationDecision {
394
- pub decision_id: String,
395
- pub request_hash: String,
396
- pub decision_hash: String,
397
- pub allowed: bool,
398
- pub subject: Subject,
399
- pub tenant_id: String,
400
- pub application_id: String,
401
- pub resource: Resource,
402
- pub capability: String,
403
- pub grant_id: Option<String>,
404
- pub policy_version: String,
405
- pub evaluated_at: i64,
406
- pub reason: String,
407
- }
408
-
409
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
410
- pub struct AuthorizationError {
411
- pub code: String,
412
- pub message: String,
413
- }
414
- impl AuthorizationError {
415
- pub fn new(code: &str, message: impl Into<String>) -> Self {
416
- Self {
417
- code: code.into(),
418
- message: message.into(),
419
- }
420
- }
421
- }
422
- impl std::fmt::Display for AuthorizationError {
423
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
424
- write!(f, "{}: {}", self.code, self.message)
425
- }
426
- }
427
- impl std::error::Error for AuthorizationError {}
428
-
429
- #[derive(Debug, Clone, Default, Serialize, Deserialize)]
430
- pub struct RevocationState {
431
- #[serde(default)]
432
- pub grants: BTreeSet<String>,
433
- #[serde(default)]
434
- pub subjects: BTreeSet<Subject>,
435
- #[serde(default)]
436
- pub applications: BTreeSet<String>,
437
- #[serde(default)]
438
- pub tenants: BTreeSet<String>,
439
- }
440
-
441
- /// File-backed control-plane state. Every mutation is persisted before it is
442
- /// returned to callers, so revocation and delegation survive process restarts.
443
- #[derive(Debug, Clone, Serialize, Deserialize)]
444
- pub struct GrantStore {
445
- #[serde(skip)]
446
- path: PathBuf,
447
- #[serde(default)]
448
- pub grants: BTreeMap<String, SignedGrant>,
449
- #[serde(default)]
450
- pub revocations: RevocationState,
451
- #[serde(default)]
452
- pub decisions: BTreeMap<String, AuthorizationDecision>,
453
- }
454
- impl GrantStore {
455
- pub fn load(path: impl AsRef<Path>) -> Result<Self, AuthorizationError> {
456
- let path = path.as_ref().to_path_buf();
457
- if !path.exists() {
458
- return Ok(Self {
459
- path,
460
- grants: BTreeMap::new(),
461
- revocations: Default::default(),
462
- decisions: BTreeMap::new(),
463
- });
464
- }
465
- let mut store: Self = serde_json::from_slice(
466
- &fs::read(&path)
467
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))?,
468
- )
469
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))?;
470
- store.path = path;
471
- Ok(store)
472
- }
473
- fn persist(&self) -> Result<(), AuthorizationError> {
474
- if let Some(parent) = self.path.parent() {
475
- fs::create_dir_all(parent)
476
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))?;
477
- }
478
- let temporary = self.path.with_extension("tmp");
479
- fs::write(
480
- &temporary,
481
- serde_json::to_vec_pretty(self)
482
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))?,
483
- )
484
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))?;
485
- fs::rename(temporary, &self.path)
486
- .map_err(|e| AuthorizationError::new("GRANT_INVALID", e.to_string()))
487
- }
488
- pub fn create(
489
- &mut self,
490
- grant: Grant,
491
- signer: &GrantSigner,
492
- ) -> Result<SignedGrant, AuthorizationError> {
493
- if grant.contract_version != AUTHORIZATION_CONTRACT_VERSION
494
- || grant.expires_at <= grant.issued_at
495
- || grant.status != GrantStatus::Active
496
- || self.grants.contains_key(&grant.id)
497
- {
498
- return Err(AuthorizationError::new(
499
- "GRANT_INVALID",
500
- "invalid or duplicate grant lifecycle",
501
- ));
502
- }
503
- if grant.parent_grant.is_some() {
504
- return Err(AuthorizationError::new(
505
- "GRANT_DELEGATION_INVALID",
506
- "use delegate for a derived grant",
507
- ));
508
- }
509
- validate_scopes(&grant)?;
510
- let signed = signer.sign(grant)?;
511
- self.grants.insert(signed.grant.id.clone(), signed.clone());
512
- self.persist()?;
513
- Ok(signed)
514
- }
515
- pub fn delegate(
516
- &mut self,
517
- mut grant: Grant,
518
- parent_id: &str,
519
- signer: &GrantSigner,
520
- ) -> Result<SignedGrant, AuthorizationError> {
521
- let parent = self.grants.get(parent_id).ok_or_else(|| {
522
- AuthorizationError::new("GRANT_DELEGATION_INVALID", "parent grant not found")
523
- })?;
524
- if self.revocations.grants.contains(parent_id) || parent.grant.status != GrantStatus::Active
525
- {
526
- return Err(AuthorizationError::new(
527
- "GRANT_REVOKED",
528
- "parent grant is not active",
529
- ));
530
- }
531
- grant.parent_grant = Some(parent_id.into());
532
- if grant.issuer != parent.grant.subject {
533
- return Err(AuthorizationError::new(
534
- "GRANT_DELEGATION_INVALID",
535
- "derived grant issuer must be the parent subject",
536
- ));
537
- }
538
- if !grant.is_subset_of(&parent.grant) {
539
- return Err(AuthorizationError::new(
540
- "GRANT_DELEGATION_INVALID",
541
- "delegation broadens parent authority",
542
- ));
543
- }
544
- validate_scopes(&grant)?;
545
- let signed = signer.sign(grant)?;
546
- self.grants.insert(signed.grant.id.clone(), signed.clone());
547
- self.persist()?;
548
- Ok(signed)
549
- }
550
- pub fn revoke(
551
- &mut self,
552
- grant_id: &str,
553
- signer: &GrantSigner,
554
- ) -> Result<SignedGrant, AuthorizationError> {
555
- let signed = self
556
- .grants
557
- .get_mut(grant_id)
558
- .ok_or_else(|| AuthorizationError::new("GRANT_INVALID", "grant not found"))?;
559
- signed.grant.status = GrantStatus::Revoked;
560
- *signed = signer.sign(signed.grant.clone())?;
561
- self.revocations.grants.insert(grant_id.into());
562
- let result = signed.clone();
563
- self.persist()?;
564
- Ok(result)
565
- }
566
- pub fn check(
567
- &mut self,
568
- request: &AuthorizationRequest,
569
- signer: &GrantSigner,
570
- ) -> AuthorizationDecision {
571
- let signed = request.grant_id.as_ref().and_then(|id| self.grants.get(id));
572
- let decision = authorize(request, signed, signer, &self.grants, &self.revocations);
573
- self.decisions
574
- .insert(decision.decision_id.clone(), decision.clone());
575
- let _ = self.persist();
576
- decision
577
- }
578
- }
579
-
580
- fn validate_scopes(grant: &Grant) -> Result<(), AuthorizationError> {
581
- if grant.capabilities.is_empty() || grant.resources.is_empty() {
582
- return Err(AuthorizationError::new(
583
- "GRANT_INVALID",
584
- "grant requires capabilities and resource scopes",
585
- ));
586
- }
587
- for scope in &grant.resources {
588
- validate_resource_uri(
589
- scope.uri.trim_end_matches('*').trim_end_matches('/'),
590
- &grant.application_id,
591
- )?;
592
- }
593
- Ok(())
594
- }
595
-
596
- pub fn verify_grant(
597
- signed: &SignedGrant,
598
- signer: &GrantSigner,
599
- request: &AuthorizationRequest,
600
- grants: &BTreeMap<String, SignedGrant>,
601
- revoked: &RevocationState,
602
- ) -> Result<(), AuthorizationError> {
603
- signer.verify_signature(signed)?;
604
- let grant = &signed.grant;
605
- if grant.subject != request.subject {
606
- return Err(AuthorizationError::new(
607
- "GRANT_SCOPE_DENIED",
608
- "wrong subject",
609
- ));
610
- }
611
- if grant.tenant_id != request.tenant_id || grant.application_id != request.application_id {
612
- return Err(AuthorizationError::new(
613
- "GRANT_SCOPE_DENIED",
614
- "cross-tenant or cross-application grant",
615
- ));
616
- }
617
- validate_resource_uri(&request.resource.uri, &request.application_id)?;
618
- if grant.expires_at <= request.context.timestamp || grant.status == GrantStatus::Expired {
619
- return Err(AuthorizationError::new(
620
- "GRANT_EXPIRED",
621
- "grant has expired",
622
- ));
623
- }
624
- if grant.status == GrantStatus::Revoked
625
- || revoked.grants.contains(&grant.id)
626
- || revoked.subjects.contains(&grant.subject)
627
- || revoked.applications.contains(&grant.application_id)
628
- || revoked.tenants.contains(&grant.tenant_id)
629
- {
630
- return Err(AuthorizationError::new("GRANT_REVOKED", "grant is revoked"));
631
- }
632
- if !grant.capabilities.contains(&request.capability)
633
- || !grant
634
- .resources
635
- .iter()
636
- .any(|scope| scope.matches(&request.resource))
637
- {
638
- return Err(AuthorizationError::new(
639
- "GRANT_SCOPE_DENIED",
640
- "capability has no applicable resource scope",
641
- ));
642
- }
643
- if !grant
644
- .constraints
645
- .permits(&grant.subject, &request.resource, &request.context)
646
- {
647
- return Err(AuthorizationError::new(
648
- "GRANT_SCOPE_DENIED",
649
- "authorization context does not satisfy grant constraints",
650
- ));
651
- }
652
- if let Some(parent_id) = &grant.parent_grant {
653
- let parent = grants.get(parent_id).ok_or_else(|| {
654
- AuthorizationError::new("GRANT_DELEGATION_INVALID", "parent grant not found")
655
- })?;
656
- let parent_request = AuthorizationRequest {
657
- subject: parent.grant.subject.clone(),
658
- grant_id: Some(parent_id.clone()),
659
- ..request.clone()
660
- };
661
- verify_grant(parent, signer, &parent_request, grants, revoked)?;
662
- if !grant.is_subset_of(&parent.grant) {
663
- return Err(AuthorizationError::new(
664
- "GRANT_DELEGATION_INVALID",
665
- "derived grant broadens parent authority",
666
- ));
667
- }
668
- }
669
- Ok(())
670
- }
671
-
672
- pub fn authorize(
673
- request: &AuthorizationRequest,
674
- signed: Option<&SignedGrant>,
675
- signer: &GrantSigner,
676
- grants: &BTreeMap<String, SignedGrant>,
677
- revoked: &RevocationState,
678
- ) -> AuthorizationDecision {
679
- let evaluated_at = request.context.timestamp;
680
- let request_bytes = serde_json::to_vec(request).unwrap_or_default();
681
- let request_hash = hex(&Sha256::digest(request_bytes));
682
- let result = signed
683
- .ok_or_else(|| AuthorizationError::new("GRANT_SCOPE_DENIED", "a scoped grant is required"))
684
- .and_then(|grant| verify_grant(grant, signer, request, grants, revoked));
685
- let allowed = result.is_ok();
686
- let reason = result
687
- .err()
688
- .map(|e| format!("{}: {}", e.code, e.message))
689
- .unwrap_or_else(|| "GRANT_ALLOWED: signed grant authorizes resource operation".into());
690
- let seed = format!("{request_hash}:{evaluated_at}:{allowed}:{reason}");
691
- let decision_id = format!("decision_{}", &hex(&Sha256::digest(seed.as_bytes()))[..24]);
692
- let mut decision = AuthorizationDecision {
693
- decision_id,
694
- request_hash,
695
- decision_hash: String::new(),
696
- allowed,
697
- subject: request.subject.clone(),
698
- tenant_id: request.tenant_id.clone(),
699
- application_id: request.application_id.clone(),
700
- resource: request.resource.clone(),
701
- capability: request.capability.clone(),
702
- grant_id: signed.map(|v| v.grant.id.clone()),
703
- policy_version: POLICY_VERSION.into(),
704
- evaluated_at,
705
- reason,
706
- };
707
- decision.decision_hash = hex(&Sha256::digest(
708
- serde_json::to_vec(&decision).unwrap_or_default(),
709
- ));
710
- decision
711
- }
712
-
713
- #[derive(Debug, Clone, Serialize, Deserialize)]
714
- pub struct ReconciliationRequest {
715
- pub original_grant: SignedGrant,
716
- pub current_request: AuthorizationRequest,
717
- pub original_revision: String,
718
- pub current_revision: String,
719
- }
720
- pub fn reauthorize_reconciliation(
721
- input: &ReconciliationRequest,
722
- signer: &GrantSigner,
723
- grants: &BTreeMap<String, SignedGrant>,
724
- revoked: &RevocationState,
725
- ) -> Result<AuthorizationDecision, AuthorizationError> {
726
- let decision = authorize(
727
- &input.current_request,
728
- Some(&input.original_grant),
729
- signer,
730
- grants,
731
- revoked,
732
- );
733
- if !decision.allowed {
734
- return Err(AuthorizationError::new(
735
- "REAUTHORIZATION_REQUIRED",
736
- decision.reason,
737
- ));
738
- }
739
- if input.original_revision != input.current_revision {
740
- return Err(AuthorizationError::new(
741
- "REAUTHORIZATION_REQUIRED",
742
- "application revision changed while offline",
743
- ));
744
- }
745
- Ok(decision)
746
- }
747
-
748
- #[derive(Debug, Clone, Serialize, Deserialize)]
749
- pub struct ExecutionClaim {
750
- pub execution_id: String,
751
- pub workload_id: String,
752
- pub worker: Subject,
753
- pub grant: SignedGrant,
754
- }
755
- #[derive(Default)]
756
- pub struct ClaimRegistry {
757
- claimed: BTreeSet<String>,
758
- }
759
- impl ClaimRegistry {
760
- pub fn claim(
761
- &mut self,
762
- claim: &ExecutionClaim,
763
- request: &AuthorizationRequest,
764
- signer: &GrantSigner,
765
- grants: &BTreeMap<String, SignedGrant>,
766
- revoked: &RevocationState,
767
- ) -> Result<AuthorizationDecision, AuthorizationError> {
768
- if !matches!(&claim.worker, Subject::Worker(_))
769
- || claim.worker != request.subject
770
- || request.context.execution_id.as_ref() != Some(&claim.execution_id)
771
- || request.context.workload_id.as_ref() != Some(&claim.workload_id)
772
- {
773
- return Err(AuthorizationError::new(
774
- "GRANT_SCOPE_DENIED",
775
- "worker is not assigned to this workload execution",
776
- ));
777
- }
778
- if self.claimed.contains(&claim.execution_id) {
779
- return Err(AuthorizationError::new(
780
- "GRANT_INVALID",
781
- "execution already claimed",
782
- ));
783
- }
784
- let decision = authorize(request, Some(&claim.grant), signer, grants, revoked);
785
- if !decision.allowed {
786
- return Err(AuthorizationError::new(
787
- "GRANT_SCOPE_DENIED",
788
- decision.reason,
789
- ));
790
- }
791
- self.claimed.insert(claim.execution_id.clone());
792
- Ok(decision)
793
- }
794
- }
795
-
796
- pub fn validate_resource_uri(uri: &str, application_id: &str) -> Result<(), AuthorizationError> {
797
- let prefix = format!("flow://{application_id}/");
798
- let root = format!("flow://{application_id}");
799
- if (uri != root && !uri.starts_with(&prefix))
800
- || uri.contains("..")
801
- || uri.contains('?')
802
- || uri.contains('#')
803
- {
804
- return Err(AuthorizationError::new(
805
- "GRANT_SCOPE_DENIED",
806
- "resource is not a stable application-scoped FeltDB identity",
807
- ));
808
- }
809
- Ok(())
810
- }
811
- fn unix_seconds() -> i64 {
812
- SystemTime::now()
813
- .duration_since(UNIX_EPOCH)
814
- .unwrap_or_default()
815
- .as_secs() as i64
816
- }
817
- fn hex(bytes: &[u8]) -> String {
818
- bytes.iter().map(|v| format!("{v:02x}")).collect()
819
- }
820
- fn decode_hex(value: &str) -> Result<Vec<u8>, AuthorizationError> {
821
- if value.len() % 2 != 0 {
822
- return Err(AuthorizationError::new(
823
- "GRANT_INVALID",
824
- "invalid hex encoding",
825
- ));
826
- }
827
- (0..value.len())
828
- .step_by(2)
829
- .map(|index| {
830
- u8::from_str_radix(&value[index..index + 2], 16)
831
- .map_err(|_| AuthorizationError::new("GRANT_INVALID", "invalid hex encoding"))
832
- })
833
- .collect()
834
- }
835
-
836
- #[cfg(test)]
837
- mod tests {
838
- use super::*;
839
- fn signer() -> GrantSigner {
840
- GrantSigner::new("test", vec![7; 32]).unwrap()
841
- }
842
- fn grant() -> Grant {
843
- Grant {
844
- contract_version: 1,
845
- id: "g1".into(),
846
- issuer: Subject::Human("owner".into()),
847
- subject: Subject::Agent("coordinator".into()),
848
- tenant_id: "relief".into(),
849
- application_id: "relief-fabric".into(),
850
- capabilities: ["state:read".into(), "state:write".into()].into(),
851
- resources: vec![ResourceScope {
852
- kind: ResourceScopeKind::ResourcePrefix,
853
- uri: "flow://relief-fabric/incidents/*".into(),
854
- }],
855
- constraints: Default::default(),
856
- issued_at: 100,
857
- expires_at: 300,
858
- parent_grant: None,
859
- revision: 1,
860
- status: GrantStatus::Active,
861
- }
862
- }
863
- fn request() -> AuthorizationRequest {
864
- AuthorizationRequest {
865
- subject: Subject::Agent("coordinator".into()),
866
- tenant_id: "relief".into(),
867
- application_id: "relief-fabric".into(),
868
- resource: Resource {
869
- uri: "flow://relief-fabric/incidents/123".into(),
870
- attributes: BTreeMap::new(),
871
- },
872
- capability: "state:read".into(),
873
- context: AuthorizationContext {
874
- timestamp: 200,
875
- ..Default::default()
876
- },
877
- grant_id: Some("g1".into()),
878
- subject_attributes: BTreeMap::new(),
879
- }
880
- }
881
- #[test]
882
- fn signed_scope_and_tampering() {
883
- let signer = signer();
884
- let signed = signer.sign(grant()).unwrap();
885
- let grants = [("g1".into(), signed.clone())].into();
886
- assert!(
887
- authorize(
888
- &request(),
889
- Some(&signed),
890
- &signer,
891
- &grants,
892
- &Default::default()
893
- )
894
- .allowed
895
- );
896
- let mut bad = signed.clone();
897
- bad.grant.capabilities.insert("secret:read".into());
898
- assert_eq!(
899
- verify_grant(&bad, &signer, &request(), &grants, &Default::default())
900
- .unwrap_err()
901
- .code,
902
- "GRANT_INVALID"
903
- );
904
- }
905
- #[test]
906
- fn cross_boundary_and_context_denied() {
907
- let signer = signer();
908
- let signed = signer.sign(grant()).unwrap();
909
- let grants = [("g1".into(), signed.clone())].into();
910
- let mut r = request();
911
- r.application_id = "other".into();
912
- assert!(!authorize(&r, Some(&signed), &signer, &grants, &Default::default()).allowed);
913
- let mut r = request();
914
- r.resource.uri = "flow://relief-fabric/assignments/1".into();
915
- assert!(!authorize(&r, Some(&signed), &signer, &grants, &Default::default()).allowed);
916
- }
917
- #[test]
918
- fn delegation_only_narrows() {
919
- let signer = signer();
920
- let parent = signer.sign(grant()).unwrap();
921
- let mut child = grant();
922
- child.id = "g2".into();
923
- child.issuer = child.subject.clone();
924
- child.subject = Subject::Workload("w1".into());
925
- child.capabilities = ["state:write".into()].into();
926
- child.resources = vec![ResourceScope {
927
- kind: ResourceScopeKind::Resource,
928
- uri: "flow://relief-fabric/incidents/123".into(),
929
- }];
930
- child.expires_at = 250;
931
- child.parent_grant = Some("g1".into());
932
- assert!(child.is_subset_of(&parent.grant));
933
- child.capabilities.insert("secret:read".into());
934
- assert!(!child.is_subset_of(&parent.grant));
935
- }
936
- #[test]
937
- fn expiration_and_parent_revocation_propagate() {
938
- let signer = signer();
939
- let signed = signer.sign(grant()).unwrap();
940
- let grants = [("g1".into(), signed.clone())].into();
941
- let mut r = request();
942
- r.context.timestamp = 301;
943
- assert!(
944
- authorize(&r, Some(&signed), &signer, &grants, &Default::default())
945
- .reason
946
- .starts_with("GRANT_EXPIRED")
947
- );
948
- let mut revoked = RevocationState::default();
949
- revoked.grants.insert("g1".into());
950
- assert!(
951
- authorize(&request(), Some(&signed), &signer, &grants, &revoked)
952
- .reason
953
- .starts_with("GRANT_REVOKED")
954
- );
955
- }
956
- #[test]
957
- fn connection_use_never_implies_secret_read() {
958
- let signer = signer();
959
- let mut g = grant();
960
- g.capabilities = ["connection:use".into()].into();
961
- g.resources = vec![ResourceScope {
962
- kind: ResourceScopeKind::Connection,
963
- uri: "flow://relief-fabric/connections/weather".into(),
964
- }];
965
- let signed = signer.sign(g).unwrap();
966
- let grants = [("g1".into(), signed.clone())].into();
967
- let mut r = request();
968
- r.resource.uri = "flow://relief-fabric/connections/weather".into();
969
- r.capability = "connection:use".into();
970
- assert!(authorize(&r, Some(&signed), &signer, &grants, &Default::default()).allowed);
971
- r.capability = "secret:read".into();
972
- assert!(!authorize(&r, Some(&signed), &signer, &grants, &Default::default()).allowed);
973
- }
974
- #[test]
975
- fn offline_reconciliation_rechecks_revision_and_revocation() {
976
- let signer = signer();
977
- let signed = signer.sign(grant()).unwrap();
978
- let grants = [("g1".into(), signed.clone())].into();
979
- let input = ReconciliationRequest {
980
- original_grant: signed,
981
- current_request: request(),
982
- original_revision: "r1".into(),
983
- current_revision: "r2".into(),
984
- };
985
- assert_eq!(
986
- reauthorize_reconciliation(&input, &signer, &grants, &Default::default())
987
- .unwrap_err()
988
- .code,
989
- "REAUTHORIZATION_REQUIRED"
990
- );
991
- }
992
- #[test]
993
- fn public_verifier_and_worker_claim_enforce_assignment_and_replay() {
994
- let signer = signer();
995
- let verifier = GrantSigner::verifier_hex("test", &signer.public_key_hex()).unwrap();
996
- let mut g = grant();
997
- g.subject = Subject::Worker("worker-42".into());
998
- g.capabilities = ["workload:claim".into()].into();
999
- g.resources = vec![ResourceScope {
1000
- kind: ResourceScopeKind::Workload,
1001
- uri: "flow://relief-fabric/workloads/w1".into(),
1002
- }];
1003
- let signed = signer.sign(g).unwrap();
1004
- assert!(verifier.verify_signature(&signed).is_ok());
1005
- let grants = [("g1".into(), signed.clone())].into();
1006
- let mut request = request();
1007
- request.subject = Subject::Worker("worker-42".into());
1008
- request.capability = "workload:claim".into();
1009
- request.resource.uri = "flow://relief-fabric/workloads/w1".into();
1010
- request.context.execution_id = Some("e1".into());
1011
- request.context.workload_id = Some("w1".into());
1012
- let claim = ExecutionClaim {
1013
- execution_id: "e1".into(),
1014
- workload_id: "w1".into(),
1015
- worker: request.subject.clone(),
1016
- grant: signed,
1017
- };
1018
- let mut registry = ClaimRegistry::default();
1019
- assert!(registry
1020
- .claim(&claim, &request, &verifier, &grants, &Default::default())
1021
- .is_ok());
1022
- assert_eq!(
1023
- registry
1024
- .claim(&claim, &request, &verifier, &grants, &Default::default())
1025
- .unwrap_err()
1026
- .message,
1027
- "execution already claimed"
1028
- );
1029
- }
1030
- }