diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-17 23:46:32 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-17 23:46:32 +0800 |
commit | 3a178ebb7ea7270181abbdef95871f98f03c61d1 (patch) | |
tree | 7971fb9bd0925540cc7a58ed5d926af5e62bdc40 /ssh_handle_receive_pack.go | |
parent | remote_url.go: Don't use path.Join (it strips :// into :/) (diff) | |
download | forge-3a178ebb7ea7270181abbdef95871f98f03c61d1.tar.gz forge-3a178ebb7ea7270181abbdef95871f98f03c61d1.tar.zst forge-3a178ebb7ea7270181abbdef95871f98f03c61d1.zip |
hooks, etc.: Restructure concurrency and data flow
Previously we accepted handler connections at hooks_handle and used a
mess of channels and concurrent maps to let receive_pack handle the
session. This doesn't work well because there are conditions where a
push occurs but the hook is not called, e.g. when the destination branch
is up to date. There is no reliable way of checking whether the
subprocess is going to call the hook or not; it's technically possible
to parse stderr but that interface is not guaranteed to be stable and
IIRC has changed in the past). So receive_pack would be waiting on the
channel to receive a hooks connection to handle but it'll never receive
one, causing a deadlock. This entire thing was overengineered and was
very prone to error.
Here we let receive_pack put the cookie into the map, then start and
wait for the subprocess to finish. When the hook actually runs and
connects to its UNIX domain socket, the handler would check its cookie
within the map. If the hook doesn't run, then nothing happens. The
git-receive-pack subprocess blocks the execution of the SSH handler,
and when git-receive-pack exists, the SSH handler (using a defer)
deletes the cookie from the map.
There may be caveats in signal handling or other cases that cause the
cookie to be deleted from the map prematurely.
Diffstat (limited to 'ssh_handle_receive_pack.go')
-rw-r--r-- | ssh_handle_receive_pack.go | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/ssh_handle_receive_pack.go b/ssh_handle_receive_pack.go index 85655b1..c680011 100644 --- a/ssh_handle_receive_pack.go +++ b/ssh_handle_receive_pack.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "errors" "fmt" - "net" "os" "os/exec" @@ -14,13 +13,14 @@ import ( var err_unauthorized_push = errors.New("You are not authorized to push to this repository") -type hooks_cookie_deployer_return struct { - args []string - callback chan struct{} - conn net.Conn +type pack_to_hook_t struct { + session *glider_ssh.Session + pubkey string + direct_access bool + repo_path string } -var hooks_cookie_deployer = cmap.ComparableMap[string, chan hooks_cookie_deployer_return]{} +var pack_to_hook_by_cookie = cmap.Map[string, pack_to_hook_t]{} func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_identifier string) (err error) { repo_path, access, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) @@ -33,9 +33,13 @@ func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_ide fmt.Fprintln(session.Stderr(), "Error while generating cookie:", err) } - deployer_channel := make(chan hooks_cookie_deployer_return) - hooks_cookie_deployer.Store(cookie, deployer_channel) - defer hooks_cookie_deployer.Delete(cookie) + pack_to_hook_by_cookie.Store(cookie, pack_to_hook_t{ + session: &session, + pubkey: pubkey, + direct_access: access, + repo_path: repo_path, + }) + defer pack_to_hook_by_cookie.Delete(cookie) proc := exec.CommandContext(session.Context(), "git-receive-pack", repo_path) proc.Env = append(os.Environ(), @@ -52,17 +56,6 @@ func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_ide return err } - deployer := <-deployer_channel - - if access { - deployer.conn.Write([]byte{0}) - } else { - deployer.conn.Write([]byte{1}) - fmt.Fprintln(deployer.conn, "Hi! We don't support pushing from non-authorized users yet. This will be implemented soon.") - } - - deployer.callback <- struct{}{} - err = proc.Wait() if exitError, ok := err.(*exec.ExitError); ok { fmt.Fprintln(session.Stderr(), "Process exited with error", exitError.ExitCode()) |