Skip to main content

fakecloud_cloudfront/
state.rs

1//! In-memory state for CloudFront resources.
2
3use std::collections::BTreeMap;
4use std::sync::Arc;
5
6use chrono::{DateTime, Utc};
7use parking_lot::RwLock;
8use serde::{Deserialize, Serialize};
9
10use crate::cfunctions::StoredConnectionFunction;
11use crate::extras::{StoredAnycastIpList, StoredResourcePolicy, StoredTrustStore, StoredVpcOrigin};
12use crate::extras2::StoredConnectionGroup;
13use crate::fle::{
14    StoredFieldLevelEncryption, StoredFieldLevelEncryptionProfile, StoredRealtimeLogConfig,
15};
16use crate::functions::{
17    StoredFunction, StoredKeyGroup, StoredKeyValueStore, StoredMonitoringSubscription,
18    StoredOriginAccessIdentity, StoredPublicKey,
19};
20use crate::model::{DistributionConfig, InvalidationBatch};
21use crate::policies::{
22    StoredCachePolicy, StoredContinuousDeploymentPolicy, StoredOriginAccessControl,
23    StoredOriginRequestPolicy, StoredResponseHeadersPolicy,
24};
25use crate::streaming::StoredStreamingDistribution;
26use crate::tenants::{StoredDistributionTenant, StoredTenantInvalidation};
27
28pub type SharedCloudFrontState = Arc<RwLock<CloudFrontAccounts>>;
29
30#[derive(Debug, Default, Clone, Serialize, Deserialize)]
31pub struct CloudFrontAccounts {
32    pub accounts: BTreeMap<String, AccountState>,
33}
34
35/// On-disk snapshot envelope for CloudFront state. Versioned so format changes
36/// fail loudly on upgrade rather than silently mis-parsing.
37#[derive(Clone, Serialize, Deserialize)]
38pub struct CloudFrontSnapshot {
39    pub schema_version: u32,
40    #[serde(default)]
41    pub accounts: Option<CloudFrontAccounts>,
42}
43
44pub const CLOUDFRONT_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
45
46impl CloudFrontAccounts {
47    pub fn new() -> Self {
48        Self::default()
49    }
50
51    pub fn account_count(&self) -> usize {
52        self.accounts.len()
53    }
54
55    pub fn entry(&mut self, account_id: &str) -> &mut AccountState {
56        self.accounts.entry(account_id.to_string()).or_default()
57    }
58
59    pub fn get(&self, account_id: &str) -> Option<&AccountState> {
60        self.accounts.get(account_id)
61    }
62
63    /// Iterate every stored distribution across all accounts, paired with the
64    /// owning account id. Used by the `/_fakecloud/cloudfront/distributions`
65    /// introspection route (and, later, the data-plane supervisor).
66    pub fn all_distributions(&self) -> impl Iterator<Item = (&String, &StoredDistribution)> {
67        self.accounts.iter().flat_map(|(account_id, state)| {
68            state.distributions.values().map(move |d| (account_id, d))
69        })
70    }
71}
72
73#[derive(Debug, Default, Clone, Serialize, Deserialize)]
74pub struct AccountState {
75    pub distributions: BTreeMap<String, StoredDistribution>,
76    pub invalidations: BTreeMap<String, StoredInvalidation>,
77    /// Tags keyed by ARN.
78    pub tags: BTreeMap<String, Vec<Tag>>,
79    pub origin_access_controls: BTreeMap<String, StoredOriginAccessControl>,
80    pub cache_policies: BTreeMap<String, StoredCachePolicy>,
81    pub origin_request_policies: BTreeMap<String, StoredOriginRequestPolicy>,
82    pub response_headers_policies: BTreeMap<String, StoredResponseHeadersPolicy>,
83    pub continuous_deployment_policies: BTreeMap<String, StoredContinuousDeploymentPolicy>,
84    pub functions: BTreeMap<String, StoredFunction>,
85    pub public_keys: BTreeMap<String, StoredPublicKey>,
86    pub key_groups: BTreeMap<String, StoredKeyGroup>,
87    pub key_value_stores: BTreeMap<String, StoredKeyValueStore>,
88    pub origin_access_identities: BTreeMap<String, StoredOriginAccessIdentity>,
89    /// Per-distribution monitoring subscription, keyed by distribution id.
90    pub monitoring_subscriptions: BTreeMap<String, StoredMonitoringSubscription>,
91    pub streaming_distributions: BTreeMap<String, StoredStreamingDistribution>,
92    pub field_level_encryptions: BTreeMap<String, StoredFieldLevelEncryption>,
93    pub field_level_encryption_profiles: BTreeMap<String, StoredFieldLevelEncryptionProfile>,
94    /// Realtime log configs keyed by ARN.
95    pub realtime_log_configs: BTreeMap<String, StoredRealtimeLogConfig>,
96    pub vpc_origins: BTreeMap<String, StoredVpcOrigin>,
97    pub anycast_ip_lists: BTreeMap<String, StoredAnycastIpList>,
98    pub trust_stores: BTreeMap<String, StoredTrustStore>,
99    /// Resource policies keyed by resource ARN.
100    pub resource_policies: BTreeMap<String, StoredResourcePolicy>,
101    pub connection_groups: BTreeMap<String, StoredConnectionGroup>,
102    pub distribution_tenants: BTreeMap<String, StoredDistributionTenant>,
103    pub tenant_invalidations: BTreeMap<String, StoredTenantInvalidation>,
104    pub connection_functions: BTreeMap<String, StoredConnectionFunction>,
105}
106
107impl CloudFrontAccounts {
108    /// Pre-seed the AWS-managed Cache, Origin Request, and Response
109    /// Headers policies into the default account so callers that look
110    /// them up by their well-known IDs (Terraform, CDK) get the same
111    /// shape they get against AWS. The IDs and names mirror the AWS
112    /// console output verbatim — the easiest way to keep tests source
113    /// of truth.
114    pub fn seed_managed_policies(&mut self, account_id: &str) {
115        let account = self.entry(account_id);
116        crate::policies::seed_managed(account);
117    }
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct StoredDistribution {
122    pub id: String,
123    pub arn: String,
124    pub status: String,
125    pub last_modified_time: DateTime<Utc>,
126    pub domain_name: String,
127    pub in_progress_invalidation_batches: u32,
128    pub etag: String,
129    pub config: DistributionConfig,
130    /// Data-plane listener port (fakecloud extension; None until the
131    /// supervisor binds a listener for an enabled distribution). Not part
132    /// of the AWS API surface — surfaced only via /_fakecloud/cloudfront/*.
133    /// Runtime-only: never persisted (restarts get a fresh port on the first
134    /// reconcile tick), mirroring the ELBv2 data plane's `bound_port`.
135    #[serde(skip)]
136    pub bound_port: Option<u16>,
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct StoredInvalidation {
141    pub id: String,
142    pub distribution_id: String,
143    pub status: String,
144    pub create_time: DateTime<Utc>,
145    pub batch: InvalidationBatch,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
149pub struct Tag {
150    pub key: String,
151    pub value: Option<String>,
152}