OXYGENHUB
◆ v0.9.0 oxygenhub / crates / hub / src / protocol.rs
32.5 KiB · Rust · last changed Create and withdraw tags on the website (closes #295, closes #301) 11 days ago source blame history raw
d23464c0 Andy McCall ↶ 1
//! The wire protocol, embedded (hub spec §3.3, §5.4): `oxy-server`'s
19bb31ff Andy McCall ↶ 2
//! `Server` over a backend that resolves `owner/name`, authorises reads
3
//! and pushes by account role and token scope, and records who pushed.
d23464c0 Andy McCall ↶ 4
19bb31ff Andy McCall ↶ 5
use crate::auth::{self, Access, Role};
6
use crate::db::{self, accounts::Account};
d23464c0 Andy McCall ↶ 7
use crate::repos::{RepoName, Repos};
19bb31ff Andy McCall ↶ 8
use crate::Shared;
d23464c0 Andy McCall ↶ 9
use axum::body::Bytes;
10
use axum::extract::{Path, State};
11
use axum::http::{HeaderMap, Method, StatusCode};
12
use axum::response::{IntoResponse, Response};
62d6e969 Andy McCall ↶ 13
use oxy_core::repo::{PushReport, Repo};
19bb31ff Andy McCall ↶ 14
use oxy_server::{Backend, Endpoint, Failure};
d23464c0 Andy McCall ↶ 15
use std::sync::Arc;
16
17
pub struct HubBackend {
18
    repos: Arc<Repos>,
66fd9d6b Andy McCall ↶ 19
    /// Wikis, reached as `OWNER/NAME.wiki` (hub spec §6.11).
20
    wikis: Arc<Repos>,
d23464c0 Andy McCall ↶ 21
    pool: db::Pool,
19bb31ff Andy McCall ↶ 22
    /// Site tokens from `hub.toml`: full access, for bootstrapping.
23
    site_tokens: Vec<String>,
d23464c0 Andy McCall ↶ 24
    runtime: tokio::runtime::Handle,
18e381ea Andy McCall ↶ 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>>>>,
3eb491e9 Andy McCall ↶ 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)>>,
0ee5c3fb Andy McCall ↶ 30
    /// Push protection's scan budget (#268, `[limits]`).
31
    scan_budget: crate::push_protection::Budget,
32
    /// Repositories whose push protection scan stopped early, by id: the
33
    /// note goes on the push log entry of the push that moves references
34
    /// (the parts of a long push before it move none).
35
    scan_notes: Arc<std::sync::Mutex<std::collections::HashMap<i64, String>>>,
b349e15a Andy McCall ↶ 36
    /// The keys that signed what a push to each repository (by name)
37
    /// brought, noted when it is vetted and recorded once it is accepted
38
    /// (#286, Settings › Keys' "last used").
39
    signers: Arc<std::sync::Mutex<std::collections::HashMap<String, std::collections::BTreeSet<String>>>>,
d23464c0 Andy McCall ↶ 40
}
41
19bb31ff Andy McCall ↶ 42
/// Who a request's credential resolved to.
43
enum Caller {
44
    Anonymous,
45
    /// A site token from the configuration.
46
    Site,
47
    Token(Box<Account>, Box<db::accounts::Token>),
fd3db644 Andy McCall ↶ 48
    /// A running OxygenFlow job: reads its own repository and the subrepos
49
    /// its state pins, nothing else (hub spec §13.2).
42fa9554 Andy McCall ↶ 50
    Job {
51
        repo_id: i64,
fd3db644 Andy McCall ↶ 52
        also: Vec<i64>,
42fa9554 Andy McCall ↶ 53
    },
19bb31ff Andy McCall ↶ 54
}
55
d23464c0 Andy McCall ↶ 56
impl HubBackend {
66fd9d6b Andy McCall ↶ 57
    pub fn new(
58
        repos: Arc<Repos>,
59
        wikis: Arc<Repos>,
60
        pool: db::Pool,
61
        site_tokens: Vec<String>,
62
        runtime: tokio::runtime::Handle,
63
    ) -> HubBackend {
0ee5c3fb Andy McCall ↶ 64
        HubBackend {
65
            repos,
66
            wikis,
67
            pool,
68
            site_tokens,
69
            runtime,
70
            app_hint: Arc::new(std::sync::Mutex::new(None)),
71
            acceptor: None,
72
            scan_budget: crate::push_protection::Budget::default(),
73
            scan_notes: Arc::default(),
b349e15a Andy McCall ↶ 74
            signers: Arc::default(),
0ee5c3fb Andy McCall ↶ 75
        }
18e381ea Andy McCall ↶ 76
    }
77
0ee5c3fb Andy McCall ↶ 78
    /// Scan pushes for secrets within `budget` (#268).
79
    pub fn with_scan_budget(mut self, budget: crate::push_protection::Budget) -> Self {
80
        self.scan_budget = budget;
81
        self
82
    }
83
84
    /// Who pushes, for the push log: the account (id 0 for a site token)
85
    /// and, for an agent's token, the agent's name.
86
    fn pusher(&self, headers: &HeaderMap) -> (Option<(i64, String)>, String) {
87
        match self.caller(headers) {
88
            Ok(Caller::Token(account, token)) => {
89
                (Some((account.id, account.username)), if token.agent { token.name.clone() } else { String::new() })
90
            }
91
            Ok(Caller::Site) => (Some((0, "site token".to_string())), String::new()),
92
            _ => (None, String::new()),
93
        }
94
    }
95
3eb491e9 Andy McCall ↶ 96
    /// Countersign accepted updates by keys this hub certified with `identity`.
97
    pub fn with_acceptor(mut self, identity: oxy_core::identity::Identity, secret: oxy_core::identity::SecretKey) -> Self {
98
        self.acceptor = Some(Arc::new((identity, secret)));
99
        self
100
    }
101
18e381ea Andy McCall ↶ 102
    /// The slot the application registers itself in after construction.
103
    pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> {
104
        self.app_hint.clone()
19bb31ff Andy McCall ↶ 105
    }
106
107
    fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T {
108
        tokio::task::block_in_place(|| self.runtime.block_on(f))
d23464c0 Andy McCall ↶ 109
    }
110
111
    fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> {
112
        let pool = self.pool.clone();
19bb31ff Andy McCall ↶ 113
        match self.block(db::repos::get(&pool, &name.owner, &name.name)) {
d23464c0 Andy McCall ↶ 114
            Ok(Some(r)) => Ok(r),
d0fbea2e Andy McCall ↶ 115
            // A transferred repository still answers to its old name (#108).
116
            Ok(None) => match self.block(db::repos::moved_to(&pool, &name.owner, &name.name)) {
117
                Ok(Some(r)) => Ok(r),
118
                Ok(None) => Err(not_found()),
119
                Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
120
            },
d23464c0 Andy McCall ↶ 121
            Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
122
        }
123
    }
124
19bb31ff Andy McCall ↶ 125
    fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> {
126
        let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) };
