Compare commits

..

No commits in common. "master" and "0.0.3" have entirely different histories.

15 changed files with 59 additions and 499 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
__pycache__/
.vscode/

9
LICENSE Normal file
View file

@ -0,0 +1,9 @@
LICENSE
Copyright
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

15
Pipfile
View file

@ -1,15 +0,0 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
pep8 = "*"
[packages]
pyotp = "*"
requests = "*"
apixu-python = {git = "https://github.com/apixu/apixu-python.git",ref = "master"}
[requires]
python_version = "3.7"

78
Pipfile.lock generated
View file

@ -1,78 +0,0 @@
{
"_meta": {
"hash": {
"sha256": "b35d443f952246f5828abb12b75c9f9d02f03d016921244c04538216f0944693"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.7"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"apixu-python": {
"git": "https://github.com/apixu/apixu-python.git",
"ref": "4beb003c71c0213720e930350f46e5fa6af9ef12"
},
"certifi": {
"hashes": [
"sha256:59b7658e26ca9c7339e00f8f4636cdfe59d34fa37b9b04f6f9e9926b3cece1a5",
"sha256:b26104d6835d1f5e49452a26eb2ff87fe7090b89dfcaee5ea2212697e1e1d7ae"
],
"version": "==2019.3.9"
},
"chardet": {
"hashes": [
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
],
"version": "==3.0.4"
},
"idna": {
"hashes": [
"sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
"sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
],
"version": "==2.8"
},
"pyotp": {
"hashes": [
"sha256:1e3dc3d16919c4efac528d1dbecc17de1a97c4ecfdacb89d7726ed2c6645adff",
"sha256:be0ffeabddaa5ee53e7204e7740da842d070cf69168247a3d0c08541b84de602"
],
"index": "pypi",
"version": "==2.2.7"
},
"requests": {
"hashes": [
"sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
"sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"
],
"index": "pypi",
"version": "==2.22.0"
},
"urllib3": {
"hashes": [
"sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1",
"sha256:dbe59173209418ae49d485b87d1681aefa36252ee85884c31346debd19463232"
],
"version": "==1.25.3"
}
},
"develop": {
"pep8": {
"hashes": [
"sha256:b22cfae5db09833bb9bd7c8463b53e1a9c9b39f12e304a8d0bba729c501827ee",
"sha256:fe249b52e20498e59e0b5c5256aa52ee99fc295b26ec9eaa85776ffdb9fe6374"
],
"index": "pypi",
"version": "==1.7.1"
}
}
}

75
README.md Executable file → Normal file
View file

