Skip to content

Commit

Permalink
Merge pull request #1 from haravich/SSH_PUBLIC_KEY
Browse files Browse the repository at this point in the history
Updated to for usages
  • Loading branch information
haravich committed Aug 14, 2023
2 parents b85b357 + dbc2d02 commit 092cb28
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: custom-ssh-server build

on:
schedule:
- cron: "0 0 * * 0"
push:
branches:
- "**"
Expand Down Expand Up @@ -33,6 +35,7 @@ jobs:
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ RUN apk update && \
# Copy the custom script, sshd_config, and host keys
COPY setup-ssh-user.sh /usr/local/bin/
COPY ssh-host-keys/* /etc/ssh/
COPY ssh-keys/*.pub /ssh-keys/
COPY sshd_config /etc/ssh/
RUN chmod +x /usr/local/bin/setup-ssh-user.sh

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ Before you begin, ensure you have the following installed:
```bash
docker build -t custom-ssh-server .
docker run -d -p 2222:22 -e SSH_USER=<desired_username> -e SSH_PASSWORD=<desired_password> custom-ssh-server
(or)
docker run -d -p 2222:22 -e SSH_USER=<desired_username> -e SSH_PUBLIC_KEY="<value_of_public_key> || $(cat /ssh-keys/*.pub)" custom-ssh-server
(or)
docker run -d -p 2222:22 -e SSH_USER=<desired_username> -e SSH_PASSWORD=<desired_password> -e SSH_PUBLIC_KEY="<value_of_public_key> | $(cat /ssh-keys/*.pub)" custom-ssh-server
```
Replace <desired_username> and <desired_password> with appropriate values.
Replace <desired_username>, <desired_password> and <value_of_public_key> with appropriate values.

## Usage

Expand Down
12 changes: 8 additions & 4 deletions setup-ssh-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
# Fetch username, password, and public key from environment variables
SSH_USER="${SSH_USER:-}"
SSH_PASSWORD="${SSH_PASSWORD:-}"
SSH_PUBLIC_KEY="$(cat /ssh-keys/*.pub)"
SSH_PUBLIC_KEY="${SSH_PUBLIC_KEY:-}"

# Check if both username, password and public key are provided
if [ -z "$SSH_USER" ] || [ -z "$SSH_PASSWORD" ] || [ -z "$SSH_PUBLIC_KEY" ]; then
echo "SSH_USER, SSH_PASSWORD and SSH_PUBLIC_KEY environment variables must be set."
if [ -z "$SSH_USER" ] || ([ -z "$SSH_PASSWORD" ] && [ -z "$SSH_PUBLIC_KEY" ]); then
echo "SSH_USER, SSH_PASSWORD or SSH_PUBLIC_KEY environment variables must be set."
exit 1
fi

# Create the user and set up password or public key authentication
adduser -D -s /bin/bash "$SSH_USER"
echo "$SSH_USER:$SSH_PASSWORD" | chpasswd

# User is activated here eventhough if SSH_PASSWORD is not set
# running this enables the user and can be used with key based auth
echo "$SSH_USER:$SSH_PASSWORD" | chpasswd

mkdir -p /home/"$SSH_USER"/.ssh
if [ -n "$SSH_PUBLIC_KEY" ]; then
echo "$SSH_PUBLIC_KEY" >> /home/"$SSH_USER"/.ssh/authorized_keys
Expand Down

0 comments on commit 092cb28

Please sign in to comment.