Skip to content

Commit

Permalink
read openvpn client list through management console
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Jul 28, 2023
1 parent 597fc6d commit a9f09cc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
19 changes: 19 additions & 0 deletions services/ansible_openvpn/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Create a file in this folder with your own generated credentials for the docker compose:

```shell
MYSQL_PASSWORD=DATABASE_PASSWORD
SEMAPHORE_ADMIN_PASSWORD=YOUR_ADMIN_PASSWORD
SEMAPHORE_ACCESS_KEY_ENCRYPTION="YOUR_RANDOM_KEY"
MANAGEMENT_OPENVPN_PASSWORD=YOUR_OPENVPN_MANAGEMENT_CONSOLE_PASSWORD
MANAGEMENT_OPENVPN_PORT=5555
OPENVPN_STATUS_PATH=""
```

SEMAPHORE_ACCESS_KEY_ENCRYPTION value is generated with

```shell
head -c32 /dev/urandom | base64
```


OPENVPN_STATUS_PATH could be `/var/log/openvpn/openvpn-status.log` if the container have access to it
2 changes: 2 additions & 0 deletions services/ansible_openvpn/docker/backup_ansible.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# From
# https://docs.docker.com/storage/volumes/#back-up-restore-or-migrate-data-volumes
# will save backup.tar in the current working directory
# mysql container should be shutdown before doing this backup
sudo docker run --rm --volumes-from semaphore-mysql-1 -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /var/lib/mysql

1 change: 1 addition & 0 deletions services/ansible_openvpn/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
restart: unless-stopped
ports:
- 3000:3000
- 5555:5555 # OpenVPN management console port, update in the .env file, look at README.md
image: semaphoreui/semaphore:latest
environment:
SEMAPHORE_DB_USER: semaphore
Expand Down
1 change: 0 additions & 1 deletion services/ansible_openvpn/docker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
pyserial==3.5
36 changes: 33 additions & 3 deletions services/ansible_openvpn/openvpn_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@
import os
import argparse
import json
import socket
import sys
import time

# This script convert OpenVPN status into a dynamic Ansible inventory
# It can use local /var/log/openvpn/openvpn-status.log file
# or OpenVPN telnet management console


def receive(s):
time.sleep(0.1)
return s.recv(4096).decode("utf-8")


def send(s, command):
s.send(bytes(command, "utf-8"))


def parse_openvpn_status(log_text):
# Split the log text into lines
lines = log_text.strip().split('\n')
Expand Down Expand Up @@ -89,10 +101,28 @@ def main():
)
args = arg_parser.parse_args()
# Replace this with the path to your openvpn-status.log file
log_file_path = "openvpn-status.log"
log_text = ""

with open(log_file_path, 'r') as file:
log_text = file.read()
if "OPENVPN_STATUS_PATH" in os.environ and \
os.environ["OPENVPN_STATUS_PATH"]:
if not os.path.exists(os.environ["OPENVPN_STATUS_PATH"]):
print("Could not find "+os.environ["OPENVPN_STATUS_PATH"] +
" file, abort..", file=sys.stderr)
exit(-1)
with open(os.environ["OPENVPN_STATUS_PATH"], 'r') as file:
log_text = file.read()
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect("localhost:"+os.environ["MANAGEMENT_OPENVPN_PORT"])
sout = receive(s)
if sout == 'ENTER PASSWORD:':
s.send(os.environ["MANAGEMENT_OPENVPN_PASSWORD"] + "\n")
sout = receive(s)
if not ">INFO:" not in sout:
print(sout, file=sys.stderr)
exit(-1)
s.send("status\n")
log_text = receive(s)

hosts = parse_openvpn_status(log_text)

Expand Down

0 comments on commit a9f09cc

Please sign in to comment.