2016-04-11 10:52:02 +01:00
|
|
|
import grp
|
2016-04-11 21:24:58 +01:00
|
|
|
import json
|
2016-04-11 10:52:02 +01:00
|
|
|
import pwd
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
from contextlib import contextmanager
|
2016-04-11 10:52:02 +01:00
|
|
|
from shutil import rmtree
|
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
CERTTOOL_COMMAND = "@certtool@"
|
2016-04-12 03:14:33 +01:00
|
|
|
CERT_BITS = "@certBits@"
|
|
|
|
CLIENT_EXPIRATION = "@clientExpiration@"
|
|
|
|
CRL_EXPIRATION = "@crlExpiration@"
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
TASKD_COMMAND = "@taskd@"
|
|
|
|
TASKD_DATA_DIR = "@dataDir@"
|
|
|
|
TASKD_USER = "@user@"
|
|
|
|
TASKD_GROUP = "@group@"
|
|
|
|
FQDN = "@fqdn@"
|
|
|
|
|
2016-04-12 03:57:03 +01:00
|
|
|
CA_KEY = os.path.join(TASKD_DATA_DIR, "keys", "ca.key")
|
|
|
|
CA_CERT = os.path.join(TASKD_DATA_DIR, "keys", "ca.cert")
|
|
|
|
CRL_FILE = os.path.join(TASKD_DATA_DIR, "keys", "server.crl")
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
RE_CONFIGUSER = re.compile(r'^\s*user\s*=(.*)$')
|
2016-04-11 20:25:55 +01:00
|
|
|
RE_USERKEY = re.compile(r'New user key: (.+)$', re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
|
|
def lazyprop(fun):
|
|
|
|
"""
|
|
|
|
Decorator which only evaluates the specified function when accessed.
|
|
|
|
"""
|
|
|
|
name = '_lazy_' + fun.__name__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _lazy(self):
|
|
|
|
val = getattr(self, name, None)
|
|
|
|
if val is None:
|
|
|
|
val = fun(self)
|
|
|
|
setattr(self, name, val)
|
|
|
|
return val
|
|
|
|
|
|
|
|
return _lazy
|
|
|
|
|
|
|
|
|
|
|
|
class TaskdError(OSError):
|
|
|
|
pass
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run_as_taskd_user():
|
|
|
|
uid = pwd.getpwnam(TASKD_USER).pw_uid
|
|
|
|
gid = grp.getgrnam(TASKD_GROUP).gr_gid
|
|
|
|
os.setgid(gid)
|
|
|
|
os.setuid(uid)
|
|
|
|
|
|
|
|
|
|
|
|
def taskd_cmd(cmd, *args, **kwargs):
|
2016-04-11 20:25:55 +01:00
|
|
|
"""
|
|
|
|
Invoke taskd with the specified command with the privileges of the 'taskd'
|
|
|
|
user and 'taskd' group.
|
|
|
|
|
|
|
|
If 'capture_stdout' is passed as a keyword argument with the value True,
|
|
|
|
the return value are the contents the command printed to stdout.
|
|
|
|
"""
|
|
|
|
capture_stdout = kwargs.pop("capture_stdout", False)
|
|
|
|
fun = subprocess.check_output if capture_stdout else subprocess.check_call
|
|
|
|
return fun(
|
2016-04-11 10:52:02 +01:00
|
|
|
[TASKD_COMMAND, cmd, "--data", TASKD_DATA_DIR] + list(args),
|
|
|
|
preexec_fn=run_as_taskd_user,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-04-12 01:16:35 +01:00
|
|
|
def certtool_cmd(*args, **kwargs):
|
|
|
|
"""
|
|
|
|
Invoke certtool from GNUTLS and return the output of the command.
|
|
|
|
|
|
|
|
The provided arguments are added to the certtool command and keyword
|
|
|
|
arguments are added to subprocess.check_output().
|
|
|
|
|
|
|
|
Note that this will suppress all output of certtool and it will only be
|
|
|
|
printed whenever there is an unsuccessful return code.
|
|
|
|
"""
|
|
|
|
return subprocess.check_output(
|
|
|
|
[CERTTOOL_COMMAND] + list(args),
|
|
|
|
preexec_fn=lambda: os.umask(0077),
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
def label(msg):
|
|
|
|
if sys.stdout.isatty() or sys.stderr.isatty():
|
|
|
|
sys.stderr.write(msg + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
def mkpath(*args):
|
|
|
|
return os.path.join(TASKD_DATA_DIR, "orgs", *args)
|
|
|
|
|
|
|
|
|
2016-04-12 02:42:13 +01:00
|
|
|
def mark_imperative(*path):
|
|
|
|
"""
|
|
|
|
Mark the specified path as being imperatively managed by creating an empty
|
|
|
|
file called ".imperative", so that it doesn't interfere with the
|
|
|
|
declarative configuration.
|
|
|
|
"""
|
|
|
|
open(os.path.join(mkpath(*path), ".imperative"), 'a').close()
|
|
|
|
|
|
|
|
|
|
|
|
def is_imperative(*path):
|
|
|
|
"""
|
|
|
|
Check whether the given path is marked as imperative, see mark_imperative()
|
|
|
|
for more information.
|
|
|
|
"""
|
|
|
|
full_path = []
|
|
|
|
for component in path:
|
|
|
|
full_path.append(component)
|
|
|
|
if os.path.exists(os.path.join(mkpath(*full_path), ".imperative")):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
def fetch_username(org, key):
|
|
|
|
for line in open(mkpath(org, "users", key, "config"), "r"):
|
|
|
|
match = RE_CONFIGUSER.match(line)
|
|
|
|
if match is None:
|
|
|
|
continue
|
|
|
|
return match.group(1).strip()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
@contextmanager
|
|
|
|
def create_template(contents):
|
|
|
|
"""
|
|
|
|
Generate a temporary file with the specified contents as a list of strings
|
|
|
|
and yield its path as the context.
|
|
|
|
"""
|
|
|
|
template = NamedTemporaryFile(mode="w", prefix="certtool-template")
|
|
|
|
template.writelines(map(lambda l: l + "\n", contents))
|
|
|
|
template.flush()
|
|
|
|
yield template.name
|
|
|
|
template.close()
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
def generate_key(org, user):
|
|
|
|
basedir = os.path.join(TASKD_DATA_DIR, "keys", org, user)
|
|
|
|
if os.path.exists(basedir):
|
|
|
|
raise OSError("Keyfile directory for {} already exists.".format(user))
|
|
|
|
|
|
|
|
privkey = os.path.join(basedir, "private.key")
|
|
|
|
pubcert = os.path.join(basedir, "public.cert")
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs(basedir, mode=0700)
|
|
|
|
|
2016-04-12 03:14:33 +01:00
|
|
|
certtool_cmd("-p", "--bits", CERT_BITS, "--outfile", privkey)
|
2016-04-11 10:52:02 +01:00
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
template_data = [
|
2016-04-11 10:52:02 +01:00
|
|
|
"organization = {0}".format(org),
|
|
|
|
"cn = {}".format(FQDN),
|
2016-04-12 03:14:33 +01:00
|
|
|
"expiration_days = {}".format(CLIENT_EXPIRATION),
|
2016-04-11 10:52:02 +01:00
|
|
|
"tls_www_client",
|
|
|
|
"encryption_key",
|
|
|
|
"signing_key"
|
2016-04-12 00:08:34 +01:00
|
|
|
]
|
2016-04-11 10:52:02 +01:00
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
with create_template(template_data) as template:
|
2016-04-12 01:16:35 +01:00
|
|
|
certtool_cmd(
|
|
|
|
"-c",
|
|
|
|
"--load-privkey", privkey,
|
2016-04-12 03:57:03 +01:00
|
|
|
"--load-ca-privkey", CA_KEY,
|
|
|
|
"--load-ca-certificate", CA_CERT,
|
2016-04-12 01:16:35 +01:00
|
|
|
"--template", template,
|
|
|
|
"--outfile", pubcert
|
|
|
|
)
|
2016-04-11 10:52:02 +01:00
|
|
|
except:
|
|
|
|
rmtree(basedir)
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
def revoke_key(org, user):
|
|
|
|
basedir = os.path.join(TASKD_DATA_DIR, "keys", org, user)
|
|
|
|
if not os.path.exists(basedir):
|
|
|
|
raise OSError("Keyfile directory for {} doesn't exist.".format(user))
|
|
|
|
|
|
|
|
pubcert = os.path.join(basedir, "public.cert")
|
|
|
|
|
2016-04-12 03:14:33 +01:00
|
|
|
expiration = "expiration_days = {}".format(CRL_EXPIRATION)
|
|
|
|
|
|
|
|
with create_template([expiration]) as template:
|
2016-04-12 00:08:34 +01:00
|
|
|
oldcrl = NamedTemporaryFile(mode="wb", prefix="old-crl")
|
2016-04-12 03:57:03 +01:00
|
|
|
oldcrl.write(open(CRL_FILE, "rb").read())
|
2016-04-12 00:08:34 +01:00
|
|
|
oldcrl.flush()
|
2016-04-12 01:16:35 +01:00
|
|
|
certtool_cmd(
|
|
|
|
"--generate-crl",
|
|
|
|
"--load-crl", oldcrl.name,
|
2016-04-12 03:57:03 +01:00
|
|
|
"--load-ca-privkey", CA_KEY,
|
|
|
|
"--load-ca-certificate", CA_CERT,
|
2016-04-12 01:16:35 +01:00
|
|
|
"--load-certificate", pubcert,
|
|
|
|
"--template", template,
|
2016-04-12 03:57:03 +01:00
|
|
|
"--outfile", CRL_FILE
|
2016-04-12 01:16:35 +01:00
|
|
|
)
|
2016-04-12 00:08:34 +01:00
|
|
|
oldcrl.close()
|
|
|
|
rmtree(basedir)
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
def is_key_line(line, match):
|
|
|
|
return line.startswith("---") and line.lstrip("- ").startswith(match)
|
|
|
|
|
|
|
|
|
|
|
|
def getkey(*args):
|
|
|
|
path = os.path.join(TASKD_DATA_DIR, "keys", *args)
|
|
|
|
buf = []
|
|
|
|
for line in open(path, "r"):
|
|
|
|
if len(buf) == 0:
|
|
|
|
if is_key_line(line, "BEGIN"):
|
|
|
|
buf.append(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
buf.append(line)
|
|
|
|
|
|
|
|
if is_key_line(line, "END"):
|
|
|
|
return ''.join(buf)
|
|
|
|
raise IOError("Unable to get key from {}.".format(path))
|
|
|
|
|
|
|
|
|
|
|
|
def mktaskkey(cfg, path, keydata):
|
|
|
|
heredoc = 'cat > "{}" <<EOF\n{}EOF'.format(path, keydata)
|
|
|
|
cmd = 'task config taskd.{} -- "{}"'.format(cfg, path)
|
|
|
|
return heredoc + "\n" + cmd
|
|
|
|
|
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
class User(object):
|
|
|
|
def __init__(self, org, name, key):
|
|
|
|
self.__org = org
|
|
|
|
self.name = name
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
def export(self):
|
|
|
|
pubcert = getkey(self.__org, self.name, "public.cert")
|
|
|
|
privkey = getkey(self.__org, self.name, "private.key")
|
|
|
|
cacert = getkey("ca.cert")
|
|
|
|
|
|
|
|
keydir = "${TASKDATA:-$HOME/.task}/keys"
|
|
|
|
|
|
|
|
credentials = '/'.join([self.__org, self.name, self.key])
|
|
|
|
allow_unquoted = string.ascii_letters + string.digits + "/-_."
|
|
|
|
if not all((c in allow_unquoted) for c in credentials):
|
|
|
|
credentials = "'" + credentials.replace("'", r"'\''") + "'"
|
|
|
|
|
|
|
|
script = [
|
|
|
|
"umask 0077",
|
|
|
|
'mkdir -p "{}"'.format(keydir),
|
|
|
|
mktaskkey("certificate", os.path.join(keydir, "public.cert"),
|
|
|
|
pubcert),
|
|
|
|
mktaskkey("key", os.path.join(keydir, "private.key"), privkey),
|
|
|
|
mktaskkey("ca", os.path.join(keydir, "ca.cert"), cacert),
|
|
|
|
"task config taskd.credentials -- {}".format(credentials)
|
|
|
|
]
|
|
|
|
|
|
|
|
return "\n".join(script) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
class Group(object):
|
|
|
|
def __init__(self, org, name):
|
|
|
|
self.__org = org
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
|
|
|
class Organisation(object):
|
2016-04-12 02:42:13 +01:00
|
|
|
def __init__(self, name, ignore_imperative):
|
2016-04-11 20:25:55 +01:00
|
|
|
self.name = name
|
2016-04-12 02:42:13 +01:00
|
|
|
self.ignore_imperative = ignore_imperative
|
2016-04-11 20:25:55 +01:00
|
|
|
|
|
|
|
def add_user(self, name):
|
|
|
|
"""
|
|
|
|
Create a new user along with a certificate and key.
|
|
|
|
|
|
|
|
Returns a 'User' object or None if the user already exists.
|
|
|
|
"""
|
2016-04-12 02:42:13 +01:00
|
|
|
if self.ignore_imperative and is_imperative(self.name):
|
|
|
|
return None
|
2016-04-11 20:25:55 +01:00
|
|
|
if name not in self.users.keys():
|
|
|
|
output = taskd_cmd("add", "user", self.name, name,
|
|
|
|
capture_stdout=True)
|
|
|
|
key = RE_USERKEY.search(output)
|
|
|
|
if key is None:
|
|
|
|
msg = "Unable to find key while creating user {}."
|
|
|
|
raise TaskdError(msg.format(name))
|
|
|
|
|
|
|
|
generate_key(self.name, name)
|
2016-04-12 02:42:13 +01:00
|
|
|
newuser = User(self.name, name, key.group(1))
|
2016-04-11 20:25:55 +01:00
|
|
|
self._lazy_users[name] = newuser
|
|
|
|
return newuser
|
|
|
|
return None
|
|
|
|
|
2016-04-11 21:24:58 +01:00
|
|
|
def del_user(self, name):
|
|
|
|
"""
|
|
|
|
Delete a user and revoke its keys.
|
|
|
|
"""
|
2016-04-12 00:08:34 +01:00
|
|
|
if name in self.users.keys():
|
|
|
|
user = self.get_user(name)
|
2016-04-12 02:42:13 +01:00
|
|
|
if self.ignore_imperative and \
|
|
|
|
is_imperative(self.name, "users", user.key):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Work around https://bug.tasktools.org/browse/TD-40:
|
2016-04-12 00:08:34 +01:00
|
|
|
rmtree(mkpath(self.name, "users", user.key))
|
|
|
|
|
|
|
|
revoke_key(self.name, name)
|
|
|
|
del self._lazy_users[name]
|
2016-04-11 21:24:58 +01:00
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
def add_group(self, name):
|
|
|
|
"""
|
|
|
|
Create a new group.
|
|
|
|
|
|
|
|
Returns a 'Group' object or None if the group already exists.
|
|
|
|
"""
|
2016-04-12 02:42:13 +01:00
|
|
|
if self.ignore_imperative and is_imperative(self.name):
|
|
|
|
return None
|
2016-04-11 20:25:55 +01:00
|
|
|
if name not in self.groups.keys():
|
|
|
|
taskd_cmd("add", "group", self.name, name)
|
|
|
|
newgroup = Group(self.name, name)
|
|
|
|
self._lazy_groups[name] = newgroup
|
|
|
|
return newgroup
|
|
|
|
return None
|
|
|
|
|
2016-04-11 21:24:58 +01:00
|
|
|
def del_group(self, name):
|
|
|
|
"""
|
|
|
|
Delete a group.
|
|
|
|
"""
|
2016-04-12 00:08:34 +01:00
|
|
|
if name in self.users.keys():
|
2016-04-12 02:42:13 +01:00
|
|
|
if self.ignore_imperative and \
|
|
|
|
is_imperative(self.name, "groups", name):
|
|
|
|
return
|
2016-04-12 00:08:34 +01:00
|
|
|
taskd_cmd("remove", "group", self.name, name)
|
|
|
|
del self._lazy_groups[name]
|
2016-04-11 21:24:58 +01:00
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
def get_user(self, name):
|
|
|
|
return self.users.get(name)
|
|
|
|
|
|
|
|
@lazyprop
|
|
|
|
def users(self):
|
|
|
|
result = {}
|
|
|
|
for key in os.listdir(mkpath(self.name, "users")):
|
|
|
|
user = fetch_username(self.name, key)
|
|
|
|
if user is not None:
|
|
|
|
result[user] = User(self.name, user, key)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_group(self, name):
|
|
|
|
return self.groups.get(name)
|
|
|
|
|
|
|
|
@lazyprop
|
|
|
|
def groups(self):
|
|
|
|
result = {}
|
|
|
|
for group in os.listdir(mkpath(self.name, "groups")):
|
|
|
|
result[group] = Group(self.name, group)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class Manager(object):
|
2016-04-12 02:42:13 +01:00
|
|
|
def __init__(self, ignore_imperative=False):
|
|
|
|
"""
|
|
|
|
Instantiates an organisations manager.
|
|
|
|
|
|
|
|
If ignore_imperative is True, all actions that modify data are checked
|
|
|
|
whether they're created imperatively and if so, they will result in no
|
|
|
|
operation.
|
|
|
|
"""
|
|
|
|
self.ignore_imperative = ignore_imperative
|
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
def add_org(self, name):
|
|
|
|
"""
|
|
|
|
Create a new organisation.
|
|
|
|
|
|
|
|
Returns an 'Organisation' object or None if the organisation already
|
|
|
|
exists.
|
|
|
|
"""
|
|
|
|
if name not in self.orgs.keys():
|
|
|
|
taskd_cmd("add", "org", name)
|
2016-04-12 02:42:13 +01:00
|
|
|
neworg = Organisation(name, self.ignore_imperative)
|
2016-04-11 20:25:55 +01:00
|
|
|
self._lazy_orgs[name] = neworg
|
|
|
|
return neworg
|
|
|
|
return None
|
|
|
|
|
2016-04-11 21:24:58 +01:00
|
|
|
def del_org(self, name):
|
|
|
|
"""
|
|
|
|
Delete and revoke keys of an organisation with all its users and
|
|
|
|
groups.
|
|
|
|
"""
|
2016-04-12 00:08:34 +01:00
|
|
|
org = self.get_org(name)
|
|
|
|
if org is not None:
|
2016-04-12 02:42:13 +01:00
|
|
|
if self.ignore_imperative and is_imperative(name):
|
|
|
|
return
|
2016-04-12 00:08:34 +01:00
|
|
|
for user in org.users.keys():
|
|
|
|
org.del_user(user)
|
|
|
|
for group in org.groups.keys():
|
|
|
|
org.del_group(group)
|
|
|
|
taskd_cmd("remove", "org", name)
|
|
|
|
del self._lazy_orgs[name]
|
2016-04-11 21:24:58 +01:00
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
def get_org(self, name):
|
|
|
|
return self.orgs.get(name)
|
|
|
|
|
|
|
|
@lazyprop
|
|
|
|
def orgs(self):
|
|
|
|
result = {}
|
|
|
|
for org in os.listdir(mkpath()):
|
2016-04-12 02:42:13 +01:00
|
|
|
result[org] = Organisation(org, self.ignore_imperative)
|
2016-04-11 20:25:55 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class OrganisationType(click.ParamType):
|
|
|
|
name = 'organisation'
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
org = Manager().get_org(value)
|
|
|
|
if org is None:
|
|
|
|
self.fail("Organisation {} does not exist.".format(value))
|
|
|
|
return org
|
|
|
|
|
|
|
|
ORGANISATION = OrganisationType()
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
@click.group()
|
2016-04-12 03:57:03 +01:00
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
2016-04-11 10:52:02 +01:00
|
|
|
"""
|
|
|
|
Manage Taskserver users and certificates
|
|
|
|
"""
|
2016-04-12 03:57:03 +01:00
|
|
|
for path in (CA_KEY, CA_CERT, CRL_FILE):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
msg = "CA setup not done or incomplete, missing file {}."
|
|
|
|
ctx.fail(msg.format(path))
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("list-users")
|
2016-04-11 20:25:55 +01:00
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
2016-04-11 10:52:02 +01:00
|
|
|
def list_users(organisation):
|
|
|
|
"""
|
|
|
|
List all users belonging to the specified organisation.
|
|
|
|
"""
|
2016-04-11 20:25:55 +01:00
|
|
|
label("The following users exists for {}:".format(organisation.name))
|
|
|
|
for user in organisation.users.values():
|
|
|
|
sys.stdout.write(user.name + "\n")
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("list-orgs")
|
|
|
|
def list_orgs():
|
|
|
|
"""
|
|
|
|
List available organisations
|
|
|
|
"""
|
|
|
|
label("The following organisations exist:")
|
2016-04-11 20:25:55 +01:00
|
|
|
for org in Manager().orgs:
|
|
|
|
sys.stdout.write(org.name + "\n")
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("get-uuid")
|
2016-04-11 20:25:55 +01:00
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
2016-04-11 10:52:02 +01:00
|
|
|
@click.argument("user")
|
|
|
|
def get_uuid(organisation, user):
|
|
|
|
"""
|
|
|
|
Get the UUID of the specified user belonging to the specified organisation.
|
|
|
|
"""
|
2016-04-11 20:25:55 +01:00
|
|
|
userobj = organisation.get_user(user)
|
|
|
|
if userobj is None:
|
|
|
|
msg = "User {} doesn't exist in organisation {}."
|
|
|
|
sys.exit(msg.format(userobj.name, organisation.name))
|
|
|
|
|
|
|
|
label("User {} has the following UUID:".format(userobj.name))
|
|
|
|
sys.stdout.write(user.key + "\n")
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("export-user")
|
2016-04-11 20:25:55 +01:00
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
2016-04-11 10:52:02 +01:00
|
|
|
@click.argument("user")
|
|
|
|
def export_user(organisation, user):
|
|
|
|
"""
|
|
|
|
Export user of the specified organisation as a series of shell commands
|
|
|
|
that can be used on the client side to easily import the certificates.
|
|
|
|
|
|
|
|
Note that the private key will be exported as well, so use this with care!
|
|
|
|
"""
|
2016-04-11 20:25:55 +01:00
|
|
|
userobj = organisation.get_user(user)
|
|
|
|
if userobj is None:
|
2016-04-11 10:52:02 +01:00
|
|
|
msg = "User {} doesn't exist in organisation {}."
|
2016-04-11 20:25:55 +01:00
|
|
|
sys.exit(msg.format(userobj.name, organisation.name))
|
2016-04-11 10:52:02 +01:00
|
|
|
|
2016-04-11 20:25:55 +01:00
|
|
|
sys.stdout.write(userobj.export())
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("add-org")
|
|
|
|
@click.argument("name")
|
2016-04-11 21:24:58 +01:00
|
|
|
def add_org(name):
|
2016-04-11 10:52:02 +01:00
|
|
|
"""
|
|
|
|
Create an organisation with the specified name.
|
|
|
|
"""
|
|
|
|
if os.path.exists(mkpath(name)):
|
|
|
|
msg = "Organisation with name {} already exists."
|
|
|
|
sys.exit(msg.format(name))
|
|
|
|
|
|
|
|
taskd_cmd("add", "org", name)
|
2016-04-12 02:42:13 +01:00
|
|
|
mark_imperative(name)
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
@cli.command("del-org")
|
|
|
|
@click.argument("name")
|
|
|
|
def del_org(name):
|
|
|
|
"""
|
|
|
|
Delete the organisation with the specified name.
|
|
|
|
|
|
|
|
All of the users and groups will be deleted as well and client certificates
|
|
|
|
will be revoked.
|
|
|
|
"""
|
|
|
|
Manager().del_org(name)
|
|
|
|
msg = ("Organisation {} deleted. Be sure to restart the Taskserver"
|
|
|
|
" using 'systemctl restart taskserver.service' in order for"
|
|
|
|
" the certificate revocation to apply.")
|
|
|
|
click.echo(msg.format(name), err=True)
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
@cli.command("add-user")
|
2016-04-11 20:25:55 +01:00
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
2016-04-11 10:52:02 +01:00
|
|
|
@click.argument("user")
|
2016-04-11 21:24:58 +01:00
|
|
|
def add_user(organisation, user):
|
2016-04-11 10:52:02 +01:00
|
|
|
"""
|
|
|
|
Create a user for the given organisation along with a client certificate
|
|
|
|
and print the key of the new user.
|
|
|
|
|
|
|
|
The client certificate along with it's public key can be shown via the
|
|
|
|
'export-user' subcommand.
|
|
|
|
"""
|
2016-04-11 20:25:55 +01:00
|
|
|
userobj = organisation.add_user(user)
|
|
|
|
if userobj is None:
|
|
|
|
msg = "User {} already exists in organisation {}."
|
|
|
|
sys.exit(msg.format(user, organisation))
|
2016-04-12 02:42:13 +01:00
|
|
|
else:
|
|
|
|
mark_imperative(organisation.name, "users", userobj.key)
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
@cli.command("del-user")
|
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
|
|
|
@click.argument("user")
|
|
|
|
def del_user(organisation, user):
|
|
|
|
"""
|
|
|
|
Delete a user from the given organisation.
|
|
|
|
|
|
|
|
This will also revoke the client certificate of the given user.
|
|
|
|
"""
|
|
|
|
organisation.del_user(user)
|
|
|
|
msg = ("User {} deleted. Be sure to restart the Taskserver using"
|
|
|
|
" 'systemctl restart taskserver.service' in order for the"
|
|
|
|
" certificate revocation to apply.")
|
|
|
|
click.echo(msg.format(user), err=True)
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
@cli.command("add-group")
|
2016-04-11 20:25:55 +01:00
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
2016-04-11 10:52:02 +01:00
|
|
|
@click.argument("group")
|
2016-04-11 21:24:58 +01:00
|
|
|
def add_group(organisation, group):
|
2016-04-11 10:52:02 +01:00
|
|
|
"""
|
|
|
|
Create a group for the given organisation.
|
|
|
|
"""
|
2016-04-12 02:42:13 +01:00
|
|
|
groupobj = organisation.add_group(group)
|
|
|
|
if groupobj is None:
|
2016-04-11 10:52:02 +01:00
|
|
|
msg = "Group {} already exists in organisation {}."
|
|
|
|
sys.exit(msg.format(group, organisation))
|
2016-04-12 02:42:13 +01:00
|
|
|
else:
|
|
|
|
mark_imperative(organisation.name, "groups", groupobj.name)
|
2016-04-11 10:52:02 +01:00
|
|
|
|
|
|
|
|
2016-04-12 00:08:34 +01:00
|
|
|
@cli.command("del-group")
|
|
|
|
@click.argument("organisation", type=ORGANISATION)
|
|
|
|
@click.argument("group")
|
|
|
|
def del_group(organisation, group):
|
|
|
|
"""
|
|
|
|
Delete a group from the given organisation.
|
|
|
|
"""
|
|
|
|
organisation.del_group(group)
|
|
|
|
click("Group {} deleted.".format(group), err=True)
|
|
|
|
|
|
|
|
|
2016-04-11 21:24:58 +01:00
|
|
|
def add_or_delete(old, new, add_fun, del_fun):
|
|
|
|
"""
|
|
|
|
Given an 'old' and 'new' list, figure out the intersections and invoke
|
|
|
|
'add_fun' against every element that is not in the 'old' list and 'del_fun'
|
|
|
|
against every element that is not in the 'new' list.
|
|
|
|
|
|
|
|
Returns a tuple where the first element is the list of elements that were
|
|
|
|
added and the second element consisting of elements that were deleted.
|
|
|
|
"""
|
|
|
|
old_set = set(old)
|
|
|
|
new_set = set(new)
|
|
|
|
to_delete = old_set - new_set
|
|
|
|
to_add = new_set - old_set
|
|
|
|
for elem in to_delete:
|
|
|
|
del_fun(elem)
|
|
|
|
for elem in to_add:
|
|
|
|
add_fun(elem)
|
|
|
|
return to_add, to_delete
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("process-json")
|
|
|
|
@click.argument('json-file', type=click.File('rb'))
|
|
|
|
def process_json(json_file):
|
|
|
|
"""
|
|
|
|
Create and delete users, groups and organisations based on a JSON file.
|
|
|
|
|
|
|
|
The structure of this file is exactly the same as the
|
|
|
|
'services.taskserver.organisations' option of the NixOS module and is used
|
|
|
|
for declaratively adding and deleting users.
|
|
|
|
|
|
|
|
Hence this subcommand is not recommended outside of the scope of the NixOS
|
|
|
|
module.
|
|
|
|
"""
|
|
|
|
data = json.load(json_file)
|
|
|
|
|
2016-04-12 02:42:13 +01:00
|
|
|
mgr = Manager(ignore_imperative=True)
|
2016-04-11 21:24:58 +01:00
|
|
|
add_or_delete(mgr.orgs.keys(), data.keys(), mgr.add_org, mgr.del_org)
|
|
|
|
|
|
|
|
for org in mgr.orgs.values():
|
2016-04-12 02:42:13 +01:00
|
|
|
if is_imperative(org.name):
|
|
|
|
continue
|
2016-04-11 21:24:58 +01:00
|
|
|
add_or_delete(org.users.keys(), data[org.name]['users'],
|
|
|
|
org.add_user, org.del_user)
|
|
|
|
add_or_delete(org.groups.keys(), data[org.name]['groups'],
|
|
|
|
org.add_group, org.del_group)
|
|
|
|
|
|
|
|
|
2016-04-11 10:52:02 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|