git clone https://garage44.eu/jeroen/midas.gitcartopolis-agent · 2026-11-28T12:20:18Z
feat(hosted): the forgejo_* tools answer for a hosted repository, and 2222's recipe A session's forgejo_branches, forgejo_api and the rest go through the hub's Forgejo proxy. For a repository the hub hosts, Forgejo's copy is stale from the import on, so the proxy answers itself: a branch list in Forgejo's shape, and for anything else a 410 naming project_read and project_set. With no forgejo_api in hub.conf it says the workspace has no Forgejo, and `midas check` takes that as fine rather than as a broken route. hosted::branches never listed a branch: for-each-ref writes a hex escape as %1f, where git log writes %x1f, so no line split and every one was dropped. docs/hub.md gets the recipe for port 2222 as the hub's: a second sshd for the user `git`, with Forgejo's host key, running each key as the hub's user.
docs/hub.md | 43 +++++++++++++++++++++++++++++++++++++++++++
src/check.rs | 2 ++
src/hosted.rs | 2 +-
src/hub.rs | 32 ++++++++++++++++++++++++++++++++
src/tests.rs | 16 ++++++++++++++++
5 files changed, 94 insertions(+), 1 deletion(-)
diff --git a/docs/hub.md b/docs/hub.md
index d6af962..fabdc8f 100644
--- a/docs/hub.md
+++ b/docs/hub.md
@@ -323,6 +323,49 @@ leaves Forgejo's copy as it was. The move around it, in order:
3. Point checkouts' `origin` at `midas@<hub>:<owner>/<name>.git` until port
2222 is the hub's (git-hosting.md, step 3), when the old URL works again.
+**The old address, `ssh://git@code.garage44.eu:2222/<owner>/<name>.git`**,
+is the hub's once Forgejo's own ssh server is off: a second sshd on 2222 lets
+in only the user `git`, with the hub's person and agent keys, and runs each as
+the hub's user. It presents Forgejo's host key, so no known_hosts changes.
+
+```
+# /etc/midas/sshd_git.conf
+Port 2222
+HostKey /etc/midas/git_host_rsa # Forgejo's data/ssh/gitea.rsa, copied
+PidFile /run/midas-git-sshd.pid
+AllowUsers git
+UsePAM no
+PasswordAuthentication no
+KbdInteractiveAuthentication no
+AuthorizedKeysFile none
+AuthorizedKeysCommand /usr/local/libexec/midas-git-keys %u
+AuthorizedKeysCommandUser midas
+AcceptEnv GIT_PROTOCOL
+PermitTTY no
+AllowTcpForwarding no
+X11Forwarding no
+```
+
+```sh
+#!/bin/sh
+# /usr/local/libexec/midas-git-keys: the hub's key lines (never a node's), run as the hub's user
+[ "$1" = git ] || exit 0
+exec sed -n 's|^restrict,command="\([^"]*\) hub git --key \([A-Za-z0-9_-]*\)"|restrict,command="sudo -n -u midas \1 hub git --key \2"|p' \
+ /srv/midas/.ssh/authorized_keys
+```
+
+```
+# /etc/sudoers.d/midas-git
+Defaults:git env_keep += "SSH_ORIGINAL_COMMAND GIT_PROTOCOL"
+git ALL=(midas) NOPASSWD: /usr/local/bin/midas hub git --key *
+```
+
+and a unit running `/usr/sbin/sshd -D -f /etc/midas/sshd_git.conf`
+(`midas-git-sshd.service`). The `git` account must not be locked (`!` in
+shadow), or sshd refuses every key without PAM. LFS goes to the hub's own
+address (`http://10.44.0.1:4791/lfs`), so pushing LFS objects this way works on
+the WireGuard network only.
+
## 6. Hear about pushes
The hub learns that a branch moved when it next fetches. If the git server runs
diff --git a/src/check.rs b/src/check.rs
index 93018ec..21f45be 100644
--- a/src/check.rs
+++ b/src/check.rs
@@ -205,6 +205,8 @@ pub fn check(verbose: bool) -> Result<i32> {
};
match crate::mcp::forgejo_version() {
Ok(v) => r.check(true, &format!("Forgejo through the hub: {v}")),
+ // Every repository at the hub, and hub.conf names no Forgejo: nothing to reach.
+ Err(e) if format!("{e:#}").contains("has no Forgejo") => r.check(true, "no Forgejo: the hub hosts every repository"),
Err(e) => r.check(false, &format!("Forgejo through the hub: {e:#}")),
};
} else {
diff --git a/src/hosted.rs b/src/hosted.rs
index 11c86f4..8159d44 100644
--- a/src/hosted.rs
+++ b/src/hosted.rs
@@ -560,7 +560,7 @@ pub fn log(repo: &str, rev: Option<&str>, n: usize) -> Result<Vec<Value>> {
/// The branches, with their heads.
pub fn branches(repo: &str) -> Result<Vec<Value>> {
- let out = git(&path(repo), &["for-each-ref", "--format=%(refname:short)%x1f%(objectname)%x1f%(committerdate:iso-strict)", "refs/heads"])?;
+ let out = git(&path(repo), &["for-each-ref", "--format=%(refname:short)%1f%(objectname)%1f%(committerdate:iso-strict)", "refs/heads"])?;
Ok(out.lines().filter_map(|l| {
let f: Vec<&str> = l.split('\x1f').collect();
Some(json!({"name": f.first()?, "sha": f.get(1)?, "at": f.get(2)?}))
diff --git a/src/hub.rs b/src/hub.rs
index 30032ef..e889dc4 100644
--- a/src/hub.rs
+++ b/src/hub.rs
@@ -1923,7 +1923,19 @@ fn forgejo_proxy(
let mut buf = vec![0u8; len];
reader.read_exact(&mut buf)?;
let body = (len > 0).then(|| String::from_utf8_lossy(&buf).to_string());
+ // A repository the hub hosts is not Forgejo's to answer for, even while
+ // Forgejo keeps its old copy (hosted::import): its branches are read here, in
+ // Forgejo's shape, and anything else is pointed at what replaces it.
+ if let Some((code, v)) = forgejo_for_hosted(method, path) {
+ events::emit("forgejo_call", json!({"actor": name, "method": method, "path": path.split('?').next().unwrap_or(""),
+ "status": code, "peer": peer, "hosted": true}));
+ return respond(stream, code, "application/json", v.to_string().as_bytes());
+ }
let conf_api = crate::conf::hub().forgejo_api;
+ if conf_api.is_empty() && caller["api"].as_str().is_none() {
+ return respond(stream, 404, "application/json",
+ br#"{"message":"this workspace has no Forgejo: its repositories are the hub's (project_read, project_create)"}"#);
+ }
let api = caller["api"].as_str().unwrap_or(&conf_api);
let token = caller["token"].as_str().unwrap_or("");
let (code, text) = match crate::mcp::curl_forgejo(api, token, method, path, body.as_deref()) {
@@ -1936,6 +1948,26 @@ fn forgejo_proxy(
respond(stream, code, "application/json", text.as_bytes())
}
+/// The answer to a Forgejo API call about a repository the hub hosts, or none
+/// when the call is not about one.
+pub(crate) fn forgejo_for_hosted(method: &str, path: &str) -> Option<(u16, Value)> {
+ let rest = path.strip_prefix("/repos/")?;
+ let mut seg = rest.split(['/', '?']);
+ let (owner, name, what) = (seg.next()?, seg.next()?, seg.next().unwrap_or(""));
+ let repo = format!("{owner}/{name}");
+ if !crate::hosted::is_hosted(&repo) {
+ return None;
+ }
+ if method == "GET" && what == "branches" {
+ let list: Vec<Value> = crate::hosted::branches(&repo).ok()?.into_iter()
+ .map(|b| json!({"name": b["name"], "commit": {"id": b["sha"], "timestamp": b["at"]}})).collect();
+ return Some((200, json!(list)));
+ }
+ Some((410, json!({"message": format!(
+ "{repo} is hosted by midas, not Forgejo: read it with project_read (or `midas project show|log|branches`), \
+ change its landing with project_set; there are no pull requests or issues")})))
+}
+
/// A minimal HTTP client for the node side: the node asks one question of one
/// server, and pulling in a TLS-capable client for it would be all cost.
pub fn request(base: &str, method: &str, path: &str, body: Option<&str>) -> Result<(u16, String)> {
diff --git a/src/tests.rs b/src/tests.rs
index cb837f4..26d272e 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -3059,3 +3059,19 @@ fn a_forgejo_project_is_imported_with_its_branches_and_its_landing_but_not_its_d
assert_eq!(v["lfs_missing"], json!([format!("{:064}", 7)]));
assert!(!crate::hosted::is_public("acme/art") && crate::conf::hub().project("acme/art").unwrap()["gate"].is_null());
}
+
+#[test]
+fn forgejo_calls_about_a_hosted_repository_are_answered_by_the_hub() {
+ let _g = SERIAL.lock().unwrap();
+ scratch("hosted-forgejo");
+ new_project("here", false);
+ let (code, v) = crate::hub::forgejo_for_hosted("GET", "/repos/acme/here/branches?limit=50").unwrap();
+ assert_eq!(code, 200);
+ assert_eq!(v[0]["name"], "main");
+ assert_eq!(v[0]["commit"]["id"].as_str().unwrap().len(), 40);
+ let (code, v) = crate::hub::forgejo_for_hosted("POST", "/repos/acme/here/pulls").unwrap();
+ assert_eq!(code, 410);
+ assert!(v["message"].as_str().unwrap().contains("project_read"));
+ assert!(crate::hub::forgejo_for_hosted("GET", "/repos/acme/elsewhere/branches").is_none(), "Forgejo's own");
+ assert!(crate::hub::forgejo_for_hosted("GET", "/version").is_none());
+}