Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Fixing validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasraabe committed Aug 22, 2017
1 parent d49c200 commit 4ae04f2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
12 changes: 1 addition & 11 deletions ovmm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import click

from ovmm import __version__
from .commands.add_user import add_user
from .commands.backup_user import backup_user
from .commands.count_user import count_user
Expand Down Expand Up @@ -35,17 +34,8 @@ def get_command(self, ctx, cmd_name):
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))


# Add version flag
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('Version ' + __version__)
ctx.exit()


@click.group(cls=AliasedGroup, context_settings=CONTEXT_SETTINGS)
@click.option('--version', is_flag=True, callback=print_version,
expose_value=False, is_eager=True)
@click.version_option()
def main():
"""These scripts help the server admin to perform user related tasks."""
if os.geteuid() != 0:
Expand Down
6 changes: 4 additions & 2 deletions ovmm/commands/add_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ovmm.commands.delete_user import delete_user
from ovmm.config.static import HOME, OSF, USER_CONFIGS
from ovmm.config.environment import PASSWORD_LENGTH
from ovmm.handlers.nginx import NginxConfigHandler
from ovmm.handlers.postgres import PostgreSQLDatabaseHandler
from ovmm.handlers.samba import SambaConfigHandler
Expand All @@ -22,7 +23,7 @@
validate_password, validate_telephone

try:
from ovmm.config.environment import ADMIN_PASSWORD, PASSWORD_LENGTH
from ovmm.config.environment import ADMIN_PASSWORD
except ImportError:
pass

Expand All @@ -44,7 +45,8 @@
prompt='Telephone number of user',
callback=validate_telephone, default=DUMMY_USER['telephone'])
@click.option('--password', '-p', help='Set password of user',
callback=validate_password, )
prompt='Set password of user', default=PASSWORD,
callback=validate_password)
def add_user(user_name: str, full_name: str, email: str, telephone: str,
password: str):
"""Creates a new user.
Expand Down
7 changes: 5 additions & 2 deletions ovmm/commands/delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

import click
# noinspection PyUnresolvedReferences
from plumbum.cmd import sudo

from ovmm.commands.backup_user import backup_user
Expand Down Expand Up @@ -58,7 +59,8 @@ def delete_user(ctx, user_name: str):

exception_raised = False
try:
sudo['pkill', '-u', dict_user['user_name']](retcode=2)
# retcode 1: unkown, retcode 2: invalid user name
sudo['pkill', '-u', dict_user['user_name']](retcode=(1, 2))
sudo['userdel', '--remove', dict_user['user_name']](retcode=(0, 6))
click.secho('SUCCESS: Removed user and home directory.'
.format(dict_user['user_name']), fg='green')
Expand Down Expand Up @@ -98,9 +100,10 @@ def delete_user(ctx, user_name: str):
pass

try:
# retcode 0: everythings fine, retcode 1: file not found
sudo['rm', os.path.join(
HOME, OSF, USER_CONFIGS,
'{}.txt'.format(dict_user['user_name']))](retcode=1)
'{}.txt'.format(dict_user['user_name']))](retcode=(0, 1))
except Exception as e:
click.secho(
'ERROR: User configuration text file could not be deleted.',
Expand Down
3 changes: 2 additions & 1 deletion ovmm/commands/upgrade_statics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def upgrade_statics():
# Extract and overwrite statics for each user
for user in user_list:
sudo['-u', user, '7z', 'x', archive,
'-o' + os.path.join(HOME, user), '-y']()
'-o' + os.path.join('/home', user), '-y']()
click.secho('SUCCESS: Updated statics for {}'.format(user), fg='green')

click.echo('{:-^60}\n'.format(' Process: End '))
20 changes: 17 additions & 3 deletions ovmm/config/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import click
import yaml

from .static import HOME, OSF


def check_list_integer(func):
"""Decorator which checks a list if it contains only integers."""
Expand Down Expand Up @@ -50,7 +52,6 @@ def literal_eval_lc(lc_string: str) -> list:
'WARNING: The password is not correctly set and defaults to 8.',
fg='yellow')
PASSWORD_LENGTH = 8
pass


# Environment variables
Expand All @@ -76,8 +77,8 @@ def literal_eval_lc(lc_string: str) -> list:
literal_eval_lc(os.environ['OVMM_REDIS_RANGE'])),
}
except KeyError:
click.secho('ERROR: Environment variables could not be loaded. Have you\n'
'run sudo ovmm initialise?', fg='red')
click.secho('ERROR: Environment variables could not be loaded. Have\n'
'you run sudo ovmm initialise?', fg='red')
pass
except ValueError:
click.secho('ERROR: Check if ports and password length are integers\n'
Expand All @@ -86,4 +87,17 @@ def literal_eval_lc(lc_string: str) -> list:
fg='red')
pass
else:
# Required
ADMIN_PASSWORD = conf['OTREE_ADMIN_PASSWORD']
PSQL_CONN = dict(conf['OVMM_PSQL_CONN'])
PSQL_TABLE = conf['OVMM_PSQL_TABLE']
PORT_RANGES = {
'daphne_port': list(
literal_eval_lc(conf['OVMM_DAPHNE_RANGE'])),
'ssl_port': list(
literal_eval_lc(conf['OVMM_SSL_RANGE'])),
'redis_port': list(
literal_eval_lc(conf['OVMM_REDIS_RANGE'])),
}

click.secho('SUCCESS: Configuration loaded.', fg='green')
10 changes: 5 additions & 5 deletions ovmm/prompts/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
from ovmm.config.environment import PASSWORD_LENGTH


def validate_user_name(value):
def validate_user_name(ctx, param, value):
if re.fullmatch(r'^[a-z][a-z0-9_]{5,}', value):
return value
else:
raise click.BadParameter(
'User names can only contain 0-9, a-z and _.')


def validate_email(value):
def validate_email(ctx, param, value):
if re.fullmatch(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)',
value):
return value
else:
raise click.BadParameter('email address is not valid.')
raise click.BadParameter('Email address is not valid.')


def validate_password(value):
def validate_password(ctx, param, value):
if value in ['password', 'hallo123', 'admin']:
raise click.BadParameter('You are kidding, right?')
elif re.fullmatch(r'[A-Za-z0-9]{{{},}}'.format(PASSWORD_LENGTH), value):
Expand All @@ -34,5 +34,5 @@ def validate_password(value):
raise click.BadParameter('Use only alphanumeric characters.')


def validate_telephone(value):
def validate_telephone(ctx, param, value):
return ''.join(filter(str.isdigit, value))

0 comments on commit 4ae04f2

Please sign in to comment.