Commit 1456aa7d authored by Fence's avatar Fence 🌈

add cli

parent 5b6b59ef
include minor/cli/get_fingerprint.sh
import click
from .rsa import rsa
@click.group()
@click.pass_context
def cli(ctx):
ctx.db = "minor"
cli.add_command(rsa)
from .__init__ import cli
cli()
import os
import subprocess
path = os.path.dirname(os.path.realpath(__file__))
def get_fingerprint(filename):
p = subprocess.Popen(
[path + "/get_fingerprint.sh", filename],
stdout=subprocess.PIPE
)
if p.wait() != 0:
return None
else:
return p.stdout.read().decode("utf-8").strip("\n")
#! /bin/sh
md5=$(openssl md5 -c < "$1")
if [ $? -eq 0 ]; then
echo "$md5" | awk '{print $2}'
else
return $?
fi
import os
import sys
import click
from mongoengine import connect
from tabulate import tabulate
from minor.model import TrustedRsaKey
from .fingerprint import get_fingerprint
@click.group()
@click.pass_context
def rsa(ctx):
ctx.db = ctx.parent.db
connect(ctx.db)
@rsa.command("list")
@click.option("--raw", is_flag=True, help="output raw csv")
@click.pass_context
def list_keys(ctx, raw):
keys = []
for key in TrustedRsaKey.objects:
temp_file = "/tmp/minor-" + key.issuer
with open(temp_file, "w") as f:
f.write(key.key)
fingerprint = get_fingerprint(temp_file)
os.remove(temp_file)
if fingerprint is None:
fingerprint = "invalid"
keys.append({"issuer": key.issuer, "fingerprint": fingerprint})
if raw:
for key in keys:
click.echo(key["issuer"] + ";" + key["fingerprint"])
else:
keys = map(lambda x: [x["issuer"], x["fingerprint"]], keys)
table = tabulate(keys, headers=["issuer", "fingerprint"])
click.echo(table)
@rsa.command("add")
@click.argument("issuer", nargs=1)
@click.argument("file", type=click.File("r"), nargs=1)
def add_rsa_key(issuer, file):
# only add if the issuer does not exist already
try:
TrustedRsaKey.objects.get(issuer=issuer)
click.echo("issuer \"{}\" does already have a rsa_key".format(issuer))
return sys.exit(1)
except TrustedRsaKey.DoesNotExist:
key = file.read()
rsa_key = TrustedRsaKey(issuer=issuer, key=key)
rsa_key.save()
click.echo("rsa key added for issuer {}".format(issuer))
@rsa.command("rm")
@click.argument("issuer", nargs=1)
def rm_rsa_key(issuer):
try:
key = TrustedRsaKey.objects.get(issuer=issuer)
key.delete()
click.echo("key of issuer \"{}\" deleted".format(issuer))
except TrustedRsaKey.DoesNotExist:
click.echo("issuer \"{}\" does not exist".format(issuer))
return sys.exit(1)
......@@ -2,4 +2,6 @@ flask
PyJWT==1.7.1
flask_mongoengine
git+https://github.com/AlexFence/FlaskController.git
mutagen
\ No newline at end of file
mutagen
click
tabulate
......@@ -3,9 +3,10 @@
from setuptools import setup, find_packages
from minor import version
setup(name=version.name,
version=version.number,
description='some cool microservice or what ever this is...',
author='fence',
packages=find_packages()
setup(
name=version.name,
version=version.number,
description='some cool microservice or what ever this is...',
author='fence',
packages=find_packages(),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment