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