aboutsummaryrefslogtreecommitdiff
path: root/src/add_config.py
blob: 4fd4feb43c9b2e8516513f25cc3ea605a07d69ff (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/python3

from sys import argv
import yaml
import psycopg2
import hashlib

db_config_path = '/etc/0tdns/db_connection_config.yml'

ovpn_config_path = argv[1]

with open(ovpn_config_path) as file:
    ovpn_config_text = file.read()

ovpn_config_raw = bytearray(ovpn_config_text, encoding='utf-8')

ovpn_config_hash = hashlib.sha256(ovpn_config_raw).hexdigest()

config = yaml.safe_load(open(db_config_path, 'r'))
connection = psycopg2.connect(user=config['user'], password=config['password'],
                              host=config['host'], port=config['port'],
                              database=config['database'])
cursor = connection.cursor()

cursor.execute('''
INSERT INTO vpn (location_id, ovpn_config, ovpn_config_sha256)
VALUES(%s, %s, %s)''', (11, ovpn_config_text, ovpn_config_hash))

connection.commit()
cursor.close()
connection.close()