127
        if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) {
128
            return Ok(Caller::Site);
d23464c0 Andy McCall ↶ 129
        }
19bb31ff Andy McCall ↶ 130
        let hash = auth::token_hash(presented);
131
        let pool = self.pool.clone();
42fa9554 Andy McCall ↶ 132
        if presented.starts_with(crate::flow::JOB_TOKEN_PREFIX) {
133
            let job = self.block(db::flow::job_by_token(&pool, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)).map_err(internal)?;
134
            let Some(job) = job else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
135
            let run = self.block(db::flow::run_by_id(&pool, job.run_id)).map_err(internal)?;
136
            let Some(run) = run else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
fd3db644 Andy McCall ↶ 137
            return Ok(Caller::Job { repo_id: run.repo_id, also: job.also_read_ids() });
42fa9554 Andy McCall ↶ 138
        }
559c3920 Andy McCall ↶ 139
        // An agent's grant token (keyless agents, #241): its owner's
140
        // account, as an agent's token, writing when the grant lets its
141
        // keys move references. Revoking the grant stops it at once.
142
        let grant_token = presented.starts_with(crate::keyless::GRANT_TOKEN_PREFIX);
143
        let token = if grant_token {
144
            let grant = self.block(db::accounts::agent_grant_by_token(&pool, &hash)).map_err(internal)?;
145
            let Some(g) = grant.filter(|g| g.live(db::now_ms())) else {
146
                return Err(Failure(StatusCode::UNAUTHORIZED, "this agent grant is not valid (revoked, ended or unknown)".into()));
147
            };
148
            let _ = self.block(db::accounts::touch_agent_grant(&pool, g.id));
149
            Some(db::accounts::Token {
150
                id: 0,
151
                account_id: g.account_id,
152
                name: g.agent.clone(),
153
                hash: String::new(),
154
                scopes: if g.scope.split(',').any(|s| s.trim() == "refs") { "repo:read,repo:write" } else { "repo:read" }.into(),
155
                repos: String::new(),
156
                created_ms: g.created_ms,
157
                expires_ms: Some(g.expires_ms),
158
                last_used_ms: g.last_used_ms,
159
                renew_ms: None,
160
                agent: true,
161
            })
162
        } else {
163
            self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)?
