Skip to content

Commit 8fa1e4f

Browse files
committed
Merge branch 'dev' into 'master'
Add configuration via ENV and general improvements See merge request docker/bastion!4
2 parents 789843c + 3ffa2d3 commit 8fa1e4f

File tree

5 files changed

+131
-24
lines changed

5 files changed

+131
-24
lines changed

Dockerfile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.8
1+
FROM alpine:3.9
22

33
LABEL maintainer="Mark <mark.binlab@gmail.com>"
44

@@ -9,22 +9,25 @@ ARG GROUP=bastion
99
ARG UID=4096
1010
ARG GID=4096
1111

12+
ENV HOST_KEYS_PATH_PREFIX="/usr"
13+
ENV HOST_KEYS_PATH="${HOST_KEYS_PATH_PREFIX}/etc/ssh"
14+
15+
COPY bastion /usr/sbin/bastion
16+
1217
RUN addgroup -S -g ${GID} ${GROUP} \
1318
&& adduser -D -h ${HOME} -s /bin/ash -g "${USER} service" \
1419
-u ${UID} -G ${GROUP} ${USER} \
1520
&& sed -i "s/${USER}:!/${USER}:*/g" /etc/shadow \
1621
&& set -x \
17-
&& apk add --no-cache openssh-server
22+
&& apk add --no-cache openssh-server \
23+
&& echo "Welcome to Bastion!" > /etc/motd \
24+
&& chmod +x /usr/sbin/bastion \
25+
&& mkdir -p ${HOST_KEYS_PATH} \
26+
&& mkdir /etc/ssh/auth_principals \
27+
&& echo "bastion" > /etc/ssh/auth_principals/bastion
1828

1929
EXPOSE 22/tcp
2030

21-
VOLUME /etc/ssh
31+
VOLUME ${HOST_KEYS_PATH}
2232

23-
CMD /usr/bin/ssh-keygen -A \
24-
&& /usr/sbin/sshd -D -e -4 \
25-
-o AuthorizedKeysFile=authorized_keys \
26-
-o PubkeyAuthentication=yes \
27-
-o PasswordAuthentication=no \
28-
-o PermitEmptyPasswords=no \
29-
-o PermitRootLogin=no \
30-
-o GatewayPorts=yes
33+
ENTRYPOINT ["bastion"]

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and usually involves access from untrusted networks orcomputers.
1111

1212
---
1313

