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