164
        };
19bb31ff Andy McCall ↶ 165
        let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
166
        if token.expired(db::now_ms()) {
167
            return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into()));
168
        }
169
        let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?;
170
        let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
171
        if account.banned {
172
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into()));
173
        }
6a178055 Andy McCall ↶ 174
        if account.disabled {
175
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is disabled; contact the hub's administrator".into()));
559c3920 Andy McCall ↶ 176
        }
177
        if !grant_token {
178
            let _ = self.block(db::accounts::touch_token(&pool, token.id));
6a178055 Andy McCall ↶ 179
        }
19bb31ff Andy McCall ↶ 180
        Ok(Caller::Token(Box::new(account), Box::new(token)))
d23464c0 Andy McCall ↶ 181
    }
182
}
183
19bb31ff Andy McCall ↶ 184
fn internal(e: sqlx::Error) -> Failure {
185
    Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
186
}
187
d23464c0 Andy McCall ↶ 188
fn not_found() -> Failure {
189
    Failure(StatusCode::NOT_FOUND, "no such repository".into())
190
}
191
192
impl Backend for HubBackend {
193
    fn open(&self, name: &str) -> Result<Repo, Failure> {
d0fbea2e Andy McCall ↶ 194
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
66fd9d6b Andy McCall ↶ 195
        // `NAME.wiki` is the repository's wiki (§6.11); the row knows the
196
        // current name, so a transferred repository still answers to its
197
        // old one (#108).
198
        let (parent, is_wiki) = crate::wiki::parent_of(&parsed);
199
        let row = self.row(&parent)?;
d0fbea2e Andy McCall ↶ 200
        let name = RepoName::new(&row.owner, &row.name).ok_or_else(not_found)?;
66fd9d6b Andy McCall ↶ 201
        let store = if is_wiki { &self.wikis } else { &self.repos };
3eb491e9 Andy McCall ↶ 202
        let repo = store.open(&name).map_err(|e| match e {
d23464c0 Andy McCall ↶ 203
            crate::repos::RepoError::NotFound => not_found(),
204
            other => Failure(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
3eb491e9 Andy McCall ↶ 205
        })?;
206
        Ok(match &self.acceptor {
207
            Some(a) => repo.with_acceptor(a.0.clone(), a.1.clone()),
208
            None => repo,
d23464c0 Andy McCall ↶ 209
        })
210
    }
211
19bb31ff Andy McCall ↶ 212
    fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> {
d23464c0 Andy McCall ↶ 213
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
66fd9d6b Andy McCall ↶ 214
        let (parent, _) = crate::wiki::parent_of(&parsed);
215
        let row = self.row(&parent)?;
19bb31ff Andy McCall ↶ 216
        let caller = self.caller(headers)?;
fd3db644 Andy McCall ↶ 217
        if let Caller::Job { repo_id, also } = &caller {
218
            // Only reads: of the job's own repository, and of the subrepos its
219
            // state pins that its pusher could read when the job was claimed.
42fa9554 Andy McCall ↶ 220
            return match access {
fd3db644 Andy McCall ↶ 221
                oxy_server::Access::Read if *repo_id == row.id || also.contains(&row.id) => Ok(()),
42fa9554 Andy McCall ↶ 222
                oxy_server::Access::Read => Err(not_found()),
223
                oxy_server::Access::Push => Err(Failure(StatusCode::FORBIDDEN, "a job token cannot push".into())),
224
            };
225
        }
19bb31ff Andy McCall ↶ 226
        let (role, scope_ok) = match &caller {
42fa9554 Andy McCall ↶ 227
            Caller::Job { .. } => unreachable!(),
19bb31ff Andy McCall ↶ 228
            Caller::Site => (Some(Role::Admin), true),
229
            Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true),
230
            Caller::Token(account, token) => {
231
                let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?;
232
                let needed = match access {
233
                    oxy_server::Access::Read => {
234
                        token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin")
235
                    }
236
                    oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"),
237
                };
d0fbea2e Andy McCall ↶ 238
                let current = format!("{}/{}", row.owner, row.name);
66fd9d6b Andy McCall ↶ 239
                let covered = token.covers(&parsed.to_string()) || token.covers(&current) || token.covers(&parent.to_string());
240
                (role, needed && covered)
19bb31ff Andy McCall ↶ 241
            }
242
        };
243
        // An unreadable repository does not exist for the caller (hub spec §4.4).
244
        if !auth::roles::allows(role, Access::Read) {
d23464c0 Andy McCall ↶ 245
            return Err(not_found());
246
        }
6e7dfa0a Andy McCall ↶ 247
        // A wiki is made, empty and unbound, when someone who may write to
248
        // its repository first reaches it, so the first page can be pushed
249
        // as well as written on the web (§6.11); the push gives it its id.
250
        let (_, is_wiki) = crate::wiki::parent_of(&parsed);
251
        let may_write = match &caller {
252
            Caller::Site => true,
253
            Caller::Token(_, token) => token.has_scope("repo:write") || token.has_scope("repo:admin"),
254
            _ => false,
255
        };
256
        if is_wiki && may_write && auth::roles::allows(role, Access::Write) && !row.archived {
257
            if let Some(name) = RepoName::new(&row.owner, &row.name) {
258
                if !self.wikis.exists(&name) {
259
                    self.wikis
260
                        .create(&name, None)
261
                        .map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, format!("make the wiki: {e}")))?;
262
                }
263
            }
264
        }
