| d23464c0 Andy McCall ↶ |
1 |
|
| 19bb31ff Andy McCall ↶ |
2 |
|
| 3 |
|
| 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 |
|
| 20 |
wikis: Arc<Repos>, |
| d23464c0 Andy McCall ↶ |
21 |
pool: db::Pool, |
| 19bb31ff Andy McCall ↶ |
22 |
|
| 23 |
site_tokens: Vec<String>, |
| d23464c0 Andy McCall ↶ |
24 |
runtime: tokio::runtime::Handle, |
| 18e381ea Andy McCall ↶ |
25 |
|
| 26 |
app_hint: Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>>, |
| 3eb491e9 Andy McCall ↶ |
27 |
|
| 28 |
|
| 29 |
acceptor: Option<Arc<(oxy_core::identity::Identity, oxy_core::identity::SecretKey)>>, |
| 0ee5c3fb Andy McCall ↶ |
30 |
|
| 31 |
scan_budget: crate::push_protection::Budget, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
scan_notes: Arc<std::sync::Mutex<std::collections::HashMap<i64, String>>>, |
| b349e15a Andy McCall ↶ |
36 |
|
| 37 |
|
| 38 |
|
| 39 |
signers: Arc<std::sync::Mutex<std::collections::HashMap<String, std::collections::BTreeSet<String>>>>, |
| d23464c0 Andy McCall ↶ |
40 |
} |
| 41 |
|
| 19bb31ff Andy McCall ↶ |
42 |
|
| 43 |
enum Caller { |
| 44 |
Anonymous, |
| 45 |
|
| 46 |
Site, |
| 47 |
Token(Box<Account>, Box<db::accounts::Token>), |
| fd3db644 Andy McCall ↶ |
48 |
|
| 49 |
|
| 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 |
|
| 79 |
pub fn with_scan_budget(mut self, budget: crate::push_protection::Budget) -> Self { |
| 80 |
self.scan_budget = budget; |
| 81 |
self |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 196 |
|
| 197 |
|
| 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 |
|
| 219 |
|
| 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(¤t) || token.covers(&parent.to_string()); |
| 240 |
(role, needed && covered) |
| 19bb31ff Andy McCall ↶ |
241 |
} |
| 242 |
}; |
| 243 |
|
| 244 |
if !auth::roles::allows(role, Access::Read) { |
| d23464c0 Andy McCall ↶ |
245 |
return Err(not_found()); |
| 246 |
} |
| 6e7dfa0a Andy McCall ↶ |
247 |
|
| 248 |
|
| 249 |
|
| 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 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 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 |
|
| 312 |
|
| 313 |
|
| 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 |
|
| 360 |
|
| 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 |
|
| 19bb31ff Andy McCall ↶ |
407 |
fn after_push(&self, name: &str, report: &PushReport, headers: &HeaderMap) { |
| b349e15a Andy McCall ↶ |
408 |
|
| 409 |
|
| 410 |
let signers = self.signers.lock().unwrap().remove(name); |
| 411 |
if let Some(keys) = signers.filter(|_| report.results.iter().any(|r| r.ok)) { |
| 412 |
let pool = self.pool.clone(); |
| 413 |
let keys: Vec<String> = keys.into_iter().collect(); |
| 414 |
tokio::spawn(async move { |
| 415 |
if let Err(e) = db::accounts::identities_used(&pool, &keys).await { |
| 416 |
tracing::warn!("noting keys used: {e}"); |
| 417 |
} |
| 418 |
}); |
| 419 |
} |
| d23464c0 Andy McCall ↶ |
420 |
let Some(parsed) = RepoName::parse(name) else { return }; |
| 66fd9d6b Andy McCall ↶ |
421 |
|
| 422 |
if crate::wiki::parent_of(&parsed).1 { |
| 423 |
return; |
| 424 |
} |
| d23464c0 Andy McCall ↶ |
425 |
let Ok(row) = self.row(&parsed) else { return }; |
| d0fbea2e Andy McCall ↶ |
426 |
let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return }; |
| 0ee5c3fb Andy McCall ↶ |
427 |
let (pusher, via) = self.pusher(headers); |
| d23464c0 Andy McCall ↶ |
428 |
let pool = self.pool.clone(); |
| 4bae285c Andy McCall ↶ |
429 |
|
| 430 |
|
| 0ee5c3fb Andy McCall ↶ |
431 |
let mut results: Vec<db::repos::PushOutcome> = report |
| 62d6e969 Andy McCall ↶ |
432 |
.results |
| 433 |
.iter() |
| 4bae285c Andy McCall ↶ |
434 |
.filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT) |
| 62d6e969 Andy McCall ↶ |
435 |
.map(|r| { |
| 436 |
let updated = report.updated.iter().find(|u| u.name == r.name); |
| 437 |
db::repos::PushOutcome { |
| 438 |
reference: r.name.clone(), |
| 439 |
ok: r.ok, |
| 440 |
reason: r.reason.clone(), |
| 441 |
old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(), |
| 442 |
new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(), |
| 443 |
heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0), |
| 444 |
changes: if r.ok { report.imported.len() as i64 } else { 0 }, |
| 445 |
} |
| 446 |
}) |
| 447 |
.collect(); |
| 0ee5c3fb Andy McCall ↶ |
448 |
|
| 449 |
|
| 450 |
if !results.is_empty() { |
| 451 |
if let Some(note) = self.scan_notes.lock().unwrap().remove(&row.id) { |
| 452 |
for r in results.iter_mut().filter(|r| r.ok) { |
| 453 |
r.reason = if r.reason.is_empty() { note.clone() } else { format!("{}; {note}", r.reason) }; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 78001759 Andy McCall ↶ |
457 |
|
| 458 |
|
| 459 |
let bookmarks: Vec<String> = report |
| 460 |
.updated |
| 461 |
.iter() |
| 462 |
.filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name)) |
| 463 |
.map(|u| u.name.clone()) |
| 464 |
.collect(); |
| 465 |
let imported: Vec<(String, String)> = if bookmarks.is_empty() { |
| 466 |
vec![] |
| 467 |
} else { |
| 18e381ea Andy McCall ↶ |
468 |
self.repos |
| 469 |
.open(&parsed) |
| df15b747 Andy McCall ↶ |
470 |
.map(|r| { |
| 471 |
report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect() |
| 472 |
}) |
| 18e381ea Andy McCall ↶ |
473 |
.unwrap_or_default() |
| 78001759 Andy McCall ↶ |
474 |
}; |
| 475 |
let messages: Vec<(String, String)> = if bookmarks.contains(&row.default_bookmark) { imported.clone() } else { vec![] }; |
| 8933faff Andy McCall ↶ |
476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
let successors: Vec<(String, String, String, Vec<u8>, String)> = self |
| 481 |
.repos |
| 482 |
.open(&parsed) |
| 483 |
.map(|r| { |
| 484 |
let mut seen = std::collections::HashSet::new(); |
| 485 |
let mut out = Vec::new(); |
| 486 |
for h in &report.imported { |
| 487 |
let Ok(meta) = r.store().get_change_meta(h) else { continue }; |
| 488 |
let mut key = meta.author; |
| 489 |
while !key.is_zero() && seen.insert(key) { |
| 490 |
let Ok(id) = r.store().get_identity(&key) else { break }; |
| 491 |
let Some(prev) = id.previous else { break }; |
| 492 |
let Ok(before) = r.store().get_identity(&prev) else { break }; |
| 493 |
if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk { |
| 494 |
break; |
| 495 |
} |
| 496 |
out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex())); |
| 497 |
key = prev; |
| 498 |
} |
| 499 |
} |
| 500 |
out |
| 501 |
}) |
| 502 |
.unwrap_or_default(); |
| 78001759 Andy McCall ↶ |
503 |
let on = if bookmarks.contains(&row.default_bookmark) { |
| 504 |
row.default_bookmark.clone() |
| 18e381ea Andy McCall ↶ |
505 |
} else { |
| 78001759 Andy McCall ↶ |
506 |
bookmarks.first().cloned().unwrap_or_default() |
| 18e381ea Andy McCall ↶ |
507 |
}; |
| 42fa9554 Andy McCall ↶ |
508 |
|
| 509 |
let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report |
| 510 |
.updated |
| 511 |
.iter() |
| c889911b Andy McCall ↶ |
512 |
|
| 513 |
.filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name)) |
| 42fa9554 Andy McCall ↶ |
514 |
.map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone())) |
| 515 |
.collect(); |
| 965b950f Andy McCall ↶ |
516 |
|
| 517 |
|
| 518 |
let app = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()); |
| 519 |
if let Some(app) = app { |
| 520 |
let to_log = self.repos.open(&parsed).map(|r| crate::tlog::push_entries(&r, Some(row.id), report)).unwrap_or_default(); |
| 521 |
if !to_log.is_empty() { |
| 522 |
self.block(crate::tlog::log(&app, to_log)); |
| 523 |
} |
| 524 |
} |
| 18e381ea Andy McCall ↶ |
525 |
let app_hint = self.app_hint.clone(); |
| d23464c0 Andy McCall ↶ |
526 |
self.runtime.spawn(async move { |
| 18e381ea Andy McCall ↶ |
527 |
let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str())); |
| 2a0c09c0 Andy McCall ↶ |
528 |
if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await { |
| d23464c0 Andy McCall ↶ |
529 |
tracing::warn!("recording push to {}: {e}", parsed); |
| 8933faff Andy McCall ↶ |
530 |
} |
| 531 |
|
| 532 |
for (key, name, email, object, prev) in successors.iter().rev() { |
| 533 |
let Ok(None) = db::accounts::identity_by_key(&pool, key).await else { continue }; |
| 534 |
let Ok(Some(earlier)) = db::accounts::identity_by_key(&pool, prev).await else { continue }; |
| 535 |
match db::accounts::link_identity(&pool, earlier.account_id, key, name, email, object).await { |
| 536 |
Ok(()) => { |
| 537 |
tracing::info!("linked identity {} to account {} as a successor of {}", &key[..12], earlier.account_id, &prev[..12]); |
| 538 |
db::accounts::audit(&pool, None, "identity.rotated", key, &format!("successor of {}", &prev[..12])).await.ok(); |
| 539 |
} |
| 540 |
Err(e) => tracing::warn!("linking successor identity {}: {e}", &key[..12]), |
| 541 |
} |
| d23464c0 Andy McCall ↶ |
542 |
} |
| 166f6e9b Andy McCall |
543 |
|
| 544 |
|
| 545 |
if row.is_drop() { |
| 546 |
return; |
| 547 |
} |
| 18e381ea Andy McCall ↶ |
548 |
let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()); |
| 549 |
if let Some(app) = app { |
| acc85ac0 Andy McCall ↶ |
550 |
|
| 551 |
|
| 552 |
|
| 553 |
if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) { |
| 554 |
let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default(); |
| 555 |
let old = r.old_state.clone(); |
| 556 |
let old_heads = crate::repos::RepoName::new(&row.owner, &row.name); |
| 557 |
let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) { |
| 558 |
(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(), |
| 559 |
_ => Vec::new(), |
| 560 |
}; |
| 561 |
crate::web::issues::close_from_arrived(&app, &row, old_heads, new_heads, None).await; |
| 562 |
} else { |
| 563 |
crate::web::issues::close_from_messages(&app, &row, &messages).await; |
| 564 |
} |
| 78001759 Andy McCall ↶ |
565 |
let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 566 |
crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await; |
| 3f4b0ea6 Andy McCall ↶ |
567 |
if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) { |
| dd327d76 Andy McCall ↶ |
568 |
|
| 569 |
if crate::mirror::refresh(&app, &row).await.is_err() { |
| 570 |
crate::queue::queue_mirror(&app, row.id).await; |
| 571 |
} |
| 3f4b0ea6 Andy McCall ↶ |
572 |
} |
| b64e2537 Andy McCall ↶ |
573 |
let accepted: Vec<serde_json::Value> = results |
| 574 |
.iter() |
| 575 |
.filter(|r| r.ok) |
| 576 |
.map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes })) |
| 577 |
.collect(); |
| 578 |
if !accepted.is_empty() { |
| 579 |
let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 580 |
crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await; |
| 42fa9554 Andy McCall ↶ |
581 |
} |
| 582 |
let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 39442b00 Andy McCall ↶ |
583 |
|
| 584 |
|
| 585 |
if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) { |
| 586 |
let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect(); |
| 587 |
let warm = app.clone(); |
| 588 |
tokio::spawn(async move { |
| 589 |
warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok(); |
| 590 |
}); |
| 591 |
} |
| f677d627 Andy McCall ↶ |
592 |
crate::diffusion::after_push(&app, &row, &moved).await; |
| a7e6e8e6 Andy McCall ↶ |
593 |
|
| 594 |
|
| 595 |
|
| 596 |
if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) { |
| 597 |
let (app, row, heads) = (app.clone(), row.clone(), heads.clone()); |
| 598 |
tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await }); |
| 599 |
} |
| 42fa9554 Andy McCall ↶ |
600 |
for (bookmark, state, heads) in &moved { |
| 601 |
crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await; |
| ac713df7 Andy McCall ↶ |
602 |
crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await; |
| b64e2537 Andy McCall ↶ |
603 |
} |
| 18e381ea Andy McCall ↶ |
604 |
} |
| d23464c0 Andy McCall ↶ |
605 |
}); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
pub async fn dispatch( |
| 611 |
State(app): State<Shared>, |
| 612 |
Path((owner, name, endpoint)): Path<(String, String, String)>, |
| 613 |
method: Method, |
| 614 |
headers: HeaderMap, |
| 615 |
body: Bytes, |
| 616 |
) -> Response { |
| 617 |
let Some(endpoint) = Endpoint::parse(&endpoint) else { |
| 618 |
return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(); |
| 619 |
}; |
| 620 |
let Some(repo) = RepoName::new(&owner, &name) else { |
| 621 |
return not_found().into_response(); |
| 622 |
}; |
| 623 |
let _guard = match endpoint { |
| 624 |
Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await), |
| 625 |
_ => Guard::Read(app.repos.read_guard(&repo).await), |
| 626 |
}; |
| 85ec74fd Andy McCall ↶ |
627 |
let (name, received) = (endpoint.name(), body.len() as u64); |
| 628 |
let res = app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await; |
| 629 |
let sent = axum::body::HttpBody::size_hint(res.body()).exact().unwrap_or(0); |
| 630 |
crate::metrics::protocol_bytes(name, received, sent); |
| 631 |
res |
| 166f6e9b Andy McCall |
632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
pub async fn dispatch_drop( |
| 637 |
State(app): State<Shared>, |
| 638 |
Path((owner, id, endpoint)): Path<(String, String, String)>, |
| 639 |
method: Method, |
| 640 |
headers: HeaderMap, |
| 641 |
body: Bytes, |
| 642 |
) -> Response { |
| 643 |
let Some(name) = RepoName::drop(&owner, &id) else { |
| 644 |
return not_found().into_response(); |
| 645 |
}; |
| 646 |
dispatch(State(app), Path((name.owner, name.name, endpoint)), method, headers, body).await |
| d23464c0 Andy McCall ↶ |
647 |
} |
| 648 |
|
| 649 |
enum Guard { |
| 7c06f5b2 Andy McCall ↶ |
650 |
Read(#[allow(dead_code)] crate::repos::ReadGuard), |
| 651 |
Write(#[allow(dead_code)] crate::repos::WriteGuard), |
| d23464c0 Andy McCall ↶ |
652 |
} |