relay_server/services/projects/source/
local.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::Path;
use std::sync::Arc;

use relay_base_schema::project::{ProjectId, ProjectKey};
use relay_config::Config;
use relay_system::{AsyncResponse, FromMessage, Interface, Receiver, Sender, Service};
use tokio::sync::mpsc;
use tokio::time::Instant;

use crate::services::projects::project::{ParsedProjectState, ProjectState};
use crate::services::projects::source::FetchOptionalProjectState;

/// Service interface of the local project source.
#[derive(Debug)]
pub struct LocalProjectSource(FetchOptionalProjectState, Sender<Option<ProjectState>>);

impl Interface for LocalProjectSource {}

impl FromMessage<FetchOptionalProjectState> for LocalProjectSource {
    type Response = AsyncResponse<Option<ProjectState>>;
    fn from_message(
        message: FetchOptionalProjectState,
        sender: Sender<Option<ProjectState>>,
    ) -> Self {
        Self(message, sender)
    }
}

/// A service which periodically loads project states from disk.
#[derive(Debug)]
pub struct LocalProjectSourceService {
    config: Arc<Config>,
    local_states: HashMap<ProjectKey, ProjectState>,
}

impl LocalProjectSourceService {
    pub fn new(config: Arc<Config>) -> Self {
        Self {
            config,
            local_states: HashMap::new(),
        }
    }

    fn handle_message(&mut self, message: LocalProjectSource) {
        let LocalProjectSource(message, sender) = message;
        let states = self.local_states.get(&message.project_key()).cloned();
        sender.send(states);
    }
}

fn get_project_id(path: &Path) -> Option<ProjectId> {
    path.file_stem()
        .and_then(OsStr::to_str)
        .and_then(|stem| stem.parse().ok())
}

fn parse_file(
    path: std::path::PathBuf,
) -> tokio::io::Result<(std::path::PathBuf, ParsedProjectState)> {
    let file = std::fs::File::open(&path)?;
    let reader = std::io::BufReader::new(file);
    let state = serde_json::from_reader(reader)?;
    Ok((path, state))
}

async fn load_local_states(
    projects_path: &Path,
) -> tokio::io::Result<HashMap<ProjectKey, ProjectState>> {
    let mut states = HashMap::new();

    let mut directory = match tokio::fs::read_dir(projects_path).await {
        Ok(directory) => directory,
        Err(error) => {
            return match error.kind() {
                tokio::io::ErrorKind::NotFound => Ok(states),
                _ => Err(error),
            };
        }
    };

    // only printed when directory even exists.
    relay_log::debug!(directory = ?projects_path, "loading local states from file system");

    while let Some(entry) = directory.next_entry().await? {
        let path = entry.path();

        let metadata = entry.metadata().await?;
        if !(metadata.is_file() || metadata.is_symlink()) {
            relay_log::warn!(?path, "skipping file, not a file");
            continue;
        }

        if path.extension().map(|x| x != "json").unwrap_or(true) {
            relay_log::warn!(?path, "skipping file, file extension must be .json");
            continue;
        }

        // serde_json is not async, so spawn a blocking task here:
        let (path, mut state) = tokio::task::spawn_blocking(move || parse_file(path)).await??;

        if state.info.project_id.is_none() {
            if let Some(project_id) = get_project_id(&path) {
                state.info.project_id = Some(project_id);
            } else {
                relay_log::warn!(?path, "skipping file, filename is not a valid project id");
                continue;
            }
        }

        // Keep a separate project state per key.
        let keys = std::mem::take(&mut state.info.public_keys);
        if keys.is_empty() {
            relay_log::warn!(
                ?path,
                "skipping file, project config is missing public keys"
            );
        }

        for key in keys {
            let mut state = state.clone();
            state.info.public_keys = smallvec::smallvec![key.clone()];
            states.insert(key.public_key, ProjectState::from(state).sanitized());
        }
    }

    Ok(states)
}

async fn poll_local_states(path: &Path, tx: &mpsc::Sender<HashMap<ProjectKey, ProjectState>>) {
    let states = load_local_states(path).await;
    match states {
        Ok(states) => {
            let res = tx.send(states).await;
            if res.is_err() {
                relay_log::error!("failed to store static project configs");
            }
        }
        Err(error) => relay_log::error!(
            error = &error as &dyn std::error::Error,
            "failed to load static project configs",
        ),
    };
}