d23464c0 Andy McCall ↶ 265
        match access {
19bb31ff Andy McCall ↶ 266
            oxy_server::Access::Read if scope_ok => Ok(()),
267
            oxy_server::Access::Read => Err(Failure(StatusCode::FORBIDDEN, "this token cannot read repositories".into())),
268
            oxy_server::Access::Push => {
269
                if matches!(caller, Caller::Anonymous) {
270
                    return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into()));
271
                }
272
                if !auth::roles::allows(role, Access::Write) {
273
                    return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into()));
d0fbea2e Andy McCall ↶ 274
                }
275
                if row.archived {
276
                    return Err(Failure(StatusCode::FORBIDDEN, "this repository is archived and read-only".into()));
19bb31ff Andy McCall ↶ 277
                }
278
                if !scope_ok {
279
                    return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into()));
280
                }
281
                Ok(())
282
            }
d23464c0 Andy McCall ↶ 283
        }
284
    }
285
e271a37b Andy McCall ↶ 286
    fn default_bookmark(&self, name: &str) -> Option<String> {
287
        let parsed = RepoName::parse(name)?;
66fd9d6b Andy McCall ↶ 288
        if crate::wiki::parent_of(&parsed).1 {
289
            return Some(crate::wiki::BOOKMARK.into());
290
        }
e271a37b Andy McCall ↶ 291
        self.row(&parsed).ok().map(|r| r.default_bookmark)
292
    }
293
e5b97ac3 Andy McCall ↶ 294
    fn add_log(&self, name: &str, refs: &mut oxy_core::protocol::RefsResponse) {
295
        let Some(app) = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()) else { return };
296
        let row = RepoName::parse(name).and_then(|p| self.row(&crate::wiki::parent_of(&p).0).ok()).map(|r| r.id);
297
        let backfill = crate::tlog::add_log(&app, row, refs);
298
        if !backfill.is_empty() {
299
            self.block(crate::tlog::log(&app, backfill));
0ee5c3fb Andy McCall ↶ 300
        }
301
    }
302
a0a1601e Andy McCall ↶ 303
    /// Tailor the refs answer to the client asking (oxygen#131): one that
304
    /// does not declare `certified-tags` in `Oxy-Features` (oxy 0.5.0 and
305
    /// older) is not sent a tag that only a certified key's certificate
306
    /// authorizes, which it would refuse and, with it, the whole clone.
307
    fn adjust_refs(&self, _name: &str, repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) {
308
        oxy_server::tailor_refs(repo, refs, headers);
309
    }
310
0ee5c3fb Andy McCall ↶ 311
    /// Push protection (hub spec §15.4, #268): a push adding what looks
312
    /// like a secret is refused, saying where, unless the repository turned
313
    /// it off or a maintainer allowed that finding's fingerprint.
