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    /// Bumped by control on every credential or bucket change, so a worker can
47    /// skip re-decrypting a target it already holds.
48    pub config_version: u64,
49}
50
51pub const STATIC_CACHE_STATE_ACTIVE: &str = "active";
52pub const STATIC_CACHE_STATE_PRE_PURGE: &str = "pre_purge";
53pub const STATIC_CACHE_STATE_ACTIVATING: &str = "activating";
54
55fn default_static_cache_state() -> String {
56    STATIC_CACHE_STATE_ACTIVE.to_string()
57}
58
59#[forte_doc]
60pub struct WorkerManifestDoc {
61    pub manifest_version: u64,
62    pub project_manifests: HashMap<String, WorkerProjectManifest>,
63}
64
65/// A certificate the worker serves for one custom hostname, issued through the
66/// project owner's own Cloudflare Origin CA. Only valid for the Cloudflare edge
67/// to origin leg, which is the only leg the worker terminates.
68#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
69pub struct WorkerHostnameCert {
70    pub project_id: String,
71    pub cert_pem: String,
72    pub key_ciphertext: String,
73    pub not_after_epoch_seconds: i64,
74}
75
76/// Certificates live outside `WorkerManifestDoc` because every worker polls
77/// that document once a second and a PEM per project is a different order of
78/// magnitude from a bucket name. Custom hostnames are far fewer than projects.
79#[forte_doc]
80pub struct WorkerCertManifestDoc {
81    pub cert_version: u64,
82    pub certs: HashMap<String, WorkerHostnameCert>,
83}
84
85#[forte_doc]
86pub struct WorkerHostStatusDoc {
87    #[sk]
88    pub host_id: String,
89    pub addr: String,
90    pub active_image_ref: Option<String>,
91    pub reported_at: i64,
92}