14-
![AWS Bastion](https://dmhnzl5mp9mj6.cloudfront.net/security_awsblog/images/NM_diagram_061316_a.png)
14+
![AWS Bastion](docs/bastion_host.png)
1515

1616
## Useful cases
1717

@@ -22,6 +22,24 @@ behind a `NAT`. This image based on `Alpine Linux` last version.
2222

2323
## Usage
2424

25+
### Describing ENV variables
26+
27+
* `PUBKEY_AUTHENTICATION [true | false]` - Specifies whether public key authentication is allowed. The default is `true`. Note that this option applies to protocol version 2 only.
28+
29+
* `AUTHORIZED_KEYS [/relative/or/not/path/to/file]` - Specifies the file that contains the public keys that can be used for user authentication. `AUTHORIZED_KEYS` may contain tokens of the form `%T` which are substituted during connection setup. The following tokens are defined: `%%` is replaced by a literal `%`, `%h` is replaced by the home directory of the user being authenticated, and `%u` is replaced by the username of that user. After expansion, `AUTHORIZED_KEYS` is taken to be an absolute path or one relative to the user's home directory. The default file is `authorized_keys` and the default home directory is `/var/lib/bastion` and should be present by Docker volume mount by `-v $PWD/authorized_keys:/var/lib/bastion/authorized_keys:ro`.
30+
31+
* `TRUSTED_USER_CA_KEYS [/full/path/to/file]` - Specifies a file containing public keys of certificate authorities that are trusted to sign user certificates for authentication, or none to not use one. Keys are listed one per line; empty lines and comments starting with `#` are allowed. If a certificate is presented for authentication and has its signing CA key listed in this file, then it may be used for authentication for any user listed in the certificate's principals list. Note that certificates that lack a list of principals will not be permitted for authentication using `TRUSTED_USER_CA_KEYS`. Directive `AuthorizedPrincipalsFile` hardcoded to `/etc/ssh/auth_principals/%u` and in time of build and generated one principals file for presented user - `/etc/ssh/auth_principals/bastion` with the one row `bastion`, and this principal should be listed in the certificate's principals list.
32+
33+
* `GATEWAY_PORTS [true | false]` - Specifies whether remote hosts are allowed to connect to ports forwarded for the client. By default, `sshd` binds remote port forwardings to the loopback address. This prevents other remote hosts from connecting to forwarded ports. `GATEWAY_PORTS` can be used to specify that `sshd` should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect. The argument may be `false` to force remote port forwardings to be available to the local host only, `true` to force remote port forwardings to bind to the wildcard address. The default is `false`.
34+
35+
* `PERMIT_TUNNEL [true | false]` - Specifies whether `tun` device forwarding is allowed. The argument must be `true` or `false`. Specifying `true` permits both `point-to-point` (layer 3) and `ethernet` (layer 2). The default is `false`.
36+
37+
* `X11_FORWARDING [true | false]` - Specifies whether `X11` forwarding is permitted. The argument must be `true` or `false`. The default is `false`.
38+
39+
* `TCP_FORWARDING [true | false]` - Specifies whether `TCP` forwarding is permitted. The default is `true`. Note that disabling `TCP` forwarding does not improve security unless users are also denied shell access, as they can always install their own forwarders.
40+
41+
* `AGENT_FORWARDING [true | false]` - Specifies whether `ssh-agent` forwarding is permitted. The default is `true`. Note that disabling agent forwarding does not improve security unless users are also denied shell access, as they can always install their own forwarders.
42+
2543
### Run Bastion and `expose` port `22222` to outside a host machine
2644

2745
The container assumes your `authorized_keys` file with `644` permissions and mounted under `/var/lib/bastion/authorized_keys`.
@@ -34,17 +52,23 @@ $ docker run -d \
3452
--name bastion \
3553
--hostname bastion \
3654
--restart unless-stopped \
37-
-v ./.bastion_keys:/var/lib/bastion/authorized_keys:ro \
38-
-v bastion:/etc/ssh:rw
39-
--add-host docker-host:172.17.0.1
55+
-v $PWD/authorized_keys:/var/lib/bastion/authorized_keys:ro \
56+
-v bastion:/usr/etc/ssh:rw \
57+
--add-host docker-host:172.17.0.1 \
4058
-p 22222:22/tcp \
59+
-e "PUBKEY_AUTHENTICATION=true" \
60+
-e "GATEWAY_PORTS=false" \
61+
-e "PERMIT_TUNNEL=false" \
62+
-e "X11_FORWARDING=false" \
63+
-e "TCP_FORWARDING=true" \
64+
-e "AGENT_FORWARDING=true" \
4165
binlab/bastion
4266
```
4367

4468
Docker-compose example:
4569

4670
```yaml
47-
version: '3.3'
71+
version: "3.6"
4872
services:
4973
bastion:
5074
image: binlab/bastion
@@ -55,9 +79,16 @@ services:
5579
- 22/tcp
5680
ports:
5781
- 22222:22/tcp
82+
environment:
83+
PUBKEY_AUTHENTICATION: "true"
84+
GATEWAY_PORTS: "false"
85+
PERMIT_TUNNEL: "false"
86+
X11_FORWARDING: "false"
87+
TCP_FORWARDING: "true"
88+
AGENT_FORWARDING: "true"
5889
volumes:
59-
- ./.bastion_keys:/var/lib/bastion/authorized_keys:ro
60-
- bastion:/etc/ssh:rw
90+
- $PWD/authorized_keys:/var/lib/bastion/authorized_keys:ro
91+
- bastion:/usr/etc/ssh:rw
6192
extra_hosts:
6293
- docker-host:172.17.0.1
6394
networks:
@@ -101,7 +132,7 @@ $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f $HOME/.ssh/id_rsa
101132
* Add `rsa` public key to `.bastion_keys` file
102133

103134
```shell
104-
$ cat $HOME/.ssh/id_rsa.pub > ./.bastion_keys
135+
$ cat $HOME/.ssh/id_rsa.pub > $PWD/.bastion_keys
105136
```
106137

107138
* Run [`docker-compose.yml`](docker-compose.yml) configuration - `bastion` & `docker-ssh`

bastion

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env sh
2+
3+
HOST_KEYS_PATH_PREFIX="${HOST_KEYS_PATH_PREFIX:='/'}"
4+
HOST_KEYS_PATH="${HOST_KEYS_PATH:='/etc/ssh'}"
5+
6+
if [ "$PUBKEY_AUTHENTICATION" == "false" ]; then
7+
CONFIG_PUBKEY_AUTHENTICATION="-o PubkeyAuthentication=no"
8+
else
9+
CONFIG_PUBKEY_AUTHENTICATION="-o PubkeyAuthentication=yes"
10+
fi
11+
12+
if [ -n "$AUTHORIZED_KEYS" ]; then
13+
CONFIG_AUTHORIZED_KEYS="-o AuthorizedKeysFile=$AUTHORIZED_KEYS"
14+
else
15+
CONFIG_AUTHORIZED_KEYS="-o AuthorizedKeysFile=authorized_keys"
16+
fi
17+
18+
if [ -n "$TRUSTED_USER_CA_KEYS" ]; then
19+
CONFIG_TRUSTED_USER_CA_KEYS="-o TrustedUserCAKeys=$TRUSTED_USER_CA_KEYS"
20+
CONFIG_AUTHORIZED_PRINCIPALS_FILE="-o AuthorizedPrincipalsFile=/etc/ssh/auth_principals/%u"
21+
fi
22+
23+
if [ "$GATEWAY_PORTS" == "true" ]; then
24+
CONFIG_GATEWAY_PORTS="-o GatewayPorts=yes"
25+
else
26+
CONFIG_GATEWAY_PORTS="-o GatewayPorts=no"
27+
fi
28+
29+
if [ "$PERMIT_TUNNEL" == "true" ]; then
30+
CONFIG_PERMIT_TUNNEL="-o PermitTunnel=yes"
31+
else
32+
CONFIG_PERMIT_TUNNEL="-o PermitTunnel=no"
33+
fi
34+
35+
if [ "$X11_FORWARDING" == "true" ]; then
36+
CONFIG_X11_FORWARDING="-o X11Forwarding=yes"
37+
else
38+
CONFIG_X11_FORWARDING="-o X11Forwarding=no"
39+
fi
40+
41+
if [ "$TCP_FORWARDING" == "false" ]; then
42+
CONFIG_TCP_FORWARDING="-o AllowTcpForwarding=no"
43+
else
44+
CONFIG_TCP_FORWARDING="-o AllowTcpForwarding=yes"
45+
fi
46+
47+
if [ "$AGENT_FORWARDING" == "false" ]; then
48+
CONFIG_AGENT_FORWARDING="-o AllowAgentForwarding=no"
49+
else
50+
CONFIG_AGENT_FORWARDING="-o AllowAgentForwarding=yes"
51+
fi
52+
53+
if [ ! -f "$HOST_KEYS_PATH/ssh_host_rsa_key" ]; then
54+
/usr/bin/ssh-keygen -A -f "$HOST_KEYS_PATH_PREFIX"
55+
fi
56+
57+
/usr/sbin/sshd -D -e -4 \
58+
-o "HostKey=$HOST_KEYS_PATH/ssh_host_rsa_key" \
59+
-o "HostKey=$HOST_KEYS_PATH/ssh_host_dsa_key" \
60+
-o "HostKey=$HOST_KEYS_PATH/ssh_host_ecdsa_key" \
61+
-o "HostKey=$HOST_KEYS_PATH/ssh_host_ed25519_key" \
62+
-o "PasswordAuthentication=no" \
63+
-o "PermitEmptyPasswords=no" \
64+
-o "PermitRootLogin=no" \
65+
$CONFIG_PUBKEY_AUTHENTICATION \
66+
$CONFIG_AUTHORIZED_KEYS \
67+
$CONFIG_GATEWAY_PORTS \
68+
$CONFIG_PERMIT_TUNNEL \
69+
$CONFIG_X11_FORWARDING \
70+
$CONFIG_AGENT_FORWARDING \
71+
$CONFIG_TCP_FORWARDING \
72+
$CONFIG_TRUSTED_USER_CA_KEYS \
73+
$CONFIG_AUTHORIZED_PRINCIPALS_FILE

docker-compose.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3.3'
1+
version: "3.6"
22
services:
33
bastion:
44
image: binlab/bastion
@@ -10,8 +10,8 @@ services:
1010
ports:
1111
- 22222:22/tcp
1212
volumes:
13-
- ./.bastion_keys:/var/lib/bastion/authorized_keys:ro
14-
- bastion:/etc/ssh:rw
13+
- $PWD/authorized_keys:/var/lib/bastion/authorized_keys:ro
14+
- bastion:/usr/etc/ssh:rw
1515
extra_hosts:
1616
- docker-host:172.17.0.1
1717
networks:
@@ -25,8 +25,8 @@ services:
2525
expose:
2626
- 22/tcp
2727
volumes:
28-
- ./.bastion_keys:/var/lib/bastion/authorized_keys:ro
29-
- docker-ssh:/etc/ssh:rw
28+
- $PWD/authorized_keys:/var/lib/bastion/authorized_keys:ro
29+
- docker-ssh:/usr/etc/ssh:rw
3030
networks:
3131
- bastion
3232

docs/bastion_host.png

50.4 KB
Loading

0 commit comments

Comments
 (0)