Skip to main content

core_api/
storage.rs

1//! Core-owned Storage access and transport. Domain types have one source in storage-api.
2use serde::{Deserialize, Serialize};
3pub use storage_api::*;
4pub const STORAGE_NODE_TYPE: &str = "storage";
5pub const STORAGE_PROTOCOL_VERSION: &str = storage_api::PROTOCOL_VERSION;
6pub const STORAGE_READINESS_ROUTE: &str = "/storage/readiness";
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct ObjectStorageSession {
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    pub host_identity: Option<artifact_api::HostTlsIdentity>,
13    pub protocol_version: String,
14    pub folder: ObjectStorageFolder,
15    pub endpoint: String,
16    pub access_token: String,
17    #[serde(with = "storage_api::decimal_u64")]
18    pub expires_at_ms: u64,
19    /// Private creation identity, used by SDKs to prevent renewal into a replacement Folder.
20    pub folder_incarnation: String,
21}
22
23/// Host-owned native HTTPS origin, projected independently of Artifact routes.
24#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
25#[serde(rename_all = "camelCase", deny_unknown_fields)]
26pub struct StorageEndpointProjection {
27    pub base_url: String,
28    pub host_identity: artifact_api::HostTlsIdentity,
29}
30
31/// Preserve the Storage failure through Core RPC and native Host adapters.
32/// Only the typed SESSION_EXPIRED detail means that a data-plane retry is safe.
33pub fn storage_error_from_core(
34    code: &str,
35    detail: impl Into<String>,
36    details: Option<serde_json::Value>,
37) -> StorageError {
38    if let Some(error) =
39        details.and_then(|value| serde_json::from_value::<StorageError>(value).ok())
40    {
41        return error;
42    }
43    use StorageErrorCode as S;
44    let code = match code {
45        "BAD_REQUEST" => S::InvalidInput,
46        "NOT_FOUND" => S::NotFound,
47        "UNAUTHORIZED" | "UNAUTHENTICATED" => S::Unauthorized,
48        "PRECONDITION_FAIL" => S::Conflict,
49        "INTERNAL" => S::Internal,
50        _ => S::Unavailable,
51    };
52    let error = StorageError::new(code, detail);
53    if matches!(code, S::Internal | S::Unavailable) {
54        error.uncertain()
55    } else {
56        error
57    }
58}
59
60#[cfg(test)]
61mod storage_transport_tests {
62    use super::*;
63    #[test]
64    fn expiry_survives_core_rpc_without_reclassifying_binding_revocation() {
65        let expired = StorageError::new(StorageErrorCode::SessionExpired, "expired");
66        let decoded = storage_error_from_core(
67            "UNAUTHENTICATED",
68            "Core exception",
69            Some(serde_json::to_value(&expired).unwrap()),
70        );
71        assert_eq!(decoded, expired);
72        assert_eq!(decoded.http_status(), 401);
73        assert_eq!(
74            storage_error_from_core("UNAUTHENTICATED", "binding revoked", None).http_status(),
75            403
76        );
77        for error in [
78            StorageError::new(StorageErrorCode::Unavailable, "offline").partial(2),
79            StorageError::new(StorageErrorCode::Unavailable, "lost response").uncertain(),
80        ] {
81            assert_eq!(
82                storage_error_from_core(
83                    "TEMPORARY_UNAVAILABLE",
84                    "Core exception",
85                    Some(serde_json::to_value(&error).unwrap())
86                ),
87                error
88            );
89        }
90    }
91}