OXYGENHUB
# af9c6ca11c49 oxygenhub / crates / hub / src / protocol.rs
11.7 KiB · Rust · last changed OxygenFlow runs: pushes start pipeline runs, a runner API with job tokens, jobs as checks, and a Flow tab with the job graph and live logs 1 month 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
    pool: db::Pool,
20
    /// Site tokens from `hub.toml`: full access, for bootstrapping.
21
    site_tokens: Vec<String>,
22
    runtime: tokio::runtime::Handle,
23
    /// The application, set once it exists (the backend is built first).
24
    app_hint: Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>>,
25
}
26
27
/// Who a request's credential resolved to.
28
enum Caller {
29
    Anonymous,
30
    /// A site token from the configuration.
31
    Site,
32
    Token(Box<Account>, Box<db::accounts::Token>),
33
    /// A running OxygenFlow job: reads its own repository, nothing else (hub spec §13.2).
34
    Job {
35
        repo_id: i64,
36
    },
37
}
38
39
impl HubBackend {
40
    pub fn new(repos: Arc<Repos>, pool: db::Pool, site_tokens: Vec<String>, runtime: tokio::runtime::Handle) -> HubBackend {
41
        HubBackend { repos, pool, site_tokens, runtime, app_hint: Arc::new(std::sync::Mutex::new(None)) }
42
    }
43
44
    /// The slot the application registers itself in after construction.
45
    pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> {
46
        self.app_hint.clone()
47
    }
48
49
    fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T {
50
        tokio::task::block_in_place(|| self.runtime.block_on(f))
51
    }
52
53
    fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> {
54
        let pool = self.pool.clone();
55
        match self.block(db::repos::get(&pool, &name.owner, &name.name)) {
56
            Ok(Some(r)) => Ok(r),
57
            Ok(None) => Err(not_found()),
58
            Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
59
        }
60
    }
61
62
    fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> {
63
        let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) };
64
        if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) {
65
            return Ok(Caller::Site);
66
        }
67
        let hash = auth::token_hash(presented);
68
        let pool = self.pool.clone();
69
        if presented.starts_with(crate::flow::JOB_TOKEN_PREFIX) {
70
            let job = self.block(db::flow::job_by_token(&pool, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)).map_err(internal)?;
71
            let Some(job) = job else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
72
            let run = self.block(db::flow::run_by_id(&pool, job.run_id)).map_err(internal)?;
73
            let Some(run) = run else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
74
            return Ok(Caller::Job { repo_id: run.repo_id });
75
        }
76
        let token = self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)?;
77
        let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
78
        if token.expired(db::now_ms()) {
79
            return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into()));
80
        }
81
        let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?;
82
        let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
83
        if account.banned {
84
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into()));
85
        }
86
        let _ = self.block(db::accounts::touch_token(&pool, token.id));
87
        Ok(Caller::Token(Box::new(account), Box::new(token)))
88
    }
89
}
90
91
fn internal(e: sqlx::Error) -> Failure {
92
    Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
93
}
94
95
fn not_found() -> Failure {
96
    Failure(StatusCode::NOT_FOUND, "no such repository".into())
97
}
98
99
impl Backend for HubBackend {
100
    fn open(&self, name: &str) -> Result<Repo, Failure> {
101
        let name = RepoName::parse(name).ok_or_else(not_found)?;
102
        self.repos.open(&name).map_err(|e| match e {
103
            crate::repos::RepoError::NotFound => not_found(),
104
            other => Failure(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
105
        })
106
    }
107
108
    fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> {
109
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
110
        let row = self.row(&parsed)?;
111
        let caller = self.caller(headers)?;
112
        if let Caller::Job { repo_id } = caller {
113
            // Only reads, only of the job's own repository.
114
            return match access {
115
                oxy_server::Access::Read if repo_id == row.id => Ok(()),
116
                oxy_server::Access::Read => Err(not_found()),
117
                oxy_server::Access::Push => Err(Failure(StatusCode::FORBIDDEN, "a job token cannot push".into())),
118
            };
119
        }
120
        let (role, scope_ok) = match &caller {
121
            Caller::Job { .. } => unreachable!(),
122
            Caller::Site => (Some(Role::Admin), true),
123
            Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true),
124
            Caller::Token(account, token) => {
125
                let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?;
126
                let needed = match access {
127
                    oxy_server::Access::Read => {
128
                        token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin")
129
                    }
130
                    oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"),
131
                };
132
                (role, needed && token.covers(&parsed.to_string()))
133
            }
134
        };
135
        // An unreadable repository does not exist for the caller (hub spec §4.4).
136
        if !auth::roles::allows(role, Access::Read) {
137
            return Err(not_found());
138
        }
139
        match access {
140
            oxy_server::Access::Read if scope_ok => Ok(()),
141
            oxy_server::Access::Read => Err(Failure(StatusCode::FORBIDDEN, "this token cannot read repositories".into())),
142
            oxy_server::Access::Push => {
143
                if matches!(caller, Caller::Anonymous) {
144
                    return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into()));
145
                }
146
                if !auth::roles::allows(role, Access::Write) {
147
                    return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into()));
148
                }
