3
3
# Copyright (C) 2023 Collabora Limited
4
4
# Author: Guillaume Tucker <[email protected] >
5
5
# Author: Jeny Sadadia <[email protected] >
6
+ # Author: Paweł Wieczorek <[email protected] >
6
7
7
8
"""Tool to manage KernelCI API users"""
8
9
10
+ import getpass
9
11
import json
10
12
11
13
import click
12
14
13
15
import kernelci .api
14
16
import kernelci .config
15
- from . import Args , kci
17
+ from . import Args , kci , split_attributes
16
18
17
19
18
20
@kci .group (name = 'user' )
@@ -25,9 +27,83 @@ def kci_user():
25
27
@Args .api
26
28
@Args .indent
27
29
def whoami (config , api , indent , secrets ):
28
- """Use whoami to get current user info with API authentication"""
30
+ """Get the current user's details with API authentication"""
29
31
configs = kernelci .config .load (config )
30
32
api_config = configs ['api' ][api ]
31
33
api = kernelci .api .get_api (api_config , secrets .api .token )
32
34
data = api .whoami ()
33
35
click .echo (json .dumps (data , indent = indent ))
36
+
37
+
38
+ @kci_user .command
39
+ @click .argument ('attributes' , nargs = - 1 )
40
+ @Args .config
41
+ @Args .api
42
+ @Args .indent
43
+ def find (attributes , config , api , indent ):
44
+ """Find user profiles with arbitrary attributes"""
45
+ configs = kernelci .config .load (config )
46
+ api_config = configs ['api' ][api ]
47
+ api = kernelci .api .get_api (api_config )
48
+ users = api .get_user_profiles (split_attributes (attributes ))
49
+ data = json .dumps (users , indent = indent )
50
+ echo = click .echo_via_pager if len (users ) > 1 else click .echo
51
+ echo (data )
52
+
53
+
54
+ @kci_user .command
55
+ @click .argument ('username' )
56
+ @Args .config
57
+ @Args .api
58
+ @Args .indent
59
+ @click .option ('--scope' , multiple = True , help = "Security scope(s)" )
60
+ def token (username , config , api , indent , scope ):
61
+ """Create a new API token using a user name and password"""
62
+ password = getpass .getpass ()
63
+ configs = kernelci .config .load (config )
64
+ api_config = configs ['api' ][api ]
65
+ api = kernelci .api .get_api (api_config )
66
+ user_token = api .create_token (username , password , scope )
67
+ click .echo (json .dumps (user_token , indent = indent ))
68
+
69
+
70
+ @kci_user .group (name = 'password' )
71
+ def user_password ():
72
+ """Manage user passwords"""
73
+
74
+
75
+ @user_password .command
76
+ @click .argument ('username' )
77
+ @Args .config
78
+ @Args .api
79
+ def update (username , config , api ):
80
+ """Update the password for a given user"""
81
+ current = getpass .getpass ("Current password: " )
82
+ new = getpass .getpass ("New password: " )
83
+ retyped = getpass .getpass ("Retype new password: " )
84
+ if new != retyped :
85
+ raise click .ClickException ("Sorry, passwords do not match" )
86
+ configs = kernelci .config .load (config )
87
+ api_config = configs ['api' ][api ]
88
+ api = kernelci .api .get_api (api_config )
89
+ api .change_password (username , current , new )
90
+
91
+
92
+ @kci_user .command (secrets = True )
93
+ @click .argument ('username' )
94
+ @click .argument ('email' )
95
+ @Args .config
96
+ @Args .api
97
+ def add (username , email , config , api , secrets ):
98
+ """Add a new user account"""
99
+ profile = {
100
+ 'email' : email ,
101
+ }
102
+ password = getpass .getpass ()
103
+ retyped = getpass .getpass ("Confirm password: " )
104
+ if password != retyped :
105
+ raise click .ClickException ("Sorry, passwords do not match" )
106
+ configs = kernelci .config .load (config )
107
+ api_config = configs ['api' ][api ]
108
+ api = kernelci .api .get_api (api_config , secrets .api .token )
109
+ api .create_user (username , password , profile )
0 commit comments