314
    fn vet_push(
315
        &self,
316
        name: &str,
317
        repo: &Repo,
318
        bundle: &oxy_core::protocol::Bundle,
319
        updates: &[oxy_core::protocol::RefUpdate],
320
        headers: &HeaderMap,
321
    ) -> Result<(), Failure> {
b349e15a Andy McCall ↶ 322
        let keys: std::collections::BTreeSet<String> = bundle
323
            .changes
324
            .iter()
325
            .flat_map(|(_, sigs)| sigs.iter())
326
            .chain(bundle.attests.iter().flat_map(|a| a.sigs.iter()))
327
            .map(|sig| sig.key.to_hex())
328
            .collect();
329
        if !keys.is_empty() {
330
            self.signers.lock().unwrap().entry(name.to_string()).or_default().extend(keys);
331
        }
0ee5c3fb Andy McCall ↶ 332
        if bundle.changes.is_empty() {
333
            return Ok(());
334
        }
335
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
336
        let row = self.row(&crate::wiki::parent_of(&parsed).0)?;
337
        if !row.push_protection {
338
            return Ok(());
339
        }
340
        let scan = crate::push_protection::scan(bundle, self.scan_budget);
341
        if let Some(note) = scan.note() {
342
            tracing::info!("push to {name}: {note}");
343
            self.scan_notes.lock().unwrap().insert(row.id, note);
344
        }
345
        if scan.findings.is_empty() {
346
            return Ok(());
e5b97ac3 Andy McCall ↶ 347
        }
0ee5c3fb Andy McCall ↶ 348
        let allowed = self.block(db::push_protection::allowed(&self.pool, row.id)).map_err(internal)?;
349
        let mut findings: Vec<_> = scan.findings.into_iter().filter(|f| !allowed.contains(&f.fingerprint)).collect();
350
        if findings.is_empty() {
351
            return Ok(());
352
        }
353
        crate::push_protection::locate(repo, bundle, &mut findings);
354
        let (pusher, via) = self.pusher(headers);
355
        let base = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()).map(|a| a.config.base_url().to_string());
166f6e9b Andy McCall 356
        let settings =
357
            if row.is_drop() { String::new() } else { format!("{}/{}/{}/settings", base.unwrap_or_default(), row.owner, row.name) };
0ee5c3fb Andy McCall ↶ 358
        let text = crate::push_protection::refusal(&findings, &settings, &via);
359
        // Recorded for the maintainers (Settings › Push protection) and in
360
        // the push log, as every refused push is.
361
        let records: Vec<db::push_protection::NewFinding> = findings
362
            .iter()
363
            .map(|f| db::push_protection::NewFinding {
364
                rule: &f.rule,
365
                description: &f.description,
366
                path: &f.path,
367
                line: f.line as i64,
368
                fingerprint: &f.fingerprint,
369
                excerpt: &f.excerpt,
370
            })
371
            .collect();
372
        let who = pusher.as_ref().map(|(id, n)| (*id, n.as_str()));
373
        if let Err(e) = self.block(db::push_protection::record(&self.pool, row.id, who.map(|w| w.1).unwrap_or(""), &via, &records)) {
374
            tracing::warn!("recording push protection findings for {name}: {e}");
375
        }
376
        let reason = format!(
166f6e9b Andy McCall 377
            "push protection: {} possible secret{} ({}){}",
0ee5c3fb Andy McCall ↶ 378
            findings.len(),
379
            if findings.len() == 1 { "" } else { "s" },
166f6e9b Andy McCall 380
            findings.iter().take(3).map(|f| format!("{}:{}", f.path, f.line)).collect::<Vec<_>>().join(", "),
381
            if row.is_drop() { "" } else { "; see Settings › Push protection" }
0ee5c3fb Andy McCall ↶ 382
        );
383
        let refs: Vec<String> = if updates.is_empty() {
384
            vec![oxy_core::repo::OBJECTS_RESULT.to_string()]
385
        } else {
386
            updates.iter().map(|u| u.name.clone()).collect()
387
        };
388
        let outcomes: Vec<db::repos::PushOutcome> = refs
389
            .into_iter()
390
            .map(|reference| db::repos::PushOutcome {
391
                reference,
392
                ok: false,
393
                reason: reason.clone(),
394
                old_state: String::new(),
395
                new_state: String::new(),
396
                heads: 0,
397
                changes: 0,
398
            })
399
            .collect();
400
        if let Err(e) = self.block(db::repos::record_push_via(&self.pool, row.id, who, &via, &outcomes)) {
401
            tracing::warn!("recording refused push to {name}: {e}");
402
        }
403
        tracing::info!("push to {name} refused by push protection: {} finding(s)", findings.len());
404
        Err(Failure(StatusCode::UNPROCESSABLE_ENTITY, text))
