Skip to content

Commit f84e9de

Browse files
committed
Git Init Container for cloning #69
* create init container that can be used to checkout git repositories with https and ssh
1 parent e957794 commit f84e9de

File tree

8 files changed

+187
-2
lines changed

8 files changed

+187
-2
lines changed

.vscode/extensions.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"recommendations": [
3-
"hashicorp.terraform"
3+
"hashicorp.terraform",
4+
"ms-python.python"
45
]
5-
}
6+
}

doc/docs/Building-Internal.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,10 @@ Build and push the operator with:
6161
docker build --no-cache -t theiacloud/theia-cloud-operator:latest -f dockerfiles/operator/Dockerfile .
6262
docker push theiacloud/theia-cloud-operator:latest
6363
```
64+
65+
Build and pish the git-init container:
66+
67+
```bash
68+
docker build -t theiacloud/theia-cloud-git-init:latest -f dockerfiles/git-init/Dockerfile .
69+
docker push theiacloud/theia-cloud-git-init:latest
70+
```

dockerfiles/git-init/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM debian:11-slim
2+
3+
RUN apt update && \
4+
apt install python git -y && \
5+
apt clean
6+
7+
WORKDIR /tmp
8+
COPY python/git-init/entrypoint.sh .
9+
COPY python/git-init/ssh-keyscan.sh .
10+
COPY python/git-init/git-init.py .
11+
COPY python/git-init/git-askpw.py .
12+
13+
ENV GIT_ASKPASS=/tmp/git-askpw.py
14+
15+
ENTRYPOINT [ "/tmp/entrypoint.sh" ]
16+
CMD ["-h"]

python/git-init/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Git Init Container
2+
3+
## Scenarios
4+
5+
- HTTP(S)
6+
- No Auth
7+
- Ask for password only
8+
- Ask for username and password
9+
- SSH
10+
- No Auth
11+
- Ask for password
12+
13+
## Testing
14+
15+
### Build init container
16+
17+
```bash
18+
docker build -t theiacloud/theia-cloud-git-init:local -f dockerfiles/git-init/Dockerfile .
19+
```
20+
21+
### Generate Test SSH Key Pair
22+
23+
```bash
24+
# don't save in ~/.ssh/... but e.g. in ~/tmp/ssh/id_theiacloud
25+
ssh-keygen -t ed25519 -C "Test TC Git Init SSH Keypair"
26+
```
27+
28+
### Test Checkout
29+
30+
```bash
31+
# Adjust URLs and Password/PATs below
32+
# keep spaces in front to avoid command being added to bash history
33+
export HTTP_PUBLIC=https://github.com/eclipsesource/theia-cloud.git
34+
export HTTP_PRIVATE=https://gitlab.eclipse.org/username/my.repository.git
35+
export HTTP_PRIVATE_WITH_USERNAME=https://[email protected]/username/my.repository.git
36+
export HTTP_PRIVATE_WITH_USERNAME_AND_PASSWORD=https://username:[email protected]/username/my.repository.git
37+
export HTTP_USERNAME=username
38+
export HTTP_PASSWORD=pat
39+
export SSH_PASSWORD=sshpw
40+
export SSH_REPO="[email protected]:username/my.repository.git"
41+
42+
# HTTPS Public
43+
docker run --rm theiacloud/theia-cloud-git-init:local "$HTTP_PUBLIC" "/tmp/my-repo"
44+
45+
# HTTPS Private
46+
docker run --env GIT_PROMPT1=$HTTP_USERNAME --env GIT_PROMPT2=$HTTP_PASSWORD --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE" "/tmp/my-repo"
47+
48+
# HTTPS Private with Username
49+
docker run --env GIT_PROMPT1=$HTTP_PASSWORD --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE_WITH_USERNAME" "/tmp/my-repo"
50+
51+
# HTTPS Private with Username and Password
52+
docker run --rm theiacloud/theia-cloud-git-init:local "$HTTP_PRIVATE_WITH_USERNAME_AND_PASSWORD" "/tmp/my-repo"
53+
54+
# SSH
55+
docker run --env GIT_PROMPT1=$SSH_PASSWORD -v ~/tmp/ssh/:/etc/theia-cloud-ssh --rm theiacloud/theia-cloud-git-init:local "$SSH_REPO" "/tmp/my-repo"
56+
```

python/git-init/entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
eval `ssh-agent`
3+
mkdir $HOME/.ssh
4+
touch $HOME/.ssh/known_hosts
5+
[ -e /etc/theia-cloud-ssh/id_theiacloud ] && { sleep 1; echo $GIT_PROMPT1; } | script -q /dev/null -c 'ssh-add /etc/theia-cloud-ssh/id_theiacloud'
6+
python git-init.py "$@"

python/git-init/git-askpw.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
import os
3+
4+
path = "/tmp/theia-cloud-askpw"
5+
6+
if os.path.isfile(path):
7+
prompt2 = os.environ['GIT_PROMPT2']
8+
print(prompt2)
9+
else:
10+
prompt1 = os.environ['GIT_PROMPT1']
11+
print(prompt1)
12+
os.mknod(path)

python/git-init/git-init.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import subprocess
5+
import os
6+
import sys
7+
8+
debugLogging = False
9+
sshKey = "/etc/theia-cloud-ssh/id_theiacloud"
10+
NL = "\n"
11+
12+
13+
def runProcess(args):
14+
process = subprocess.Popen(
15+
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
16+
stdout, stderr = process.communicate()
17+
process.wait()
18+
out = stdout.decode('ascii')
19+
if len(out) > 0:
20+
sys.stdout.write(out + NL)
21+
if process.returncode != 0:
22+
sys.stderr.write(stderr.decode('ascii') + NL)
23+
return process.returncode
24+
25+
def getHostname(repository):
26+
# remove protocol, if any
27+
split = repository.split("://", 1)
28+
if len(split) == 1:
29+
repository = split[0]
30+
else:
31+
repository = split[1]
32+
if debugLogging:
33+
sys.stdout.write("getHostname 1: " + repository + NL)
34+
35+
# remove path, if any
36+
split = repository.split("/", 1)
37+
repository = split[0]
38+
if debugLogging:
39+
sys.stdout.write("getHostname 2: " + repository + NL)
40+
41+
# remove user information, if any
42+
split = repository.split("@", 1)
43+
if len(split) == 1:
44+
repository = split[0]
45+
else:
46+
repository = split[1]
47+
if debugLogging:
48+
sys.stdout.write("getHostname 3: " + repository + NL)
49+
50+
# remove trailing information, if any
51+
split = repository.split(":", 1)
52+
repository = split[0]
53+
if debugLogging:
54+
sys.stdout.write("getHostname 4: " + repository + NL)
55+
56+
return repository
57+
58+
parser = argparse.ArgumentParser()
59+
parser.add_argument("repository", help="The repository URL", type=str)
60+
parser.add_argument("directory", help="The directory to clone into", type=str)
61+
args = parser.parse_args()
62+
63+
# Set up git credential helper
64+
code = runProcess(["git", "config", "--global", "credential.helper", "store"])
65+
if code != 0:
66+
exit(code)
67+
68+
# Check if SSH key is available, if so prepare clone with SSH
69+
if os.path.isfile(sshKey):
70+
# Add know host
71+
code = runProcess(["/tmp/ssh-keyscan.sh", getHostname(args.repository)])
72+
if code != 0:
73+
exit(code)
74+
75+
if debugLogging:
76+
runProcess(["ssh-add", "-l"])
77+
runProcess(["cat", "/root/.ssh/known_hosts"])
78+
79+
# Clone repository
80+
code = runProcess(["git", "clone", args.repository, args.directory])
81+
if code != 0:
82+
exit(code)
83+
84+
if debugLogging:
85+
runProcess(["ls", "-al", args.directory])

python/git-init/ssh-keyscan.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
ssh-keyscan -H $@ >> /root/.ssh/known_hosts

0 commit comments

Comments
 (0)