OXYGENHUB
# 9a777fec4ed9 oxygenhub / crates / hub / src / protocol.rs
20.6 KiB · Rust · last changed Profile: the year as a signal 20 days ago source blame history raw
1
//! The wire protocol, embedded (hub spec §3.3, §5.4): `oxy-server`'s
2
//! `Server` over a backend that resolves `owner/name`, authorises reads
3
//! and pushes by account role and token scope, and records who pushed.
4
5
use crate::auth::{self, Access, Role};
6
use crate::db::{self, accounts::Account};
7
use crate::repos::{RepoName, Repos};
8
use crate::Shared;
9
use axum::body::Bytes;
10
use axum::extract::{Path, State};
11
use axum::http::{HeaderMap, Method, StatusCode};
12
use axum::response::{IntoResponse, Response};
13
use oxy_core::repo::{PushReport, Repo};
14
use oxy_server::{Backend, Endpoint, Failure};
15
use std::sync::Arc;
16
17
pub struct HubBackend {
18
    repos: Arc<Repos>,
19
    /// Wikis, reached as `OWNER/NAME.wiki` (hub spec §6.11).
20
    wikis: Arc<Repos>,
21
    pool: db::Pool,
22
    /// Site tokens from `hub.toml`: full access, for bootstrapping.
23
    site_tokens: Vec<String>,
24
    runtime: tokio::runtime::Handle,
25
    /// The application, set once it exists (the backend is built first).
26
    app_hint: Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>>,
27
}
28
29
/// Who a request's credential resolved to.
30
enum Caller {
31
    Anonymous,
32
    /// A site token from the configuration.
33
    Site,
34
    Token(Box<Account>, Box<db::accounts::Token>),
35
    /// A running OxygenFlow job: reads its own repository and the subrepos
36
    /// its state pins, nothing else (hub spec §13.2).
37
    Job {
38
        repo_id: i64,
39
        also: Vec<i64>,
40
    },
41
}
42
43
impl HubBackend {
44
    pub fn new(
45
        repos: Arc<Repos>,
46
        wikis: Arc<Repos>,
47
        pool: db::Pool,
48
        site_tokens: Vec<String>,
49
        runtime: tokio::runtime::Handle,
50
    ) -> HubBackend {
51
        HubBackend { repos, wikis, pool, site_tokens, runtime, app_hint: Arc::new(std::sync::Mutex::new(None)) }
52
    }
53
54
    /// The slot the application registers itself in after construction.
55
    pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> {
56
        self.app_hint.clone()
57
    }
58
59
    fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T {
60
        tokio::task::block_in_place(|| self.runtime.block_on(f))
61
    }
62
63
    fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> {
64
        let pool = self.pool.clone();
65
        match self.block(db::repos::get(&pool, &name.owner, &name.name)) {
66
            Ok(Some(r)) => Ok(r),
67
            // A transferred repository still answers to its old name (#108).
68
            Ok(None) => match self.block(db::repos::moved_to(&pool, &name.owner, &name.name)) {
69
                Ok(Some(r)) => Ok(r),
70
                Ok(None) => Err(not_found()),
71
                Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
72
            },
73
            Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
74
        }
75
    }
76
77
    fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> {
78
        let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) };
79
        if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) {
80
            return Ok(Caller::Site);
81
        }
82
        let hash = auth::token_hash(presented);
83
        let pool = self.pool.clone();
84
        if presented.starts_with(crate::flow::JOB_TOKEN_PREFIX) {
85
            let job = self.block(db::flow::job_by_token(&pool, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)).map_err(internal)?;
86
            let Some(job) = job else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
87
            let run = self.block(db::flow::run_by_id(&pool, job.run_id)).map_err(internal)?;
88
            let Some(run) = run else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
89
            return Ok(Caller::Job { repo_id: run.repo_id, also: job.also_read_ids() });
90
        }
91
        let token = self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)?;
92
        let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
93
        if token.expired(db::now_ms()) {
94
            return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into()));
95
        }
96
        let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?;
97
        let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
98
        if account.banned {
99
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into()));
100
        }
101
        if account.disabled {
102
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is disabled; contact the hub's administrator".into()));
103
        }
104
        let _ = self.block(db::accounts::touch_token(&pool, token.id));
105
        Ok(Caller::Token(Box::new(account), Box::new(token)))
106
    }
107
}
108
109
fn internal(e: sqlx::Error) -> Failure {
110
    Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
111
}
112
113
fn not_found() -> Failure {
114
    Failure(StatusCode::NOT_FOUND, "no such repository".into())
115
}
116
117
impl Backend for HubBackend {
118
    fn open(&self, name: &str) -> Result<Repo, Failure> {
119
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
120
        // `NAME.wiki` is the repository's wiki (§6.11); the row knows the
121
        // current name, so a transferred repository still answers to its
122
        // old one (#108).
123
        let (parent, is_wiki) = crate::wiki::parent_of(&parsed);
124
        let row = self.row(&parent)?;
125
        let name = RepoName::new(&row.owner, &row.name).ok_or_else(not_found)?;
126
        let store = if is_wiki { &self.wikis } else { &self.repos };
127
        store.open(&name).map_err(|e| match e {
128
            crate::repos::RepoError::NotFound => not_found(),
129
            other => Failure(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
130
        })
131
    }
132
133
    fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> {
134
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
135
        let (parent, _) = crate::wiki::parent_of(&parsed);
136
        let row = self.row(&parent)?;
137
        let caller = self.caller(headers)?;
138
        if let Caller::Job { repo_id, also } = &caller {
139
            // Only reads: of the job's own repository, and of the subrepos its
140
            // state pins that its pusher could read when the job was claimed.
141
            return match access {
142
                oxy_server::Access::Read if *repo_id == row.id || also.contains(&row.id) => Ok(()),
143
                oxy_server::Access::Read => Err(not_found()),
144
                oxy_server::Access::Push => Err(Failure(StatusCode::FORBIDDEN, "a job token cannot push".into())),
145
            };
146
        }
147
        let (role, scope_ok) = match &caller {
148
            Caller::Job { .. } => unreachable!(),
149
            Caller::Site => (Some(Role::Admin), true),
150
            Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true),
151
            Caller::Token(account, token) => {
152
                let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?;
153
                let needed = match access {
154
                    oxy_server::Access::Read => {
155
                        token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin")
156
                    }
157
                    oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"),
158
                };
159
                let current = format!("{}/{}", row.owner, row.name);
160
                let covered = token.covers(&parsed.to_string()) || token.covers(&current) || token.covers(&parent.to_string());
161
                (role, needed && covered)
162
            }
163
        };
164
        // An unreadable repository does not exist for the caller (hub spec §4.4).
165
        if !auth::roles::allows(role, Access::Read) {
166
            return Err(not_found());
167
        }
168
        // A wiki is made, empty and unbound, when someone who may write to
169
        // its repository first reaches it, so the first page can be pushed
170
        // as well as written on the web (§6.11); the push gives it its id.
171
        let (_, is_wiki) = crate::wiki::parent_of(&parsed);
172
        let may_write = match &caller {
173
            Caller::Site => true,
174
            Caller::Token(_, token) => token.has_scope("repo:write") || token.has_scope("repo:admin"),
175
            _ => false,
176
        };
177
        if is_wiki && may_write && auth::roles::allows(role, Access::Write) && !row.archived {
178
            if let Some(name) = RepoName::new(&row.owner, &row.name) {
179
                if !self.wikis.exists(&name) {
180
                    self.wikis
181
                        .create(&name, None)
182
                        .map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, format!("make the wiki: {e}")))?;
183
                }
184
            }
185
        }
186
        match access {
187
            oxy_server::Access::Read if scope_ok => Ok(()),
188
            oxy_server::Access::Read => Err(Failure(StatusCode::FORBIDDEN, "this token cannot read repositories".into())),
189
            oxy_server::Access::Push => {
190
                if matches!(caller, Caller::Anonymous) {
191
                    return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into()));
192
                }
193
                if !auth::roles::allows(role, Access::Write) {
194
                    return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into()));
195
                }
196
                if row.archived {
197
                    return Err(Failure(StatusCode::FORBIDDEN, "this repository is archived and read-only".into()));
198
                }
199
                if !scope_ok {
200
                    return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into()));
