-
Notifications
You must be signed in to change notification settings - Fork 109
Rework kci user
with new framework
#2141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
# | ||
# Copyright (C) 2023 Collabora Limited | ||
# Author: Guillaume Tucker <[email protected]> | ||
# Author: Jeny Sadadia <[email protected]> | ||
# Author: Paweł Wieczorek <[email protected]> | ||
|
||
"""Tool to manage KernelCI API users""" | ||
|
||
import getpass | ||
import json | ||
|
||
import click | ||
|
||
import kernelci.api | ||
import kernelci.config | ||
from . import Args, kci, split_attributes | ||
|
||
|
||
@kci.group(name='user') | ||
def kci_user(): | ||
"""Manage user accounts""" | ||
|
||
|
||
@kci_user.command(secrets=True) | ||
@Args.config | ||
@Args.api | ||
@Args.indent | ||
def whoami(config, api, indent, secrets): | ||
"""Get the current user's details with API authentication""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
data = api.whoami() | ||
click.echo(json.dumps(data, indent=indent)) | ||
|
||
|
||
@kci_user.command | ||
@click.argument('attributes', nargs=-1) | ||
@Args.config | ||
@Args.api | ||
@Args.indent | ||
def find(attributes, config, api, indent): | ||
"""Find user profiles with arbitrary attributes""" | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config) | ||
users = api.get_user_profiles(split_attributes(attributes)) | ||
data = json.dumps(users, indent=indent) | ||
echo = click.echo_via_pager if len(users) > 1 else click.echo | ||
echo(data) | ||
|
||
|
||
@kci_user.command | ||
@click.argument('username') | ||
@Args.config | ||
@Args.api | ||
@Args.indent | ||
@click.option('--scope', multiple=True, help="Security scope(s)") | ||
def token(username, config, api, indent, scope): | ||
"""Create a new API token using a user name and password""" | ||
password = getpass.getpass() | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config) | ||
user_token = api.create_token(username, password, scope) | ||
click.echo(json.dumps(user_token, indent=indent)) | ||
|
||
|
||
@kci_user.group(name='password') | ||
def user_password(): | ||
"""Manage user passwords""" | ||
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After I added sub-commands to this group, it became There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this was as per the proposed syntax on the GitHub discussion: It means all the user-related commands are grouped under |
||
|
||
|
||
@user_password.command | ||
@click.argument('username') | ||
@Args.config | ||
@Args.api | ||
def update(username, config, api): | ||
"""Update the password for a given user""" | ||
current = getpass.getpass("Current password: ") | ||
new = getpass.getpass("New password: ") | ||
retyped = getpass.getpass("Retype new password: ") | ||
if new != retyped: | ||
raise click.ClickException("Sorry, passwords do not match") | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config) | ||
api.change_password(username, current, new) | ||
|
||
|
||
@kci_user.command(secrets=True) | ||
@click.argument('username') | ||
@click.argument('email') | ||
@Args.config | ||
@Args.api | ||
def add(username, email, config, api, secrets): | ||
"""Add a new user account""" | ||
profile = { | ||
'email': email, | ||
} | ||
password = getpass.getpass() | ||
retyped = getpass.getpass("Confirm password: ") | ||
if password != retyped: | ||
raise click.ClickException("Sorry, passwords do not match") | ||
configs = kernelci.config.load(config) | ||
api_config = configs['api'][api] | ||
api = kernelci.api.get_api(api_config, secrets.api.token) | ||
api.create_user(username, password, profile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are unused imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it is required to register sub-commands. A comment about it would have been helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's quite implicit, which is nice in a way as it means no additional code is required but it's against the Zen of Python. I'll add some comments there.