Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git urls are incorrectly transformed from relative to absolute (unable to clone git repo from server when the repo is relative to the git user's home dir) #4322

Closed
alexcb opened this issue Oct 11, 2023 · 3 comments · Fixed by #4326
Labels

Comments

@alexcb
Copy link
Collaborator

alexcb commented Oct 11, 2023

#4142 introduced a regression, which makes it impossible to access a git repo from a ssh server that is configured to store the hosted git repos out of a user's home directory.

Here's a reproduction case:

First setup a git repo on a server:

$ ssh alex@192.168.0.2
Welcome to Ubuntu 21.04 (GNU/Linux 5.11.0-1027-raspi aarch64)
...
alex@coho:~$ mkdir repo.git
alex@coho:~$ cd repo.git
alex@coho:~/repo.git$ git init --bare .
...
Initialized empty Git repository in /home/alex/repo.git/
alex@coho:~/repo.git$ exit
logout
Connection to 192.168.0.2 closed.

Then add something to it:

alex@surfperch:/tmp$ git clone alex@192.168.0.2:repo.git
Cloning into 'repo'...
warning: You appear to have cloned an empty repository.
alex@surfperch:/tmp$ cd repo/
alex@surfperch:/tmp/repo$ echo -e "FROM alpine\nRUN echo hello"
FROM alpine
RUN echo hello
alex@surfperch:/tmp/repo$ echo -e "FROM alpine\nRUN echo hello" > Dockerfile
alex@surfperch:/tmp/repo$ git add Dockerfile 
alex@surfperch:/tmp/repo$ git commit -m 'my initial commit'
[master (root-commit) b696fd5] my initial commit
 Author: Alex Couture-Beil <alex@earthly.dev>
 1 file changed, 2 insertions(+)
 create mode 100644 Dockerfile
alex@surfperch:/tmp/repo$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 773 bytes | 773.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.0.2:repo.git
 * [new branch]      master -> master

This works under docker (which might be an older copy without #4142)

alex@surfperch:~/gh/earthly/earthly$ DOCKER_BUILDKIT=1 docker build alex@192.168.0.2:repo.git
[+] Building 13.0s (5/5) FINISHED                                                                                                                   docker:default
 => [internal] load git source alex@192.168.0.2:repo.git                                                                                                     12.7s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                              0.0s
 => CACHED [1/2] FROM docker.io/library/alpine                                                                                                                0.0s
 => [2/2] RUN echo hello                                                                                                                                      0.3s
 => exporting to image                                                                                                                                        0.0s 
 => => exporting layers                                                                                                                                       0.0s 
 => => writing image sha256:b73f8eb80a7ed8374984c53627794db5dba9716a9cb0dae66e282cdc004185fc                                                                  0.0s 
alex@surfperch:~/gh/earthly/earthly$ docker --version
Docker version 24.0.5, build ced0996

However when I use the latest version of buildkit (which includes #4142), it fails:

$ sudo -E ./buildctl build --ssh=default --frontend=dockerfile.v0 --opt source=docker/dockerfile --opt context=alex@192.168.0.2:repo.git
[+] Building 3.1s (1/1) FINISHED                                                                                                                                                                                                                     
 => ERROR [internal] load git source alex@192.168.0.2:repo.git                                                                                                                                                                                  3.0s
------                                                                                                                                                                                                                                               
 > [internal] load git source alex@192.168.0.2:repo.git:
2.991 fatal: '/repo.git' does not appear to be a git repository
2.993 fatal: Could not read from remote repository.
2.993 
2.993 Please make sure you have the correct access rights
2.993 and the repository exists.
------
error: failed to solve: failed to read dockerfile: failed to load cache key: error fetching default branch for repository ssh://xxxxx@192.168.0.2/repo.git: git error: exit status 128
stderr:
fatal: '/repo.git' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

This is because buildkit has incorrectly transformed alex@192.168.0.2:repo.git into ssh://alex@192.168.0.2/repo.git.

These two git URLs are not the same, the former is relative to my home director, and the later is absolute to the / filesystem.

@alexcb
Copy link
Collaborator Author

alexcb commented Oct 11, 2023

cc @jedevc

@jedevc
Copy link
Member

jedevc commented Oct 11, 2023

So does that imply you cannot construct a URL that has a home directory set up with git, as in this issue, that has a custom port number as in #4069?

@alexcb
Copy link
Collaborator Author

alexcb commented Oct 11, 2023

you can embed a ~ in the ssh:// form, which would then allow you to use a custom port:

e.g. this works on my server (Ubuntu with OpenSSH)

git clone ssh://alex@192.168.0.2:22/~/repo.git

unfortunately you can't assume all git hosts (e.g. github), support repos with a path like that.

e.g. this does not work:

$ git clone ssh://git@github.com:22/~/moby/buildkit.git
Cloning into 'buildkit'...
fatal: remote error: 
  is not a valid repository name
Visit https://support.github.com/ for help

Ultimately if a user gives you an implicit git url, I think it needs to kept in it's implicit form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants