[READ-ONLY] Mirror of https://github.com/fliphess/k8s-ssh-service. A docker container and helm chart for running an SSH service in kubernete
Files
README.md

Docker Image CI

ssh-service #

An SSH service container that can be used for tunneling and accessing a kubernetes platform without a bastion server.

This service can be used for debugging or providing access to a database and other required service, without the need of configuring a bastion host with all the requirements on itself. All configuration can be done by manipulating the settings of the helm chart rather than rebuilding the container.

Usage #

Installing #

To install clone this repo and, configure your values in a values.yaml file.

Then, use helm:

git@github.com:fliphess/k8s-ssh-service.git
cd k8s-ssh-service

helm install ./chart  --name ssh-service --values <your values.yaml>

Configurating users #

Users can't use password login, so instead SSH key authentication should be used. To create ssh keys, use ssh-keygen:

ssh-keygen -t ecdsa-sha2-nistp521 -c "user1@example.com" ssh-key-user1
ssh-keygen -t ecdsa-sha2-nistp521 -c "user2@example.com" ssh-key-user2

By adding users to the userConfig in the chart, users can be created. Create a user dict for each username:

userConfig:
  user-one:
    uid: 1001
    shell: /bin/bash
    ip_addresses:
    - 1.2.3.4/32
    - 5.6.7.8/32
    ssh_keys:
    - comment: "user1@example.com"
      key: "<the content of ssh-key-user1.pub>"
      type: ecdsa-sha2-nistp521

  user-two:
    uid: 1001
    shell: /bin/zsh
    ip_addresses:
    - 3.4.5.6/32
    - 9.8.7.6/32
    ssh_keys:
    - comment: "user2@example.com"
      key: "<the content of ssh-key-user2.pub>"
      type: ecdsa-sha2-nistp521

These users will be added in /var/lib/extrausers in the extra shadow and passwd file that are mounted in the container from a kubernetes secret.

SSHD Config #

The SSHD config in /etc/ssh/sshd_config is mounted in the container from a kubernetes ConfigMap that is generated by helm. To add or change the sshd daemon configuration you can adjust the sshdConfig mapping in the values.yaml of the chart.

SSH Authentication #

Through the sshd configuration that is mounted in the container from a configmap, the ssh daemon is configured to use the AuthorizedKeysCommand to execute a script that reads the user configuration and retrieves the authorized keys from a yaml file that is mounted in the container. this way users can be added by only updating the helm values file rather than updating files in the container itself.

SSH Host keys #

For a more secure use of this service it is recommended to configure the host keys for this service. If non given in the values.yaml, the host keys that are created on container build are used, which is not safe.

Instead, generate a set of host keys and add them to the values to ensure these are used inside the container:

sshHostKeys:
- name: ssh_host_dsa_key
  content: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    ...
    -----END OPENSSH PRIVATE KEY-----

- name: ssh_host_ecdsa_key
  content: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    ...
    -----END OPENSSH PRIVATE KEY-----

- name: ssh_host_ed25519_key
  content: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    ...
    -----END OPENSSH PRIVATE KEY-----

- name: ssh_host_rsa_key
  content: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    ...
    -----END OPENSSH PRIVATE KEY-----

Make sure to check periodically whether the host keys you are using are still considered safe.

To generate a set of new host keys, use ssh-keygen -A, or do this in the container:

mkdir hostkeys
docker pull ghcr.io/fliphess/k8s-ssh-service:latest
docker run -v $(pwd)/hostkeys:/etc/ssh ghcr.io/fliphess/k8s-ssh-service ssh-keygen -A

TCP Wrappers #

TCP Wrappers are configured to be used. Each user can have some ip addresses that will be added to the /etc/hosts.allow file that is mounted from a configmap in the container.

By configuring a single 0.0.0.0/0 for any of the users, this is effective for all users so there is no use to use strict ip's for one user and leave it all open for another user. Using any rules like 0.0.0.0/0 is discouraged.

Motd #

To show a message on login, configure the motdContent variable in the helm chart. The content will be mounted from a ConfigMap in the container as /etc/motd.

Homedir #

As there is no static volume used for this service, all files that are created in the homedir are gone when the pod is restarted or when the service is updated or moved to another worker nodel. Therefor it is not possible to manually create configuration files like a .profile or a .bashrc`.

To work around this, you can fork this service and move some of your favorite configuration files to config/users/<username>.

At login time, a pam module triggers the execution of the /usr/local/bin/create-home script that checks if there are known configuration files for the user that just logged in. If there are files found inside the container in /etc/users/<username>, the files are copied to the homedir of said user prior to spawning the login shell.

This way a homedir is created prior to logging in.

If no configuration files are found for this user, the default configuration in /etc/users/default is copied in place and given the correct file permissions

It is not recommended to create or download large files inside the container. This is a mechanism for configuring your shell, not for filling up your homedir.