@ -5,43 +5,19 @@ It is intended to be used by ungleich engineers and skilled customers.
## Requirements / Installation
* ensure you have **python3** and **pipenv**
* ensure you have python3
* git clone this repo
* cd into this repo
* run the following command
```
pipenv install
pipenv shell
```
## Usage general
```
ungleich --help
```
## Usage: Weather
```
ungleich weather
```
## Usage: Config
```
ungleich config-add \
--name <otp username>
--realm <otp realm>
--seed <otp seed>
```
## Usage: DNS
```
ungleich dns \
--set-reverse <ip>
--user <user_id>
--email <email>
--name mirror.example.com
ungleich dns --set-reverse <ip> --user <username> --token <token> --name mirror.example.com
```
### Usage: RIPE
@ -54,50 +30,3 @@ ungleich ripe-add-route6 \
--description "First REST /32"
--password "very secure"
```
### Usage: Account
Creating a new account object:
```
ungleich account \
--create-user <username>
--name <firstname>
--lastname <lastname>
--email <email>
```
### Usage: SSH Key
Requires you to have
https://code.ungleich.ch/ungleich-public/ungleich-ssh-keys/ checked
out in ~/vcs/ungleich-ssh-keys
```
ungleich sshkey --key raul.ascencio --host dev.raul.ungleich.cloud
```
### Usage: VPN Management
Creating a new VPN:
```
ungleich vpn-create \
--server_url <server_url>
--name <name>
--realm <realm>
--seed <seed_value>
--email <email>
--public_key <public_key>
```
Deleting a VPN:
```
ungleich vpn-delete \
--server_url <server_url>
--name <name>
--realm <realm>
--seed <seed_value>
```

2
setup.cfg Normal file
View file

@ -0,0 +1,2 @@
[metadata]
description-file = README.md

34
setup.py Normal file
View file

@ -0,0 +1,34 @@
from setuptools import setup
def readme():
with open('README.md') as f:
README = f.read()
return README
setup(
name='ungleich-cli',
version='1.0.0',
description="A Python package for ungleich dns administration.",
long_description=readme(),
long_description_content_type="text/markdown",
author="William Colmenares",
author_email="colmenares.william@gmail.com",
license="MIT",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
],
py_modules=['ungleichcli'],
install_requires=[
'requests',
],
entry_points={
"console_scripts": [
"ungleich-cli=ungleichcli:cli"
]
},
)

View file

@ -4,14 +4,8 @@ import argparse
from ungleich_dns import ungleichDNS
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
from ungleich_vpn import ungleichVPN
VERSION = "0.0.4"
VERSION = "0.0.3"
class ungleichCLI(object):
def __init__(self):
@ -20,11 +14,6 @@ class ungleichCLI(object):
# FIXME: make it generic
dns = ungleichDNS(self.parser, self.parser_parents)
ripe = ungleichRIPE(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)
ungleichVPN(self.parser, self.parser_parents)
def _init_parser(self):
self.parser = {}

View file

@ -1,30 +0,0 @@
import requests
import argparse
class Account_Create(object):
def __init__(self, parser, parents):
self.parser = parser
self.parser['account'] = self.parser['sub'].add_parser(
'account',
help="Create a valid ungleich account",
parents=[parents])
self.parser['account'].add_argument('--create-user', help='REQUIRED: Username', required=True)
self.parser['account'].add_argument('--name', help='User\'s firstname', type=str, required=True)
self.parser['account'].add_argument('--lastname', help='User\'s lastname', type=str, required=True)
self.parser['account'].add_argument('--email', help='Email', required=True)
self.parser['account'].set_defaults(func=self._handle_account)
def _handle_account(self, args):
"""Reverse account endpoint."""
r = requests.post(
'https://account.ungleich.ch/create/',
data={
'username': args.create_user,
'firstname': args.name,
'lastname': args.lastname,
'email': args.email
})
print(r.text)

View file

@ -1,82 +0,0 @@
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.')

46
ungleich_dns.py Executable file → Normal file
View file

@ -1,7 +1,5 @@
import urllib.request
import requests
import argparse
import json
from pyotp import TOTP
class ungleichDNS(object):
def __init__(self, parser, parents):
@ -12,36 +10,14 @@ class ungleichDNS(object):
help="Manage DNS entries @ ungleich",
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 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'].set_defaults(func=self._handle_dns)
self.parser['dns'].add_argument('--set-reverse', help='REQUIRED: IPv6 Address of your VM', metavar='', required=True)
self.parser['dns'].add_argument('--user', help='Your ungleich username', metavar='', required=True)
self.parser['dns'].add_argument('--token', help='Your ungleich 6 digit OTP generated token', metavar='', type=int, required=True)
self.parser['dns'].add_argument('--name', help='Hostname', metavar='', required=True)
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,
method='POST',
headers={
"Content-Type": "application/json"
},
data=json.dumps({
'username': data['name'],
'token': TOTP(data['seed']).now(),
'ipaddress': args.set_reverse,
'name': args.name,
'email': args.email,
'realm': data['realm']
}).encode('utf-8')
)
try:
response = urllib.request.urlopen(req).read()
print(response)
except urllib.error.HTTPError as e:
error_message = e.read()
print(json.loads(error_message))
def _handle_dns(self):
"""A dummy endpoint, to check what endpoint will be reverse-dns service."""
r = requests.post(
'https://en53kfc0hydpg.x.pipedream.net',
json={'username': args.user, 'token': args.token, 'ipaddress': args.set_reverse, 'name': args.name})
return r.text

0
ungleich_ripe.py Executable file → Normal file
View file

View file

@ -1,51 +0,0 @@
#!/usr/bin/env python3
import os
import sys
import subprocess
class SSHKey(object):
def __init__(self, parser, parents):
self.parser = parser
self.parser['sshkey'] = self.parser['sub'].add_parser(
'sshkey',
help="Manage SSH keys",
parents=[parents])
self.parser['sshkey'].add_argument('--user',
help='Username on the host',
required=False,
default="root")
self.parser['sshkey'].add_argument('--key',
help='Name of the key',
required=True)
self.parser['sshkey'].add_argument('--key-dir',
help='Directory holding keys',
default=os.path.join(os.environ['HOME'], "vcs/ungleich-ssh-keys/current"),
required=False)
self.parser['sshkey'].add_argument('--host',
help='Host to use',
required=True)
self.parser['sshkey'].add_argument('--delete',
help='Delete key instead of adding',
action="store_true")
self.parser['sshkey'].set_defaults(func=self._manage_key)
def _manage_key(self, args):
if args.delete:
print("Not yet supported to delete, sorry :-)")
sys.exit(1)
keyfile = os.path.join(args.key_dir, "{}.pub".format(args.key))
if not os.path.exists(keyfile):
print("Key for {} does not exist in {}. Aborting".format(args.key, args.key_dir))
sys.exit(1)
cmd = 'cat {} | ssh {}@{} "mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys"'.format(keyfile,
args.user,
args.host)
subprocess.run(cmd, shell=True)

View file

@ -1,87 +0,0 @@
import argparse
import requests
import json
class ungleichVPN(object):
def __init__(self, parser, parents):
self.parser = parser
self.parser['vpn-create'] = self.parser['sub'].add_parser(
'vpn-create',
help="Create VPN",
parents=[parents])
self.parser['vpn-delete'] = self.parser['sub'].add_parser(
'vpn-delete',
help="Delete VPN",
parents=[parents])
self.parser['vpn-create'].set_defaults(func=ungleichVPN.create_vpn)
self.parser['vpn-create'].add_argument("--server_url", required=True,
dest="server_url")
self.parser['vpn-create'].add_argument("--name", required=True,
dest="name")
self.parser['vpn-create'].add_argument("--realm", required=True,
dest="realm")
self.parser['vpn-create'].add_argument("--seed", required=True,
dest="seed")
# email argument would be deleted once we can
# get email from OTP
self.parser['vpn-create'].add_argument("--email", required=True,
dest="email")
self.parser['vpn-create'].add_argument("--public_key", required=True,
dest="public_key")
self.parser['vpn-delete'].set_defaults(func=ungleichVPN.delete_vpn)
self.parser['vpn-delete'].add_argument("--server_url", required=True,
dest="server_url")
self.parser['vpn-delete'].add_argument("--name", required=True,
dest="name")
self.parser['vpn-delete'].add_argument("--realm", required=True,
dest="realm")
self.parser['vpn-delete'].add_argument("--seed", required=True,
dest="seed")
def create_vpn(args):
try:
# email argument from the below request would
# be deleted once we can get email from OTP
r = requests.post(args.server_url + "/create",
data={'name': args.name,
'realm': args.realm,
'seed': args.seed,
'email': args.email,
'public_key': args.public_key
})
except Exception as e:
print(e)
return None
if r.status_code == 200:
response = r.content.decode("utf-8")
with open("wg0.conf", "w") as f:
f.write(response)
print("""VPN successfully created. VPN Configuration \
file is saved as wg0.conf""")
else:
response = r.json()
print(response.get("message"))
def delete_vpn(args):
try:
# email argument from the below request would
# be deleted once we can get email from OTP
r = requests.post(args.server_url + "/delete",
data={'name': args.name,
'realm': args.realm,
'seed': args.seed
})
except Exception as e:
print(e)
return None
response = r.json()
print(response.get("message"))

View file

@ -1,34 +0,0 @@
import argparse
import requests
import json
from apixu.client import ApixuClient
def get_loc():
response = requests.get('http://ip-api.com/json/')
data = json.loads(response.content)
return data['city'], data['countryCode']
class ungleichWeather(object):
def __init__(self, parser, parents):
self.parser = parser
self.parser['weather'] = self.parser['sub'].add_parser(
'weather',
help="Weather Enquiries",
parents=[parents])
self.parser['weather'].set_defaults(func=ungleichWeather.forecast_weather)
def forecast_weather(args):
_city, _country_code = get_loc()
print("Weather for {}\n".format(_city))
client = ApixuClient("cc33a1e3237a4b78b3174104190206")
forecast = client.forecast(q=f'{ _city},{_country_code}', days=7)
print(f"{'Date':^12}|{'Min Temp':^10}|{'Max Temp':^10}|{'Condition':^15}")
print(f"{'*'*47:^47}")
for day in forecast['forecast']['forecastday']:
print(f"{day['date']:^12}|{day['day']['mintemp_c']:^10}|{day['day']['maxtemp_c']:^10}|{day['day']['condition']['text']:^15}")