-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpermission.py
1402 lines (1107 loc) · 54.1 KB
/
permission.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- 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.
import logging
from configparser import NoOptionError, NoSectionError
from datetime import datetime
from sqlalchemy.sql import func
from typing import TYPE_CHECKING
import rucio.core.scope
from rucio.common.config import config_get
from rucio.common.exception import InvalidRSEExpression
from rucio.common.types import InternalScope
from rucio.core.account import has_account_attribute
from rucio.core.did import list_files
from rucio.core.identity import exist_identity_account
from rucio.core.rse import list_rse_attributes, get_rse
from rucio.core.rse_expression_parser import parse_expression
from rucio.core.rule import get_rule, list_rules
from rucio.db.sqla.constants import IdentityType
from rucio.db.sqla.models import ReplicaLock, ReplicationRule
if TYPE_CHECKING:
from typing import Optional
from sqlalchemy.orm import Session
from rucio.common.types import InternalAccount
def has_permission(issuer, action, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account has the specified permission to
execute an action with parameters.
:param issuer: Account identifier which issues the command..
:param action: The action(API call) called by the account.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
perm = {'add_account': perm_add_account,
'del_account': perm_del_account,
'update_account': perm_update_account,
'add_rule': perm_add_rule,
'add_subscription': perm_add_subscription,
'add_scope': perm_add_scope,
'add_rse': perm_add_rse,
'update_rse': perm_update_rse,
'add_protocol': perm_add_protocol,
'del_protocol': perm_del_protocol,
'update_protocol': perm_update_protocol,
'add_qos_policy': perm_add_qos_policy,
'delete_qos_policy': perm_delete_qos_policy,
'declare_bad_file_replicas': perm_declare_bad_file_replicas,
'declare_suspicious_file_replicas': perm_declare_suspicious_file_replicas,
'add_replicas': perm_add_replicas,
'delete_replicas': perm_delete_replicas,
'skip_availability_check': perm_skip_availability_check,
'update_replicas_states': perm_update_replicas_states,
'add_rse_attribute': perm_add_rse_attribute,
'del_rse_attribute': perm_del_rse_attribute,
'del_rse': perm_del_rse,
'del_rule': perm_del_rule,
'update_rule': perm_update_rule,
'approve_rule': perm_approve_rule,
'update_subscription': perm_update_subscription,
'reduce_rule': perm_reduce_rule,
'move_rule': perm_move_rule,
'get_auth_token_user_pass': perm_get_auth_token_user_pass,
'get_auth_token_gss': perm_get_auth_token_gss,
'get_auth_token_x509': perm_get_auth_token_x509,
'get_auth_token_saml': perm_get_auth_token_saml,
'add_account_identity': perm_add_account_identity,
'add_did': perm_add_did,
'add_dids': perm_add_dids,
'attach_dids': perm_attach_dids,
'detach_dids': perm_detach_dids,
'attach_dids_to_dids': perm_attach_dids_to_dids,
'create_did_sample': perm_create_did_sample,
'set_metadata': perm_set_metadata,
'set_status': perm_set_status,
'queue_requests': perm_queue_requests,
'set_rse_usage': perm_set_rse_usage,
'set_rse_limits': perm_set_rse_limits,
'get_request_by_did': perm_get_request_by_did,
'cancel_request': perm_cancel_request,
'get_next': perm_get_next,
'set_local_account_limit': perm_set_local_account_limit,
'set_global_account_limit': perm_set_global_account_limit,
'delete_local_account_limit': perm_delete_local_account_limit,
'delete_global_account_limit': perm_delete_global_account_limit,
'config_sections': perm_config,
'config_add_section': perm_config,
'config_has_section': perm_config,
'config_options': perm_config,
'config_has_option': perm_config,
'config_get': perm_config,
'config_items': perm_config,
'config_set': perm_config,
'config_remove_section': perm_config,
'config_remove_option': perm_config,
'get_local_account_usage': perm_get_local_account_usage,
'get_global_account_usage': perm_get_global_account_usage,
'add_attribute': perm_add_account_attribute,
'del_attribute': perm_del_account_attribute,
'list_heartbeats': perm_list_heartbeats,
'resurrect': perm_resurrect,
'update_lifetime_exceptions': perm_update_lifetime_exceptions,
'get_auth_token_ssh': perm_get_auth_token_ssh,
'get_signed_url': perm_get_signed_url,
'add_bad_pfns': perm_add_bad_pfns,
'del_account_identity': perm_del_account_identity,
'del_identity': perm_del_identity,
'remove_did_from_followed': perm_remove_did_from_followed,
'remove_dids_from_followed': perm_remove_dids_from_followed,
'add_vo': perm_add_vo,
'list_vos': perm_list_vos,
'recover_vo_root_identity': perm_recover_vo_root_identity,
'update_vo': perm_update_vo,
'access_rule_vo': perm_access_rule_vo}
return perm.get(action, perm_default)(issuer=issuer, kwargs=kwargs, session=session)
def _is_root(issuer):
return issuer.external == 'root'
def perm_default(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Default permission.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_add_rse(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_update_rse(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can update a RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def _check_for_auto_approve_eligibility(issuer, rses, kwargs, session: "Optional[Session]" = None):
def _get_rule_size(rules):
rule_size = 0
for rule in rules:
scope = rule['scope']
name = rule['name']
rule_files = list_files(scope, name, session=session)
rule_size += sum([file['bytes'] for file in rule_files])
return rule_size
# prevent rule creation under 'User AutoApprove' for rules without ask_approval
if not kwargs["ask_approval"]:
return False
# prevent rule creation to tape and Tier3 and Tier0 under the 'User AutoApprove' activity
rule_rses = {rse['rse'] for rse in rses}
try:
t3_rses = {rse['rse'] for rse in parse_expression("tier=3|tier=0", session=session)}
except InvalidRSEExpression:
t3_rses = set()
try:
tape_rses = {rse['rse'] for rse in parse_expression("rse_type=TAPE", session=session)}
except InvalidRSEExpression:
tape_rses = set()
if rule_rses & t3_rses or rule_rses & tape_rses:
return False
account = kwargs['account']
auto_approve_activity = 'User AutoApprove'
dids = kwargs['dids']
try:
global_usage_all_accounts = float(config_get(
'rules', 'global_usage_all_accounts', raise_exception=True, default=1e16))
except (NoOptionError, NoSectionError, RuntimeError):
global_usage_all_accounts = 1e16
try:
global_usage_per_account = float(config_get(
'rules', 'global_usage_per_account', raise_exception=True, default=1e15))
except (NoOptionError, NoSectionError, RuntimeError):
global_usage_per_account = 1e15
try:
rule_lifetime_threshold = int(config_get('rules', 'rule_lifetime_threshold',
raise_exception=True, default=30*24*3600))
except (NoOptionError, NoSectionError, RuntimeError):
rule_lifetime_threshold = 30*24*3600
try:
single_rse_rule_size_threshold = float(config_get(
'rules', 'single_rse_rule_size_threshold', raise_exception=True, default=50e12))
except (NoOptionError, NoSectionError, RuntimeError):
single_rse_rule_size_threshold = 50e12
# Check if the account is banned
if has_account_attribute(account, 'auto_approve_banned', session=session):
return False
# Check if the rule is locked
if kwargs['locked']:
return False
if kwargs['lifetime'] is None:
return False
if kwargs['lifetime'] > rule_lifetime_threshold:
return False
for did in dids:
size_of_rule = sum([file['bytes']
for file in list_files(InternalScope(did['scope']),
did['name'],
session=session)])
# Limit single RSE rules to 50 TB
# This does not mean that the total locks size at a RSE will be limited to 50 TB
# as other rules that are spread over multiple RSEs may claim the same space
# This is just a simple check to avoid a single RSE rules from being too large
rse_expression = kwargs['rse_expression']
rses = parse_expression(rse_expression, filter_={'availability_write': True}, session=session)
if len(rses) == 1:
this_rse_autoapprove_rules = list_rules(
filters={'account': account, 'activity': auto_approve_activity, 'rse_expression': rse_expression},
session=session)
this_rse_autoapprove_usage = _get_rule_size(this_rse_autoapprove_rules)
if this_rse_autoapprove_usage + size_of_rule > single_rse_rule_size_threshold:
logging.warning(
'Single RSE usage exceeded for auto approve rules for account %s and RSE %s, this_rse_autoapprove_usage, size_of_rule, single_rse_rule_size_threshold: %s, %s, %s',
account, rse_expression, this_rse_autoapprove_usage, size_of_rule,
single_rse_rule_size_threshold)
return False
# Check global usage of the account under this activity
all_auto_approve_rules_by_account = list_rules(
filters={'account': account, 'activity': auto_approve_activity}, session=session)
global_auto_approve_usage_by_account = _get_rule_size(all_auto_approve_rules_by_account)
if global_auto_approve_usage_by_account + size_of_rule > global_usage_per_account:
logging.warning(
'Global usage exceeded for auto approve rules for account %s, current usage, size of rule, global_usage_per_account: %s, %s, %s',
account, global_auto_approve_usage_by_account, size_of_rule, global_usage_per_account)
return False
# Check global usage under the AutoApprove category by all accounts
query = session.query(
func.sum(ReplicaLock.bytes)).join(
ReplicationRule, ReplicaLock.rule_id == ReplicationRule.id).filter(
ReplicationRule.activity == 'User AutoApprove')
current_auto_approve_usage = query.scalar()
if current_auto_approve_usage is None:
current_auto_approve_usage = 0
if current_auto_approve_usage + size_of_rule > global_usage_all_accounts:
logging.warning('Global usage exceeded for auto approve rules, current usage, size of rule, '
'global_usage_all_accounts: %s, %s, %s', current_auto_approve_usage, size_of_rule,
global_usage_all_accounts)
return False
return True
def perm_add_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
rses = parse_expression(kwargs['rse_expression'], filter_={'vo': issuer.vo}, session=session)
# If any of RSEs matching the expression needs approval, the rule cannot be created
if not kwargs['ask_approval']:
for rse in rses:
rse_attr = list_rse_attributes(rse_id=rse['id'])
if rse_attr.get('requires_approval', False):
return False
# If asked for approval, rse_expression can only be a single RSE
if kwargs["activity"] != "User AutoApprove" and kwargs["ask_approval"] and len(rses) != 1:
return False
if kwargs["activity"] == "User AutoApprove":
return _check_for_auto_approve_eligibility(issuer, rses, kwargs, session=session)
if kwargs["activity"] == "Analysis TapeRecall" and issuer.external == "crab_tape_recall":
return True
# Anyone can use _Temp RSEs if a lifetime is set and under a month
all_temp = True
for rse in rses:
rse_attr = list_rse_attributes(rse_id=rse['id'], session=session)
cms_type = rse_attr.get('cms_type', None)
if cms_type not in ['temp']:
all_temp = False
if all_temp and kwargs['lifetime'] is not None and kwargs['lifetime'] < 31 * 24 * 60 * 60:
return True
# Check if any of the rses is a tape RSE (This does not include the _Test and _Temp RSEs)
rse_names = [rse['rse'] for rse in rses]
any_tape = any(name.endswith('_Tape') for name in rse_names)
if any_tape and kwargs['lifetime'] is not None:
return False
# Non admin users cannot create rules with locked flag
# A locked rule cannot be deleted; and is not removed ever after the rule expires
if kwargs['account'] == issuer and not kwargs['locked']:
return True
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_add_subscription(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a subscription.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_add_rse_attribute(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a RSE attribute.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer):
return True
if _restricted_rse_attribute(kwargs['rse'], kwargs['key'], kwargs['value']):
return False
if has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_del_rse_attribute(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete a RSE attribute.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_del_rse(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete a RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_add_account(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer)
def perm_del_account(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can del an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer)
def perm_update_account(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can update an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_add_scope(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a scop to a account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_get_auth_token_user_pass(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if a user can request a token with user_pass for an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if exist_identity_account(
identity=kwargs['username'],
type_=IdentityType.USERPASS, account=kwargs['account'],
session=session):
return True
return False
def perm_get_auth_token_gss(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if a user can request a token with user_pass for an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if exist_identity_account(
identity=kwargs['gsscred'],
type_=IdentityType.GSS, account=kwargs['account'],
session=session):
return True
return False
def perm_get_auth_token_x509(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if a user can request a token with user_pass for an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if exist_identity_account(
identity=kwargs['dn'],
type_=IdentityType.X509, account=kwargs['account'],
session=session):
return True
return False
def perm_get_auth_token_saml(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if a user can request a token with user_pass for an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if exist_identity_account(
identity=kwargs['saml_nameid'],
type_=IdentityType.SAML, account=kwargs['account'],
session=session):
return True
return False
def perm_add_account_identity(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add an identity to an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_del_account_identity(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete an identity to an account.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_del_identity(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete an identity.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or issuer.external in kwargs.get('accounts')
def perm_add_did(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add an data identifier to a scope.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
# Check the accounts of the issued rules
if not _is_root(issuer) and not has_account_attribute(account=issuer, key='admin', session=session):
for rule in kwargs.get('rules', []):
if rule['account'] != issuer:
return False
if kwargs['scope'].external != 'cms' and not has_account_attribute(account=issuer, key='admin', session=session):
if kwargs['type'] == 'DATASET':
if '/USER#' not in kwargs['name']:
return False
elif kwargs['type'] == 'CONTAINER':
if not kwargs['name'].endswith('/USER'):
return False
return (_is_root(issuer)
or has_account_attribute(account=issuer, key='admin', session=session) # NOQA: W503
or rucio.core.scope.is_scope_owner(scope=kwargs['scope'], account=issuer, session=session) # NOQA: W503
or kwargs['scope'].external == u'mock') # NOQA: W503
def perm_add_dids(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can bulk add data identifiers.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
#TODO: Check scope ownership for bulk add operation too
# Check the accounts of the issued rules
if not _is_root(issuer) and not has_account_attribute(account=issuer, key='admin', session=session):
for did in kwargs['dids']:
for rule in did.get('rules', []):
if rule['account'] != issuer:
return False
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_attach_dids(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can append an data identifier to the other data identifier.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return (_is_root(issuer)
or has_account_attribute(account=issuer, key='admin', session=session) # NOQA: W503
or rucio.core.scope.is_scope_owner(scope=kwargs['scope'], account=issuer, session=session) # NOQA: W503
or kwargs['scope'].external == 'mock') # NOQA: W503
def perm_attach_dids_to_dids(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can append an data identifier to the other data identifier.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
else:
attachments = kwargs['attachments']
scopes = [did['scope'] for did in attachments]
scopes = list(set(scopes))
for scope in scopes:
if not rucio.core.scope.is_scope_owner(scope, issuer, session=session):
return False
return True
def perm_create_did_sample(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can create a sample of a data identifier collection.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return issuer == ('root'
or has_account_attribute(account=issuer, key='admin', session=session) # NOQA: W503
or rucio.core.scope.is_scope_owner(scope=kwargs['scope'], account=issuer, session=session) # NOQA: W503
or kwargs['scope'].external == 'mock') # NOQA: W503
def perm_del_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an issuer can delete a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed to call the API call, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
if get_rule(kwargs['rule_id'])['account'] == issuer:
return True
return False
def perm_update_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an issuer can update a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed to call the API call, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
rule = get_rule(kwargs['rule_id'])
if issuer == rule.get('account'):
# Allow users to update the following rule options when rule is sans ask-approval
allowed_options = ['lifetime', 'state', 'comment', 'purge_replicas',
'cancel_requests', 'source_replica_expression', 'child_rule_id']
update_options = kwargs.get('options', {})
for key in update_options:
if key not in allowed_options:
return False
if rule["ignore_account_limit"] is False:
return True
if rule['activity'] == 'User AutoApprove' and "lifetime" not in update_options:
return True
if rule['activity'] == 'User AutoApprove':
try:
# Default single extension duration - 1 month
extended_lifetime_limit = config_get("rules", "extended_lifetime_limit")
# Total lifetime including all extensions cannot exceed 1 year
single_extension_limit = config_get("rules", "single_extension_limit")
except (NoOptionError, NoSectionError):
extended_lifetime_limit = 31536000
single_extension_limit = 2592000
new_lifetime = update_options.get("lifetime", 0)
rule_created_at = rule['created_at']
current_time = datetime.utcnow()
current_lifetime = current_time - rule_created_at
if new_lifetime < single_extension_limit:
if current_lifetime.total_seconds() + new_lifetime < extended_lifetime_limit:
return True
return False
def perm_approve_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an issuer can approve a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed to call the API call, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
rule = get_rule(rule_id=kwargs['rule_id'])
rses = parse_expression(rule['rse_expression'], filter_={'vo': issuer.vo}, session=session)
# For a multi-RSE expression, check if admin for all RSEs
for rse in rses:
rse_attr = list_rse_attributes(rse_id=rse['id'], session=session)
rule_approvers = rse_attr.get('rule_approvers', [])
if rule_approvers and issuer.external not in rule_approvers.split(','):
return False
return True
def perm_reduce_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an issuer can reduce a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed to call the API call, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_move_rule(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an issuer can move a replication rule.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed to call the API call, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_update_subscription(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can update a subscription.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return False
def perm_detach_dids(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can detach an data identifier from the other data identifier.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return perm_attach_dids(issuer, kwargs, session=session)
def perm_set_metadata(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can set a metadata on a data identifier.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return (_is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
or rucio.core.scope.is_scope_owner(scope=kwargs['scope'], account=issuer, session=session)) # NOQA: W503
def perm_set_status(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can set status on an data identifier.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if kwargs.get('open', False):
if not _is_root(issuer) and not has_account_attribute(account=issuer, key='admin', session=session):
return False
return (_is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
or rucio.core.scope.is_scope_owner(scope=kwargs['scope'], account=issuer, session=session)) # NOQA: W503
def perm_add_protocol(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add a protocol to an RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_del_protocol(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete protocols from an RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_update_protocol(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can update protocols of an RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_add_qos_policy(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add QoS policies to an RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_delete_qos_policy(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete QoS policies from an RSE.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_declare_bad_file_replicas(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can declare bad file replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
if _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session):
return True
return _is_cms_site_admin(kwargs['rse_id'], issuer, session)
def perm_declare_suspicious_file_replicas(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can declare suspicious file replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return True
def perm_add_replicas(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can add replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
is_root = _is_root(issuer)
is_temp = str(kwargs.get('rse', '')).endswith('_Temp')
is_admin = has_account_attribute(account=issuer, key='admin', session=session)
return is_root or is_temp or is_admin
def perm_skip_availability_check(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can skip the availabity check to add/delete file replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_delete_replicas(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
# FIXME: Remove after the transition is over?
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_update_replicas_states(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can delete replicas.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer) or has_account_attribute(account=issuer, key='admin', session=session)
def perm_queue_requests(issuer, kwargs, *, session: "Optional[Session]" = None):
"""
Checks if an account can submit transfer or deletion requests on destination RSEs for data identifiers.
:param issuer: Account identifier which issues the command.
:param kwargs: List of arguments for the action.
:param session: The DB session to use
:returns: True if account is allowed, otherwise False
"""
return _is_root(issuer)