-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprofile.py
More file actions
192 lines (148 loc) · 5.45 KB
/
profile.py
File metadata and controls
192 lines (148 loc) · 5.45 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from click import style
import code42cli.password as password
from code42cli.cmds.search.cursor_store import get_all_cursor_stores_for_profile
from code42cli.config import config_accessor
from code42cli.config import ConfigAccessor
from code42cli.config import NoConfigProfileError
from code42cli.errors import Code42CLIError
class Code42Profile:
def __init__(self, profile):
self._profile = profile
@property
def name(self):
return self._profile.name
@property
def authority_url(self):
return self._profile[ConfigAccessor.AUTHORITY_KEY]
@property
def username(self):
return self._profile[ConfigAccessor.USERNAME_KEY]
@property
def ignore_ssl_errors(self):
return self._profile[ConfigAccessor.IGNORE_SSL_ERRORS_KEY]
@property
def use_v2_file_events(self):
return self._profile.get(ConfigAccessor.USE_V2_FILE_EVENTS_KEY)
@property
def api_client_auth(self):
return self._profile.get(ConfigAccessor.API_CLIENT_AUTH_KEY)
@property
def has_stored_password(self):
stored_password = password.get_stored_password(self)
return stored_password is not None and stored_password != ""
def get_password(self):
pwd = password.get_stored_password(self)
if not pwd:
pwd = password.get_password_from_prompt()
return pwd
def __str__(self):
return (
f"{self.name}: Username={self.username}, Authority URL={self.authority_url}"
)
def _get_profile(profile_name=None):
"""Returns the profile for the given name."""
config_profile = config_accessor.get_profile(profile_name)
return Code42Profile(config_profile)
def get_profile(profile_name=None):
if profile_name is None:
validate_default_profile()
try:
return _get_profile(profile_name)
except NoConfigProfileError as ex:
raise Code42CLIError(str(ex), help=CREATE_PROFILE_HELP)
def default_profile_exists():
try:
profile = _get_profile()
return profile.name and profile.name != ConfigAccessor.DEFAULT_VALUE
except NoConfigProfileError:
return False
def is_default_profile(name):
if default_profile_exists():
default = get_profile()
return name == default.name
def validate_default_profile():
if not default_profile_exists():
existing_profiles = get_all_profiles()
if not existing_profiles:
raise Code42CLIError("No existing profile.", help=CREATE_PROFILE_HELP)
else:
raise Code42CLIError(
"No default profile set.",
help=_get_set_default_profile_help(existing_profiles),
)
def profile_exists(profile_name=None):
try:
_get_profile(profile_name)
return True
except NoConfigProfileError:
return False
def switch_default_profile(profile_name):
profile = get_profile(profile_name) # Handles if profile does not exist.
config_accessor.switch_default_profile(profile.name)
def create_profile(
name, server, username, ignore_ssl_errors, use_v2_file_events, api_client_auth
):
if profile_exists(name):
raise Code42CLIError(f"A profile named '{name}' already exists.")
config_accessor.create_profile(
name, server, username, ignore_ssl_errors, use_v2_file_events, api_client_auth
)
def delete_profile(profile_name):
profile = _get_profile(profile_name)
profile_name = profile.name
if password.get_stored_password(profile) is not None:
password.delete_password(profile)
cursor_stores = get_all_cursor_stores_for_profile(profile_name)
for store in cursor_stores:
store.clean()
config_accessor.delete_profile(profile_name)
def update_profile(
name, server, username, ignore_ssl_errors, use_v2_file_events, api_client_auth=None
):
config_accessor.update_profile(
name, server, username, ignore_ssl_errors, use_v2_file_events, api_client_auth
)
def get_all_profiles():
profiles = [
Code42Profile(profile) for profile in config_accessor.get_all_profiles()
]
return profiles
def get_stored_password(profile_name=None):
profile = get_profile(profile_name)
return password.get_stored_password(profile)
def set_password(new_password, profile_name=None):
profile = get_profile(profile_name)
password.set_password(profile, new_password)
CREATE_PROFILE_HELP = (
"\nTo add a profile with username/password authentication, use:\n{}".format(
style(
"\tcode42 profile create "
"--name <profile-name> "
"--server <authority-URL> "
"--username <username>\n",
bold=True,
)
)
+ "\nOr to add a profile with API client authentication, use:\n{}".format(
style(
"\tcode42 profile create-api-client "
"--name <profile-name> "
"--server <authority-URL>"
"--api-client-id <api-client-id> "
"--secret <api-client-secret>\n",
bold=True,
)
)
)
def _get_set_default_profile_help(existing_profiles):
existing_profiles = [str(profile) for profile in existing_profiles]
help_msg = """
Use the --profile flag to specify which profile to use.
To set the default profile (used whenever --profile argument is not provided), use:
{}
Existing profiles:
\t{}""".format(
style("code42 profile use <profile-name>", bold=True),
"\n\t".join(existing_profiles),
)
return help_msg