enabling ssh to gitea #47

Merged
ben merged 3 commits from gitssh into main 2022-11-12 11:54:31 +00:00
13 changed files with 138 additions and 40 deletions

1
.gitignore vendored
View File

@ -95,3 +95,4 @@ playbooks/
/vaultwarden.yml
/mirrors.yml
/lb.yml
/gitea-proxy.yml

View File

@ -24,7 +24,7 @@ echo " roles:" >> private/playbooks/$1.yml
echo " - $1" >> private/playbooks/$1.yml
ln -s private/playbooks/$1.yml .
echo "/${1}.yml\n" >> .gitignore
echo "/${1}.yml" >> .gitignore
(
cd private/

View File

@ -0,0 +1,5 @@
---
- name: update known_hosts
command: "/usr/local/bin/update_known_hosts.sh"
become_user: "{{ gitea_user.username }}"

View File

@ -0,0 +1,55 @@
---
- name: template gitea config in sshd_config.d
template:
src: gitea.conf.j2
dest: /etc/ssh/sshd_config.d/gitea.conf
owner: root
group: root
mode: '0644'
tags:
- sshd
- gitea
notify:
# - reload ssh
- restart ssh
- name: copy the ssh keys used for the ssh proxy (gitea manages authorized_keys)
copy:
src: "private/gitea/{{ item.name }}"
dest: "{{ gitea_user.home }}/.ssh/{{ item.name }}"
mode: "{{ item.mode }}"
owner: "{{ gitea_user.username }}"
group: "{{ gitea_user.username }}"
no_log: true
with_items:
- name: id_rsa
mode: "0600"
- name: id_rsa.pub
mode: "0644"
tags:
- sshd
- gitea
- name: template scripts for ssh proxy to gitea
template:
src: "{{ item.name }}.j2"
dest: "/usr/local/bin/{{ item.name }}"
owner: "{{ item.owner }}"
group: "{{ item.owner }}"
mode: "{{ item.mode }}"
loop_control:
label: "{{ item.name }}"
with_items:
- name: update_known_hosts.sh
owner: "{{ gitea_user.username }}"
mode: '0750'
- name: gitea
owner: root
mode: '0755'
tags:
- sshd
- gitea
notify:
- update known_hosts
- restart ssh

View File

@ -0,0 +1,3 @@
---
- import_tasks: gitea-proxy.yml
tags: gitea-proxy

View File

@ -0,0 +1,14 @@
# {{ ansible_managed }}
Match User {{ gitea_user.username }}
AuthorizedKeysCommandUser {{ gitea_user.username }}
# this sshes to the gitea container, where it runs 'gitea keys': https://docs.gitea.io/en-us/command-line/#keys
# that generates an ssh authorized_file output, which the users key is checked against
AuthorizedKeysCommand /usr/bin/ssh -p {{ gitea_ssh_port }} {{ gitea_user.username }}@{{ gitea_host }} /usr/local/bin/gitea keys -e {{ gitea_user.username }} -u %u -t %t -k %k
# in the authorized_keys output from gitea, userse keys are prefixed with command="/usr/local/bin/gitea...", which needs
# to exist both inside of the container (its the path to the gitea binary), and on this system, where it is a wrapper
# script that ssh's to ssh on the gitea container.
#
# see: templates/gitea.j2

View File

@ -0,0 +1,3 @@
#!/bin/bash
# {{ ansible_managed }}
/usr/bin/ssh -p {{ gitea_ssh_port }} {{ gitea_user.username }}@{{ gitea_host }} "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"

View File

@ -0,0 +1,27 @@
#!/bin/bash
# {{ ansible_managed }}
set -e
{% set known_hosts = gitea_user.home + "/.ssh/known_hosts" -%}
# remove keys for {{ gitea_host }}:{{ gitea_ssh_port }}
ssh-keygen -R [{{ gitea_host }}]:{{ gitea_ssh_port }} || true
rm -v {{ known_hosts }} || true
touch {{ known_hosts }}
# add to known_hosts
# not piping stderr (2>&1) because that tends to corrupt the file
{% for item in ["rsa", "ecdsa", "ed22519"] -%}
# ssh-keyscan -t rsa -p {{ gitea_ssh_port }} {{ gitea_host }} >> {{ known_hosts }}
{% endfor %}
# hashed hostnames
ssh-keyscan -H -p {{ gitea_ssh_port }} {{ gitea_host }} >> {{ known_hosts }}
# hashed ip
IPADDR=$(host {{ gitea_host }} | awk '{print $4}')
ssh-keyscan -H -p {{ gitea_ssh_port }} ${IPADDR} >> {{ known_hosts }}
chmod 0600 {{ known_hosts }}
chown {{ gitea_user.uid }}:{{ gitea_user.gid }} {{ known_hosts }}

View File

@ -55,32 +55,6 @@
tags:
- gitea-mirror
- name: template ssh passthrough script
template:
src: ssh-passthrough.j2
dest: /usr/local/bin/gitea
mode: 0755
owner: git
group: git
when: gitea_ssh_enabled
tags:
- gitea-mirror
- name: copy the ssh keys used for the ssh shim (gitea manages authorized_keys)
copy:
src: "private/gitea/{{ item.name }}"
dest: "{{ gitea_user.home }}/.ssh/{{ item.name }}"
mode: "{{ item.mode }}"
owner: "{{ gitea_user.username }}"
group: "{{ gitea_user.username }}"
no_log: true
when: gitea_ssh_enabled
with_items:
- name: id_rsa
mode: "0600"
- name: id_rsa.pub
mode: "0644"
- name: template config
template:
src: app.ini.j2
@ -177,7 +151,7 @@
state: "{{ container_state | default('started') }}"
container_default_behavior: compatibility
ports:
- "127.0.0.1:{{ gitea_ssh_port }}:22"
- "{{ gitea_ssh_port }}:22"
volumes:
- "{{ gitea_user.home }}/data:/data"
- "{{ gitea_user.home }}/.ssh/:/data/git/.ssh"

View File

@ -1 +0,0 @@
ssh -o StrictHostKeyChecking=no git@{{ bridgewithdns.gitea }} "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"

6
roles/lb/meta/main.yml Normal file
View File

@ -0,0 +1,6 @@
---
dependencies:
- sshd
- gitea-proxy
- haproxy

View File

@ -4,3 +4,8 @@
service:
name: ssh
state: reloaded
- name: restart ssh
service:
name: ssh
state: restarted

View File

@ -6,17 +6,11 @@
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
#Port 22
Port {{ sshd_port | default('22') }}
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
ListenAddress {{ sshd_listen_addr4 | default('0.0.0.0') }}
#ListenAddress {{ sshd_listen_addr6 | default('::') }}
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
@ -57,7 +51,6 @@ PermitRootLogin {{ sshd_permit_root_login | default('yes') }}
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
@ -122,4 +115,17 @@ Subsystem sftp /usr/lib/openssh/sftp-server
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
PasswordAuthentication no
# the include statement does not work as you would expect. theres a bug that makes Match rules
# included files igored:
# https://unix.stackexchange.com/questions/603224/sshd-config-using-a-match-statement-inside-an-included-file
#
# but the workaround proposed here seems to work (add "Match all" befure the "Include" statement to prevent
# the included match statements form being interpreted as parts of other statements):
# https://serverfault.com/a/1106224
#
# it also works to place the "Include" statment at the top of the file with this workaround.
Match all
Include /etc/ssh/sshd_config.d/*.conf