/
account.py
426 lines (365 loc) · 15.4 KB
/
account.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
from enum import Enum
from re import match
from traceback import format_exc
from typing import TYPE_CHECKING, Any, cast, Iterator, Optional
from sqlalchemy import select, and_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import exc
import rucio.core.account_counter
import rucio.core.rse
from rucio.common import exception
from rucio.common.config import config_get_bool
from rucio.common.types import InternalAccount, AccountAttributesDict, AccountDict, AccountUsageModelDict, IdentityDict, UsageDict
from rucio.core.vo import vo_exists
from rucio.db.sqla import models
from rucio.db.sqla.constants import AccountStatus, AccountType
from rucio.db.sqla.session import read_session, transactional_session, stream_session
if TYPE_CHECKING:
from sqlalchemy.orm import Session
@transactional_session
def add_account(account: InternalAccount, type_: AccountType, email: str, *, session: "Session") -> None:
""" Add an account with the given account name and type.
:param account: the name of the new account.
:param type_: the type of the new account.
:param email: The Email address associated with the account.
:param session: the database session in use.
"""
vo = account.vo
if not vo_exists(vo=vo, session=session):
raise exception.VONotFound('VO {} not found'.format(vo))
# Reserve the name 'super_root' for multi_vo admins
if account.external == 'super_root':
if not (vo == 'def' and config_get_bool('common', 'multi_vo', raise_exception=False, default=False)):
raise exception.UnsupportedAccountName('The name "%s" cannot be used.' % account.external)
new_account = models.Account(account=account, account_type=type_, email=email,
status=AccountStatus.ACTIVE)
try:
new_account.save(session=session)
except IntegrityError:
raise exception.Duplicate('Account ID \'%s\' already exists!' % account)
@read_session
def account_exists(account: InternalAccount, *, session: "Session") -> bool:
""" Checks to see if account exists and is active.
:param account: Name of the account.
:param session: the database session in use.
:returns: True if found, otherwise false.
"""
query = select(
models.Account
).where(
models.Account.account == account,
models.Account.status == AccountStatus.ACTIVE
)
return session.execute(query).scalar() is not None
@read_session
def get_account(account: InternalAccount, *, session: "Session") -> models.Account:
""" Returns an account for the given account name.
:param account: the name of the account.
:param session: the database session in use.
:returns: a dict with all information for the account.
"""
query = select(
models.Account
).where(
models.Account.account == account
)
result = session.execute(query).scalar()
if result is None:
raise exception.AccountNotFound('Account with ID \'%s\' cannot be found' % account)
return result
@transactional_session
def del_account(account: InternalAccount, *, session: "Session") -> None:
""" Disable an account with the given account name.
:param account: the account name.
:param session: the database session in use.
"""
query = select(
models.Account
).where(
models.Account.account == account,
models.Account.status == AccountStatus.ACTIVE
)
try:
account_result = session.execute(query).scalar_one()
except exc.NoResultFound:
raise exception.AccountNotFound('Account with ID \'%s\' cannot be found' % account)
account_result.update({'status': AccountStatus.DELETED, 'deleted_at': datetime.utcnow()})
@transactional_session
def update_account(account: InternalAccount, key: str, value: Any, *, session: "Session") -> None:
""" Update a property of an account.
:param account: Name of the account.
:param key: Account property like status.
:param value: Property value.
:param session: the database session in use.
"""
query = select(
models.Account
).where(
models.Account.account == account
)
try:
query_result = session.execute(query).scalar_one()
except exc.NoResultFound:
raise exception.AccountNotFound('Account with ID \'%s\' cannot be found' % account)
if key == 'status':
if isinstance(value, str):
value = AccountStatus[value]
if value == AccountStatus.SUSPENDED:
query_result.update({'status': value, 'suspended_at': datetime.utcnow()})
elif value == AccountStatus.ACTIVE:
query_result.update({'status': value, 'suspended_at': None})
else:
query_result.update({key: value})
@stream_session
def list_accounts(filter_: Optional[dict[str, Any]] = None, *, session: "Session") -> Iterator[AccountDict]:
""" Returns a list of all account names.
:param filter_: Dictionary of attributes by which the input data should be filtered
:param session: the database session in use.
returns: a list of all account names.
"""
if filter_ is None:
filter_ = {}
query = select(
models.Account.account,
models.Account.account_type,
models.Account.email
).where(
models.Account.status == AccountStatus.ACTIVE
)
for filter_type in filter_:
if filter_type == 'account_type':
if isinstance(filter_['account_type'], str):
query = query.where(
models.Account.account_type == AccountType[filter_['account_type']]
)
elif isinstance(filter_['account_type'], Enum):
query = query.where(
models.Account.account_type == filter_['account_type']
)
elif filter_type == 'identity':
query = query.join(
models.IdentityAccountAssociation,
models.Account.account == models.IdentityAccountAssociation.account
).where(
models.IdentityAccountAssociation.identity == filter_['identity']
)
elif filter_type == 'account':
if '*' in filter_['account'].internal:
account_str = filter_['account'].internal.replace('*', '%')
query = query.where(
models.Account.account.like(account_str)
)
else:
query = query.where(
models.Account.account == filter_['account']
)
else:
query = query.join(
models.AccountAttrAssociation,
models.Account.account == models.AccountAttrAssociation.account
).where(
models.AccountAttrAssociation.key == filter_type,
models.AccountAttrAssociation.value == filter_[filter_type]
)
query = query.order_by(models.Account.account)
for account, account_type, email in session.execute(query).yield_per(25):
yield {'account': account, 'type': account_type, 'email': email}
@read_session
def list_identities(account: InternalAccount, *, session: "Session") -> list[IdentityDict]:
"""
List all identities on an account.
:param account: The account name.
:param session: the database session in use.
"""
query = select(
models.Account
).where(
models.Account.account == account,
models.Account.status == AccountStatus.ACTIVE
)
try:
session.execute(query).scalar_one()
except exc.NoResultFound:
raise exception.AccountNotFound('Account with ID \'%s\' cannot be found' % account)
query = select(
models.IdentityAccountAssociation.identity_type.label('type'),
models.IdentityAccountAssociation.identity,
models.Identity.email
).join(
models.Identity,
and_(
models.Identity.identity == models.IdentityAccountAssociation.identity,
models.Identity.identity_type == models.IdentityAccountAssociation.identity_type
)
).where(
models.IdentityAccountAssociation.account == account
)
return [cast(IdentityDict, row._asdict()) for row in session.execute(query)]
@read_session
def list_account_attributes(account: InternalAccount, *, session: "Session") -> list[AccountAttributesDict]:
"""
Get all attributes defined for an account.
:param account: the account name to list the scopes of.
:param session: The database session in use.
:returns: a list of all key, value pairs for this account.
"""
query = select(
models.Account
).where(
models.Account.account == account,
models.Account.status == AccountStatus.ACTIVE
)
try:
session.execute(query).scalar_one()
except exc.NoResultFound:
raise exception.AccountNotFound("Account ID '{0}' does not exist".format(account))
query = select(
models.AccountAttrAssociation.key,
models.AccountAttrAssociation.value
).where(
models.AccountAttrAssociation.account == account
)
return [cast(AccountAttributesDict, row._asdict()) for row in session.execute(query)]
@read_session
def has_account_attribute(account: InternalAccount, key: str, *, session: "Session") -> bool:
"""
Indicates whether the named key is present for the account.
:param account: the account name to list the scopes of.
:param key: the key for the attribute.
:param session: The database session in use.
:returns: True or False
"""
query = select(
models.AccountAttrAssociation.value
).where(
models.AccountAttrAssociation.account == account,
models.AccountAttrAssociation.key == key
)
return session.execute(query).scalar() is not None
@transactional_session
def add_account_attribute(account: InternalAccount, key: str, value: Any, *, session: "Session") -> None:
"""
Add an attribute for the given account name.
:param key: the key for the new attribute.
:param value: the value for the new attribute.
:param account: the account to add the attribute to.
:param session: The database session in use.
"""
query = select(
models.Account
).where(
models.Account.account == account,
models.Account.status == AccountStatus.ACTIVE
)
try:
session.execute(query).scalar_one()
except exc.NoResultFound:
raise exception.AccountNotFound("Account ID '{0}' does not exist".format(account))
new_attr = models.AccountAttrAssociation(account=account, key=key, value=value)
try:
new_attr.save(session=session)
except IntegrityError as error:
if match('.*IntegrityError.*ORA-00001: unique constraint.*ACCOUNT_ATTR_MAP_PK.*violated.*', error.args[0]) \
or match('.*IntegrityError.*1062.*Duplicate entry.*for key.*', error.args[0]) \
or match('.*IntegrityError.*UNIQUE constraint failed: account_attr_map.account, account_attr_map.key.*', error.args[0]) \
or match('.*IntegrityError.*columns? account.*key.*not unique.*', error.args[0]) \
or match('.*IntegrityError.*duplicate key value violates unique constraint.*', error.args[0]) \
or match('.*UniqueViolation.*duplicate key value violates unique constraint.*', error.args[0]):
raise exception.Duplicate('Key {0} already exist for account {1}!'.format(key, account))
except Exception:
raise exception.RucioException(str(format_exc()))
@transactional_session
def del_account_attribute(account: InternalAccount, key: str, *, session: "Session") -> None:
"""
Add an attribute for the given account name.
:param account: the account to add the attribute to.
:param key: the key for the new attribute.
:param session: The database session in use.
"""
query = select(
models.AccountAttrAssociation
).where(
models.AccountAttrAssociation.account == account,
models.AccountAttrAssociation.key == key
)
aid = session.execute(query).scalar()
if aid is None:
raise exception.AccountNotFound('Attribute ({0}) does not exist for the account {1}!'.format(key, account))
aid.delete(session=session)
@read_session
def get_usage(rse_id: str, account: InternalAccount, *, session: "Session") -> UsageDict:
"""
Returns current values of the specified counter, or raises CounterNotFound if the counter does not exist.
:param rse_id: The id of the RSE.
:param account: The account name.
:param session: The database session in use.
:returns: A dictionary {'bytes', 'files', 'updated_at'}
"""
query = select(
models.AccountUsage.bytes,
models.AccountUsage.files,
models.AccountUsage.updated_at
).where(
models.AccountUsage.rse_id == rse_id,
models.AccountUsage.account == account
)
try:
return cast(UsageDict, session.execute(query).one()._asdict())
except exc.NoResultFound:
return UsageDict({'bytes': 0, 'files': 0, 'updated_at': None})
@read_session
def get_all_rse_usages_per_account(account: InternalAccount, *, session: "Session") -> list[AccountUsageModelDict]:
"""
Returns current values of the specified counter, or raises CounterNotFound if the counter does not exist.
:param rse_id: The id of the RSE.
:param account: The account name.
:param session: The database session in use.
:returns: List of dicts with :py:class:`models.AccountUsage` items
"""
query = select(
models.AccountUsage
).where(
models.AccountUsage.account == account
)
try:
return [cast(AccountUsageModelDict, result.to_dict()) for result in session.execute(query).scalars()]
except exc.NoResultFound:
return []
@read_session
def get_usage_history(rse_id: str, account: InternalAccount, *, session: "Session") -> list[UsageDict]:
"""
Returns historical values of the specified counter, or raises CounterNotFound if the counter does not exist.
:param rse_id: The id of the RSE.
:param account: The account name.
:param session: The database session in use.
:returns: A list of dictionaries {'bytes', 'files', 'updated_at'}
"""
query = select(
models.AccountUsageHistory.bytes,
models.AccountUsageHistory.files,
models.AccountUsageHistory.updated_at
).where(
models.AccountUsageHistory.rse_id == rse_id,
models.AccountUsageHistory.account == account
).order_by(
models.AccountUsageHistory.updated_at
)
try:
return [cast(UsageDict, row._asdict()) for row in session.execute(query)]
except exc.NoResultFound:
raise exception.CounterNotFound('No usage can be found for account %s on RSE %s' % (account, rucio.core.rse.get_rse_name(rse_id=rse_id, session=session)))