e5b97ac3 Andy McCall ↶ 405
    }
406
ba0a55bb Andy McCall ↶ 407
    /// A tag a release names is not withdrawn (#301): the release would
408
    /// point at nothing. Delete the release first.
409
    fn vet_tag_withdrawal(&self, name: &str, tag: &str) -> Result<(), String> {
410
        let Some(parsed) = RepoName::parse(name) else { return Ok(()) };
411
        let Ok(row) = self.row(&parsed) else { return Ok(()) };
412
        match self.block(db::releases::get(&self.pool, row.id, tag)) {
413
            Ok(Some(rel)) => Err(format!("the release {:?} uses tags/{tag}: delete the release first", rel.title)),
414
            Ok(None) => Ok(()),
415
            Err(e) => Err(format!("could not check the releases of {name}: {e}")),
416
        }
417
    }
418
19bb31ff Andy McCall ↶ 419
    fn after_push(&self, name: &str, report: &PushReport, headers: &HeaderMap) {
b349e15a Andy McCall ↶ 420
        // The keys that signed what it brought were used (#286): linked
421
        // ones show it under Settings › Keys.
422
        let signers = self.signers.lock().unwrap().remove(name);
423
        if let Some(keys) = signers.filter(|_| report.results.iter().any(|r| r.ok)) {
424
            let pool = self.pool.clone();
425
            let keys: Vec<String> = keys.into_iter().collect();
426
            tokio::spawn(async move {
427
                if let Err(e) = db::accounts::identities_used(&pool, &keys).await {
428
                    tracing::warn!("noting keys used: {e}");
429
                }
430
            });
431
        }
d23464c0 Andy McCall ↶ 432
        let Some(parsed) = RepoName::parse(name) else { return };
66fd9d6b Andy McCall ↶ 433
        // A wiki push starts no run and rings no webhook.
434
        if crate::wiki::parent_of(&parsed).1 {
435
            return;
436
        }
d23464c0 Andy McCall ↶ 437
        let Ok(row) = self.row(&parsed) else { return };
d0fbea2e Andy McCall ↶ 438
        let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return };
0ee5c3fb Andy McCall ↶ 439
        let (pusher, via) = self.pusher(headers);
d23464c0 Andy McCall ↶ 440
        let pool = self.pool.clone();
4bae285c Andy McCall ↶ 441
        // A part of a long push (objects alone) moves nothing: the push it
442
        // belongs to is recorded when its last part names the references.
0ee5c3fb Andy McCall ↶ 443
        let mut results: Vec<db::repos::PushOutcome> = report
62d6e969 Andy McCall ↶ 444
            .results
445
            .iter()
4bae285c Andy McCall ↶ 446
            .filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT)
62d6e969 Andy McCall ↶ 447
            .map(|r| {
448
                let updated = report.updated.iter().find(|u| u.name == r.name);
449
                db::repos::PushOutcome {
450
                    reference: r.name.clone(),
451
                    ok: r.ok,
452
                    reason: r.reason.clone(),
453
                    old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(),
454
                    new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(),
455
                    heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0),
456
                    changes: if r.ok { report.imported.len() as i64 } else { 0 },
457
                }
458
            })
459
            .collect();
0ee5c3fb Andy McCall ↶ 460
        // Push protection's scan stopped early on this push or a part of it
461
        // (#268): its accepted references say so in the log.
462
        if !results.is_empty() {
463
            if let Some(note) = self.scan_notes.lock().unwrap().remove(&row.id) {
464
                for r in results.iter_mut().filter(|r| r.ok) {
465
                    r.reason = if r.reason.is_empty() { note.clone() } else { format!("{}; {note}", r.reason) };
466
                }
467
            }
468
        }
78001759 Andy McCall ↶ 469
        // Messages of the changes stored: `closes #N` when the default
470
        // bookmark moved, and `#N` references on any bookmark (hub spec §8).
471
        let bookmarks: Vec<String> = report
472
            .updated
473
            .iter()
474
            .filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name))
475
            .map(|u| u.name.clone())
476
            .collect();
477
        let imported: Vec<(String, String)> = if bookmarks.is_empty() {
478
            vec![]
479
        } else {
18e381ea Andy McCall ↶ 480
            self.repos
481
                .open(&parsed)
df15b747 Andy McCall ↶ 482
                .map(|r| {
483
                    report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect()
484
                })
18e381ea Andy McCall ↶ 485
                .unwrap_or_default()
78001759 Andy McCall ↶ 486
        };
