Skip to content

Commit

Permalink
Add unix domain socket support to HTTP transport.
Browse files Browse the repository at this point in the history
This changeset introduces an `http.unixSocket` option so that users can
proxy their git over HTTP remotes to a unix domain socket. In terms of
why, since UDS are local and git already has a local protocol: some
corporate environments use a UDS to proxy requests to internal resources
(ie. source control), so this change would support those use-cases. This
proxy can occasionally be necessary to attach MFA tokens or client
certificates for CLI tools.

The implementation leverages `--unix-socket` option [0] via the
`CURLOPT_UNIX_SOCKET_PATH` flag available with libcurl [1].

I chose to keep `GIT_CURL_HAVE_CURLOPT_UNIX_SOCKET_PATH` and
`NO_UNIX_SOCKETS` separate so that we can spit out better error messages
for users if git was compiled with `NO_UNIX_SOCKETS`.

[0] https://curl.se/docs/manpage.html#--unix-socket
[1] https://curl.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html

Signed-off-by: Leslie Cheng <[email protected]>
  • Loading branch information
lcfyi committed Feb 21, 2024
1 parent 3e0d3cd commit ff2e060
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Documentation/config/http.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ http.followRedirects::
the base for the follow-up requests, this is generally
sufficient. The default is `initial`.

http.unixSocket::
Connect through this Unix domain socket via HTTP, instead of using the
network. If set, this config takes precendence over `http.proxy` and
is incompatible with the proxy options (see `curl(1)`).

http.<url>.*::
Any of the http.* options above can be applied selectively to some URLs.
For a config key to match a URL, each element of the config key is
Expand Down
7 changes: 7 additions & 0 deletions git-curl-compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
#define GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH 1
#endif

/**
* CURLOPT_UNIX_SOCKET_PATH was added in 7.40.0, released in January 2015.
*/
#if LIBCURL_VERSION_NUM >= 0x074000
#define GIT_CURL_HAVE_CURLOPT_UNIX_SOCKET_PATH 1
#endif

/**
* CURL_HTTP_VERSION_2 was added in 7.43.0, released in June 2015.
*
Expand Down
23 changes: 23 additions & 0 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ static const char *http_proxy_ssl_ca_info;
static struct credential proxy_cert_auth = CREDENTIAL_INIT;
static int proxy_ssl_cert_password_required;

#if defined(GIT_CURL_HAVE_CURLOPT_UNIX_SOCKET_PATH) && !defined(NO_UNIX_SOCKETS)
static const char *curl_unix_socket_path;
#endif
static struct {
const char *name;
long curlauth_param;
Expand Down Expand Up @@ -455,6 +458,20 @@ static int http_options(const char *var, const char *value,
return 0;
}

if (!strcmp("http.unixsocket", var)) {
#ifdef GIT_CURL_HAVE_CURLOPT_UNIX_SOCKET_PATH
#ifndef NO_UNIX_SOCKETS
return git_config_string(&curl_unix_socket_path, var, value);
#else
warning(_("Unix socket support unavailable in this build of Git"));
return 0;
#endif
#else
warning(_("Unix socket support is not supported with cURL < 7.40.0"));
return 0;
#endif
}

if (!strcmp("http.cookiefile", var))
return git_config_pathname(&curl_cookie_file, var, value);
if (!strcmp("http.savecookies", var)) {
Expand Down Expand Up @@ -1203,6 +1220,12 @@ static CURL *get_curl_handle(void)
}
init_curl_proxy_auth(result);

#if defined(GIT_CURL_HAVE_CURLOPT_UNIX_SOCKET_PATH) && !defined(NO_UNIX_SOCKETS)
if (curl_unix_socket_path) {
curl_easy_setopt(result, CURLOPT_UNIX_SOCKET_PATH, curl_unix_socket_path);
}
#endif

set_curl_keepalive(result);

return result;
Expand Down
80 changes: 80 additions & 0 deletions t/t5565-http-unix-domain-socket.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/sh

test_description="test fetching through http via unix domain socket"

. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-httpd.sh

test -z "$NO_UNIX_SOCKETS" || {
skip_all='skipping http-unix-socket tests, unix sockets not available'
test_done
}

UDS_TO_TCP_FIFO=uds_to_tcp
TCP_TO_UDS_FIFO=tcp_to_uds
UDS_PID=
TCP_PID=
UDS_SOCKET="$(pwd)/uds.sock"
UNRESOLVABLE_ENDPOINT=http://localhost:4242

start_proxy_unix_to_tcp() {
local socket_path="$UDS_SOCKET"
local host=127.0.0.1
local port=$LIB_HTTPD_PORT

rm -f "$UDS_TO_TCP_FIFO"
rm -f "$TCP_TO_UDS_FIFO"
rm -f "$socket_path"
mkfifo "$UDS_TO_TCP_FIFO"
mkfifo "$TCP_TO_UDS_FIFO"
nc -klU "$socket_path" <tcp_to_uds >uds_to_tcp &
UDS_PID=$!

nc "$host" "$port" >tcp_to_uds <uds_to_tcp &
TCP_PID=$!

test_atexit 'stop_proxy_unix_to_tcp'
}

stop_proxy_unix_to_tcp() {
kill "$UDS_PID"
kill "$TCP_PID"
rm -f "$UDS_TO_TCP_FIFO"
rm -f "$TCP_TO_UDS_FIFO"
}

start_httpd
start_proxy_unix_to_tcp

test_expect_success 'setup repository' '
test_commit foo &&
git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
git push --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git"
'

# sanity check that we can't clone normally
test_expect_success 'cloning without UDS fails' '
test_must_fail git clone "$UNRESOLVABLE_ENDPOINT/smart/repo.git" clone
'

test_expect_success 'cloning with UDS succeeds' '
test_when_finished "rm -rf clone" &&
test_config_global http.unixsocket "$UDS_SOCKET" &&
git clone "$UNRESOLVABLE_ENDPOINT/smart/repo.git" clone
'

test_expect_success 'cloning with a non-existent http proxy fails' '
git clone $HTTPD_URL/smart/repo.git clone &&
rm -rf clone &&
test_config_global http.proxy 127.0.0.1:0 &&
test_must_fail git clone $HTTPD_URL/smart/repo.git clone
'

test_expect_success 'UDS socket takes precedence over http proxy' '
test_when_finished "rm -rf clone" &&
test_config_global http.proxy 127.0.0.1:0 &&
test_config_global http.unixsocket "$UDS_SOCKET" &&
git clone $HTTPD_URL/smart/repo.git clone
'

test_done

0 comments on commit ff2e060

Please sign in to comment.