예제 #1
0
def key(username, want_key, scopes, duration, text, customer, all, force):
    """
    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))

    if all and want_key:
        raise click.UsageError('Can only set API key with "--username".')

    scopes = [Scope(s) for s in scopes] or [Scope.admin, Scope.write, Scope.read]
    expires = datetime.utcnow() + timedelta(seconds=duration) if duration else None
    text = text or 'Created by alertad script'

    def create_key(admin, key=None):
        key = ApiKey(
            user=admin,
            key=key,
            scopes=scopes,
            expire_time=expires,
            text=text,
            customer=customer
        )
        try:
            key = key.create()
        except Exception as e:
            click.echo('ERROR: {}'.format(e))
        else:
            return key

    if all:
        for admin in current_app.config['ADMIN_USERS']:
            keys = [k for k in ApiKey.find_by_user(admin) if k.scopes == scopes]
            if keys and not force:
                key = keys[0]
            else:
                key = create_key(admin)
            click.echo('{:40} {}'.format(key.key, key.user))

    elif username:
        keys = [k for k in ApiKey.find_by_user(username) if k.scopes == scopes]
        if want_key:
            found_key = [k for k in keys if k.key == want_key]
            if found_key:
                key = found_key[0]
            else:
                key = create_key(username, key=want_key)
        else:
            if keys and not force:
                key = keys[0]
            else:
                key = create_key(username)
        if key:
            click.echo(key.key)
        else:
            sys.exit(1)

    else:
        raise click.UsageError("Must set '--username' or use '--all'")
예제 #2
0
파일: keys.py 프로젝트: xtavras/alerta
def list_keys():
    query = qb.from_params(request.args, customers=g.customers)
    total = ApiKey.count(query)
    paging = Page.from_params(request.args, total)
    if not current_app.config['AUTH_REQUIRED']:
        keys = ApiKey.find_all(query,
                               page=paging.page,
                               page_size=paging.page_size)
    elif Scope.admin in g.scopes or Scope.admin_keys in g.scopes:
        keys = ApiKey.find_all(query,
                               page=paging.page,
                               page_size=paging.page_size)
    elif not g.get('login', None):
        raise ApiError("Must define 'user' to list user keys", 400)
    else:
        keys = ApiKey.find_by_user(user=g.login)

    if keys:
        return jsonify(status='ok',
                       page=paging.page,
                       pageSize=paging.page_size,
                       pages=paging.pages,
                       more=paging.has_more,
                       keys=[key.serialize for key in keys],
                       total=total)
    else:
        return jsonify(status='ok',
                       page=paging.page,
                       pageSize=paging.page_size,
                       pages=paging.pages,
                       more=paging.has_more,
                       message='not found',
                       keys=[],
                       total=0)
예제 #3
0
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 = ApiKey.find_by_user(admin)
        except Exception as e:
            click.echo('ERROR: {}'.format(e))
        else:
            for key in keys:
                click.echo('{:40} {}'.format(key.key, key.user))
예제 #4
0
파일: commands.py 프로젝트: guardian/alerta
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 = ApiKey.find_by_user(admin)
        except Exception as e:
            click.echo('ERROR: {}'.format(e))
        else:
            for key in keys:
                click.echo('{:40} {}'.format(key.key, key.user))
예제 #5
0
def keys():
    """
    List admin API keys.
    """
    for admin in current_app.config['ADMIN_USERS']:
        try:
            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))
예제 #6
0
def list_keys():
    if not current_app.config['AUTH_REQUIRED']:
        keys = ApiKey.find_all()
    elif 'admin' in g.scopes or 'admin:keys' in g.scopes:
        keys = ApiKey.find_all()
    elif not g.get('user', None):
        raise ApiError("Must define 'user' to list user keys", 400)
    else:
        keys = ApiKey.find_by_user(g.user)

    if keys:
        return jsonify(status="ok",
                       keys=[key.serialize for key in keys],
                       total=len(keys))
    else:
        return jsonify(status="ok", message="not found", keys=[], total=0)
예제 #7
0
파일: keys.py 프로젝트: ngohoa211/alerta
def list_keys():
    if not current_app.config['AUTH_REQUIRED']:
        keys = ApiKey.find_all()
    elif Scope.admin in g.scopes or Scope.admin_keys in g.scopes:
        keys = ApiKey.find_all()
    elif not g.get('login', None):
        raise ApiError("Must define 'user' to list user keys", 400)
    else:
        keys = ApiKey.find_by_user(user=g.login)

    if keys:
        return jsonify(status='ok',
                       keys=[key.serialize for key in keys],
                       total=len(keys))
    else:
        return jsonify(status='ok', message='not found', keys=[], total=0)
예제 #8
0
파일: keys.py 프로젝트: kattunga/alerta
def list_keys():
    if not current_app.config['AUTH_REQUIRED']:
        keys = ApiKey.find_all()
    elif 'admin' in g.scopes or 'admin:keys' in g.scopes:
        keys = ApiKey.find_all()
    elif not g.get('user', None):
            raise ApiError("Must define 'user' to list user keys", 400)
    else:
        keys = ApiKey.find_by_user(g.user)

    if keys:
        return jsonify(
            status="ok",
            keys=[key.serialize for key in keys],
            total=len(keys)
        )
    else:
        return jsonify(
            status="ok",
            message="not found",
            keys=[],
            total=0
        )
예제 #9
0
파일: keys.py 프로젝트: guardian/alerta
def list_keys():
    if not current_app.config['AUTH_REQUIRED']:
        keys = ApiKey.find_all()
    elif Scope.admin in g.scopes or Scope.admin_keys in g.scopes:
        keys = ApiKey.find_all()
    elif not g.get('login', None):
        raise ApiError("Must define 'user' to list user keys", 400)
    else:
        keys = ApiKey.find_by_user(user=g.login)

    if keys:
        return jsonify(
            status='ok',
            keys=[key.serialize for key in keys],
            total=len(keys)
        )
    else:
        return jsonify(
            status='ok',
            message='not found',
            keys=[],
            total=0
        )