487
        let messages: Vec<(String, String)> = if bookmarks.contains(&row.default_bookmark) { imported.clone() } else { vec![] };
8933faff Andy McCall ↶ 488
        // Authors whose identity is a successor (a rotation, a renewal, a
489
        // delegation) of an earlier one: each step kept the key pair, so
490
        // if the chain reaches a linked key it is the same person, and the
491
        // successor is linked too, without re-uploading it (hub spec §5.3).
492
        let successors: Vec<(String, String, String, Vec<u8>, String)> = self
493
            .repos
494
            .open(&parsed)
495
            .map(|r| {
496
                let mut seen = std::collections::HashSet::new();
497
                let mut out = Vec::new();
498
                for h in &report.imported {
499
                    let Ok(meta) = r.store().get_change_meta(h) else { continue };
500
                    let mut key = meta.author;
501
                    while !key.is_zero() && seen.insert(key) {
502
                        let Ok(id) = r.store().get_identity(&key) else { break };
503
                        let Some(prev) = id.previous else { break };
504
                        let Ok(before) = r.store().get_identity(&prev) else { break };
505
                        if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk {
506
                            break;
507
                        }
508
                        out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex()));
509
                        key = prev;
510
                    }
511
                }
512
                out
513
            })
514
            .unwrap_or_default();
78001759 Andy McCall ↶ 515
        let on = if bookmarks.contains(&row.default_bookmark) {
516
            row.default_bookmark.clone()
18e381ea Andy McCall ↶ 517
        } else {
78001759 Andy McCall ↶ 518
            bookmarks.first().cloned().unwrap_or_default()
18e381ea Andy McCall ↶ 519
        };
42fa9554 Andy McCall ↶ 520
        // Bookmarks that moved, with their new heads, for OxygenFlow runs.
521
        let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report
522
            .updated
523
            .iter()
c889911b Andy McCall ↶ 524
            // A bookmark sent again unchanged starts nothing.
525
            .filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name))
42fa9554 Andy McCall ↶ 526
            .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone()))
527
            .collect();
965b950f Andy McCall ↶ 528
        // The transparency log (#238): what the push brought, logged before
529
        // the push is answered.
530
        let app = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
531
        if let Some(app) = app {
532
            let to_log = self.repos.open(&parsed).map(|r| crate::tlog::push_entries(&r, Some(row.id), report)).unwrap_or_default();
533
            if !to_log.is_empty() {
534
                self.block(crate::tlog::log(&app, to_log));
535
            }
536
        }
18e381ea Andy McCall ↶ 537
        let app_hint = self.app_hint.clone();
d23464c0 Andy McCall ↶ 538
        self.runtime.spawn(async move {
18e381ea Andy McCall ↶ 539
            let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str()));
2a0c09c0 Andy McCall ↶ 540
            if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await {
d23464c0 Andy McCall ↶ 541
                tracing::warn!("recording push to {}: {e}", parsed);
8933faff Andy McCall ↶ 542
            }
543
            // Oldest first: a link made for one step lets the next step link.
544
            for (key, name, email, object, prev) in successors.iter().rev() {
545
                let Ok(None) = db::accounts::identity_by_key(&pool, key).await else { continue };
546
                let Ok(Some(earlier)) = db::accounts::identity_by_key(&pool, prev).await else { continue };
547
                match db::accounts::link_identity(&pool, earlier.account_id, key, name, email, object).await {
548
                    Ok(()) => {
549
                        tracing::info!("linked identity {} to account {} as a successor of {}", &key[..12], earlier.account_id, &prev[..12]);
550
                        db::accounts::audit(&pool, None, "identity.rotated", key, &format!("successor of {}", &prev[..12])).await.ok();
551
                    }
552
                    Err(e) => tracing::warn!("linking successor identity {}: {e}", &key[..12]),
553
                }
d23464c0 Andy McCall ↶ 554
            }
166f6e9b Andy McCall 555
            // A drop (#279) has no issues, webhooks, mirror, Diffusion or
556
            // runs: its push is recorded, and that is all.
557
            if row.is_drop() {
558
                return;
559
            }
18e381ea Andy McCall ↶ 560
            let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
