forked from alerta/alerta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
129 lines (109 loc) · 4.04 KB
/
commands.py
File metadata and controls
129 lines (109 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import click
from flask import current_app
from flask.cli import FlaskGroup, with_appcontext
from alerta.app import db
from alerta.auth.utils import generate_password_hash
from alerta.models.enums import Scope
from alerta.models.key import ApiKey
from alerta.models.user import User
def _create_app(info):
from alerta.app import create_app
return create_app()
@click.group(cls=FlaskGroup, create_app=_create_app)
def cli():
pass
@cli.command('key', short_help='Create an admin API key')
@click.option('--username', '-u', help='Admin user')
@click.option('--key', '-K', help='API key (default=random UUID)')
@click.option('--all', is_flag=True, help='Create API keys for all admins')
@with_appcontext
def key(username, key, all):
"""Create an admin API key."""
if username and username not in current_app.config['ADMIN_USERS']:
raise click.UsageError('User {} not an admin'.format(username))
def create_key(admin, key):
key = ApiKey(
user=admin,
key=key,
scopes=[Scope.admin, Scope.write, Scope.read],
text='Admin key created by alertad script',
expire_time=None
)
try:
db.get_db() # init db on global app context
key = key.create()
except Exception as e:
click.echo('ERROR: {}'.format(e))
else:
click.echo('{} {}'.format(key.key, key.user))
if all:
for admin in current_app.config['ADMIN_USERS']:
create_key(admin, key)
elif username:
create_key(username, key)
else:
raise click.UsageError("Must set '--username' or use '--all'")
@cli.command('keys', short_help='List admin API keys')
@with_appcontext
def keys():
"""List admin API keys."""
for admin in current_app.config['ADMIN_USERS']:
try:
db.get_db() # init db on global app context
keys = [k for k in ApiKey.find_by_user(admin) if Scope.admin in k.scopes]
except Exception as e:
click.echo('ERROR: {}'.format(e))
else:
for key in keys:
click.echo('{:40} {}'.format(key.key, key.user))
@cli.command('user', short_help='Create admin user')
@click.option('--username', '-u', help='Admin user')
@click.password_option()
@click.option('--all', is_flag=True, help='Create users for all admins')
@with_appcontext
def user(username, password, all):
"""Create admin users (BasicAuth only)."""
if current_app.config['AUTH_PROVIDER'] != 'basic':
raise click.UsageError('Not required for {} admin users'.format(current_app.config['AUTH_PROVIDER']))
if username and username not in current_app.config['ADMIN_USERS']:
raise click.UsageError('User {} not an admin'.format(username))
if not username and not all:
raise click.UsageError('Missing option "--username".')
def create_user(admin):
email = admin if '@' in admin else None
user = User(
name='Admin user',
login=admin,
password=generate_password_hash(password),
roles=['admin'],
text='Created by alertad script',
email=email,
email_verified=bool(email)
)
try:
db.get_db() # init db on global app context
user = user.create()
except Exception as e:
click.echo('ERROR: {}'.format(e))
else:
click.echo('{} {}'.format(user.id, user.name))
if all:
for admin in current_app.config['ADMIN_USERS']:
create_user(admin)
else:
create_user(username)
@cli.command('users', short_help='List admin users')
@with_appcontext
def users():
"""List admin users."""
for admin in current_app.config['ADMIN_USERS']:
try:
db.get_db() # init db on global app context
user = User.find_by_username(admin)
except Exception as e:
click.echo('ERROR: {}'.format(e))
else:
if user:
click.echo('{} {}'.format(user.id, user.name))
if __name__ == '__main__':
cli()