201
                }
202
                Ok(())
203
            }
204
        }
205
    }
206
207
    fn default_bookmark(&self, name: &str) -> Option<String> {
208
        let parsed = RepoName::parse(name)?;
209
        if crate::wiki::parent_of(&parsed).1 {
210
            return Some(crate::wiki::BOOKMARK.into());
211
        }
212
        self.row(&parsed).ok().map(|r| r.default_bookmark)
213
    }
214
215
    fn after_push(&self, name: &str, report: &PushReport, headers: &HeaderMap) {
216
        let Some(parsed) = RepoName::parse(name) else { return };
217
        // A wiki push starts no run and rings no webhook.
218
        if crate::wiki::parent_of(&parsed).1 {
219
            return;
220
        }
221
        let Ok(row) = self.row(&parsed) else { return };
222
        let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return };
223
        let (pusher, via) = match self.caller(headers) {
224
            Ok(Caller::Token(account, token)) => {
225
                (Some((account.id, account.username)), if token.agent { token.name.clone() } else { String::new() })
226
            }
227
            Ok(Caller::Site) => (Some((0, "site token".to_string())), String::new()),
228
            _ => (None, String::new()),
229
        };
230
        let pool = self.pool.clone();
231
        // A part of a long push (objects alone) moves nothing: the push it
232
        // belongs to is recorded when its last part names the references.
233
        let results: Vec<db::repos::PushOutcome> = report
234
            .results
235
            .iter()
236
            .filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT)
237
            .map(|r| {
238
                let updated = report.updated.iter().find(|u| u.name == r.name);
239
                db::repos::PushOutcome {
240
                    reference: r.name.clone(),
241
                    ok: r.ok,
242
                    reason: r.reason.clone(),
243
                    old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(),
244
                    new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(),
245
                    heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0),
246
                    changes: if r.ok { report.imported.len() as i64 } else { 0 },
247
                }
248
            })
249
            .collect();
250
        // Messages of the changes stored: `closes #N` when the default
251
        // bookmark moved, and `#N` references on any bookmark (hub spec §8).
252
        let bookmarks: Vec<String> = report
253
            .updated
254
            .iter()
255
            .filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name))
256
            .map(|u| u.name.clone())
257
            .collect();
258
        let imported: Vec<(String, String)> = if bookmarks.is_empty() {
259
            vec![]
260
        } else {
261
            self.repos
262
                .open(&parsed)
263
                .map(|r| {
264
                    report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect()
265
                })
266
                .unwrap_or_default()
267
        };
268
        let messages: Vec<(String, String)> = if bookmarks.contains(&row.default_bookmark) { imported.clone() } else { vec![] };
269
        // Authors whose identity is a successor (a rotation, a renewal, a
270
        // delegation) of an earlier one: each step kept the key pair, so
271
        // if the chain reaches a linked key it is the same person, and the
272
        // successor is linked too, without re-uploading it (hub spec §5.3).
273
        let successors: Vec<(String, String, String, Vec<u8>, String)> = self
274
            .repos
275
            .open(&parsed)
276
            .map(|r| {
277
                let mut seen = std::collections::HashSet::new();
278
                let mut out = Vec::new();
279
                for h in &report.imported {
280
                    let Ok(meta) = r.store().get_change_meta(h) else { continue };
281
                    let mut key = meta.author;
282
                    while !key.is_zero() && seen.insert(key) {
283
                        let Ok(id) = r.store().get_identity(&key) else { break };
284
                        let Some(prev) = id.previous else { break };
285
                        let Ok(before) = r.store().get_identity(&prev) else { break };
286
                        if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk {
287
                            break;
288
                        }
289
                        out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex()));
290
                        key = prev;
291
                    }
292
                }
293
                out
294
            })
295
            .unwrap_or_default();
296
        let on = if bookmarks.contains(&row.default_bookmark) {
297
            row.default_bookmark.clone()
298
        } else {
299
            bookmarks.first().cloned().unwrap_or_default()
300
        };
301
        // Bookmarks that moved, with their new heads, for OxygenFlow runs.
302
        let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report
303
            .updated
304
            .iter()
305
            // A bookmark sent again unchanged starts nothing.
