Skip to main content

fn0_shared_schema/
lib.rs

1use forte_macros::forte_doc;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5pub use doc_db::DbRequest;
6
7#[derive(Serialize, Deserialize, Clone)]
8pub struct WorkerProjectManifest {
9    pub code_version: u64,
10    pub custom_domain: Option<String>,
11    #[serde(default = "default_static_cache_state")]
12    pub static_cache_state: String,
13    #[serde(default)]
14    pub pending_code_version: Option<u64>,
15    /// Absent only between a project's creation and its owner connecting a
16    /// Cloudflare account; a worker cannot serve the project until it is set.
17    #[serde(default)]
18    pub storage: Option<WorkerProjectStorage>,
19}
20
21/// One R2 token as it travels to the worker: the key id in the clear, the
22/// secret only as KMS ciphertext, so a leaked manifest row is not a usable
23/// credential.
24#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
25pub struct WorkerR2Credential {
26    pub access_key_id: String,
27    pub secret_ciphertext: String,
28}
29
30/// Where one project's objects live, as the worker sees it.
31///
32/// One credential, scoped to exactly the three buckets named here. The
33/// frontend-asset bucket is deliberately outside it: nothing in the worker
34/// serves assets — the CDN does, straight off the bucket — so a fleet-wide
35/// credential able to rewrite a deployed frontend would be reach with no use
36/// for it.
37#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
38pub struct WorkerProjectStorage {
39    pub account_id: String,
40    pub region: String,
41    pub credential: WorkerR2Credential,
42    pub private_object_storage_bucket: String,
43    pub public_object_storage_bucket: String,
44    /// CDN origin for `public_object_storage_bucket`, without a trailing slash.
45    pub public_object_storage_base_url: String,
46    pub rendered_html_cache_bucket: String,
47    /// Bumped by control on every credential or bucket change, so a worker can
48    /// skip re-decrypting a target it already holds.
49    pub config_version: u64,
50}
51
52pub const STATIC_CACHE_STATE_ACTIVE: &str = "active";
53pub const STATIC_CACHE_STATE_PRE_PURGE: &str = "pre_purge";
54pub const STATIC_CACHE_STATE_ACTIVATING: &str = "activating";
55
56fn default_static_cache_state() -> String {
57    STATIC_CACHE_STATE_ACTIVE.to_string()
58}
59
60#[forte_doc]
61pub struct WorkerManifestDoc {
62    pub manifest_version: u64,
63    pub project_manifests: HashMap<String, WorkerProjectManifest>,
64}
65
66/// A certificate the worker serves for one custom hostname, issued through the
67/// project owner's own Cloudflare Origin CA. Only valid for the Cloudflare edge
68/// to origin leg, which is the only leg the worker terminates.
69#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
70pub struct WorkerHostnameCert {
71    pub project_id: String,
72    pub cert_pem: String,
73    pub key_ciphertext: String,
74    pub not_after_epoch_seconds: i64,
75}
76
77/// Certificates live outside `WorkerManifestDoc` because every worker polls
78/// that document once a second and a PEM per project is a different order of
79/// magnitude from a bucket name. Custom hostnames are far fewer than projects.
80#[forte_doc]
81pub struct WorkerCertManifestDoc {
82    pub cert_version: u64,
83    pub certs: HashMap<String, WorkerHostnameCert>,
84}
85
86#[forte_doc]
87pub struct WorkerHostStatusDoc {
88    #[sk]
89    pub host_id: String,
90    pub addr: String,
91    pub active_image_ref: Option<String>,
92    pub reported_at: i64,
93}