Changes between Version 15 and Version 16 of UnprivilegedUser


Ignore:
Timestamp:
02/14/20 17:08:10 (4 years ago)
Author:
grzeg0rz
Comment:

Correction to procedure for unprivileged server

Legend:

Unmodified
Added
Removed
Modified
  • UnprivilegedUser

    v15 v16  
    255255I was able to run openvpn from within unprivileged podman container.
    256256
    257 1. install podman (https://podman.io/getting-started/installation)
    258 2. Create Dockerfile (as target unprivileged user)
    259 
    260 {{{
    261 FROM archlinux
    262 RUN pacman -Sy --noconfirm openvpn
    263 }}}
    264 
    265 3. Build image (as target unprivileged user)
    266 
    267 {{{
    268 podman build -t openvpn .
    269 }}}
    270 
    271 4. Create systemd openvpn.service file (as root)
    272 
    273 {{{
     2571. Install podman (https://podman.io/getting-started/installation)
     258
     2592. Create unprivileged user
     260
     261{{{
     262useradd -m openvpn
     263# Automatically start-up systemd user instances
     264loginctl enable-linger openvpn
     265}}}
     266
     2673. Create directories where configuration, certificates and entrypoint script will be stored
     268
     269{{{
     270mkdir -p /opt/openvpn/server/{ssl,status,ccd}
     271}}}
     272
     2734. Make systemd-networkd to create tun0 which will be required by openvpn in later step
     274
     275{{{
     276cat > /etc/systemd/network/21_openvpn.tun0.netdev<<EOF
     277[NetDev]
     278Name=tun0
     279Kind=tun
     280
     281[Tun]
     282User=openvpn
     283Group=openvpn
     284EOF
     285
     286cat > /etc/systemd/network/22_openvpn.tun0.network<<EOF
     287[Match]
     288Name=tun0
     289
     290[Network]
     291Address=10.254.254.1/24
     292
     293#KeepConfiguration=yes
     294#BindCarrier=yes
     295#CriticalConnection=yes
     296
     297ConfigureWithoutCarrier=yes
     298IgnoreCarrierLoss=yes
     299IPForward=yes
     300
     301[Link]
     302MTUBytes=1500
     303EOF
     304
     305systemctl restart systemd-networkd
     306}}}
     307
     3085. Use easy-rsa to create your CA authority and all required certificates, at the end of this step you should create ca.crt, ta.key, dh.pem, crl.pem, your_server.key, your_server.crt - copy everything to /opt/openvpn/server/ssl
     309
     3106. Create /opt/openvpn/server/server.conf - at least following keys should match (below is not a complete conf file, only key options are mentioned)
     311
     312{{{
     313dev tun0
     314ca /server/ssl/ca.crt
     315cert /server/ssl/your_server.crt
     316key /server/ssl/easy-rsa/pki/private/your_server.key
     317crl-verify /server/ssl/crl.pem
     318dh /server/ssl/dh.pem
     319tls-auth /server/ssl/ta.key 0
     320server 10.254.254.0 255.255.255.0
     321}}}
     322
     3237. Ensure /opt/openvpn is owned by and can be read only by openvpn:openvpn
     324
     3258. Create systemd openvpn.service file (as root)
     326
     327{{{
     328cat > /etc/systemd/system/openvpn.service<<EOF
    274329[Unit]
    275330Description=OpenVPN in Podman container
     
    278333
    279334[Service]
    280 User=target_unprivileged_user
    281 Group=target_unprivileged_group
    282 ExecStart=/usr/bin/podman run --rm -v /home/target_unprivileged_user/ovpn_config_files/:/ovpn_config_files -p 56787:56787 --device /dev/net/tun --device /dev/null --cap-add CAP_IPC_LOCK,CAP_NET_ADMIN,CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_SETGID,CAP_SETUID,CAP_SYS_CHROOT,CAP_DAC_OVERRIDE,CAP_AUDIT_WRITE localhost/openvpn:latest /usr/bin/openvpn --config /ovpn_config_files/openvpn_server.conf
    283 ExecStop=/usr/bin/podman stop -t 0 localhost/openvpn:latest
    284 Capabilities=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_OVERRIDE CAP_AUDIT_WRITE
     335User=openvpn
     336Group=openvpn
     337DeviceAllow=/dev/null rw
     338DeviceAllow=/dev/net/tun rw
     339AmbientCapabilities=CAP_MKNOD CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_OVERRIDE CAP_AUDIT_WRITE
     340WorkingDirectory=/opt/openvpn
     341ExecStart=/usr/bin/podman run --rm --name openvpn -v /opt/openvpn/server:/server --network="host" -p 37898:37898 --device /dev/net/tun --device /dev/null --cap-add CAP_IPC_LOCK,CAP_NET_ADMIN,CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_SETGID,CAP_SETUID,CAP_SYS_CHROOT,CAP_DAC_OVERRIDE,CAP_AUDIT_WRITE archlinux:latest /usr/bin/bash /server/entrypoint.sh
     342ExecStop=/usr/bin/podman stop -t 0 openvpn
     343ProtectSystem=true
    285344RestartSec=5s
    286345Restart=on-failure
     346TimeoutSec=5s
    287347
    288348[Install]
    289349WantedBy=multi-user.target
    290 }}}
    291 
    292 5. Drop configuration (openvpn-server.conf) and other required files under /home/target_unprivileged_user/ovpn_config_files, configuration file should have paths relative to podman container (/ovpn_config_files, not /home/target_unprivileged_user/ovpn_config_files); Also note port exposed within systemd service is 56787 (either keep this in mind when writing your configuration file or change systemd service file)
    293 
    294 6. Make sure systemd for openvpn user is always started and kept (otherwise podman won't start)
    295 {{{
    296 loginctl enable-linger openvpn
    297 }}}
    298 
    299 7. Start the service (as root) - this will result in podman container running as target_unprivileged_user, openvpn port exposed and openvpn available to receive connections
     350EOF
     351}}}
     352
     3539. Create /opt/openvpn/server/entrypoint.sh
     354
     355{{{
     356cat > /opt/openvpn/server/entrypoint.sh<<EOF
     357#!/bin/bash
     358
     359pacman -Sy --noconfirm openvpn net-tools nano
     360
     361# we have done all required network configuration so openvpn does not have to
     362cp -p /usr/bin/ip /usr/bin/ip.bak
     363echo "#!/bin/bash" > /usr/bin/ip
     364echo 'echo "$@" >> /tmp/ip_res' >> /usr/bin/ip
     365echo "exit 0" >> /usr/bin/ip
     366chmod ugo+x /usr/bin/ip
     367
     368openvpn --cd /server --config /server/server.conf
     369
     370EOF
     371
     372chmod ugo+x /opt/openvpn/server/entrypoint.sh
     373}}}
     374
     37510. Start the service (as root) - this will result in podman container running as openvpn user
    300376
    301377{{{