From ba6e83f9255642561a57cefc4f438eeaf7aef811 Mon Sep 17 00:00:00 2001 From: William Colmenares Date: Tue, 11 Jun 2019 20:57:55 -0400 Subject: [PATCH] config to read/store user data in json file --- ungleich | 4 ++- ungleich_config.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++ ungleich_dns.py | 22 ++++++++----- 3 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 ungleich_config.py diff --git a/ungleich b/ungleich index 34d86cf..dc99669 100755 --- a/ungleich +++ b/ungleich @@ -7,6 +7,7 @@ from ungleich_ripe import ungleichRIPE from ungleich_account import Account_Create from ungleich_weather import ungleichWeather from ungleich_ssh_key import SSHKey +from ungleich_config import Ungleich_Config VERSION = "0.0.4" @@ -17,7 +18,8 @@ class ungleichCLI(object): # FIXME: make it generic dns = ungleichDNS(self.parser, self.parser_parents) ripe = ungleichRIPE(self.parser, self.parser_parents) - ripe = Account_Create(self.parser, self.parser_parents) + account = Account_Create(self.parser, self.parser_parents) + config = Ungleich_Config(self.parser, self.parser_parents) SSHKey(self.parser, self.parser_parents) ungleichWeather(self.parser, self.parser_parents) diff --git a/ungleich_config.py b/ungleich_config.py new file mode 100644 index 0000000..bc2ca0d --- /dev/null +++ b/ungleich_config.py @@ -0,0 +1,82 @@ +import argparse +import json +import urllib.request + + +class Ungleich_Config(object): + def __init__(self, parser, parents): + self.parser = parser + + self.parser['add'] = self.parser['sub'].add_parser( + 'config-add', + help="Add user to configuration file", + parents=[parents]) + + self.parser['list'] = self.parser['sub'].add_parser( + 'config-list', + help="list users in configuration file", + parents=[parents]) + + self.parser['delete'] = self.parser['sub'].add_parser( + 'config-delete', + help="remove a user in configuration file", + parents=[parents]) + + self.parser['add'].add_argument('--name', help='otp name', required=True) + self.parser['add'].add_argument('--realm', help='otp realm', required=True) + self.parser['add'].add_argument('--seed', help='otp seed', required=True) + + self.parser['delete'].add_argument('--id', help='user ID to remove', required=True) + self.parser['list'].set_defaults(func=self._list_users) + self.parser['add'].set_defaults(func=self._add_user) + self.parser['delete'].set_defaults(func=self._delete_user) + + def _add_user(self, args): + try: + f = open('users.json', 'r') + json_info = json.loads(f.read()) + if len(json_info) > 0: + with open('users.json', 'w') as fp: + last_id = int(list(json_info.keys())[-1]) + 1 + json_info[last_id] = {"name": args.name, "realm": args.realm, "seed": args.seed} + json.dump(json_info, fp) + print('User added.') + else: + with open('users.json', 'w') as fp: + d = {"name": args.name, "realm": args.realm, "seed": args.seed} + json.dump({"1": d}, fp) + print('User added.') + + except FileNotFoundError: + with open('users.json', 'w') as fp: + d = {"name": args.name, "realm": args.realm, "seed": args.seed} + json.dump({"1": d}, fp) + print('User added.') + + def _list_users(self, args): + try: + f = open('users.json', 'r') + json_info = json.loads(f.read()) + for key, value in json_info.items(): + print('{}: {}'.format(key, value['name'])) + except FileNotFoundError: + print('No users loaded.') + + def _delete_user(self, args): + try: + f = open('users.json', 'r') + json_info = json.loads(f.read()) + try: + del json_info[args.id] + f = open('users.json', 'w') + x = 1 + for i in list(json_info.keys()): + json_info[str(x)] = json_info.pop(i) + x+=1 + f.write(json.dumps(json_info)) + f.close() + except KeyError: + print("No user with such id.") + + except FileNotFoundError: + print('No users loaded.') diff --git a/ungleich_dns.py b/ungleich_dns.py index 85eb6bd..5321546 100644 --- a/ungleich_dns.py +++ b/ungleich_dns.py @@ -1,7 +1,7 @@ - import urllib.request import argparse import json +from pyotp import TOTP class ungleichDNS(object): def __init__(self, parser, parents): @@ -13,15 +13,16 @@ class ungleichDNS(object): parents=[parents]) self.parser['dns'].add_argument('--set-reverse', help='REQUIRED: IPv6 Address of your VM', required=True) - self.parser['dns'].add_argument('--user', help='Your ungleich username', required=True) - self.parser['dns'].add_argument('--token', help='Your ungleich 6 digit OTP generated token', type=int, required=True) + self.parser['dns'].add_argument('--user', help='Your stored user ID', required=True) self.parser['dns'].add_argument('--name', help='Hostname', required=True) self.parser['dns'].add_argument('--email', help='registered email', required=True) - self.parser['dns'].add_argument('--realm', help='Otp realm', required=True) self.parser['dns'].set_defaults(func=self._handle_dns) def _handle_dns(self, args): """Reverse dns endpoint.""" + f = open('users.json', 'r') + data = json.loads(f.read())[args.user] + url = 'https://dns.service.ungleich.ch' req = urllib.request.Request( url=url, @@ -30,13 +31,16 @@ class ungleichDNS(object): "Content-Type": "application/json" }, data=json.dumps({ - 'username': args.user, - 'token': args.token, + 'username': data['name'], + 'token': TOTP(data['seed']).now(), 'ipaddress': args.set_reverse, 'name': args.name, 'email': args.email, - 'realm': args.realm + 'realm': data['realm'] }).encode('utf-8') ) - response = urllib.request.urlopen(req).read() - print(json.loads(response)) + try: + response = urllib.request.urlopen(req).read() + except urllib.error.HTTPError as e: + error_message = e.read() + print(json.loads(error_message))