Skip to content

Commit

Permalink
[FIX] t2d: add ssh key to authorized_keys
Browse files Browse the repository at this point in the history
Since /home/odoo/.ssh is marked as a volume in the parent image, changes
to files in the directory during docker build are being lost when a new
container is created. Therefore the authorized_keys file is set directly
on the host and copied over to the container.
  • Loading branch information
antonag32 committed Jan 16, 2024
1 parent 5b3e4fe commit 9932a6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/travis2docker/templates/Dockerfile_deployv
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ RUN . /home/odoo/build.sh && \
configure_vim && \
configure_zsh && \
chown_all && \
set_authorized_keys && \
mv /entrypoint_image /deployv_entrypoint_image && \
mv /entry_point.py /deployv_entry_point.py && \
mkdir /run/sshd
Expand Down
20 changes: 0 additions & 20 deletions src/travis2docker/templates/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,6 @@ EOF
# alias odoo
}

set_authorized_keys(){
YELLOW='\033[0;33m'
NC='\033[0m'

AUTH_FILE="${HOME}/.ssh/authorized_keys"
ED_KEY="${HOME}/.ssh/id_ed25519.pub"
RSA_KEY="${HOME}/.ssh/id_rsa.pub"

echo "INFO: Adding public key to ~/.ssh/authorized_keys"
if [ -f "${ED_KEY}" ]; then
tee -a "${AUTH_FILE}" < "${ED_KEY}"
elif [ -f "${RSA_KEY}" ]; then
printf "${YELLOW}WARNING: RSA keys are deprecated, consider changing to ed25519\n${NC}"
tee -a "${AUTH_FILE}" < "${RSA_KEY}"
else
echo "INFO: No public key found. No key added to ~/.ssh/authorized_keys"
fi
}


# You can add new packages here
install_dev_tools(){
apt update -qq
Expand Down
22 changes: 22 additions & 0 deletions src/travis2docker/travis2docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def compute_dockerfile(self, skip_after_success=False):
copies = []
for copy_path, dest in self.copy_paths:
copies.append((self.copy_path(copy_path), dest))
self.set_authorized_key()
kwargs = {
'runs': [],
'copies': copies,
Expand Down Expand Up @@ -381,3 +382,24 @@ def copy_path(self, path):
else:
raise UserWarning("Just directory or file is supported to copy [%s]" % src)
return os.path.relpath(dest_path, self.curr_work_path)

def set_authorized_key(self):
ssh_dir = os.path.expanduser("~/.ssh")
ed_key = os.path.join(ssh_dir, "id_ed25519.pub")
rsa_key = os.path.join(ssh_dir, "id_rsa.pub")

to_copy = False
if os.path.isfile(ed_key):
to_copy = ed_key
elif os.path.isfile(rsa_key):
print("RSA keys are deprecated, consider changing to ed25519")
to_copy = rsa_key

if not to_copy:
print("No public key found. No key added to ~/.ssh/authorized_keys. SSH login won't work.")

with open(to_copy, "r", encoding="utf-8") as key_fd:
pub_key = key_fd.read()

with open(os.path.join(self.curr_work_path, ".ssh", "authorized_keys"), "w", encoding="utf-8") as auth_fd:
auth_fd.write(pub_key)

0 comments on commit 9932a6c

Please sign in to comment.