561
            if let Some(app) = app {
acc85ac0 Andy McCall ↶ 562
                // Issues close from every change newly on the default
563
                // bookmark, not only those this push imported: a client's
564
                // landing moves main to changes the hub already held.
565
                if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) {
566
                    let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default();
567
                    let old = r.old_state.clone();
568
                    let old_heads = crate::repos::RepoName::new(&row.owner, &row.name);
569
                    let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) {
570
                        (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(),
571
                        _ => Vec::new(),
572
                    };
573
                    crate::web::issues::close_from_arrived(&app, &row, old_heads, new_heads, None).await;
574
                } else {
575
                    crate::web::issues::close_from_messages(&app, &row, &messages).await;
576
                }
78001759 Andy McCall ↶ 577
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
578
                crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await;
3f4b0ea6 Andy McCall ↶ 579
                if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) {
dd327d76 Andy McCall ↶ 580
                    // Failed, it is done again from the work queue (#276).
581
                    if crate::mirror::refresh(&app, &row).await.is_err() {
582
                        crate::queue::queue_mirror(&app, row.id).await;
583
                    }
3f4b0ea6 Andy McCall ↶ 584
                }
b64e2537 Andy McCall ↶ 585
                let accepted: Vec<serde_json::Value> = results
586
                    .iter()
587
                    .filter(|r| r.ok)
588
                    .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes }))
589
                    .collect();
590
                if !accepted.is_empty() {
591
                    let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
592
                    crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await;
42fa9554 Andy McCall ↶ 593
                }
594
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
39442b00 Andy McCall ↶ 595
                // The moved bookmarks' briefs, worked out now so no page
596
                // waits for them (#145).
597
                if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) {
598
                    let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect();
599
                    let warm = app.clone();
600
                    tokio::spawn(async move {
601
                        warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok();
602
                    });
603
                }
f677d627 Andy McCall ↶ 604
                crate::diffusion::after_push(&app, &row, &moved).await;
a7e6e8e6 Andy McCall ↶ 605
                // An upstream's default bookmark moved: consumers following
606
                // every move are bumped now, those following green when
607
                // its run passes (#148).
608
                if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) {
609
                    let (app, row, heads) = (app.clone(), row.clone(), heads.clone());
610
                    tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await });
611
                }
42fa9554 Andy McCall ↶ 612
                for (bookmark, state, heads) in &moved {
613
                    crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await;
ac713df7 Andy McCall ↶ 614
                    crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await;
b64e2537 Andy McCall ↶ 615
                }
18e381ea Andy McCall ↶ 616
            }
d23464c0 Andy McCall ↶ 617
        });
618
    }
619
}
620
621
/// `/{owner}/{name}/oxy/v1/{endpoint}`.
622
pub async fn dispatch(
623
    State(app): State<Shared>,
624
    Path((owner, name, endpoint)): Path<(String, String, String)>,
625
    method: Method,
626
    headers: HeaderMap,
627
    body: Bytes,
628
) -> Response {
629
    let Some(endpoint) = Endpoint::parse(&endpoint) else {
630
        return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response();
631
    };
632
    let Some(repo) = RepoName::new(&owner, &name) else {
633
        return not_found().into_response();
634
    };
635
    let _guard = match endpoint {
636
        Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await),
637
        _ => Guard::Read(app.repos.read_guard(&repo).await),
638
    };
85ec74fd Andy McCall ↶ 639
    let (name, received) = (endpoint.name(), body.len() as u64);
640
    let res = app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await;
641
    let sent = axum::body::HttpBody::size_hint(res.body()).exact().unwrap_or(0);
642
    crate::metrics::protocol_bytes(name, received, sent);
643
    res
166f6e9b Andy McCall 644
}
645
646
/// `/{owner}/drops/{id}/oxy/v1/{endpoint}`: a drop is a repository to the
647
/// wire protocol, named `owner/drops/ID` (#279).
648
pub async fn dispatch_drop(
649
    State(app): State<Shared>,
650
    Path((owner, id, endpoint)): Path<(String, String, String)>,
651
    method: Method,
652
    headers: HeaderMap,
653
    body: Bytes,
654
) -> Response {
655
    let Some(name) = RepoName::drop(&owner, &id) else {
656
        return not_found().into_response();
657
    };
658
    dispatch(State(app), Path((name.owner, name.name, endpoint)), method, headers, body).await
d23464c0 Andy McCall ↶ 659
}
660
661
enum Guard {
7c06f5b2 Andy McCall ↶ 662
    Read(#[allow(dead_code)] crate::repos::ReadGuard),
663
    Write(#[allow(dead_code)] crate::repos::WriteGuard),
d23464c0 Andy McCall ↶ 664
}