async fn spawn_poll_local_states(
    config: &Config,
    tx: mpsc::Sender<HashMap<ProjectKey, ProjectState>>,
) {
    let project_path = config.project_configs_path();
    let period = config.local_cache_interval();

    // Poll local states once before handling any message, such that the projects are
    // populated.
    poll_local_states(&project_path, &tx).await;

    // Start a background loop that polls periodically:
    relay_system::spawn!(async move {
        // To avoid running two load tasks simultaneously at startup, we delay the interval by one period:
        let start_at = Instant::now() + period;
        let mut ticker = tokio::time::interval_at(start_at, period);

        loop {
            ticker.tick().await;
            poll_local_states(&project_path, &tx).await;
        }
    });
}

impl Service for LocalProjectSourceService {
    type Interface = LocalProjectSource;

    async fn run(mut self, mut rx: Receiver<Self::Interface>) {
        // Use a channel with size 1. If the channel is full because the consumer does not
        // collect the result, the producer will block, which is acceptable.
        let (state_tx, mut state_rx) = mpsc::channel(1);

        relay_log::info!("project local cache started");

        // Start the background task that periodically reloads projects from disk:
        spawn_poll_local_states(&self.config, state_tx).await;

        loop {
            tokio::select! {
                biased;
                Some(message) = rx.recv() => self.handle_message(message),
                Some(states) = state_rx.recv() => self.local_states = states,

                else => break,
            }
        }
        relay_log::info!("project local cache stopped");
    }
}

/// This works only on Unix systems.
#[cfg(not(target_os = "windows"))]
#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;
    use crate::services::projects::project::{ProjectInfo, PublicKeyConfig};

    /// Tests that we can follow the symlinks and read the project file properly.
    #[tokio::test]
    async fn test_symlinked_projects() {
        let temp1 = tempfile::tempdir().unwrap();
        let temp2 = tempfile::tempdir().unwrap();

        let tmp_project_file = "111111.json";
        let project_key = ProjectKey::parse("55f6b2d962564e99832a39890ee4573e").unwrap();

        let mut tmp_project_state = ProjectInfo::default();
        tmp_project_state.public_keys.push(PublicKeyConfig {
            public_key: project_key,
            numeric_id: None,
        });

        // create the project file
        let project_state = serde_json::to_string(&tmp_project_state).unwrap();
        tokio::fs::write(
            temp1.path().join(tmp_project_file),
            project_state.as_bytes(),
        )
        .await
        .unwrap();

        tokio::fs::symlink(
            temp1.path().join(tmp_project_file),
            temp2.path().join(tmp_project_file),
        )
        .await
        .unwrap();

        let extracted_project_state = load_local_states(temp2.path()).await.unwrap();
        let project_info = extracted_project_state
            .get(&project_key)
            .unwrap()
            .clone()
            .enabled()
            .unwrap();

        assert_eq!(
            project_info.project_id,
            Some(ProjectId::from_str("111111").unwrap())
        );

        assert_eq!(
            project_info.public_keys.first().unwrap().public_key,
            project_key,
        )
    }

    #[tokio::test]
    async fn test_multi_pub_static_config() {
        let temp = tempfile::tempdir().unwrap();

        let tmp_project_file = "111111.json";
        let project_key1 = ProjectKey::parse("55f6b2d962564e99832a39890ee4573e").unwrap();
        let project_key2 = ProjectKey::parse("55bbb2d96256bb9983bb39890bb457bb").unwrap();

        let mut tmp_project_state = ProjectInfo::default();
        tmp_project_state.public_keys.extend(vec![
            PublicKeyConfig {
                public_key: project_key1,
                numeric_id: None,
            },
            PublicKeyConfig {
                public_key: project_key2,
                numeric_id: None,
            },
        ]);

        // create the project file
        let project_state = serde_json::to_string(&tmp_project_state).unwrap();
        tokio::fs::write(temp.path().join(tmp_project_file), project_state.as_bytes())
            .await
            .unwrap();

        let extracted_project_state = load_local_states(temp.path()).await.unwrap();

        assert_eq!(extracted_project_state.len(), 2);
        assert!(extracted_project_state.contains_key(&project_key1));
        assert!(extracted_project_state.contains_key(&project_key2));
    }
}