create-feltdb 0.5.0 → 0.5.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.
package/dist/package-versions.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// One release train keeps generated applications installable. The repository
|
|
2
2
|
// validation script checks these values against every workspace manifest.
|
|
3
|
-
export const FELTDB_PACKAGE_VERSION = '0.5.
|
|
3
|
+
export const FELTDB_PACKAGE_VERSION = '0.5.1';
|
|
4
4
|
export const feltdbPackageRange = `^${FELTDB_PACKAGE_VERSION}`;
|
|
@@ -959,6 +959,18 @@ pub struct RevisionAuditEvent {
|
|
|
959
959
|
pub diff_hash: Option<String>,
|
|
960
960
|
}
|
|
961
961
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
962
|
+
pub struct RevisionRecovery {
|
|
963
|
+
pub recovery_id: String,
|
|
964
|
+
pub application_id: String,
|
|
965
|
+
pub environment: String,
|
|
966
|
+
pub source_revision: String,
|
|
967
|
+
pub target_revision: String,
|
|
968
|
+
pub approved_by: String,
|
|
969
|
+
pub reason: String,
|
|
970
|
+
pub authorization_level: String,
|
|
971
|
+
pub recovered_at: u64,
|
|
972
|
+
}
|
|
973
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
962
974
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
963
975
|
pub enum ChangeSafety {
|
|
964
976
|
Safe,
|
|
@@ -1099,6 +1111,10 @@ struct RevisionRecords {
|
|
|
1099
1111
|
previews: Vec<ApplicationPreview>,
|
|
1100
1112
|
environment_pointers: BTreeMap<String, BTreeMap<String, String>>,
|
|
1101
1113
|
audit: Vec<RevisionAuditEvent>,
|
|
1114
|
+
#[serde(default)]
|
|
1115
|
+
untrusted_revisions: BTreeMap<String, BTreeMap<String, String>>,
|
|
1116
|
+
#[serde(default)]
|
|
1117
|
+
recoveries: Vec<RevisionRecovery>,
|
|
1102
1118
|
}
|
|
1103
1119
|
#[derive(Clone)]
|
|
1104
1120
|
pub struct ApplicationStore {
|
|
@@ -1462,6 +1478,15 @@ impl ApplicationStore {
|
|
|
1462
1478
|
})
|
|
1463
1479
|
.ok_or("revision not found")?
|
|
1464
1480
|
.clone();
|
|
1481
|
+
if r.untrusted_revisions
|
|
1482
|
+
.get(app)
|
|
1483
|
+
.is_some_and(|values| values.contains_key(&revision.revision_id))
|
|
1484
|
+
{
|
|
1485
|
+
return Err("revision is permanently untrusted".into());
|
|
1486
|
+
}
|
|
1487
|
+
if manifest_hash(&revision.manifest)? != revision.manifest_hash {
|
|
1488
|
+
return Err("revision integrity check failed".into());
|
|
1489
|
+
}
|
|
1465
1490
|
if !revision
|
|
1466
1491
|
.manifest
|
|
1467
1492
|
.environments
|
|
@@ -1536,6 +1561,108 @@ impl ApplicationStore {
|
|
|
1536
1561
|
Ok(promotion)
|
|
1537
1562
|
}
|
|
1538
1563
|
|
|
1564
|
+
#[allow(clippy::too_many_arguments)]
|
|
1565
|
+
pub fn recover_environment_pointer(
|
|
1566
|
+
&self,
|
|
1567
|
+
tenant: &str,
|
|
1568
|
+
app: &str,
|
|
1569
|
+
environment: &str,
|
|
1570
|
+
expected_current_revision: &str,
|
|
1571
|
+
target_revision: &str,
|
|
1572
|
+
actor: &str,
|
|
1573
|
+
reason: &str,
|
|
1574
|
+
authorization_level: &str,
|
|
1575
|
+
recovery_id: &str,
|
|
1576
|
+
) -> Result<RevisionRecovery, String> {
|
|
1577
|
+
let mut records = self
|
|
1578
|
+
.records
|
|
1579
|
+
.write()
|
|
1580
|
+
.map_err(|_| "application store lock poisoned")?;
|
|
1581
|
+
if let Some(existing) = records
|
|
1582
|
+
.recoveries
|
|
1583
|
+
.iter()
|
|
1584
|
+
.find(|value| value.recovery_id == recovery_id)
|
|
1585
|
+
{
|
|
1586
|
+
if existing.application_id == app
|
|
1587
|
+
&& existing.environment == environment
|
|
1588
|
+
&& existing.source_revision == expected_current_revision
|
|
1589
|
+
&& existing.target_revision == target_revision
|
|
1590
|
+
{
|
|
1591
|
+
return Ok(existing.clone());
|
|
1592
|
+
}
|
|
1593
|
+
return Err("recovery_id_conflict".into());
|
|
1594
|
+
}
|
|
1595
|
+
let current = records
|
|
1596
|
+
.environment_pointers
|
|
1597
|
+
.get(app)
|
|
1598
|
+
.and_then(|values| values.get(environment))
|
|
1599
|
+
.cloned()
|
|
1600
|
+
.unwrap_or_default();
|
|
1601
|
+
if current != expected_current_revision {
|
|
1602
|
+
return Err(format!("expected_revision_mismatch:{current}"));
|
|
1603
|
+
}
|
|
1604
|
+
let target = records
|
|
1605
|
+
.revisions
|
|
1606
|
+
.iter()
|
|
1607
|
+
.find(|value| {
|
|
1608
|
+
value.tenant_id == tenant
|
|
1609
|
+
&& value.application_id == app
|
|
1610
|
+
&& (value.revision_id == target_revision
|
|
1611
|
+
|| value.revision_number.to_string() == target_revision)
|
|
1612
|
+
})
|
|
1613
|
+
.ok_or("target revision not found")?
|
|
1614
|
+
.clone();
|
|
1615
|
+
if manifest_hash(&target.manifest)? != target.manifest_hash {
|
|
1616
|
+
return Err("target revision integrity check failed".into());
|
|
1617
|
+
}
|
|
1618
|
+
if records
|
|
1619
|
+
.untrusted_revisions
|
|
1620
|
+
.get(app)
|
|
1621
|
+
.is_some_and(|values| values.contains_key(&target.revision_id))
|
|
1622
|
+
{
|
|
1623
|
+
return Err("target revision is permanently untrusted".into());
|
|
1624
|
+
}
|
|
1625
|
+
let recovery = RevisionRecovery {
|
|
1626
|
+
recovery_id: recovery_id.into(),
|
|
1627
|
+
application_id: app.into(),
|
|
1628
|
+
environment: environment.into(),
|
|
1629
|
+
source_revision: expected_current_revision.into(),
|
|
1630
|
+
target_revision: target.revision_id.clone(),
|
|
1631
|
+
approved_by: actor.into(),
|
|
1632
|
+
reason: reason.into(),
|
|
1633
|
+
authorization_level: authorization_level.into(),
|
|
1634
|
+
recovered_at: now(),
|
|
1635
|
+
};
|
|
1636
|
+
records
|
|
1637
|
+
.untrusted_revisions
|
|
1638
|
+
.entry(app.into())
|
|
1639
|
+
.or_default()
|
|
1640
|
+
.insert(expected_current_revision.into(), recovery_id.into());
|
|
1641
|
+
records
|
|
1642
|
+
.environment_pointers
|
|
1643
|
+
.entry(app.into())
|
|
1644
|
+
.or_default()
|
|
1645
|
+
.insert(environment.into(), target.revision_id.clone());
|
|
1646
|
+
records.recoveries.push(recovery.clone());
|
|
1647
|
+
Self::event(
|
|
1648
|
+
&mut records,
|
|
1649
|
+
"application.revision.recovered",
|
|
1650
|
+
tenant,
|
|
1651
|
+
app,
|
|
1652
|
+
actor,
|
|
1653
|
+
Some(&target),
|
|
1654
|
+
Some(expected_current_revision.into()),
|
|
1655
|
+
);
|
|
1656
|
+
if let Some(event) = records.audit.last_mut() {
|
|
1657
|
+
event.environment = Some(environment.into());
|
|
1658
|
+
event.from_revision = Some(expected_current_revision.into());
|
|
1659
|
+
event.to_revision = Some(target.revision_id);
|
|
1660
|
+
event.correlation_id = recovery_id.into();
|
|
1661
|
+
}
|
|
1662
|
+
self.persist(&records)?;
|
|
1663
|
+
Ok(recovery)
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1539
1666
|
pub fn history(&self, tenant: &str, app: &str, environment: &str) -> Vec<RevisionPromotion> {
|
|
1540
1667
|
self.records
|
|
1541
1668
|
.read()
|
|
@@ -2148,6 +2275,41 @@ mod tests {
|
|
|
2148
2275
|
assert_eq!(s.pointers("a")["production"], r2.revision_id);
|
|
2149
2276
|
assert_eq!(s.history("t", "a", "production").len(), 2);
|
|
2150
2277
|
}
|
|
2278
|
+
|
|
2279
|
+
#[test]
|
|
2280
|
+
fn recovery_is_atomic_idempotent_durable_and_permanently_untrusts_source() {
|
|
2281
|
+
let s = store("revision-recovery");
|
|
2282
|
+
let d = s.create_draft("t", "a", "App", "owner", None).unwrap();
|
|
2283
|
+
let corrupt = s.commit("t", "a", &d.draft_id, "owner").unwrap();
|
|
2284
|
+
s.move_environment_pointer("t", "a", &corrupt.revision_id, "staging", None,
|
|
2285
|
+
"owner", "initial", true, false).unwrap();
|
|
2286
|
+
let d = s.create_draft("t", "a", "App", "owner", Some(&corrupt.revision_id)).unwrap();
|
|
2287
|
+
let clean = s.commit("t", "a", &d.draft_id, "owner").unwrap();
|
|
2288
|
+
|
|
2289
|
+
// Historical source corruption must not prevent recovery away from it.
|
|
2290
|
+
{
|
|
2291
|
+
let mut records = s.records.write().unwrap();
|
|
2292
|
+
records.revisions.iter_mut().find(|value| value.revision_id == corrupt.revision_id)
|
|
2293
|
+
.unwrap().manifest.metadata.name = "corrupted without updating its integrity hash".into();
|
|
2294
|
+
s.persist(&records).unwrap();
|
|
2295
|
+
}
|
|
2296
|
+
let recovered = s.recover_environment_pointer("t", "a", "staging", &corrupt.revision_id,
|
|
2297
|
+
&clean.revision_id, "sherpa", "Replace corrupt historical revision", "ELEVATED",
|
|
2298
|
+
"recovery-1").unwrap();
|
|
2299
|
+
assert_eq!(recovered.target_revision, clean.revision_id);
|
|
2300
|
+
assert_eq!(s.recover_environment_pointer("t", "a", "staging", &corrupt.revision_id,
|
|
2301
|
+
&clean.revision_id, "sherpa", "Replace corrupt historical revision", "ELEVATED",
|
|
2302
|
+
"recovery-1").unwrap(), recovered);
|
|
2303
|
+
assert!(s.move_environment_pointer("t", "a", &corrupt.revision_id, "staging",
|
|
2304
|
+
Some(&clean.revision_id), "owner", "rollback", true, true).unwrap_err()
|
|
2305
|
+
.contains("untrusted"));
|
|
2306
|
+
|
|
2307
|
+
let reloaded = ApplicationStore::load(s.path.clone()).unwrap();
|
|
2308
|
+
assert_eq!(reloaded.pointers("a")["staging"], clean.revision_id);
|
|
2309
|
+
let records = reloaded.records.read().unwrap();
|
|
2310
|
+
assert_eq!(records.recoveries.len(), 1);
|
|
2311
|
+
assert!(records.untrusted_revisions["a"].contains_key(&corrupt.revision_id));
|
|
2312
|
+
}
|
|
2151
2313
|
#[test]
|
|
2152
2314
|
fn preview_remains_bound_when_production_moves() {
|
|
2153
2315
|
let s = store("preview-bound");
|
|
@@ -2280,7 +2442,11 @@ mod tests {
|
|
|
2280
2442
|
});
|
|
2281
2443
|
|
|
2282
2444
|
let report = validate_manifest(&manifest, "tenant", "app", None);
|
|
2283
|
-
assert!(
|
|
2445
|
+
assert!(
|
|
2446
|
+
report.valid,
|
|
2447
|
+
"Policy with authenticated subject should be valid: {:?}",
|
|
2448
|
+
report.issues
|
|
2449
|
+
);
|
|
2284
2450
|
}
|
|
2285
2451
|
|
|
2286
2452
|
#[test]
|
|
@@ -2295,7 +2461,11 @@ mod tests {
|
|
|
2295
2461
|
});
|
|
2296
2462
|
|
|
2297
2463
|
let report = validate_manifest(&manifest, "tenant", "app", None);
|
|
2298
|
-
assert!(
|
|
2464
|
+
assert!(
|
|
2465
|
+
report.valid,
|
|
2466
|
+
"Policy with owner subject should be valid: {:?}",
|
|
2467
|
+
report.issues
|
|
2468
|
+
);
|
|
2299
2469
|
}
|
|
2300
2470
|
|
|
2301
2471
|
#[test]
|
|
@@ -2310,12 +2480,14 @@ mod tests {
|
|
|
2310
2480
|
});
|
|
2311
2481
|
|
|
2312
2482
|
let report = validate_manifest(&manifest, "tenant", "app", None);
|
|
2313
|
-
assert!(!report.valid, "Policy with unknown subject should be invalid");
|
|
2314
2483
|
assert!(
|
|
2315
|
-
report.
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2484
|
+
!report.valid,
|
|
2485
|
+
"Policy with unknown subject should be invalid"
|
|
2486
|
+
);
|
|
2487
|
+
assert!(
|
|
2488
|
+
report.issues.iter().any(|i| i.path.contains("BadPolicy")
|
|
2489
|
+
&& i.path.contains("read")
|
|
2490
|
+
&& i.message.contains("invalid policy subject")),
|
|
2319
2491
|
"Should have error about invalid policy subject"
|
|
2320
2492
|
);
|
|
2321
2493
|
}
|
|
@@ -2332,6 +2504,9 @@ mod tests {
|
|
|
2332
2504
|
});
|
|
2333
2505
|
|
|
2334
2506
|
let report = validate_manifest(&manifest, "tenant", "app", None);
|
|
2335
|
-
assert!(
|
|
2507
|
+
assert!(
|
|
2508
|
+
report.valid,
|
|
2509
|
+
"Policy without subjects should be valid (backward compatible)"
|
|
2510
|
+
);
|
|
2336
2511
|
}
|
|
2337
2512
|
}
|
|
@@ -40,8 +40,8 @@ use feltdb::{
|
|
|
40
40
|
authorize as authorize_resource, AuthorizationRequest as ResourceAuthorizationRequest,
|
|
41
41
|
Grant, GrantSigner, GrantStore, Subject as GrantSubject,
|
|
42
42
|
},
|
|
43
|
-
policy_evaluation::{Actor, PolicyContext, PolicyEvaluator, PolicySubject},
|
|
44
43
|
cardinality_endpoint::{CardinalityContext, CardinalityDiagnosticResponse},
|
|
44
|
+
policy_evaluation::{Actor, PolicyContext, PolicyEvaluator, PolicySubject},
|
|
45
45
|
state_contract::{
|
|
46
46
|
begin_read, compare_schemas, execute_query as execute_state_query, execute_transaction,
|
|
47
47
|
schema_from_revision, validate_schema, AuthorizationContext, CanonicalQuery, QueryFilter,
|
|
@@ -76,7 +76,7 @@ use feltdb_server::{
|
|
|
76
76
|
connections::{ConnectionProvider, ConnectionStatus, ConnectionStore, SecretReference},
|
|
77
77
|
content::ContentStore,
|
|
78
78
|
identity::IdentityStore,
|
|
79
|
-
key_management::{
|
|
79
|
+
key_management::{create_key, list_keys, revoke_key},
|
|
80
80
|
key_provider::provider_from_environment,
|
|
81
81
|
leases::LeaseStore,
|
|
82
82
|
metrics::Metrics,
|
|
@@ -103,7 +103,9 @@ struct ApiError(StatusCode, String);
|
|
|
103
103
|
|
|
104
104
|
impl IntoResponse for ApiError {
|
|
105
105
|
fn into_response(self) -> Response {
|
|
106
|
-
|
|
106
|
+
let body =
|
|
107
|
+
serde_json::from_str::<Value>(&self.1).unwrap_or_else(|_| json!({ "error": self.1 }));
|
|
108
|
+
(self.0, Json(body)).into_response()
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
|
|
@@ -2027,9 +2029,10 @@ fn is_platform_owner(principal: &Principal) -> bool {
|
|
|
2027
2029
|
.any(|owner| !owner.is_empty() && owner == identity)
|
|
2028
2030
|
}
|
|
2029
2031
|
fn require_platform_owner(principal: &Principal) -> Result<(), ApiError> {
|
|
2030
|
-
is_platform_owner(principal)
|
|
2031
|
-
|
|
2032
|
-
|
|
2032
|
+
is_platform_owner(principal).then_some(()).ok_or(ApiError(
|
|
2033
|
+
StatusCode::FORBIDDEN,
|
|
2034
|
+
"platform owner access required".into(),
|
|
2035
|
+
))
|
|
2033
2036
|
}
|
|
2034
2037
|
async fn platform_access(Extension(principal): Extension<Principal>) -> Json<Value> {
|
|
2035
2038
|
let platform_role = is_platform_owner(&principal).then_some("super_owner");
|
|
@@ -2365,7 +2368,11 @@ async fn evaluate_application_contract(
|
|
|
2365
2368
|
) -> Result<Json<Value>, ApiError> {
|
|
2366
2369
|
Ok(Json(json!(state
|
|
2367
2370
|
.contracts
|
|
2368
|
-
.evaluate(
|
|
2371
|
+
.evaluate(
|
|
2372
|
+
&user_contract_key(&principal, &id),
|
|
2373
|
+
&input.outcome,
|
|
2374
|
+
&principal.key_id
|
|
2375
|
+
)
|
|
2369
2376
|
.map_err(control_error)?)))
|
|
2370
2377
|
}
|
|
2371
2378
|
async fn patch_application_contract(
|
|
@@ -2384,7 +2391,10 @@ async fn application_contract_gaps(
|
|
|
2384
2391
|
Extension(principal): Extension<Principal>,
|
|
2385
2392
|
Path(id): Path<String>,
|
|
2386
2393
|
) -> Result<Json<Value>, ApiError> {
|
|
2387
|
-
let c = state
|
|
2394
|
+
let c = state
|
|
2395
|
+
.contracts
|
|
2396
|
+
.get(&user_contract_key(&principal, &id))
|
|
2397
|
+
.map_err(control_error)?;
|
|
2388
2398
|
Ok(Json(json!({"gaps":c.gaps})))
|
|
2389
2399
|
}
|
|
2390
2400
|
async fn application_contract_readiness(
|
|
@@ -4495,12 +4505,7 @@ async fn run_runtime_action(
|
|
|
4495
4505
|
schema_version: contract.state.schema.schema_version,
|
|
4496
4506
|
state_namespace: Some(contract.environment.state_namespace.clone()),
|
|
4497
4507
|
causal_parent: None,
|
|
4498
|
-
authorization: state_authorization(
|
|
4499
|
-
&principal,
|
|
4500
|
-
&contract,
|
|
4501
|
-
&definition.collection,
|
|
4502
|
-
"write",
|
|
4503
|
-
),
|
|
4508
|
+
authorization: state_authorization(&principal, &contract, &definition.collection, "write"),
|
|
4504
4509
|
operations: vec![TransactionOperation {
|
|
4505
4510
|
kind,
|
|
4506
4511
|
collection: definition.collection.clone(),
|
|
@@ -4603,12 +4608,7 @@ async fn run_runtime_query(
|
|
|
4603
4608
|
&state.db,
|
|
4604
4609
|
&contract.state.schema,
|
|
4605
4610
|
&contract.environment.state_namespace,
|
|
4606
|
-
state_authorization(
|
|
4607
|
-
&principal,
|
|
4608
|
-
&contract,
|
|
4609
|
-
&definition.collection,
|
|
4610
|
-
"read",
|
|
4611
|
-
),
|
|
4611
|
+
state_authorization(&principal, &contract, &definition.collection, "read"),
|
|
4612
4612
|
)
|
|
4613
4613
|
.map_err(state_contract_error)?;
|
|
4614
4614
|
|
|
@@ -4712,7 +4712,7 @@ fn state_authorization(
|
|
|
4712
4712
|
principal: &Principal,
|
|
4713
4713
|
contract: &ApplicationRuntimeContract,
|
|
4714
4714
|
collection: &str,
|
|
4715
|
-
operation: &str,
|
|
4715
|
+
operation: &str, // "read" or "write"
|
|
4716
4716
|
) -> AuthorizationContext {
|
|
4717
4717
|
let subject = format!("{}:{}", principal.subject_type, principal.key_id);
|
|
4718
4718
|
|
|
@@ -4888,12 +4888,7 @@ async fn execute_canonical_query(
|
|
|
4888
4888
|
&state.db,
|
|
4889
4889
|
&contract.state.schema,
|
|
4890
4890
|
&contract.environment.state_namespace,
|
|
4891
|
-
state_authorization(
|
|
4892
|
-
&principal,
|
|
4893
|
-
&contract,
|
|
4894
|
-
&input.query.collection,
|
|
4895
|
-
"read",
|
|
4896
|
-
),
|
|
4891
|
+
state_authorization(&principal, &contract, &input.query.collection, "read"),
|
|
4897
4892
|
)
|
|
4898
4893
|
.map_err(state_contract_error)?;
|
|
4899
4894
|
|
|
@@ -4905,8 +4900,14 @@ async fn execute_canonical_query(
|
|
|
4905
4900
|
.find(|p| p.resource == input.query.collection)
|
|
4906
4901
|
.and_then(|p| p.read.as_ref().and_then(|s| PolicySubject::from_str(s)));
|
|
4907
4902
|
|
|
4908
|
-
let result = execute_state_query(
|
|
4909
|
-
.
|
|
4903
|
+
let result = execute_state_query(
|
|
4904
|
+
&state.db,
|
|
4905
|
+
&contract.state.schema,
|
|
4906
|
+
&context,
|
|
4907
|
+
&input.query,
|
|
4908
|
+
read_policy,
|
|
4909
|
+
)
|
|
4910
|
+
.map_err(state_contract_error)?;
|
|
4910
4911
|
audit(
|
|
4911
4912
|
&state,
|
|
4912
4913
|
&principal.key_id,
|
|
@@ -4917,6 +4918,156 @@ async fn execute_canonical_query(
|
|
|
4917
4918
|
);
|
|
4918
4919
|
Ok(Json(json!(result)))
|
|
4919
4920
|
}
|
|
4921
|
+
|
|
4922
|
+
#[derive(serde::Deserialize)]
|
|
4923
|
+
struct RevisionRecoveryRequest {
|
|
4924
|
+
#[serde(rename = "applicationId")]
|
|
4925
|
+
application_id: String,
|
|
4926
|
+
#[serde(rename = "targetRevision")]
|
|
4927
|
+
target_revision: String,
|
|
4928
|
+
#[serde(rename = "expectedCurrentRevision")]
|
|
4929
|
+
expected_current_revision: String,
|
|
4930
|
+
authorization: String,
|
|
4931
|
+
actor: String,
|
|
4932
|
+
reason: String,
|
|
4933
|
+
#[serde(default)]
|
|
4934
|
+
environment: Option<String>,
|
|
4935
|
+
#[serde(default)]
|
|
4936
|
+
#[serde(rename = "recoveryId")]
|
|
4937
|
+
recovery_id: Option<String>,
|
|
4938
|
+
}
|
|
4939
|
+
|
|
4940
|
+
#[derive(serde::Serialize, serde::Deserialize)]
|
|
4941
|
+
struct RevisionRecoveryResponse {
|
|
4942
|
+
success: bool,
|
|
4943
|
+
#[serde(rename = "pointerMoved")]
|
|
4944
|
+
pointer_moved: bool,
|
|
4945
|
+
#[serde(rename = "sourceMarkedUntrusted")]
|
|
4946
|
+
source_marked_untrusted: bool,
|
|
4947
|
+
#[serde(rename = "auditDurable")]
|
|
4948
|
+
audit_durable: bool,
|
|
4949
|
+
#[serde(rename = "recoveryId")]
|
|
4950
|
+
recovery_id: String,
|
|
4951
|
+
#[serde(rename = "targetRevision")]
|
|
4952
|
+
target_revision: String,
|
|
4953
|
+
#[serde(rename = "currentRevision")]
|
|
4954
|
+
current_revision: String,
|
|
4955
|
+
audit: serde_json::Value,
|
|
4956
|
+
}
|
|
4957
|
+
|
|
4958
|
+
fn revision_recovery_error(
|
|
4959
|
+
status: StatusCode,
|
|
4960
|
+
code: &str,
|
|
4961
|
+
message: String,
|
|
4962
|
+
request_id: &str,
|
|
4963
|
+
transaction_id: &str,
|
|
4964
|
+
) -> ApiError {
|
|
4965
|
+
ApiError(
|
|
4966
|
+
status,
|
|
4967
|
+
json!({"code":code,"message":message,"request_id":request_id,
|
|
4968
|
+
"transaction_id":transaction_id,"http_status":status.as_u16()})
|
|
4969
|
+
.to_string(),
|
|
4970
|
+
)
|
|
4971
|
+
}
|
|
4972
|
+
|
|
4973
|
+
async fn execute_revision_recovery(
|
|
4974
|
+
State(state): State<AppState>,
|
|
4975
|
+
Extension(principal): Extension<Principal>,
|
|
4976
|
+
Json(input): Json<RevisionRecoveryRequest>,
|
|
4977
|
+
) -> Result<Json<RevisionRecoveryResponse>, ApiError> {
|
|
4978
|
+
let request_id = uuid::Uuid::new_v4().to_string();
|
|
4979
|
+
let transaction_id = input
|
|
4980
|
+
.recovery_id
|
|
4981
|
+
.clone()
|
|
4982
|
+
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
|
|
4983
|
+
|
|
4984
|
+
// Validate authorization level
|
|
4985
|
+
let auth_level = input.authorization.as_str();
|
|
4986
|
+
|
|
4987
|
+
if !["ELEVATED", "ADMIN", "EMERGENCY"].contains(&auth_level) {
|
|
4988
|
+
return Err(revision_recovery_error(
|
|
4989
|
+
StatusCode::FORBIDDEN,
|
|
4990
|
+
"PERMISSION_DENIED",
|
|
4991
|
+
"Recovery requires ELEVATED, ADMIN, or EMERGENCY authorization".into(),
|
|
4992
|
+
&request_id,
|
|
4993
|
+
&transaction_id,
|
|
4994
|
+
));
|
|
4995
|
+
}
|
|
4996
|
+
|
|
4997
|
+
// Validate input
|
|
4998
|
+
if input.reason.len() < 10 {
|
|
4999
|
+
return Err(revision_recovery_error(
|
|
5000
|
+
StatusCode::UNPROCESSABLE_ENTITY,
|
|
5001
|
+
"PRECONDITION_FAILED",
|
|
5002
|
+
"Recovery reason must be at least 10 characters".into(),
|
|
5003
|
+
&request_id,
|
|
5004
|
+
&transaction_id,
|
|
5005
|
+
));
|
|
5006
|
+
}
|
|
5007
|
+
|
|
5008
|
+
let recovery_id = transaction_id.clone();
|
|
5009
|
+
let environment = input
|
|
5010
|
+
.environment
|
|
5011
|
+
.unwrap_or_else(|| "production".to_string());
|
|
5012
|
+
let tenant = application_scope(
|
|
5013
|
+
&state,
|
|
5014
|
+
&principal.key_id,
|
|
5015
|
+
&input.application_id,
|
|
5016
|
+
"application:revision:promote",
|
|
5017
|
+
)?;
|
|
5018
|
+
let recovery = state
|
|
5019
|
+
.applications
|
|
5020
|
+
.recover_environment_pointer(
|
|
5021
|
+
&tenant,
|
|
5022
|
+
&input.application_id,
|
|
5023
|
+
&environment,
|
|
5024
|
+
&input.expected_current_revision,
|
|
5025
|
+
&input.target_revision,
|
|
5026
|
+
&input.actor,
|
|
5027
|
+
&input.reason,
|
|
5028
|
+
auth_level,
|
|
5029
|
+
&recovery_id,
|
|
5030
|
+
)
|
|
5031
|
+
.map_err(|message| {
|
|
5032
|
+
let (status, code) = if message.starts_with("expected_revision_mismatch")
|
|
5033
|
+
|| message == "recovery_id_conflict"
|
|
5034
|
+
{
|
|
5035
|
+
(StatusCode::CONFLICT, "CONFLICT")
|
|
5036
|
+
} else if message.contains("not found") {
|
|
5037
|
+
(StatusCode::NOT_FOUND, "PRECONDITION_FAILED")
|
|
5038
|
+
} else if message.contains("integrity") || message.contains("untrusted") {
|
|
5039
|
+
(StatusCode::UNPROCESSABLE_ENTITY, "VALIDATION_FAILED")
|
|
5040
|
+
} else {
|
|
5041
|
+
(StatusCode::SERVICE_UNAVAILABLE, "STORAGE_FAILURE")
|
|
5042
|
+
};
|
|
5043
|
+
revision_recovery_error(status, code, message, &request_id, &transaction_id)
|
|
5044
|
+
})?;
|
|
5045
|
+
|
|
5046
|
+
// Build response
|
|
5047
|
+
let response = RevisionRecoveryResponse {
|
|
5048
|
+
success: true,
|
|
5049
|
+
pointer_moved: true,
|
|
5050
|
+
source_marked_untrusted: true,
|
|
5051
|
+
audit_durable: true,
|
|
5052
|
+
recovery_id: recovery_id.clone(),
|
|
5053
|
+
target_revision: recovery.target_revision.clone(),
|
|
5054
|
+
current_revision: recovery.target_revision.clone(),
|
|
5055
|
+
audit: json!(recovery),
|
|
5056
|
+
};
|
|
5057
|
+
|
|
5058
|
+
// Audit log
|
|
5059
|
+
audit(
|
|
5060
|
+
&state,
|
|
5061
|
+
&principal.key_id,
|
|
5062
|
+
"revision.recover",
|
|
5063
|
+
&recovery_id,
|
|
5064
|
+
"allowed",
|
|
5065
|
+
200,
|
|
5066
|
+
);
|
|
5067
|
+
|
|
5068
|
+
Ok(Json(response))
|
|
5069
|
+
}
|
|
5070
|
+
|
|
4920
5071
|
async fn execute_canonical_transaction(
|
|
4921
5072
|
State(state): State<AppState>,
|
|
4922
5073
|
Extension(principal): Extension<Principal>,
|
|
@@ -4942,24 +5093,27 @@ async fn execute_canonical_transaction(
|
|
|
4942
5093
|
input.transaction.state_namespace = Some(contract.environment.state_namespace.clone());
|
|
4943
5094
|
// For transactions with operations, use the first operation's collection for policy evaluation
|
|
4944
5095
|
// Multi-collection transactions will still be checked at the operation level in execute_transaction
|
|
4945
|
-
let collection = input
|
|
5096
|
+
let collection = input
|
|
5097
|
+
.transaction
|
|
5098
|
+
.operations
|
|
4946
5099
|
.first()
|
|
4947
5100
|
.map(|op| op.collection.clone())
|
|
4948
5101
|
.unwrap_or_default();
|
|
4949
|
-
input.transaction.authorization =
|
|
4950
|
-
&principal,
|
|
4951
|
-
&contract,
|
|
4952
|
-
&collection,
|
|
4953
|
-
"write",
|
|
4954
|
-
);
|
|
5102
|
+
input.transaction.authorization =
|
|
5103
|
+
state_authorization(&principal, &contract, &collection, "write");
|
|
4955
5104
|
let write_policy = contract
|
|
4956
5105
|
.policies
|
|
4957
5106
|
.definitions
|
|
4958
5107
|
.iter()
|
|
4959
5108
|
.find(|p| p.resource == collection)
|
|
4960
5109
|
.and_then(|p| p.write.as_ref().and_then(|s| PolicySubject::from_str(s)));
|
|
4961
|
-
let result = execute_transaction(
|
|
4962
|
-
.
|
|
5110
|
+
let result = execute_transaction(
|
|
5111
|
+
&state.db,
|
|
5112
|
+
&contract.state.schema,
|
|
5113
|
+
&input.transaction,
|
|
5114
|
+
write_policy,
|
|
5115
|
+
)
|
|
5116
|
+
.map_err(state_contract_error)?;
|
|
4963
5117
|
audit(
|
|
4964
5118
|
&state,
|
|
4965
5119
|
&principal.key_id,
|
|
@@ -4975,13 +5129,19 @@ async fn get_cardinality_diagnostic(
|
|
|
4975
5129
|
State(state): State<AppState>,
|
|
4976
5130
|
Path(pattern): Path<String>,
|
|
4977
5131
|
) -> Result<Json<CardinalityDiagnosticResponse>, ApiError> {
|
|
4978
|
-
let all_cardinalities = state
|
|
5132
|
+
let all_cardinalities = state
|
|
5133
|
+
.db
|
|
5134
|
+
.list_cardinalities()
|
|
4979
5135
|
.map_err(|e| ApiError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
4980
5136
|
|
|
4981
|
-
let actual_rows = state
|
|
5137
|
+
let actual_rows = state
|
|
5138
|
+
.db
|
|
5139
|
+
.diagnostic_row_count(&pattern)
|
|
4982
5140
|
.map_err(|e| ApiError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
4983
5141
|
|
|
4984
|
-
let persisted_capability_keys = state
|
|
5142
|
+
let persisted_capability_keys = state
|
|
5143
|
+
.db
|
|
5144
|
+
.diagnostic_capability_keys(&pattern)
|
|
4985
5145
|
.map_err(|e| ApiError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
4986
5146
|
|
|
4987
5147
|
let matching_keys: Vec<_> = all_cardinalities
|
|
@@ -4992,10 +5152,7 @@ async fn get_cardinality_diagnostic(
|
|
|
4992
5152
|
|
|
4993
5153
|
let cardinality_map_keys: Vec<String> = matching_keys.iter().map(|(k, _)| k.clone()).collect();
|
|
4994
5154
|
|
|
4995
|
-
let maintained = matching_keys
|
|
4996
|
-
.iter()
|
|
4997
|
-
.map(|(_, v)| v)
|
|
4998
|
-
.sum::<u64>();
|
|
5155
|
+
let maintained = matching_keys.iter().map(|(_, v)| v).sum::<u64>();
|
|
4999
5156
|
|
|
5000
5157
|
let now_ms = std::time::SystemTime::now()
|
|
5001
5158
|
.duration_since(std::time::UNIX_EPOCH)
|
|
@@ -5752,6 +5909,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
5752
5909
|
"/v1/transactions",
|
|
5753
5910
|
axum::routing::post(execute_canonical_transaction),
|
|
5754
5911
|
)
|
|
5912
|
+
.route(
|
|
5913
|
+
"/v1/revision/recover",
|
|
5914
|
+
axum::routing::post(execute_revision_recovery),
|
|
5915
|
+
)
|
|
5755
5916
|
.route(
|
|
5756
5917
|
"/debug/cardinality/{pattern}",
|
|
5757
5918
|
get(get_cardinality_diagnostic),
|