149
                if !scope_ok {
150
                    return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into()));
151
                }
152
                Ok(())
153
            }
154
        }
155
    }
156
157
    fn after_push(&self, name: &str, report: &PushReport, headers: &HeaderMap) {
158
        let Some(parsed) = RepoName::parse(name) else { return };
159
        let Ok(row) = self.row(&parsed) else { return };
160
        let pusher = match self.caller(headers) {
161
            Ok(Caller::Token(account, _)) => Some((account.id, account.username)),
162
            Ok(Caller::Site) => Some((0, "site token".to_string())),
163
            _ => None,
164
        };
165
        let pool = self.pool.clone();
166
        let results: Vec<db::repos::PushOutcome> = report
167
            .results
168
            .iter()
169
            .map(|r| {
170
                let updated = report.updated.iter().find(|u| u.name == r.name);
171
                db::repos::PushOutcome {
172
                    reference: r.name.clone(),
173
                    ok: r.ok,
174
                    reason: r.reason.clone(),
175
                    old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(),
176
                    new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(),
177
                    heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0),
178
                    changes: if r.ok { report.imported.len() as i64 } else { 0 },
179
                }
180
            })
181
            .collect();
182
        // Messages of the changes stored, for `closes #N` (hub spec §8), when
183
        // the default bookmark moved.
184
        let messages: Vec<(String, String)> = if report.updated.iter().any(|u| u.name == row.default_bookmark) {
185
            self.repos
186
                .open(&parsed)
187
                .map(|r| report.imported.iter().filter_map(|h| r.store().get_change(h).ok().map(|c| (h.to_hex(), c.message))).collect())
188
                .unwrap_or_default()
189
        } else {
190
            vec![]
191
        };
192
        // Bookmarks that moved, with their new heads, for OxygenFlow runs.
193
        let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report
194
            .updated
195
            .iter()
196
            .filter(|u| report.results.iter().any(|r| r.ok && r.name == u.name))
197
            .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone()))
198
            .collect();
199
        let app_hint = self.app_hint.clone();
200
        self.runtime.spawn(async move {
201
            let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str()));
202
            if let Err(e) = db::repos::record_push(&pool, row.id, who, &results).await {
203
                tracing::warn!("recording push to {}: {e}", parsed);
204
            }
205
            let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
206
            if let Some(app) = app {
207
                crate::web::issues::close_from_messages(&app, &row, &messages).await;
208
                if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) {
209
                    crate::mirror::refresh(&app, &row).await;
210
                }
211
                let accepted: Vec<serde_json::Value> = results
212
                    .iter()
213
                    .filter(|r| r.ok)
214
                    .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes }))
215
                    .collect();
216
                if !accepted.is_empty() {
217
                    let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
218
                    crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await;
219
                }
220
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
221
                for (bookmark, state, heads) in &moved {
222
                    crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await;
223
                }
224
            }
225
        });
226
    }
227
}
228
229
/// `/{owner}/{name}/oxy/v1/{endpoint}`.
230
pub async fn dispatch(
231
    State(app): State<Shared>,
232
    Path((owner, name, endpoint)): Path<(String, String, String)>,
233
    method: Method,
234
    headers: HeaderMap,
235
    body: Bytes,
236
) -> Response {
237
    let Some(endpoint) = Endpoint::parse(&endpoint) else {
238
        return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response();
239
    };
240
    let Some(repo) = RepoName::new(&owner, &name) else {
241
        return not_found().into_response();
242
    };
243
    let _guard = match endpoint {
244
        Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await),
245
        _ => Guard::Read(app.repos.read_guard(&repo).await),
246
    };
247
    app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await
248
}
249
250
enum Guard {
251
    Read(#[allow(dead_code)] tokio::sync::OwnedRwLockReadGuard<()>),
252
    Write(#[allow(dead_code)] tokio::sync::OwnedRwLockWriteGuard<()>),
253
}