306
            .filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name))
307
            .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone()))
308
            .collect();
309
        let app_hint = self.app_hint.clone();
310
        self.runtime.spawn(async move {
311
            let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str()));
312
            if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await {
313
                tracing::warn!("recording push to {}: {e}", parsed);
314
            }
315
            // Oldest first: a link made for one step lets the next step link.
316
            for (key, name, email, object, prev) in successors.iter().rev() {
317
                let Ok(None) = db::accounts::identity_by_key(&pool, key).await else { continue };
318
                let Ok(Some(earlier)) = db::accounts::identity_by_key(&pool, prev).await else { continue };
319
                match db::accounts::link_identity(&pool, earlier.account_id, key, name, email, object).await {
320
                    Ok(()) => {
321
                        tracing::info!("linked identity {} to account {} as a successor of {}", &key[..12], earlier.account_id, &prev[..12]);
322
                        db::accounts::audit(&pool, None, "identity.rotated", key, &format!("successor of {}", &prev[..12])).await.ok();
323
                    }
324
                    Err(e) => tracing::warn!("linking successor identity {}: {e}", &key[..12]),
325
                }
326
            }
327
            let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
328
            if let Some(app) = app {
329
                // Issues close from every change newly on the default
330
                // bookmark, not only those this push imported: a client's
331
                // landing moves main to changes the hub already held.
332
                if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) {
333
                    let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default();
334
                    let old = r.old_state.clone();
335
                    let old_heads = crate::repos::RepoName::new(&row.owner, &row.name);
336
                    let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) {
337
                        (Some(n), Some(s)) => app.repos.read(&n, move |repo, _| Ok(repo.store().get_state(&s).map(|st| st.heads).unwrap_or_default())).await.unwrap_or_default(),
338
                        _ => Vec::new(),
339
                    };
340
                    crate::web::issues::close_from_arrived(&app, &row, old_heads, new_heads, None).await;
341
                } else {
342
                    crate::web::issues::close_from_messages(&app, &row, &messages).await;
343
                }
344
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
345
                crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await;
346
                if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) {
347
                    crate::mirror::refresh(&app, &row).await;
348
                }
349
                let accepted: Vec<serde_json::Value> = results
350
                    .iter()
351
                    .filter(|r| r.ok)
352
                    .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes }))
353
                    .collect();
354
                if !accepted.is_empty() {
355
                    let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
356
                    crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await;
357
                }
358
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
359
                // The moved bookmarks' briefs, worked out now so no page
360
                // waits for them (#145).
361
                if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) {
362
                    let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect();
363
                    let warm = app.clone();
364
                    tokio::spawn(async move {
365
                        warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok();
366
                    });
367
                }
368
                crate::diffusion::after_push(&app, &row, &moved).await;
369
                // An upstream's default bookmark moved: consumers following
370
                // every move are bumped now, those following green when
371
                // its run passes (#148).
372
                if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) {
373
                    let (app, row, heads) = (app.clone(), row.clone(), heads.clone());
374
                    tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await });
375
                }
376
                for (bookmark, state, heads) in &moved {
377
                    crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await;
378
                    crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await;
379
                }
380
            }
381
        });
382
    }
383
}
384
385
/// `/{owner}/{name}/oxy/v1/{endpoint}`.
386
pub async fn dispatch(
387
    State(app): State<Shared>,
388
    Path((owner, name, endpoint)): Path<(String, String, String)>,
389
    method: Method,
390
    headers: HeaderMap,
391
    body: Bytes,
392
) -> Response {
393
    let Some(endpoint) = Endpoint::parse(&endpoint) else {
394
        return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response();
395
    };
396
    let Some(repo) = RepoName::new(&owner, &name) else {
397
        return not_found().into_response();
398
    };
399
    let _guard = match endpoint {
400
        Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await),
401
        _ => Guard::Read(app.repos.read_guard(&repo).await),
402
    };
403
    app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await
404
}
405
406
enum Guard {
407
    Read(#[allow(dead_code)] tokio::sync::OwnedRwLockReadGuard<()>),
408
    Write(#[allow(dead_code)] tokio::sync::OwnedRwLockWriteGuard<()>),
409
}