Skip to content

Commit f04d5f6

Browse files
CR-19066 -- gerrit ssh (#62)
* bump version * add ability to set known host for ssh url with port * add home folder
1 parent 4c7e8fc commit f04d5f6

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

.ssh/config

Lines changed: 0 additions & 1 deletion
This file was deleted.

.ssh/known_hosts

Whitespace-only changes.

Dockerfile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ RUN apt-get install git-lfs && \
77
git lfs install
88

99
RUN apt-get update -y && apt-get install busybox -y && ln -s /bin/busybox /usr/bin/[[
10-
# add ssh record on which ssh key to use
11-
COPY ./.ssh/ /root/.ssh/
12-
13-
# add fingerprint for major git providers
14-
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
15-
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
1610

1711
COPY ./start.sh /run/start.sh
1812
RUN chmod +x /run/start.sh
1913

2014
# USER nodeuser
21-
RUN addgroup --gid 3000 nodegroup && \
22-
adduser --uid 3000 --ingroup nodegroup --shell /bin/sh --gecos "" --disabled-password nodeuser
15+
RUN addgroup --gid 3000 nodegroup \
16+
&& adduser --uid 3000 --home /home/nodeuser --ingroup nodegroup --shell /bin/sh --gecos "" --disabled-password nodeuser
2317
USER nodeuser
2418

2519
CMD ["/run/start.sh"]

service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 10.1.20
1+
version: 10.1.21

start.sh

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,47 @@ if [ "$USE_SSH" = "true" ]; then
8787

8888
[ -z "$PRIVATE_KEY" ] && (echo "missing PRIVATE_KEY var" | tee /dev/stderr) && exit 1
8989

90-
echo "$PRIVATE_KEY" > /root/.ssh/codefresh
90+
# it does not exist by default
91+
mkdir -p ~/.ssh
92+
# copy private key to a file
93+
echo "$PRIVATE_KEY" > ~/.ssh/codefresh
94+
# use this private key when using git with ssh
95+
echo "IdentityFile ~/.ssh/codefresh" > ~/.ssh/config
96+
97+
# set correct permissions for ssh agent
9198
chmod 700 ~/.ssh/
9299
chmod 600 ~/.ssh/*
93100

94101
# ssh://git@github.com:username/repo.git
95102
# match "github.com" from ssh uri
96-
REPO=${REPO#"ssh://"}
97-
SSH_HOST=$(echo "$REPO" | cut -d ":" -f 1 | cut -d "@" -f 2)
103+
SSH_REPO=${REPO#"ssh://"}
104+
105+
106+
# was: git@host:1234:username/repo.git
107+
# or: git@host:1234/repo.git
108+
# or: git@host:username/repo.git
109+
# became: `1234` (will be accepted by check)
110+
# or: `username` (will be skipped by check)
111+
SSH_PORT=$(echo "$SSH_REPO" | cut -d ":" -f 2 | cut -d "/" -f 1)
112+
113+
# we need to add port to ssh host in the known_hosts file
114+
# otherwise it will ask to add host to known_hosts
115+
# during git clone
116+
SSH_PORT_PARAM=
117+
SSH_PORT_LOG=''
118+
if [[ "$SSH_PORT" =~ ^[0-9]{1,5}$ ]]; then
119+
SSH_PORT_PARAM="-p $SSH_PORT"
120+
SSH_PORT_LOG=":$SSH_PORT"
121+
fi
122+
123+
# was: git@github.com:username/repo.git
124+
# became: github.com
125+
SSH_HOST=$(echo "$SSH_REPO" | cut -d ":" -f 1 | cut -d "@" -f 2)
98126

99-
echo "Adding "$SSH_HOST" to known_hosts"
127+
echo "Adding "$SSH_HOST$SSH_PORT_LOG" to known_hosts"
100128

101-
# removes all keys belonging to hostname from a known_hosts file
102-
ssh-keygen -R $SSH_HOST 2>/dev/null
103129
# skip stderr logs that start with '#'
104-
ssh-keyscan -H $SSH_HOST > ~/.ssh/known_hosts 2> >(grep -v '^#' >&2)
130+
ssh-keyscan $SSH_PORT_PARAM -H $SSH_HOST > ~/.ssh/known_hosts 2> >(grep -v '^#' >&2)
105131
fi
106132

107133
mkdir -p "$WORKING_DIRECTORY"

0 commit comments

Comments
 (0)