-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
238 lines (218 loc) · 8.21 KB
/
Copy pathusers.py
File metadata and controls
238 lines (218 loc) · 8.21 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Models for users and tools for loading users config."""
import logging
import os
from time import time
from typing import Any
from typing import Dict
from typing import List
from typing import Tuple
from typing import cast
from jsonschema import validate
from dm_mac.utils import load_json_config
logger: logging.Logger = logging.getLogger(__name__)
CONFIG_SCHEMA: Dict[str, Any] = {
"type": "array",
"items": {
"type": "object",
"properties": {
"fob_codes": {
"type": "array",
"items": {"type": "string"},
"description": "List of fob codes for user.",
},
"account_id": {
"type": "string",
"description": "Unique Account ID for user.",
},
"full_name": {"type": "string", "description": "Full name of user."},
"first_name": {"type": "string", "description": "First name of user."},
"last_name": {"type": "string", "description": "Last name of user."},
"preferred_name": {
"type": "string",
"description": "Preferred name of user.",
},
"email": {"type": "string", "description": "User email address."},
"expiration_ymd": {
"type": "string",
"description": "User membership expiration in YYYY-MM-DD format.",
},
"authorizations": {
"type": "array",
"items": {"type": "string"},
"description": "List of authorized field names for user.",
},
"oops_override": {
"type": "boolean",
"description": "Whether user can perform override logins on "
"oopsed/locked-out machines.",
},
},
"required": [
"fob_codes",
"account_id",
"full_name",
"first_name",
"last_name",
"preferred_name",
"email",
"expiration_ymd",
"authorizations",
],
"additionalProperties": False,
},
}
class User:
"""Class representing one user."""
def __init__(
self,
fob_codes: List[str],
account_id: str,
full_name: str,
first_name: str,
last_name: str,
preferred_name: str,
email: str,
expiration_ymd: str,
authorizations: List[str],
oops_override: bool = False,
):
"""Initialize one user."""
self.fob_codes: List[str] = fob_codes
self.account_id: str = account_id
self.full_name: str = full_name
self.first_name: str = first_name
self.last_name: str = last_name
self.preferred_name: str = preferred_name
self.email: str = email
self.expiration_ymd: str = expiration_ymd
self.authorizations: List[str] = authorizations
self.oops_override: bool = oops_override
def __eq__(self, other: Any) -> bool:
"""Check equality between Users."""
if not isinstance(other, User):
return NotImplemented
return self.account_id == other.account_id
def __repr__(self) -> str:
"""Return a string representation of the user."""
return f"User(account_id={self.account_id}, full_name={self.full_name})"
@property
def as_dict(self) -> Dict[str, Any]:
"""Return a dict representation of this user."""
return {
"account_id": self.account_id,
"authorizations": self.authorizations,
"email": self.email,
"expiration_ymd": self.expiration_ymd,
"fob_codes": self.fob_codes,
"full_name": self.full_name,
"first_name": self.first_name,
"last_name": self.last_name,
"preferred_name": self.preferred_name,
"oops_override": self.oops_override,
}
class UsersConfig:
"""Class representing users configuration file."""
def __init__(self) -> None:
"""Initialize UsersConfig."""
logger.debug("Initializing UsersConfig")
self.users_by_fob: Dict[str, User] = {}
self.users: List[User] = []
udict: Dict[str, Any]
fob: str
for udict in self._load_and_validate_config():
user: User = User(**udict)
self.users.append(user)
for fob in user.fob_codes:
self.users_by_fob[fob] = user
self.load_time: float = time()
self.file_mtime: float = os.path.getmtime(self._get_config_path())
def _get_config_path(self) -> str:
"""Get the path to the users config file."""
if "USERS_CONFIG" in os.environ:
return os.environ["USERS_CONFIG"]
return "users.json"
def _load_and_validate_config(self) -> List[Dict[str, Any]]:
"""Load and validate the config file."""
config: List[Dict[str, Any]] = cast(
List[Dict[str, Any]],
# if changing, be sure to also update _get_config_path()
load_json_config("USERS_CONFIG", "users.json"),
)
UsersConfig.validate_config(config)
return config
@staticmethod
def validate_config(config: List[Dict[str, Any]]) -> None:
"""Validate configuration via jsonschema."""
logger.debug("Validating Users config")
validate(config, CONFIG_SCHEMA)
logger.debug("Users is valid")
def reload(self) -> Tuple[int, int, int]:
"""Reload configuration from config file on disk.
Returns a 3-tuple of counts of users removed, updated, and added.
"""
logger.info("Reloading users config.")
try:
nconf: UsersConfig = UsersConfig()
except Exception as ex:
logger.error("Error reloading users config: %s", ex, exc_info=True)
raise
added: int = 0
updated: int = 0
removed: int = 0
nusers: Dict[str, User] = {x.account_id: x for x in nconf.users}
users: Dict[str, User] = {x.account_id: x for x in self.users}
user: User
nuser: User
for acctid, user in users.items():
if acctid not in nusers:
logger.warning("Removing user: %s", user)
for fc in user.fob_codes:
self.users_by_fob.pop(fc)
self.users.remove(user)
removed += 1
continue
nuser = nusers[acctid]
if user.as_dict != nuser.as_dict:
updated += 1
for k in [
"full_name",
"first_name",
"last_name",
"preferred_name",
"email",
"expiration_ymd",
"authorizations",
"oops_override",
]:
if getattr(user, k) != getattr(nuser, k):
logger.warning(
"Updating user: %s %s from %s to %s",
user,
k,
getattr(user, k),
getattr(nuser, k),
)
setattr(user, k, getattr(nuser, k))
if user.fob_codes != nuser.fob_codes:
logger.warning(
"Updating user: %s fob codes from %s to %s",
user,
getattr(user, k),
getattr(nuser, k),
)
for fc in user.fob_codes:
self.users_by_fob.pop(fc)
user.fob_codes = nuser.fob_codes
for fob in nuser.fob_codes:
self.users_by_fob[fob] = nuser
for acctid, nuser in nusers.items():
if acctid not in users:
logger.warning("Adding new user: %s", nuser)
self.users.append(nuser)
for fob in nuser.fob_codes:
self.users_by_fob[fob] = nuser
added += 1
logger.info("Done reloading users config.")
self.load_time = time()
self.file_mtime = os.path.getmtime(self._get_config_path())
return removed, updated, added