rucio 32.8.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rucio might be problematic. Click here for more details.

Files changed (481) hide show
  1. rucio/__init__.py +18 -0
  2. rucio/alembicrevision.py +16 -0
  3. rucio/api/__init__.py +14 -0
  4. rucio/api/account.py +266 -0
  5. rucio/api/account_limit.py +287 -0
  6. rucio/api/authentication.py +302 -0
  7. rucio/api/config.py +218 -0
  8. rucio/api/credential.py +60 -0
  9. rucio/api/did.py +726 -0
  10. rucio/api/dirac.py +71 -0
  11. rucio/api/exporter.py +60 -0
  12. rucio/api/heartbeat.py +62 -0
  13. rucio/api/identity.py +160 -0
  14. rucio/api/importer.py +46 -0
  15. rucio/api/lifetime_exception.py +95 -0
  16. rucio/api/lock.py +131 -0
  17. rucio/api/meta.py +85 -0
  18. rucio/api/permission.py +72 -0
  19. rucio/api/quarantined_replica.py +69 -0
  20. rucio/api/replica.py +528 -0
  21. rucio/api/request.py +220 -0
  22. rucio/api/rse.py +601 -0
  23. rucio/api/rule.py +335 -0
  24. rucio/api/scope.py +89 -0
  25. rucio/api/subscription.py +255 -0
  26. rucio/api/temporary_did.py +49 -0
  27. rucio/api/vo.py +112 -0
  28. rucio/client/__init__.py +16 -0
  29. rucio/client/accountclient.py +413 -0
  30. rucio/client/accountlimitclient.py +155 -0
  31. rucio/client/baseclient.py +929 -0
  32. rucio/client/client.py +77 -0
  33. rucio/client/configclient.py +113 -0
  34. rucio/client/credentialclient.py +54 -0
  35. rucio/client/didclient.py +691 -0
  36. rucio/client/diracclient.py +48 -0
  37. rucio/client/downloadclient.py +1674 -0
  38. rucio/client/exportclient.py +44 -0
  39. rucio/client/fileclient.py +51 -0
  40. rucio/client/importclient.py +42 -0
  41. rucio/client/lifetimeclient.py +74 -0
  42. rucio/client/lockclient.py +99 -0
  43. rucio/client/metaclient.py +137 -0
  44. rucio/client/pingclient.py +45 -0
  45. rucio/client/replicaclient.py +444 -0
  46. rucio/client/requestclient.py +109 -0
  47. rucio/client/rseclient.py +664 -0
  48. rucio/client/ruleclient.py +287 -0
  49. rucio/client/scopeclient.py +88 -0
  50. rucio/client/subscriptionclient.py +161 -0
  51. rucio/client/touchclient.py +78 -0
  52. rucio/client/uploadclient.py +871 -0
  53. rucio/common/__init__.py +14 -0
  54. rucio/common/cache.py +74 -0
  55. rucio/common/config.py +796 -0
  56. rucio/common/constants.py +92 -0
  57. rucio/common/constraints.py +18 -0
  58. rucio/common/didtype.py +187 -0
  59. rucio/common/dumper/__init__.py +306 -0
  60. rucio/common/dumper/consistency.py +449 -0
  61. rucio/common/dumper/data_models.py +325 -0
  62. rucio/common/dumper/path_parsing.py +65 -0
  63. rucio/common/exception.py +1092 -0
  64. rucio/common/extra.py +37 -0
  65. rucio/common/logging.py +404 -0
  66. rucio/common/pcache.py +1387 -0
  67. rucio/common/policy.py +84 -0
  68. rucio/common/schema/__init__.py +143 -0
  69. rucio/common/schema/atlas.py +411 -0
  70. rucio/common/schema/belleii.py +406 -0
  71. rucio/common/schema/cms.py +478 -0
  72. rucio/common/schema/domatpc.py +399 -0
  73. rucio/common/schema/escape.py +424 -0
  74. rucio/common/schema/generic.py +431 -0
  75. rucio/common/schema/generic_multi_vo.py +410 -0
  76. rucio/common/schema/icecube.py +404 -0
  77. rucio/common/schema/lsst.py +423 -0
  78. rucio/common/stomp_utils.py +160 -0
  79. rucio/common/stopwatch.py +56 -0
  80. rucio/common/test_rucio_server.py +148 -0
  81. rucio/common/types.py +158 -0
  82. rucio/common/utils.py +1946 -0
  83. rucio/core/__init__.py +14 -0
  84. rucio/core/account.py +426 -0
  85. rucio/core/account_counter.py +171 -0
  86. rucio/core/account_limit.py +357 -0
  87. rucio/core/authentication.py +563 -0
  88. rucio/core/config.py +386 -0
  89. rucio/core/credential.py +218 -0
  90. rucio/core/did.py +3102 -0
  91. rucio/core/did_meta_plugins/__init__.py +250 -0
  92. rucio/core/did_meta_plugins/did_column_meta.py +326 -0
  93. rucio/core/did_meta_plugins/did_meta_plugin_interface.py +116 -0
  94. rucio/core/did_meta_plugins/filter_engine.py +573 -0
  95. rucio/core/did_meta_plugins/json_meta.py +215 -0
  96. rucio/core/did_meta_plugins/mongo_meta.py +199 -0
  97. rucio/core/did_meta_plugins/postgres_meta.py +317 -0
  98. rucio/core/dirac.py +208 -0
  99. rucio/core/distance.py +164 -0
  100. rucio/core/exporter.py +59 -0
  101. rucio/core/heartbeat.py +263 -0
  102. rucio/core/identity.py +290 -0
  103. rucio/core/importer.py +248 -0
  104. rucio/core/lifetime_exception.py +377 -0
  105. rucio/core/lock.py +474 -0
  106. rucio/core/message.py +241 -0
  107. rucio/core/meta.py +190 -0
  108. rucio/core/monitor.py +441 -0
  109. rucio/core/naming_convention.py +154 -0
  110. rucio/core/nongrid_trace.py +124 -0
  111. rucio/core/oidc.py +1339 -0
  112. rucio/core/permission/__init__.py +107 -0
  113. rucio/core/permission/atlas.py +1333 -0
  114. rucio/core/permission/belleii.py +1076 -0
  115. rucio/core/permission/cms.py +1166 -0
  116. rucio/core/permission/escape.py +1076 -0
  117. rucio/core/permission/generic.py +1128 -0
  118. rucio/core/permission/generic_multi_vo.py +1148 -0
  119. rucio/core/quarantined_replica.py +190 -0
  120. rucio/core/replica.py +3627 -0
  121. rucio/core/replica_sorter.py +368 -0
  122. rucio/core/request.py +2241 -0
  123. rucio/core/rse.py +1835 -0
  124. rucio/core/rse_counter.py +155 -0
  125. rucio/core/rse_expression_parser.py +460 -0
  126. rucio/core/rse_selector.py +277 -0
  127. rucio/core/rule.py +3419 -0
  128. rucio/core/rule_grouping.py +1473 -0
  129. rucio/core/scope.py +152 -0
  130. rucio/core/subscription.py +316 -0
  131. rucio/core/temporary_did.py +188 -0
  132. rucio/core/topology.py +448 -0
  133. rucio/core/trace.py +361 -0
  134. rucio/core/transfer.py +1233 -0
  135. rucio/core/vo.py +151 -0
  136. rucio/core/volatile_replica.py +123 -0
  137. rucio/daemons/__init__.py +14 -0
  138. rucio/daemons/abacus/__init__.py +14 -0
  139. rucio/daemons/abacus/account.py +106 -0
  140. rucio/daemons/abacus/collection_replica.py +113 -0
  141. rucio/daemons/abacus/rse.py +107 -0
  142. rucio/daemons/atropos/__init__.py +14 -0
  143. rucio/daemons/atropos/atropos.py +243 -0
  144. rucio/daemons/auditor/__init__.py +261 -0
  145. rucio/daemons/auditor/hdfs.py +86 -0
  146. rucio/daemons/auditor/srmdumps.py +284 -0
  147. rucio/daemons/automatix/__init__.py +14 -0
  148. rucio/daemons/automatix/automatix.py +281 -0
  149. rucio/daemons/badreplicas/__init__.py +14 -0
  150. rucio/daemons/badreplicas/minos.py +311 -0
  151. rucio/daemons/badreplicas/minos_temporary_expiration.py +173 -0
  152. rucio/daemons/badreplicas/necromancer.py +200 -0
  153. rucio/daemons/bb8/__init__.py +14 -0
  154. rucio/daemons/bb8/bb8.py +356 -0
  155. rucio/daemons/bb8/common.py +762 -0
  156. rucio/daemons/bb8/nuclei_background_rebalance.py +147 -0
  157. rucio/daemons/bb8/t2_background_rebalance.py +146 -0
  158. rucio/daemons/c3po/__init__.py +14 -0
  159. rucio/daemons/c3po/algorithms/__init__.py +14 -0
  160. rucio/daemons/c3po/algorithms/simple.py +131 -0
  161. rucio/daemons/c3po/algorithms/t2_free_space.py +125 -0
  162. rucio/daemons/c3po/algorithms/t2_free_space_only_pop.py +127 -0
  163. rucio/daemons/c3po/algorithms/t2_free_space_only_pop_with_network.py +279 -0
  164. rucio/daemons/c3po/c3po.py +342 -0
  165. rucio/daemons/c3po/collectors/__init__.py +14 -0
  166. rucio/daemons/c3po/collectors/agis.py +108 -0
  167. rucio/daemons/c3po/collectors/free_space.py +62 -0
  168. rucio/daemons/c3po/collectors/jedi_did.py +48 -0
  169. rucio/daemons/c3po/collectors/mock_did.py +46 -0
  170. rucio/daemons/c3po/collectors/network_metrics.py +63 -0
  171. rucio/daemons/c3po/collectors/workload.py +110 -0
  172. rucio/daemons/c3po/utils/__init__.py +14 -0
  173. rucio/daemons/c3po/utils/dataset_cache.py +40 -0
  174. rucio/daemons/c3po/utils/expiring_dataset_cache.py +45 -0
  175. rucio/daemons/c3po/utils/expiring_list.py +63 -0
  176. rucio/daemons/c3po/utils/popularity.py +82 -0
  177. rucio/daemons/c3po/utils/timeseries.py +76 -0
  178. rucio/daemons/cache/__init__.py +14 -0
  179. rucio/daemons/cache/consumer.py +191 -0
  180. rucio/daemons/common.py +391 -0
  181. rucio/daemons/conveyor/__init__.py +14 -0
  182. rucio/daemons/conveyor/common.py +530 -0
  183. rucio/daemons/conveyor/finisher.py +492 -0
  184. rucio/daemons/conveyor/poller.py +372 -0
  185. rucio/daemons/conveyor/preparer.py +198 -0
  186. rucio/daemons/conveyor/receiver.py +206 -0
  187. rucio/daemons/conveyor/stager.py +127 -0
  188. rucio/daemons/conveyor/submitter.py +379 -0
  189. rucio/daemons/conveyor/throttler.py +468 -0
  190. rucio/daemons/follower/__init__.py +14 -0
  191. rucio/daemons/follower/follower.py +97 -0
  192. rucio/daemons/hermes/__init__.py +14 -0
  193. rucio/daemons/hermes/hermes.py +738 -0
  194. rucio/daemons/judge/__init__.py +14 -0
  195. rucio/daemons/judge/cleaner.py +149 -0
  196. rucio/daemons/judge/evaluator.py +172 -0
  197. rucio/daemons/judge/injector.py +154 -0
  198. rucio/daemons/judge/repairer.py +144 -0
  199. rucio/daemons/oauthmanager/__init__.py +14 -0
  200. rucio/daemons/oauthmanager/oauthmanager.py +199 -0
  201. rucio/daemons/reaper/__init__.py +14 -0
  202. rucio/daemons/reaper/dark_reaper.py +272 -0
  203. rucio/daemons/reaper/light_reaper.py +255 -0
  204. rucio/daemons/reaper/reaper.py +701 -0
  205. rucio/daemons/replicarecoverer/__init__.py +14 -0
  206. rucio/daemons/replicarecoverer/suspicious_replica_recoverer.py +487 -0
  207. rucio/daemons/storage/__init__.py +14 -0
  208. rucio/daemons/storage/consistency/__init__.py +14 -0
  209. rucio/daemons/storage/consistency/actions.py +753 -0
  210. rucio/daemons/tracer/__init__.py +14 -0
  211. rucio/daemons/tracer/kronos.py +513 -0
  212. rucio/daemons/transmogrifier/__init__.py +14 -0
  213. rucio/daemons/transmogrifier/transmogrifier.py +753 -0
  214. rucio/daemons/undertaker/__init__.py +14 -0
  215. rucio/daemons/undertaker/undertaker.py +137 -0
  216. rucio/db/__init__.py +14 -0
  217. rucio/db/sqla/__init__.py +38 -0
  218. rucio/db/sqla/constants.py +192 -0
  219. rucio/db/sqla/migrate_repo/__init__.py +14 -0
  220. rucio/db/sqla/migrate_repo/env.py +111 -0
  221. rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py +71 -0
  222. rucio/db/sqla/migrate_repo/versions/0437a40dbfd1_add_eol_at_in_rules.py +50 -0
  223. rucio/db/sqla/migrate_repo/versions/0f1adb7a599a_create_transfer_hops_table.py +61 -0
  224. rucio/db/sqla/migrate_repo/versions/102efcf145f4_added_stuck_at_column_to_rules.py +46 -0
  225. rucio/db/sqla/migrate_repo/versions/13d4f70c66a9_introduce_transfer_limits.py +93 -0
  226. rucio/db/sqla/migrate_repo/versions/140fef722e91_cleanup_distances_table.py +78 -0
  227. rucio/db/sqla/migrate_repo/versions/14ec5aeb64cf_add_request_external_host.py +46 -0
  228. rucio/db/sqla/migrate_repo/versions/156fb5b5a14_add_request_type_to_requests_idx.py +53 -0
  229. rucio/db/sqla/migrate_repo/versions/1677d4d803c8_split_rse_availability_into_multiple.py +69 -0
  230. rucio/db/sqla/migrate_repo/versions/16a0aca82e12_create_index_on_table_replicas_path.py +42 -0
  231. rucio/db/sqla/migrate_repo/versions/1803333ac20f_adding_provenance_and_phys_group.py +46 -0
  232. rucio/db/sqla/migrate_repo/versions/1a29d6a9504c_add_didtype_chck_to_requests.py +61 -0
  233. rucio/db/sqla/migrate_repo/versions/1a80adff031a_create_index_on_rules_hist_recent.py +42 -0
  234. rucio/db/sqla/migrate_repo/versions/1c45d9730ca6_increase_identity_length.py +141 -0
  235. rucio/db/sqla/migrate_repo/versions/1d1215494e95_add_quarantined_replicas_table.py +75 -0
  236. rucio/db/sqla/migrate_repo/versions/1d96f484df21_asynchronous_rules_and_rule_approval.py +75 -0
  237. rucio/db/sqla/migrate_repo/versions/1f46c5f240ac_add_bytes_column_to_bad_replicas.py +46 -0
  238. rucio/db/sqla/migrate_repo/versions/1fc15ab60d43_add_message_history_table.py +51 -0
  239. rucio/db/sqla/migrate_repo/versions/2190e703eb6e_move_rse_settings_to_rse_attributes.py +135 -0
  240. rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py +65 -0
  241. rucio/db/sqla/migrate_repo/versions/22cf51430c78_add_availability_column_to_table_rses.py +42 -0
  242. rucio/db/sqla/migrate_repo/versions/22d887e4ec0a_create_sources_table.py +66 -0
  243. rucio/db/sqla/migrate_repo/versions/25821a8a45a3_remove_unique_constraint_on_requests.py +54 -0
  244. rucio/db/sqla/migrate_repo/versions/25fc855625cf_added_unique_constraint_to_rules.py +43 -0
  245. rucio/db/sqla/migrate_repo/versions/269fee20dee9_add_repair_cnt_to_locks.py +46 -0
  246. rucio/db/sqla/migrate_repo/versions/271a46ea6244_add_ignore_availability_column_to_rules.py +47 -0
  247. rucio/db/sqla/migrate_repo/versions/277b5fbb41d3_switch_heartbeats_executable.py +54 -0
  248. rucio/db/sqla/migrate_repo/versions/27e3a68927fb_remove_replicas_tombstone_and_replicas_.py +39 -0
  249. rucio/db/sqla/migrate_repo/versions/2854cd9e168_added_rule_id_column.py +48 -0
  250. rucio/db/sqla/migrate_repo/versions/295289b5a800_processed_by_and__at_in_requests.py +47 -0
  251. rucio/db/sqla/migrate_repo/versions/2962ece31cf4_add_nbaccesses_column_in_the_did_table.py +48 -0
  252. rucio/db/sqla/migrate_repo/versions/2af3291ec4c_added_replicas_history_table.py +59 -0
  253. rucio/db/sqla/migrate_repo/versions/2b69addda658_add_columns_for_third_party_copy_read_.py +47 -0
  254. rucio/db/sqla/migrate_repo/versions/2b8e7bcb4783_add_config_table.py +72 -0
  255. rucio/db/sqla/migrate_repo/versions/2ba5229cb54c_add_submitted_at_to_requests_table.py +46 -0
  256. rucio/db/sqla/migrate_repo/versions/2cbee484dcf9_added_column_volume_to_rse_transfer_.py +45 -0
  257. rucio/db/sqla/migrate_repo/versions/2edee4a83846_add_source_to_requests_and_requests_.py +48 -0
  258. rucio/db/sqla/migrate_repo/versions/2eef46be23d4_change_tokens_pk.py +48 -0
  259. rucio/db/sqla/migrate_repo/versions/2f648fc909f3_index_in_rule_history_on_scope_name.py +42 -0
  260. rucio/db/sqla/migrate_repo/versions/3082b8cef557_add_naming_convention_table_and_closed_.py +69 -0
  261. rucio/db/sqla/migrate_repo/versions/30fa38b6434e_add_index_on_service_column_in_the_message_table.py +46 -0
  262. rucio/db/sqla/migrate_repo/versions/3152492b110b_added_staging_area_column.py +78 -0
  263. rucio/db/sqla/migrate_repo/versions/32c7d2783f7e_create_bad_replicas_table.py +62 -0
  264. rucio/db/sqla/migrate_repo/versions/3345511706b8_replicas_table_pk_definition_is_in_.py +74 -0
  265. rucio/db/sqla/migrate_repo/versions/35ef10d1e11b_change_index_on_table_requests.py +44 -0
  266. rucio/db/sqla/migrate_repo/versions/379a19b5332d_create_rse_limits_table.py +67 -0
  267. rucio/db/sqla/migrate_repo/versions/384b96aa0f60_created_rule_history_tables.py +134 -0
  268. rucio/db/sqla/migrate_repo/versions/3ac1660a1a72_extend_distance_table.py +58 -0
  269. rucio/db/sqla/migrate_repo/versions/3ad36e2268b0_create_collection_replicas_updates_table.py +79 -0
  270. rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py +61 -0
  271. rucio/db/sqla/migrate_repo/versions/3d9813fab443_add_a_new_state_lost_in_badfilesstatus.py +45 -0
  272. rucio/db/sqla/migrate_repo/versions/40ad39ce3160_add_transferred_at_to_requests_table.py +46 -0
  273. rucio/db/sqla/migrate_repo/versions/4207be2fd914_add_notification_column_to_rules.py +65 -0
  274. rucio/db/sqla/migrate_repo/versions/42db2617c364_create_index_on_requests_external_id.py +42 -0
  275. rucio/db/sqla/migrate_repo/versions/436827b13f82_added_column_activity_to_table_requests.py +46 -0
  276. rucio/db/sqla/migrate_repo/versions/44278720f774_update_requests_typ_sta_upd_idx_index.py +46 -0
  277. rucio/db/sqla/migrate_repo/versions/45378a1e76a8_create_collection_replica_table.py +80 -0
  278. rucio/db/sqla/migrate_repo/versions/469d262be19_removing_created_at_index.py +43 -0
  279. rucio/db/sqla/migrate_repo/versions/4783c1f49cb4_create_distance_table.py +61 -0
  280. rucio/db/sqla/migrate_repo/versions/49a21b4d4357_create_index_on_table_tokens.py +47 -0
  281. rucio/db/sqla/migrate_repo/versions/4a2cbedda8b9_add_source_replica_expression_column_to_.py +46 -0
  282. rucio/db/sqla/migrate_repo/versions/4a7182d9578b_added_bytes_length_accessed_at_columns.py +52 -0
  283. rucio/db/sqla/migrate_repo/versions/4bab9edd01fc_create_index_on_requests_rule_id.py +42 -0
  284. rucio/db/sqla/migrate_repo/versions/4c3a4acfe006_new_attr_account_table.py +65 -0
  285. rucio/db/sqla/migrate_repo/versions/4cf0a2e127d4_adding_transient_metadata.py +46 -0
  286. rucio/db/sqla/migrate_repo/versions/50280c53117c_add_qos_class_to_rse.py +47 -0
  287. rucio/db/sqla/migrate_repo/versions/52153819589c_add_rse_id_to_replicas_table.py +45 -0
  288. rucio/db/sqla/migrate_repo/versions/52fd9f4916fa_added_activity_to_rules.py +46 -0
  289. rucio/db/sqla/migrate_repo/versions/53b479c3cb0f_fix_did_meta_table_missing_updated_at_.py +48 -0
  290. rucio/db/sqla/migrate_repo/versions/5673b4b6e843_add_wfms_metadata_to_rule_tables.py +50 -0
  291. rucio/db/sqla/migrate_repo/versions/575767d9f89_added_source_history_table.py +59 -0
  292. rucio/db/sqla/migrate_repo/versions/58bff7008037_add_started_at_to_requests.py +48 -0
  293. rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py +108 -0
  294. rucio/db/sqla/migrate_repo/versions/5f139f77382a_added_child_rule_id_column.py +57 -0
  295. rucio/db/sqla/migrate_repo/versions/688ef1840840_adding_did_meta_table.py +51 -0
  296. rucio/db/sqla/migrate_repo/versions/6e572a9bfbf3_add_new_split_container_column_to_rules.py +50 -0
  297. rucio/db/sqla/migrate_repo/versions/70587619328_add_comment_column_for_subscriptions.py +46 -0
  298. rucio/db/sqla/migrate_repo/versions/739064d31565_remove_history_table_pks.py +42 -0
  299. rucio/db/sqla/migrate_repo/versions/7541902bf173_add_didsfollowed_and_followevents_table.py +93 -0
  300. rucio/db/sqla/migrate_repo/versions/7ec22226cdbf_new_replica_state_for_temporary_.py +73 -0
  301. rucio/db/sqla/migrate_repo/versions/810a41685bc1_added_columns_rse_transfer_limits.py +52 -0
  302. rucio/db/sqla/migrate_repo/versions/83f991c63a93_correct_rse_expression_length.py +45 -0
  303. rucio/db/sqla/migrate_repo/versions/8523998e2e76_increase_size_of_extended_attributes_.py +46 -0
  304. rucio/db/sqla/migrate_repo/versions/8ea9122275b1_adding_missing_function_based_indices.py +54 -0
  305. rucio/db/sqla/migrate_repo/versions/90f47792bb76_add_clob_payload_to_messages.py +48 -0
  306. rucio/db/sqla/migrate_repo/versions/914b8f02df38_new_table_for_lifetime_model_exceptions.py +70 -0
  307. rucio/db/sqla/migrate_repo/versions/94a5961ddbf2_add_estimator_columns.py +48 -0
  308. rucio/db/sqla/migrate_repo/versions/9a1b149a2044_add_saml_identity_type.py +95 -0
  309. rucio/db/sqla/migrate_repo/versions/9a45bc4ea66d_add_vp_table.py +55 -0
  310. rucio/db/sqla/migrate_repo/versions/9eb936a81eb1_true_is_true.py +74 -0
  311. rucio/db/sqla/migrate_repo/versions/a118956323f8_added_vo_table_and_vo_col_to_rse.py +78 -0
  312. rucio/db/sqla/migrate_repo/versions/a193a275255c_add_status_column_in_messages.py +49 -0
  313. rucio/db/sqla/migrate_repo/versions/a5f6f6e928a7_1_7_0.py +124 -0
  314. rucio/db/sqla/migrate_repo/versions/a616581ee47_added_columns_to_table_requests.py +60 -0
  315. rucio/db/sqla/migrate_repo/versions/a6eb23955c28_state_idx_non_functional.py +53 -0
  316. rucio/db/sqla/migrate_repo/versions/a74275a1ad30_added_global_quota_table.py +56 -0
  317. rucio/db/sqla/migrate_repo/versions/a93e4e47bda_heartbeats.py +67 -0
  318. rucio/db/sqla/migrate_repo/versions/ae2a56fcc89_added_comment_column_to_rules.py +50 -0
  319. rucio/db/sqla/migrate_repo/versions/b4293a99f344_added_column_identity_to_table_tokens.py +46 -0
  320. rucio/db/sqla/migrate_repo/versions/b7d287de34fd_removal_of_replicastate_source.py +92 -0
  321. rucio/db/sqla/migrate_repo/versions/b818052fa670_add_index_to_quarantined_replicas.py +42 -0
  322. rucio/db/sqla/migrate_repo/versions/b8caac94d7f0_add_comments_column_for_subscriptions_.py +46 -0
  323. rucio/db/sqla/migrate_repo/versions/b96a1c7e1cc4_new_bad_pfns_table_and_bad_replicas_.py +147 -0
  324. rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py +78 -0
  325. rucio/db/sqla/migrate_repo/versions/bc68e9946deb_add_staging_timestamps_to_request.py +53 -0
  326. rucio/db/sqla/migrate_repo/versions/bf3baa1c1474_correct_pk_and_idx_for_history_tables.py +74 -0
  327. rucio/db/sqla/migrate_repo/versions/c0937668555f_add_qos_policy_map_table.py +56 -0
  328. rucio/db/sqla/migrate_repo/versions/c129ccdb2d5_add_lumiblocknr_to_dids.py +46 -0
  329. rucio/db/sqla/migrate_repo/versions/ccdbcd48206e_add_did_type_column_index_on_did_meta_.py +68 -0
  330. rucio/db/sqla/migrate_repo/versions/cebad904c4dd_new_payload_column_for_heartbeats.py +48 -0
  331. rucio/db/sqla/migrate_repo/versions/d1189a09c6e0_oauth2_0_and_jwt_feature_support_adding_.py +149 -0
  332. rucio/db/sqla/migrate_repo/versions/d23453595260_extend_request_state_for_preparer.py +106 -0
  333. rucio/db/sqla/migrate_repo/versions/d6dceb1de2d_added_purge_column_to_rules.py +47 -0
  334. rucio/db/sqla/migrate_repo/versions/d6e2c3b2cf26_remove_third_party_copy_column_from_rse.py +45 -0
  335. rucio/db/sqla/migrate_repo/versions/d91002c5841_new_account_limits_table.py +105 -0
  336. rucio/db/sqla/migrate_repo/versions/e138c364ebd0_extending_columns_for_filter_and_.py +52 -0
  337. rucio/db/sqla/migrate_repo/versions/e59300c8b179_support_for_archive.py +106 -0
  338. rucio/db/sqla/migrate_repo/versions/f1b14a8c2ac1_postgres_use_check_constraints.py +30 -0
  339. rucio/db/sqla/migrate_repo/versions/f41ffe206f37_oracle_global_temporary_tables.py +75 -0
  340. rucio/db/sqla/migrate_repo/versions/f85a2962b021_adding_transfertool_column_to_requests_.py +49 -0
  341. rucio/db/sqla/migrate_repo/versions/fa7a7d78b602_increase_refresh_token_size.py +45 -0
  342. rucio/db/sqla/migrate_repo/versions/fb28a95fe288_add_replicas_rse_id_tombstone_idx.py +38 -0
  343. rucio/db/sqla/migrate_repo/versions/fe1a65b176c9_set_third_party_copy_read_and_write_.py +44 -0
  344. rucio/db/sqla/migrate_repo/versions/fe8ea2fa9788_added_third_party_copy_column_to_rse_.py +46 -0
  345. rucio/db/sqla/models.py +1834 -0
  346. rucio/db/sqla/sautils.py +48 -0
  347. rucio/db/sqla/session.py +470 -0
  348. rucio/db/sqla/types.py +207 -0
  349. rucio/db/sqla/util.py +521 -0
  350. rucio/rse/__init__.py +97 -0
  351. rucio/rse/protocols/__init__.py +14 -0
  352. rucio/rse/protocols/cache.py +123 -0
  353. rucio/rse/protocols/dummy.py +112 -0
  354. rucio/rse/protocols/gfal.py +701 -0
  355. rucio/rse/protocols/globus.py +243 -0
  356. rucio/rse/protocols/gsiftp.py +93 -0
  357. rucio/rse/protocols/http_cache.py +83 -0
  358. rucio/rse/protocols/mock.py +124 -0
  359. rucio/rse/protocols/ngarc.py +210 -0
  360. rucio/rse/protocols/posix.py +251 -0
  361. rucio/rse/protocols/protocol.py +530 -0
  362. rucio/rse/protocols/rclone.py +365 -0
  363. rucio/rse/protocols/rfio.py +137 -0
  364. rucio/rse/protocols/srm.py +339 -0
  365. rucio/rse/protocols/ssh.py +414 -0
  366. rucio/rse/protocols/storm.py +207 -0
  367. rucio/rse/protocols/webdav.py +547 -0
  368. rucio/rse/protocols/xrootd.py +295 -0
  369. rucio/rse/rsemanager.py +752 -0
  370. rucio/tests/__init__.py +14 -0
  371. rucio/tests/common.py +244 -0
  372. rucio/tests/common_server.py +132 -0
  373. rucio/transfertool/__init__.py +14 -0
  374. rucio/transfertool/fts3.py +1484 -0
  375. rucio/transfertool/globus.py +200 -0
  376. rucio/transfertool/globus_library.py +182 -0
  377. rucio/transfertool/mock.py +81 -0
  378. rucio/transfertool/transfertool.py +212 -0
  379. rucio/vcsversion.py +11 -0
  380. rucio/version.py +46 -0
  381. rucio/web/__init__.py +14 -0
  382. rucio/web/rest/__init__.py +14 -0
  383. rucio/web/rest/flaskapi/__init__.py +14 -0
  384. rucio/web/rest/flaskapi/authenticated_bp.py +28 -0
  385. rucio/web/rest/flaskapi/v1/__init__.py +14 -0
  386. rucio/web/rest/flaskapi/v1/accountlimits.py +234 -0
  387. rucio/web/rest/flaskapi/v1/accounts.py +1088 -0
  388. rucio/web/rest/flaskapi/v1/archives.py +100 -0
  389. rucio/web/rest/flaskapi/v1/auth.py +1642 -0
  390. rucio/web/rest/flaskapi/v1/common.py +385 -0
  391. rucio/web/rest/flaskapi/v1/config.py +305 -0
  392. rucio/web/rest/flaskapi/v1/credentials.py +213 -0
  393. rucio/web/rest/flaskapi/v1/dids.py +2204 -0
  394. rucio/web/rest/flaskapi/v1/dirac.py +116 -0
  395. rucio/web/rest/flaskapi/v1/export.py +77 -0
  396. rucio/web/rest/flaskapi/v1/heartbeats.py +129 -0
  397. rucio/web/rest/flaskapi/v1/identities.py +263 -0
  398. rucio/web/rest/flaskapi/v1/import.py +133 -0
  399. rucio/web/rest/flaskapi/v1/lifetime_exceptions.py +315 -0
  400. rucio/web/rest/flaskapi/v1/locks.py +360 -0
  401. rucio/web/rest/flaskapi/v1/main.py +83 -0
  402. rucio/web/rest/flaskapi/v1/meta.py +226 -0
  403. rucio/web/rest/flaskapi/v1/metrics.py +37 -0
  404. rucio/web/rest/flaskapi/v1/nongrid_traces.py +97 -0
  405. rucio/web/rest/flaskapi/v1/ping.py +89 -0
  406. rucio/web/rest/flaskapi/v1/redirect.py +366 -0
  407. rucio/web/rest/flaskapi/v1/replicas.py +1866 -0
  408. rucio/web/rest/flaskapi/v1/requests.py +841 -0
  409. rucio/web/rest/flaskapi/v1/rses.py +2204 -0
  410. rucio/web/rest/flaskapi/v1/rules.py +824 -0
  411. rucio/web/rest/flaskapi/v1/scopes.py +161 -0
  412. rucio/web/rest/flaskapi/v1/subscriptions.py +646 -0
  413. rucio/web/rest/flaskapi/v1/templates/auth_crash.html +80 -0
  414. rucio/web/rest/flaskapi/v1/templates/auth_granted.html +82 -0
  415. rucio/web/rest/flaskapi/v1/tmp_dids.py +115 -0
  416. rucio/web/rest/flaskapi/v1/traces.py +100 -0
  417. rucio/web/rest/flaskapi/v1/vos.py +280 -0
  418. rucio/web/rest/main.py +19 -0
  419. rucio/web/rest/metrics.py +28 -0
  420. rucio-32.8.6.data/data/rucio/etc/alembic.ini.template +71 -0
  421. rucio-32.8.6.data/data/rucio/etc/alembic_offline.ini.template +74 -0
  422. rucio-32.8.6.data/data/rucio/etc/globus-config.yml.template +5 -0
  423. rucio-32.8.6.data/data/rucio/etc/ldap.cfg.template +30 -0
  424. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_approval_request.tmpl +38 -0
  425. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_approved_admin.tmpl +4 -0
  426. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_approved_user.tmpl +17 -0
  427. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_denied_admin.tmpl +6 -0
  428. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_denied_user.tmpl +17 -0
  429. rucio-32.8.6.data/data/rucio/etc/mail_templates/rule_ok_notification.tmpl +19 -0
  430. rucio-32.8.6.data/data/rucio/etc/rse-accounts.cfg.template +25 -0
  431. rucio-32.8.6.data/data/rucio/etc/rucio.cfg.atlas.client.template +42 -0
  432. rucio-32.8.6.data/data/rucio/etc/rucio.cfg.template +257 -0
  433. rucio-32.8.6.data/data/rucio/etc/rucio_multi_vo.cfg.template +234 -0
  434. rucio-32.8.6.data/data/rucio/requirements.txt +55 -0
  435. rucio-32.8.6.data/data/rucio/tools/bootstrap.py +34 -0
  436. rucio-32.8.6.data/data/rucio/tools/merge_rucio_configs.py +147 -0
  437. rucio-32.8.6.data/data/rucio/tools/reset_database.py +40 -0
  438. rucio-32.8.6.data/scripts/rucio +2540 -0
  439. rucio-32.8.6.data/scripts/rucio-abacus-account +75 -0
  440. rucio-32.8.6.data/scripts/rucio-abacus-collection-replica +47 -0
  441. rucio-32.8.6.data/scripts/rucio-abacus-rse +79 -0
  442. rucio-32.8.6.data/scripts/rucio-admin +2434 -0
  443. rucio-32.8.6.data/scripts/rucio-atropos +61 -0
  444. rucio-32.8.6.data/scripts/rucio-auditor +199 -0
  445. rucio-32.8.6.data/scripts/rucio-automatix +51 -0
  446. rucio-32.8.6.data/scripts/rucio-bb8 +58 -0
  447. rucio-32.8.6.data/scripts/rucio-c3po +86 -0
  448. rucio-32.8.6.data/scripts/rucio-cache-client +135 -0
  449. rucio-32.8.6.data/scripts/rucio-cache-consumer +43 -0
  450. rucio-32.8.6.data/scripts/rucio-conveyor-finisher +59 -0
  451. rucio-32.8.6.data/scripts/rucio-conveyor-poller +67 -0
  452. rucio-32.8.6.data/scripts/rucio-conveyor-preparer +38 -0
  453. rucio-32.8.6.data/scripts/rucio-conveyor-receiver +44 -0
  454. rucio-32.8.6.data/scripts/rucio-conveyor-stager +77 -0
  455. rucio-32.8.6.data/scripts/rucio-conveyor-submitter +140 -0
  456. rucio-32.8.6.data/scripts/rucio-conveyor-throttler +105 -0
  457. rucio-32.8.6.data/scripts/rucio-dark-reaper +54 -0
  458. rucio-32.8.6.data/scripts/rucio-dumper +159 -0
  459. rucio-32.8.6.data/scripts/rucio-follower +45 -0
  460. rucio-32.8.6.data/scripts/rucio-hermes +55 -0
  461. rucio-32.8.6.data/scripts/rucio-judge-cleaner +90 -0
  462. rucio-32.8.6.data/scripts/rucio-judge-evaluator +138 -0
  463. rucio-32.8.6.data/scripts/rucio-judge-injector +45 -0
  464. rucio-32.8.6.data/scripts/rucio-judge-repairer +45 -0
  465. rucio-32.8.6.data/scripts/rucio-kronos +45 -0
  466. rucio-32.8.6.data/scripts/rucio-light-reaper +53 -0
  467. rucio-32.8.6.data/scripts/rucio-minos +54 -0
  468. rucio-32.8.6.data/scripts/rucio-minos-temporary-expiration +51 -0
  469. rucio-32.8.6.data/scripts/rucio-necromancer +121 -0
  470. rucio-32.8.6.data/scripts/rucio-oauth-manager +64 -0
  471. rucio-32.8.6.data/scripts/rucio-reaper +84 -0
  472. rucio-32.8.6.data/scripts/rucio-replica-recoverer +249 -0
  473. rucio-32.8.6.data/scripts/rucio-storage-consistency-actions +75 -0
  474. rucio-32.8.6.data/scripts/rucio-transmogrifier +78 -0
  475. rucio-32.8.6.data/scripts/rucio-undertaker +77 -0
  476. rucio-32.8.6.dist-info/METADATA +83 -0
  477. rucio-32.8.6.dist-info/RECORD +481 -0
  478. rucio-32.8.6.dist-info/WHEEL +5 -0
  479. rucio-32.8.6.dist-info/licenses/AUTHORS.rst +94 -0
  480. rucio-32.8.6.dist-info/licenses/LICENSE +201 -0
  481. rucio-32.8.6.dist-info/top_level.txt +1 -0
rucio/core/oidc.py ADDED
@@ -0,0 +1,1339 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import json
17
+ import logging
18
+ import random
19
+ import subprocess
20
+ import traceback
21
+ from datetime import datetime, timedelta
22
+ from typing import Any, TYPE_CHECKING, Optional
23
+ from urllib.parse import urlparse, parse_qs
24
+
25
+ from jwkest.jws import JWS
26
+ from jwkest.jwt import JWT
27
+ from math import floor
28
+ from oic import rndstr
29
+ from oic.oauth2.message import CCAccessTokenRequest
30
+ from oic.oic import Client, Grant, Token, REQUEST2ENDPOINT
31
+ from oic.oic.message import (AccessTokenResponse, AuthorizationResponse,
32
+ Message, RegistrationResponse)
33
+ from oic.utils import time_util
34
+ from oic.utils.authn.client import CLIENT_AUTHN_METHOD
35
+ from sqlalchemy import delete, select, update
36
+ from sqlalchemy.sql.expression import true
37
+
38
+ from rucio.common import types
39
+ from rucio.common.config import config_get, config_get_int
40
+ from rucio.common.exception import (CannotAuthenticate, CannotAuthorize,
41
+ RucioException)
42
+ from rucio.common.stopwatch import Stopwatch
43
+ from rucio.common.utils import all_oidc_req_claims_present, build_url, val_to_space_sep_str
44
+ from rucio.core.account import account_exists
45
+ from rucio.core.identity import exist_identity_account, get_default_account
46
+ from rucio.core.monitor import MetricManager
47
+ from rucio.db.sqla import filter_thread_work
48
+ from rucio.db.sqla import models
49
+ from rucio.db.sqla.constants import IdentityType
50
+ from rucio.db.sqla.session import read_session, transactional_session
51
+
52
+ if TYPE_CHECKING:
53
+ from sqlalchemy.orm import Session
54
+
55
+
56
+ METRICS = MetricManager(module=__name__)
57
+
58
+ # worokaround for a bug in pyoidc (as of Dec 2019)
59
+ REQUEST2ENDPOINT['CCAccessTokenRequest'] = 'token_endpoint'
60
+
61
+ # private/protected file containing Rucio Client secrets known to the Identity Provider as well
62
+ IDPSECRETS = config_get('oidc', 'idpsecrets', False)
63
+ ADMIN_ISSUER_ID = config_get('oidc', 'admin_issuer', False)
64
+ EXPECTED_OIDC_AUDIENCE = config_get('oidc', 'expected_audience', False, 'rucio')
65
+ EXPECTED_OIDC_SCOPE = config_get('oidc', 'expected_scope', False, 'openid profile')
66
+ EXCHANGE_GRANT_TYPE = config_get('oidc', 'exchange_grant_type', False, 'urn:ietf:params:oauth:grant-type:token-exchange')
67
+ REFRESH_LIFETIME_H = config_get_int('oidc', 'default_jwt_refresh_lifetime', False, 96)
68
+
69
+ # Allow 2 mins of leeway in case Rucio and IdP server clocks are not perfectly synchronized
70
+ # this affects the token issued time (a token could be issued in the future if IdP clock is ahead)
71
+ LEEWAY_SECS = 120
72
+
73
+
74
+ # TO-DO permission layer: if scope == 'wlcg.groups'
75
+ # --> check 'profile' info (requested profile scope)
76
+
77
+
78
+ def __get_rucio_oidc_clients(keytimeout: int = 43200) -> tuple[dict, dict]:
79
+ """
80
+ Creates a Rucio OIDC Client instances per Identity Provider (IdP)
81
+ according to etc/idpsecrets.json configuration file.
82
+ Clients have to be pre-registered with the respective IdP with the appropriate settings:
83
+ allowed to request refresh tokens which have lifetime set in their unverified header,
84
+ allowed to request token exchange, immediate refresh tokens expiration after first use)
85
+
86
+ :returns: Dictionary of {'https://issuer_1/': <Rucio OIDC Client_1 instance>,
87
+ 'https://issuer_2/': <Rucio OIDC Client_2 instance>,}.
88
+ In case of trouble, Exception is raised.
89
+ """
90
+ clients = {}
91
+ admin_clients = {}
92
+ try:
93
+ with open(IDPSECRETS) as client_secret_file:
94
+ client_secrets = json.load(client_secret_file)
95
+ except:
96
+ return (clients, admin_clients)
97
+ for iss in client_secrets:
98
+ try:
99
+ client_secret = client_secrets[iss]
100
+ issuer = client_secret["issuer"]
101
+ client = Client(client_authn_method=CLIENT_AUTHN_METHOD)
102
+ # general parameter discovery about the Identity Provider via issuers URL
103
+ client.provider_config(issuer)
104
+ # storing client specific parameters into the client itself
105
+ client_reg = RegistrationResponse(**client_secret)
106
+ client.store_registration_info(client_reg)
107
+ # setting public_key cache timeout to 'keytimeout' seconds
108
+ keybundles = client.keyjar.issuer_keys[client.issuer]
109
+ for keybundle in keybundles:
110
+ keybundle.cache_time = keytimeout
111
+ clients[issuer] = client
112
+ # doing the same to store a Rucio Admin client
113
+ # which has client credential flow allowed
114
+ client_secret = client_secrets[iss]["SCIM"]
115
+ client = Client(client_authn_method=CLIENT_AUTHN_METHOD)
116
+ client.provider_config(issuer)
117
+ client_reg = RegistrationResponse(**client_secret)
118
+ client.store_registration_info(client_reg)
119
+ admin_clients[issuer] = client
120
+ except Exception as error:
121
+ raise RucioException(error.args) from error
122
+ return (clients, admin_clients)
123
+
124
+
125
+ # global variables to represent the IdP clients
126
+ OIDC_CLIENTS = {}
127
+ OIDC_ADMIN_CLIENTS = {}
128
+
129
+
130
+ def __initialize_oidc_clients() -> None:
131
+ """
132
+ Initialising Rucio OIDC Clients
133
+ """
134
+
135
+ try:
136
+ ALL_OIDC_CLIENTS = __get_rucio_oidc_clients()
137
+ global OIDC_CLIENTS
138
+ global OIDC_ADMIN_CLIENTS
139
+ OIDC_CLIENTS = ALL_OIDC_CLIENTS[0]
140
+ OIDC_ADMIN_CLIENTS = ALL_OIDC_CLIENTS[1]
141
+ except Exception as error:
142
+ logging.debug("OIDC clients not properly loaded: %s", error)
143
+ pass
144
+
145
+
146
+ # try loading OIDC clients uppon module import
147
+ __initialize_oidc_clients()
148
+
149
+
150
+ def __get_init_oidc_client(token_object: models.Token = None, token_type: str = None, **kwargs) -> dict[Any, Any]:
151
+ """
152
+ Get an OIDC client object, (re-)initialised with parameters corresponding
153
+ to authorization flows used to get a token. For special cases - token refresh,
154
+ token exchange - these parameters are being mocked as pyoidc library
155
+ has to develop these areas. Initialisation can be made either by kwargs
156
+ (for a authorization code flow e.g.) or via kwargs (for token exchange or token refresh).
157
+
158
+ :param session_state: state value of the first authorization request
159
+ :param token_object: DB token token to be included in a Grant for
160
+ the token exchange or token refresh mechanisms
161
+ :param token_type: e.g. "subject_token" for token exchange or "refresh_token"
162
+ :param kwargs: optional strings which contain expected oauth session parameters:
163
+ issuer_id/issuer, redirect_uri, redirect_to, state, nonce, code,
164
+ scope, audience,
165
+
166
+ :returns: if first_init == True: dict {'client': oidc client object, 'request': auth_url}
167
+ for all other cases return oidc client object. If anything goes wrong, exception is thrown.
168
+ """
169
+
170
+ if not OIDC_CLIENTS:
171
+ # retry once loading OIDC clients
172
+ __initialize_oidc_clients()
173
+ if not OIDC_CLIENTS:
174
+ raise CannotAuthenticate(traceback.format_exc())
175
+
176
+ try:
177
+
178
+ auth_args = {"grant_types": ["authorization_code"],
179
+ "response_type": "code",
180
+ "state": kwargs.get('state', rndstr()),
181
+ "nonce": kwargs.get('nonce', rndstr())}
182
+ auth_args["scope"] = token_object.oidc_scope if token_object else kwargs.get('scope', " ")
183
+ auth_args["audience"] = token_object.audience if token_object else kwargs.get('audience', " ")
184
+
185
+ if token_object:
186
+ issuer = token_object.identity.split(", ")[1].split("=")[1]
187
+ oidc_client = OIDC_CLIENTS[issuer]
188
+ auth_args["client_id"] = oidc_client.client_id
189
+ token = ''
190
+ if not token_type:
191
+ token_type = kwargs.get('token_type', None)
192
+ if token_type == 'subject_token':
193
+ token = token_object.token
194
+ # do not remove - even though None, oic expects this key to exist
195
+ auth_args["redirect_uri"] = None
196
+ if token_type == 'refresh_token':
197
+ token = token_object.refresh_token
198
+ # do not remove - even though None, oic expects this key to exist
199
+ auth_args["redirect_uri"] = None
200
+ if token_type and token:
201
+ oidc_client.grant[auth_args['state']] = Grant()
202
+ oidc_client.grant[auth_args['state']].grant_expiration_time = time_util.utc_time_sans_frac() + 300
203
+ resp = AccessTokenResponse()
204
+ resp[token_type] = token
205
+ oidc_client.grant[auth_args['state']].tokens.append(Token(resp))
206
+ else:
207
+ secrets, client_secret = {}, {}
208
+ try:
209
+ with open(IDPSECRETS) as client_secret_file:
210
+ secrets = json.load(client_secret_file)
211
+ except Exception as error:
212
+ raise CannotAuthenticate("Rucio server is missing information from the idpsecrets.json file.") from error
213
+ if 'issuer_id' in kwargs:
214
+ client_secret = secrets[kwargs.get('issuer_id', ADMIN_ISSUER_ID)]
215
+ elif 'issuer' in kwargs:
216
+ client_secret = next((secrets[i] for i in secrets if 'issuer' in secrets[i] and # NOQA: W504
217
+ kwargs.get('issuer') in secrets[i]['issuer']), None)
218
+ redirect_url = kwargs.get('redirect_uri', None)
219
+ if not redirect_url:
220
+ redirect_to = kwargs.get("redirect_to", "auth/oidc_token")
221
+ redirect_urls = [u for u in client_secret["redirect_uris"] if redirect_to in u]
222
+ redirect_url = random.choice(redirect_urls)
223
+ if not redirect_url:
224
+ raise CannotAuthenticate("Could not pick any redirect URL(s) from the ones defined "
225
+ + "in Rucio OIDC Client configuration file.") # NOQA: W503
226
+ auth_args["redirect_uri"] = redirect_url
227
+ oidc_client = OIDC_CLIENTS[client_secret["issuer"]]
228
+ auth_args["client_id"] = oidc_client.client_id
229
+
230
+ if kwargs.get('first_init', False):
231
+ auth_url = build_url(oidc_client.authorization_endpoint, params=auth_args)
232
+ return {'redirect': redirect_url, 'auth_url': auth_url}
233
+
234
+ oidc_client.construct_AuthorizationRequest(request_args=auth_args)
235
+ # parsing the authorization query string by the Rucio OIDC Client (creates a Grant)
236
+ oidc_client.parse_response(AuthorizationResponse,
237
+ info='code=' + kwargs.get('code', rndstr()) + '&state=' + auth_args['state'],
238
+ sformat="urlencoded")
239
+ return {'client': oidc_client, 'state': auth_args['state']}
240
+ except Exception as error:
241
+ raise CannotAuthenticate(traceback.format_exc()) from error
242
+
243
+
244
+ @transactional_session
245
+ def get_auth_oidc(account: str, *, session: "Session", **kwargs) -> str:
246
+ """
247
+ Assembles the authorization request of the Rucio Client tailored to the Rucio user
248
+ & Identity Provider. Saves authentication session parameters in the oauth_requests
249
+ DB table (for later use-cases). This information is saved for the token lifetime
250
+ of a token to allow token exchange and refresh.
251
+ Returns authorization URL as a string or a redirection url to
252
+ be used in user's browser for authentication.
253
+
254
+ :param account: Rucio Account identifier as a string.
255
+ :param auth_scope: space separated list of scope names. Scope parameter
256
+ defines which user's info the user allows to provide
257
+ to the Rucio Client.
258
+ :param audience: audience for which tokens are requested (EXPECTED_OIDC_AUDIENCE is the default)
259
+ :param auto: If True, the function will return authorization URL to the Rucio Client
260
+ which will log-in with user's IdP credentials automatically.
261
+ Also it will instruct the IdP to return an AuthZ code to another Rucio REST
262
+ endpoint /oidc_token. If False, the function will return a URL
263
+ to be used by the user in the browser in order to authenticate via IdP
264
+ (which will then return with AuthZ code to /oidc_code REST endpoint).
265
+ :param polling: If True, '_polling' string will be appended to the access_msg
266
+ in the DB oauth_requests table to inform the authorization stage
267
+ that the Rucio Client is polling the server for a token
268
+ (and no fetchcode needs to be returned at the end).
269
+ :param refresh_lifetime: specifies how long the OAuth daemon should
270
+ be refreshing this token. Default is 96 hours.
271
+ :param ip: IP address of the client as a string.
272
+ :param session: The database session in use.
273
+
274
+ :returns: User & Rucio OIDC Client specific Authorization or Redirection URL as a string
275
+ OR a redirection url to be used in user's browser for authentication.
276
+ """
277
+ # TO-DO - implement a check if that account already has a valid
278
+ # token withthe required scope and audience and return such token !
279
+ auth_scope = kwargs.get('auth_scope', EXPECTED_OIDC_SCOPE)
280
+ if not auth_scope:
281
+ auth_scope = EXPECTED_OIDC_SCOPE
282
+ audience = kwargs.get('audience', EXPECTED_OIDC_AUDIENCE)
283
+ if not audience:
284
+ audience = EXPECTED_OIDC_AUDIENCE
285
+ # checking that minimal audience and scope requirements (required by Rucio) are satisfied !
286
+ if not all_oidc_req_claims_present(auth_scope, audience, EXPECTED_OIDC_SCOPE, EXPECTED_OIDC_AUDIENCE):
287
+ raise CannotAuthenticate("Requirements of scope and audience do not satisfy minimal requirements of the Rucio server.")
288
+ issuer_id = kwargs.get('issuer', ADMIN_ISSUER_ID)
289
+ if not issuer_id:
290
+ issuer_id = ADMIN_ISSUER_ID
291
+ auto = kwargs.get('auto', False)
292
+ polling = kwargs.get('polling', False)
293
+ refresh_lifetime = kwargs.get('refresh_lifetime', REFRESH_LIFETIME_H)
294
+ ip = kwargs.get('ip', None)
295
+ webhome = kwargs.get('webhome', None)
296
+ # For webui a mock account will be used here and default account
297
+ # will be assigned to the identity during get_token_oidc
298
+ if account.external == 'webui':
299
+ pass
300
+ else:
301
+ # Make sure the account exists
302
+ if not account_exists(account, session=session):
303
+ logging.debug("Account %s does not exist.", account)
304
+ return None
305
+
306
+ try:
307
+ stopwatch = Stopwatch()
308
+ # redirect_url needs to be specified & one of those defined
309
+ # in the Rucio OIDC Client configuration
310
+ redirect_to = "auth/oidc_code"
311
+ if auto:
312
+ redirect_to = "auth/oidc_token"
313
+ # random strings in order to keep track of responses to outstanding requests (state)
314
+ # and to associate a client session with an ID Token and to mitigate replay attacks (nonce).
315
+ state, nonce = rndstr(50), rndstr(50)
316
+ # in the following statement we retrieve the authorization endpoint
317
+ # from the client of the issuer and build url
318
+ oidc_dict = __get_init_oidc_client(issuer_id=issuer_id, redirect_to=redirect_to,
319
+ state=state, nonce=nonce,
320
+ scope=auth_scope, audience=audience, first_init=True)
321
+ auth_url = oidc_dict['auth_url']
322
+ redirect_url = oidc_dict['redirect']
323
+ # redirect code is put in access_msg and returned to the user (if auto=False)
324
+ access_msg = None
325
+ if not auto:
326
+ access_msg = rndstr(23)
327
+ if polling:
328
+ access_msg += '_polling'
329
+ if auto and webhome:
330
+ access_msg = str(webhome)
331
+ # Making sure refresh_lifetime is an integer or None.
332
+ if refresh_lifetime:
333
+ refresh_lifetime = int(refresh_lifetime)
334
+ # Specifying temporarily 5 min lifetime for the authentication session.
335
+ expired_at = datetime.utcnow() + timedelta(seconds=300)
336
+ # saving session parameters into the Rucio DB
337
+ oauth_session_params = models.OAuthRequest(account=account,
338
+ state=state,
339
+ nonce=nonce,
340
+ access_msg=access_msg,
341
+ redirect_msg=auth_url,
342
+ expired_at=expired_at,
343
+ refresh_lifetime=refresh_lifetime,
344
+ ip=ip)
345
+ oauth_session_params.save(session=session)
346
+ # If user selected authentication via web browser, a redirection
347
+ # URL is returned instead of the direct URL pointing to the IdP.
348
+ if not auto:
349
+ # the following takes into account deployments where the base url of the rucio server is
350
+ # not equivalent to the network location, e.g. if the server is proxied
351
+ auth_server = urlparse(redirect_url)
352
+ auth_url = build_url('https://' + auth_server.netloc, path='{}auth/oidc_redirect'.format(
353
+ auth_server.path.split('auth/')[0].lstrip('/')), params=access_msg)
354
+
355
+ METRICS.timer('IdP_authentication.request').observe(stopwatch.elapsed)
356
+ return auth_url
357
+
358
+ except Exception as error:
359
+ raise CannotAuthenticate(traceback.format_exc()) from error
360
+
361
+
362
+ @transactional_session
363
+ def get_token_oidc(auth_query_string: str, ip: str = None, *, session: "Session"):
364
+ """
365
+ After Rucio User got redirected to Rucio /auth/oidc_token (or /auth/oidc_code)
366
+ REST endpoints with authz code and session state encoded within the URL.
367
+ These parameters are used to eventually gets user's info and tokens from IdP.
368
+
369
+ :param auth_query_string: IdP redirection URL query string (AuthZ code & user session state).
370
+ :param ip: IP address of the client as a string.
371
+ :param session: The database session in use.
372
+
373
+ :returns: One of the following tuples: ("fetchcode", <code>); ("token", <token>);
374
+ ("polling", True); The result depends on the authentication strategy being used
375
+ (no auto, auto, polling).
376
+ """
377
+ try:
378
+ stopwatch = Stopwatch()
379
+ parsed_authquery = parse_qs(auth_query_string)
380
+ state = parsed_authquery["state"][0]
381
+ code = parsed_authquery["code"][0]
382
+ # getting oauth request params from the oauth_requests DB Table
383
+ query = select(
384
+ models.OAuthRequest
385
+ ).where(
386
+ models.OAuthRequest.state == state
387
+ )
388
+ oauth_req_params = session.execute(query).scalar()
389
+ if oauth_req_params is None:
390
+ raise CannotAuthenticate("User related Rucio OIDC session could not keep "
391
+ + "track of responses from outstanding requests.") # NOQA: W503
392
+ req_url = urlparse(oauth_req_params.redirect_msg or '')
393
+ issuer = req_url.scheme + "://" + req_url.netloc
394
+ req_params = parse_qs(req_url.query)
395
+ client_params = {}
396
+ for key in list(req_params):
397
+ client_params[key] = val_to_space_sep_str(req_params[key])
398
+
399
+ oidc_client = __get_init_oidc_client(issuer=issuer, code=code, **client_params)['client']
400
+ METRICS.counter(name='IdP_authentication.code_granted').inc()
401
+ # exchange access code for a access token
402
+ oidc_tokens = oidc_client.do_access_token_request(state=state,
403
+ request_args={"code": code},
404
+ authn_method="client_secret_basic",
405
+ skew=LEEWAY_SECS)
406
+ if 'error' in oidc_tokens:
407
+ raise CannotAuthorize(oidc_tokens['error'])
408
+ # mitigate replay attacks
409
+ nonce = oauth_req_params.nonce
410
+ if oidc_tokens['id_token']['nonce'] != nonce:
411
+ raise CannotAuthenticate("ID token could not be associated with the Rucio OIDC Client"
412
+ + " session. This points to possible replay attack !") # NOQA: W503
413
+
414
+ # starting to fill dictionary with parameters for token DB row
415
+ jwt_row_dict, extra_dict = {}, {}
416
+ jwt_row_dict['identity'] = oidc_identity_string(oidc_tokens['id_token']['sub'],
417
+ oidc_tokens['id_token']['iss'])
418
+ jwt_row_dict['account'] = oauth_req_params.account
419
+
420
+ if jwt_row_dict['account'].external == 'webui':
421
+ try:
422
+ jwt_row_dict['account'] = get_default_account(jwt_row_dict['identity'], IdentityType.OIDC, True, session=session)
423
+ except Exception:
424
+ return {'webhome': None, 'token': None}
425
+
426
+ # check if given account has the identity registered
427
+ if not exist_identity_account(jwt_row_dict['identity'], IdentityType.OIDC, jwt_row_dict['account'], session=session):
428
+ raise CannotAuthenticate("OIDC identity '%s' of the '%s' account is unknown to Rucio."
429
+ % (jwt_row_dict['identity'], str(jwt_row_dict['account'])))
430
+ METRICS.counter(name='success').inc()
431
+ # get access token expiry timestamp
432
+ jwt_row_dict['lifetime'] = datetime.utcnow() + timedelta(seconds=oidc_tokens['expires_in'])
433
+ # get audience and scope info from the token
434
+ if 'scope' in oidc_tokens and 'audience' in oidc_tokens:
435
+ jwt_row_dict['authz_scope'] = val_to_space_sep_str(oidc_tokens['scope'])
436
+ jwt_row_dict['audience'] = val_to_space_sep_str(oidc_tokens['audience'])
437
+ elif 'access_token' in oidc_tokens:
438
+ try:
439
+ values = __get_keyvalues_from_claims(oidc_tokens['access_token'], ['scope', 'aud'])
440
+ jwt_row_dict['authz_scope'] = values['scope']
441
+ jwt_row_dict['audience'] = values['aud']
442
+ except Exception:
443
+ # we assume the Identity Provider did not do the right job here
444
+ jwt_row_dict['authz_scope'] = None
445
+ jwt_row_dict['audience'] = None
446
+ # groups = oidc_tokens['id_token']['groups']
447
+ # nothing done with group info for the moment - TO-DO !
448
+ # collect extra token DB row parameters
449
+ extra_dict = {}
450
+ extra_dict['ip'] = ip
451
+ extra_dict['state'] = state
452
+ # In case user requested to grant Rucio a refresh token,
453
+ # this token will get saved in the DB and an automatic refresh
454
+ # for a specified period of time will be initiated (done by the Rucio daemon).
455
+ if 'refresh_token' in oidc_tokens:
456
+ extra_dict['refresh_token'] = oidc_tokens['refresh_token']
457
+ extra_dict['refresh'] = True
458
+ extra_dict['refresh_lifetime'] = REFRESH_LIFETIME_H
459
+ try:
460
+ if oauth_req_params.refresh_lifetime is not None:
461
+ extra_dict['refresh_lifetime'] = int(oauth_req_params.refresh_lifetime)
462
+ except Exception:
463
+ pass
464
+ try:
465
+ values = __get_keyvalues_from_claims(oidc_tokens['refresh_token'], ['exp'])
466
+ exp = values['exp']
467
+ extra_dict['refresh_expired_at'] = datetime.utcfromtimestamp(float(exp))
468
+ except Exception:
469
+ # 4 day expiry period by default
470
+ extra_dict['refresh_expired_at'] = datetime.utcnow() + timedelta(hours=REFRESH_LIFETIME_H)
471
+
472
+ new_token = __save_validated_token(oidc_tokens['access_token'], jwt_row_dict, extra_dict=extra_dict, session=session)
473
+ METRICS.counter(name='IdP_authorization.access_token.saved').inc()
474
+ if 'refresh_token' in oidc_tokens:
475
+ METRICS.counter(name='IdP_authorization.refresh_token.saved').inc()
476
+ # In case authentication via browser was requested,
477
+ # we save the token in the oauth_requests table
478
+ if oauth_req_params.access_msg:
479
+ # If Rucio Client waits for a fetchcode, we save the token under this code in the DB.
480
+ if 'http' not in oauth_req_params.access_msg:
481
+ if '_polling' not in oauth_req_params.access_msg:
482
+ fetchcode = rndstr(50)
483
+ query = update(
484
+ models.OAuthRequest
485
+ ).where(
486
+ models.OAuthRequest.state == state
487
+ ).values({
488
+ models.OAuthRequest.access_msg: fetchcode,
489
+ models.OAuthRequest.redirect_msg: new_token['token']
490
+ })
491
+ # If Rucio Client was requested to poll the Rucio Auth server
492
+ # for a token automatically, we save the token under a access_msg.
493
+ else:
494
+ query = update(
495
+ models.OAuthRequest
496
+ ).where(
497
+ models.OAuthRequest.state == state
498
+ ).values({
499
+ models.OAuthRequest.access_msg: oauth_req_params.access_msg,
500
+ models.OAuthRequest.redirect_msg: new_token['token']
501
+ })
502
+ session.execute(query)
503
+ session.commit()
504
+ METRICS.timer('IdP_authorization').observe(stopwatch.elapsed)
505
+ if '_polling' in oauth_req_params.access_msg:
506
+ return {'polling': True}
507
+ elif 'http' in oauth_req_params.access_msg:
508
+ return {'webhome': oauth_req_params.access_msg, 'token': new_token}
509
+ else:
510
+ return {'fetchcode': fetchcode}
511
+ else:
512
+ METRICS.timer('IdP_authorization').observe(stopwatch.elapsed)
513
+ return {'token': new_token}
514
+
515
+ except Exception:
516
+ # TO-DO catch different exceptions - InvalidGrant etc. ...
517
+ METRICS.counter(name='IdP_authorization.access_token.exception').inc()
518
+ logging.debug(traceback.format_exc())
519
+ return None
520
+ # raise CannotAuthenticate(traceback.format_exc())
521
+
522
+
523
+ @transactional_session
524
+ def __get_admin_token_oidc(account: types.InternalAccount, req_scope, req_audience, issuer, *, session: "Session"):
525
+ """
526
+ Get a token for Rucio application to act on behalf of itself.
527
+ client_credential flow is used for this purpose.
528
+ No refresh token is expected to be used.
529
+
530
+ :param account: the Rucio Admin account name to be used (InternalAccount object expected)
531
+ :param req_scope: the audience requested for the Rucio client's token
532
+ :param req_audience: the scope requested for the Rucio client's token
533
+ :param issuer: the Identity Provider nickname or the Rucio instance in use
534
+ :param session: The database session in use.
535
+ :returns: A dict with token and expires_at entries.
536
+ """
537
+
538
+ if not OIDC_ADMIN_CLIENTS:
539
+ # retry once loading OIDC clients
540
+ __initialize_oidc_clients()
541
+ if not OIDC_ADMIN_CLIENTS:
542
+ raise CannotAuthenticate(traceback.format_exc())
543
+
544
+ try:
545
+
546
+ oidc_client = OIDC_ADMIN_CLIENTS[issuer]
547
+ args = {"client_id": oidc_client.client_id,
548
+ "client_secret": oidc_client.client_secret,
549
+ "grant_type": "client_credentials",
550
+ "scope": req_scope,
551
+ "audience": req_audience}
552
+ # in the future should use oauth2 pyoidc client (base) instead
553
+ oidc_tokens = oidc_client.do_any(request=CCAccessTokenRequest,
554
+ request_args=args,
555
+ response=AccessTokenResponse)
556
+ if 'error' in oidc_tokens:
557
+ raise CannotAuthorize(oidc_tokens['error'])
558
+ METRICS.counter(name='IdP_authentication.rucio_admin_token_granted').inc()
559
+ # save the access token in the Rucio DB
560
+ if 'access_token' in oidc_tokens:
561
+ validate_dict = __get_rucio_jwt_dict(oidc_tokens['access_token'], account=account, session=session)
562
+ if validate_dict:
563
+ METRICS.counter(name='IdP_authentication.success').inc()
564
+ new_token = __save_validated_token(oidc_tokens['access_token'], validate_dict, extra_dict={}, session=session)
565
+ METRICS.counter(name='IdP_authentication.access_token.saved').inc()
566
+ return new_token
567
+ else:
568
+ logging.debug("Rucio could not get a valid admin token from the Identity Provider.")
569
+ return None
570
+ else:
571
+ logging.debug("Rucio could not get its admin access token from the Identity Provider.")
572
+ return None
573
+
574
+ except Exception:
575
+ # TO-DO catch different exceptions - InvalidGrant etc. ...
576
+ METRICS.counter(name='IdP_authorization.access_token.exception').inc()
577
+ logging.debug(traceback.format_exc())
578
+ return None
579
+ # raise CannotAuthenticate(traceback.format_exc())
580
+
581
+
582
+ @read_session
583
+ def __get_admin_account_for_issuer(*, session: "Session"):
584
+ """ Gets admin account for the IdP issuer
585
+ :returns : dictionary { 'issuer_1': (account, identity), ... }
586
+ """
587
+
588
+ if not OIDC_ADMIN_CLIENTS:
589
+ # retry once loading OIDC clients
590
+ __initialize_oidc_clients()
591
+ if not OIDC_ADMIN_CLIENTS:
592
+ raise CannotAuthenticate(traceback.format_exc())
593
+
594
+ issuer_account_dict = {}
595
+ for issuer in OIDC_ADMIN_CLIENTS:
596
+ admin_identity = oidc_identity_string(OIDC_ADMIN_CLIENTS[issuer].client_id, issuer)
597
+ query = select(
598
+ models.IdentityAccountAssociation.account
599
+ ).where(
600
+ models.IdentityAccountAssociation.identity_type == IdentityType.OIDC,
601
+ models.IdentityAccountAssociation.identity == admin_identity
602
+ )
603
+ admin_account = session.execute(query).scalar()
604
+ issuer_account_dict[issuer] = (admin_account, admin_identity)
605
+ return issuer_account_dict
606
+
607
+
608
+ @transactional_session
609
+ def get_token_for_account_operation(account: str, req_audience: str = None, req_scope: str = None, admin: bool = False, *, session: "Session"):
610
+ """
611
+ Looks-up a JWT token with the required scope and audience claims with the account OIDC issuer.
612
+ If tokens are found, and none contains the requested audience and scope a new token is requested
613
+ (via token exchange or client credential grants in case admin = True)
614
+ :param account: Rucio account name in order to lookup the issuer and corresponding valid tokens
615
+ :param req_audience: audience required to be present in the token (e.g. 'fts:atlas')
616
+ :param req_scope: scope requested to be present in the token (e.g. fts:submit-transfer)
617
+ :param admin: If True tokens will be requested for the Rucio admin root account,
618
+ preferably with the same issuer as the requesting account OIDC identity
619
+ :param session: DB session in use
620
+
621
+ :return: token dictionary or None, throws an exception in case of problems
622
+ """
623
+ try:
624
+ if not req_scope:
625
+ req_scope = EXPECTED_OIDC_SCOPE
626
+ if not req_audience:
627
+ req_audience = EXPECTED_OIDC_AUDIENCE
628
+
629
+ # get all identities for the corresponding account
630
+ query = select(
631
+ models.IdentityAccountAssociation.identity
632
+ ).where(
633
+ models.IdentityAccountAssociation.identity_type == IdentityType.OIDC,
634
+ models.IdentityAccountAssociation.account == account
635
+ )
636
+ identities = session.execute(query).scalars().all()
637
+ # get all active/valid OIDC tokens
638
+ query = select(
639
+ models.Token
640
+ ).where(
641
+ models.Token.identity.in_(identities),
642
+ models.Token.account == account,
643
+ models.Token.expired_at > datetime.utcnow()
644
+ ).with_for_update(
645
+ skip_locked=True
646
+ )
647
+ account_tokens = session.execute(query).scalars().all()
648
+
649
+ # for Rucio Admin account we ask IdP for a token via client_credential grant
650
+ # for each user account OIDC identity there is an OIDC issuer that must be, by construction,
651
+ # supported by Rucio server (have OIDC admin client registered as well)
652
+ # that is why we take the issuer of the account identity that has an active/valid token
653
+ # and look for admin account identity which has this issuer assigned
654
+ # requestor should always have at least one active subject token unless it is root
655
+ # this is why we first discover if the requestor is root or not
656
+ get_token_for_adminacc = False
657
+ admin_identity = None
658
+ admin_issuer = None
659
+ admin_iss_acc_idt_dict = __get_admin_account_for_issuer(session=session)
660
+
661
+ # check if preferred issuer exists - if multiple present last one is taken
662
+ preferred_issuer = None
663
+ for token in account_tokens:
664
+ preferred_issuer = token.identity.split(", ")[1].split("=")[1]
665
+ # loop through all OIDC identities registerd for the account of the requestor
666
+ for identity in identities:
667
+ issuer = identity.split(", ")[1].split("=")[1]
668
+ # compare the account of the requestor with the account of the admin
669
+ if account == admin_iss_acc_idt_dict[issuer][0]:
670
+ # take first matching case which means root is requesting OIDC authentication
671
+ admin_identity = admin_iss_acc_idt_dict[issuer][1]
672
+ if preferred_issuer and preferred_issuer != issuer:
673
+ continue
674
+ else:
675
+ admin_issuer = issuer
676
+ get_token_for_adminacc = True
677
+ break
678
+
679
+ # Rucio admin account requesting OIDC token
680
+ if get_token_for_adminacc:
681
+ # openid scope is not supported for client_credentials auth flow - removing it if being asked for
682
+ if 'openid' in req_scope:
683
+ req_scope = req_scope.replace("openid", "").strip()
684
+ # checking if there is not already a token to use
685
+ query = select(
686
+ models.Token
687
+ ).where(
688
+ models.Token.account == account,
689
+ models.Token.expired_at > datetime.utcnow()
690
+ )
691
+ admin_account_tokens = session.execute(query).scalars().all()
692
+ for admin_token in admin_account_tokens:
693
+ if hasattr(admin_token, 'audience') and hasattr(admin_token, 'oidc_scope') and\
694
+ all_oidc_req_claims_present(admin_token.oidc_scope, admin_token.audience, req_scope, req_audience):
695
+ return token_dictionary(admin_token)
696
+ # if not found request a new one
697
+ new_admin_token = __get_admin_token_oidc(account, req_scope, req_audience, admin_issuer, session=session)
698
+ return new_admin_token
699
+
700
+ # Rucio server requests Rucio user to be represented by Rucio admin OIDC identity
701
+ if admin and not get_token_for_adminacc:
702
+ # we require any other account than admin to have valid OIDC token in the Rucio DB
703
+ if not account_tokens:
704
+ logging.debug("No valid token exists for account %s.", account)
705
+ return None
706
+ # we also require that these tokens at least one has the Rucio scopes and audiences
707
+ valid_subject_token_exists = False
708
+ for account_token in account_tokens:
709
+ if all_oidc_req_claims_present(account_token.oidc_scope, account_token.audience, EXPECTED_OIDC_SCOPE, EXPECTED_OIDC_AUDIENCE):
710
+ valid_subject_token_exists = True
711
+ if not valid_subject_token_exists:
712
+ logging.debug("No valid audience/scope exists for account %s token.", account)
713
+ return None
714
+ # openid scope is not supported for client_credentials auth flow - removing it if being asked for
715
+ if 'openid' in req_scope:
716
+ req_scope = req_scope.replace("openid", "").strip()
717
+
718
+ admin_account = None
719
+ for account_token in account_tokens:
720
+ # for each valid account token in the DB we need to check if a valid root token does not exist with the required
721
+ # scope and audience
722
+ admin_issuer = account_token.identity.split(", ")[1].split("=")[1]
723
+ # assuming the requesting account is using Rucio supported IdPs, we check if any token of this admin identity
724
+ # has already a token with the requested scopes and audiences
725
+ admin_acc_idt_tuple = admin_iss_acc_idt_dict[admin_issuer]
726
+ admin_account = admin_acc_idt_tuple[0]
727
+ admin_identity = admin_acc_idt_tuple[1]
728
+ query = select(
729
+ models.Token
730
+ ).where(
731
+ models.Token.identity == admin_identity,
732
+ models.Token.account == admin_account,
733
+ models.Token.expired_at > datetime.utcnow()
734
+ )
735
+ admin_account_tokens = session.execute(query).scalars().all()
736
+ for admin_token in admin_account_tokens:
737
+ if hasattr(admin_token, 'audience') and hasattr(admin_token, 'oidc_scope') and\
738
+ all_oidc_req_claims_present(admin_token.oidc_scope, admin_token.audience, req_scope, req_audience):
739
+ return token_dictionary(admin_token)
740
+ # if no admin token existing was found for the issuer of the valid user token
741
+ # we request a new one
742
+ new_admin_token = __get_admin_token_oidc(admin_account, req_scope, req_audience, admin_issuer, session=session)
743
+ return new_admin_token
744
+ # Rucio server requests exchange token for a Rucio user
745
+ if not admin and not get_token_for_adminacc:
746
+ # we require any other account than admin to have valid OIDC token in the Rucio DB
747
+ if not account_tokens:
748
+ logging.debug("No valid token exists for account %s.", account)
749
+ return None
750
+ # we also require that these tokens at least one has the Rucio scopes and audiences
751
+ valid_subject_token_exists = False
752
+ for account_token in account_tokens:
753
+ if all_oidc_req_claims_present(account_token.oidc_scope, account_token.audience, EXPECTED_OIDC_SCOPE, EXPECTED_OIDC_AUDIENCE):
754
+ valid_subject_token_exists = True
755
+ if not valid_subject_token_exists:
756
+ logging.debug("No valid audience/scope exists for account %s token.", account)
757
+ return None
758
+ subject_token = None
759
+ for token in account_tokens:
760
+ if hasattr(token, 'audience') and hasattr(token, 'oidc_scope'):
761
+ if all_oidc_req_claims_present(token.oidc_scope, token.audience, req_scope, req_audience):
762
+ return token_dictionary(token)
763
+ # from available tokens select preferentially the one which are being refreshed
764
+ if hasattr(token, 'oidc_scope') and ('offline_access' in str(token['oidc_scope'])):
765
+ subject_token = token
766
+ # if not proceed with token exchange
767
+ if not subject_token:
768
+ subject_token = random.choice(account_tokens)
769
+ exchanged_token = __exchange_token_oidc(subject_token,
770
+ scope=req_scope,
771
+ audience=req_audience,
772
+ identity=subject_token.identity,
773
+ refresh_lifetime=subject_token.refresh_lifetime,
774
+ account=account,
775
+ session=session)
776
+ return exchanged_token
777
+ logging.debug("No token could be returned for account operation for account %s.", account)
778
+ return None
779
+ except Exception:
780
+ # raise CannotAuthorize(traceback.format_exc(), type(account), account)
781
+ logging.debug(traceback.format_exc())
782
+ return None
783
+
784
+
785
+ @METRICS.time_it
786
+ @transactional_session
787
+ def __exchange_token_oidc(subject_token_object: models.Token, *, session: "Session", **kwargs):
788
+ """
789
+ Exchanged an access_token for a new one with different scope &/ audience
790
+ providing that the scope specified is registered with IdP for the Rucio OIDC Client
791
+ and the Rucio user has this scope linked to the subject token presented
792
+ for the token exchange.
793
+
794
+ :param subject_token_object: DB subject token to be exchanged
795
+ :param kwargs: 'scope', 'audience', 'grant_type', 'ip' and 'account' doing the exchange
796
+ :param session: The database session in use.
797
+
798
+ :returns: A dict with token and expires_at entries.
799
+ """
800
+ grant_type = kwargs.get('grant_type', EXCHANGE_GRANT_TYPE)
801
+ jwt_row_dict, extra_dict = {}, {}
802
+ jwt_row_dict['account'] = kwargs.get('account', '')
803
+ jwt_row_dict['authz_scope'] = kwargs.get('scope', '')
804
+ jwt_row_dict['audience'] = kwargs.get('audience', '')
805
+ jwt_row_dict['identity'] = kwargs.get('identity', '')
806
+ extra_dict['ip'] = kwargs.get('ip', None)
807
+
808
+ # if subject token has offline access scope but *no* refresh token in the DB
809
+ # (happens when user presents subject token acquired from other sources then Rucio CLI mechanism),
810
+ # add offline_access scope to the token exchange request !
811
+ if 'offline_access' in str(subject_token_object.oidc_scope) and not subject_token_object.refresh_token:
812
+ jwt_row_dict['authz_scope'] += ' offline_access'
813
+ if not grant_type:
814
+ grant_type = EXCHANGE_GRANT_TYPE
815
+ try:
816
+ oidc_dict = __get_init_oidc_client(token_object=subject_token_object, token_type="subject_token")
817
+ oidc_client = oidc_dict['client']
818
+ args = {"subject_token": subject_token_object.token,
819
+ "scope": jwt_row_dict['authz_scope'],
820
+ "audience": jwt_row_dict['audience'],
821
+ "grant_type": grant_type}
822
+ # exchange , access token for a new one
823
+ oidc_token_response = oidc_dict['client'].do_any(Message,
824
+ endpoint=oidc_client.provider_info["token_endpoint"],
825
+ state=oidc_dict['state'],
826
+ request_args=args,
827
+ authn_method="client_secret_basic")
828
+ oidc_tokens = oidc_token_response.json()
829
+ if 'error' in oidc_tokens:
830
+ raise CannotAuthorize(oidc_tokens['error'])
831
+ # get audience and scope information
832
+ if 'scope' in oidc_tokens and 'audience' in oidc_tokens:
833
+ jwt_row_dict['authz_scope'] = val_to_space_sep_str(oidc_tokens['scope'])
834
+ jwt_row_dict['audience'] = val_to_space_sep_str(oidc_tokens['audience'])
835
+ elif 'access_token' in oidc_tokens:
836
+ values = __get_keyvalues_from_claims(oidc_tokens['access_token'], ['scope', 'aud'])
837
+ jwt_row_dict['authz_scope'] = values['scope']
838
+ jwt_row_dict['audience'] = values['aud']
839
+ jwt_row_dict['lifetime'] = datetime.utcnow() + timedelta(seconds=oidc_tokens['expires_in'])
840
+ if 'refresh_token' in oidc_tokens:
841
+ extra_dict['refresh_token'] = oidc_tokens['refresh_token']
842
+ extra_dict['refresh'] = True
843
+ extra_dict['refresh_lifetime'] = kwargs.get('refresh_lifetime', REFRESH_LIFETIME_H)
844
+ if extra_dict['refresh_lifetime'] is None:
845
+ extra_dict['refresh_lifetime'] = REFRESH_LIFETIME_H
846
+ try:
847
+ values = __get_keyvalues_from_claims(oidc_tokens['refresh_token'], ['exp'])
848
+ extra_dict['refresh_expired_at'] = datetime.utcfromtimestamp(float(values['exp']))
849
+ except Exception:
850
+ # 4 day expiry period by default
851
+ extra_dict['refresh_expired_at'] = datetime.utcnow() + timedelta(hours=REFRESH_LIFETIME_H)
852
+
853
+ new_token = __save_validated_token(oidc_tokens['access_token'], jwt_row_dict, extra_dict=extra_dict, session=session)
854
+ METRICS.counter(name='IdP_authorization.access_token.saved').inc()
855
+ if 'refresh_token' in oidc_tokens:
856
+ METRICS.counter(name='IdP_authorization.refresh_token.saved').inc()
857
+ return new_token
858
+
859
+ except Exception:
860
+ # raise CannotAuthorize(traceback.format_exc())
861
+ logging.debug(traceback.format_exc())
862
+ return None
863
+
864
+
865
+ @transactional_session
866
+ def __change_refresh_state(token: str, refresh: bool = False, *, session: "Session"):
867
+ """
868
+ Changes token refresh state to True/False.
869
+
870
+ :param token: the access token for which the refresh value should be changed.
871
+ """
872
+ try:
873
+ query = update(
874
+ models.Token
875
+ ).where(
876
+ models.Token.token == token
877
+ )
878
+ if refresh:
879
+ # update refresh column for a token to True
880
+ query = query.values({
881
+ models.Token.refresh: True
882
+ })
883
+ else:
884
+ query = query.values({
885
+ models.Token.refresh: False,
886
+ models.Token.refresh_expired_at: datetime.utcnow()
887
+ })
888
+ session.execute(query)
889
+ except Exception as error:
890
+ raise RucioException(error.args) from error
891
+
892
+
893
+ @transactional_session
894
+ def refresh_cli_auth_token(token_string: str, account: str, *, session: "Session"):
895
+ """
896
+ Checks if there is active refresh token and if so returns
897
+ either active token with expiration timestamp or requests a new
898
+ refresh and returns new access token.
899
+ :param token_string: token string
900
+ :param account: Rucio account for which token refresh should be considered
901
+
902
+ :return: tuple of (access token, expiration epoch), None otherswise
903
+ """
904
+ # only validated tokens are in the DB, check presence of token_string
905
+ query = select(
906
+ models.Token
907
+ ).where(
908
+ models.Token.token == token_string,
909
+ models.Token.account == account,
910
+ models.Token.expired_at > datetime.utcnow()
911
+ ).with_for_update(
912
+ skip_locked=True
913
+ )
914
+ account_token = session.execute(query).scalar()
915
+
916
+ # if token does not exist in the DB, return None
917
+ if account_token is None:
918
+ logging.debug("No valid token exists for account %s.", account)
919
+ return None
920
+
921
+ # protection (!) no further action should be made
922
+ # for token_string without refresh_token in the DB !
923
+ if account_token.refresh_token is None:
924
+ logging.debug("No refresh token exists for account %s.", account)
925
+ return None
926
+
927
+ # if the token exists, check if it was refreshed already, if not, refresh it
928
+ if account_token.refresh:
929
+ # protection (!) returning the same token if the token_string
930
+ # is a result of a refresh which happened in the last 5 min
931
+ datetime_min_ago = datetime.utcnow() - timedelta(seconds=300)
932
+ if account_token.updated_at > datetime_min_ago:
933
+ epoch_exp = int(floor((account_token.expired_at - datetime(1970, 1, 1)).total_seconds()))
934
+ new_token_string = account_token.token
935
+ return new_token_string, epoch_exp
936
+
937
+ # asking for a refresh of this token
938
+ new_token = __refresh_token_oidc(account_token, session=session)
939
+ new_token_string = new_token['token']
940
+ epoch_exp = int(floor((new_token['expires_at'] - datetime(1970, 1, 1)).total_seconds()))
941
+ return new_token_string, epoch_exp
942
+
943
+ else:
944
+ # find account token with the same scope,
945
+ # audience and has a valid refresh token
946
+ query = select(
947
+ models.Token
948
+ ).where(
949
+ models.Token.refresh == true(),
950
+ models.Token.refresh_expired_at > datetime.utcnow(),
951
+ models.Token.account == account,
952
+ models.Token.expired_at > datetime.utcnow()
953
+ ).with_for_update(
954
+ skip_locked=True
955
+ )
956
+ new_token = session.execute(query).scalar()
957
+ if new_token is None:
958
+ return None
959
+
960
+ # if the new_token has same audience and scopes as the original
961
+ # account_token --> return this token and exp timestamp to the user
962
+ if all_oidc_req_claims_present(new_token.oidc_scope, new_token.audience,
963
+ account_token.oidc_scope, account_token.audience):
964
+ epoch_exp = int(floor((new_token.expired_at - datetime(1970, 1, 1)).total_seconds()))
965
+ new_token_string = new_token.token
966
+ return new_token_string, epoch_exp
967
+ # if scopes and audience are not the same, return None
968
+ logging.debug("No token could be returned for refresh operation for account %s.", account)
969
+ return None
970
+
971
+
972
+ @transactional_session
973
+ def refresh_jwt_tokens(total_workers: int, worker_number: int, refreshrate: int = 3600, limit: int = 1000, *, session: "Session"):
974
+ """
975
+ Refreshes tokens which expired or will expire before (now + refreshrate)
976
+ next run of this function and which have valid refresh token.
977
+
978
+ :param total_workers: Number of total workers.
979
+ :param worker_number: id of the executing worker.
980
+ :param limit: Maximum number of tokens to refresh per call.
981
+ :param session: Database session in use.
982
+
983
+ :return: numper of tokens refreshed
984
+ """
985
+ nrefreshed = 0
986
+ try:
987
+ # get tokens for refresh that expire in the next <refreshrate> seconds
988
+ expiration_future = datetime.utcnow() + timedelta(seconds=refreshrate)
989
+ query = select(
990
+ models.Token
991
+ ).where(
992
+ models.Token.refresh == true(),
993
+ models.Token.refresh_expired_at > datetime.utcnow(),
994
+ models.Token.expired_at < expiration_future
995
+ ).order_by(
996
+ models.Token.expired_at
997
+ )
998
+ query = filter_thread_work(session=session, query=query, total_threads=total_workers, thread_id=worker_number, hash_variable='token')
999
+ # limiting the number of tokens for refresh
1000
+ query = query.limit(limit)
1001
+ # Oracle does not support chaining order_by(), limit(), and
1002
+ # with_for_update(). Use a nested query to overcome this.
1003
+ if session.bind.dialect.name == 'oracle':
1004
+ query = select(
1005
+ models.Token
1006
+ ).where(
1007
+ models.Token.token.in_(query.with_only_columns(models.Token.token))
1008
+ ).with_for_update(
1009
+ skip_locked=True
1010
+ )
1011
+ else:
1012
+ query = query.with_for_update(skip_locked=True)
1013
+ filtered_tokens = session.execute(query).scalars().all()
1014
+
1015
+ # refreshing these tokens
1016
+ for token in filtered_tokens:
1017
+ new_token = __refresh_token_oidc(token, session=session)
1018
+ if new_token:
1019
+ nrefreshed += 1
1020
+
1021
+ except Exception as error:
1022
+ raise RucioException(error.args) from error
1023
+
1024
+ return nrefreshed
1025
+
1026
+
1027
+ @METRICS.time_it
1028
+ @transactional_session
1029
+ def __refresh_token_oidc(token_object: models.Token, *, session: "Session"):
1030
+ """
1031
+ Requests new access and refresh tokens from the Identity Provider.
1032
+ Assumption: The Identity Provider issues refresh tokens for one time use only and
1033
+ with a limited lifetime. The refresh tokens are invalidated no matter which of these
1034
+ situations happens first.
1035
+
1036
+ :param token_object: Rucio models.Token DB row object
1037
+
1038
+ :returns: A dict with token and expires_at entries if all went OK, None if
1039
+ refresh was not possible due to token invalidity or refresh lifetime
1040
+ constraints. Otherwise, throws an an Exception.
1041
+ """
1042
+ try:
1043
+ jwt_row_dict, extra_dict = {}, {}
1044
+ jwt_row_dict['account'] = token_object.account
1045
+ jwt_row_dict['identity'] = token_object.identity
1046
+ extra_dict['refresh_start'] = datetime.utcnow()
1047
+ # check if refresh token started in the past already
1048
+ if hasattr(token_object, 'refresh_start'):
1049
+ if token_object.refresh_start:
1050
+ extra_dict['refresh_start'] = token_object.refresh_start
1051
+ # check if refresh lifetime is set for the token
1052
+ extra_dict['refresh_lifetime'] = REFRESH_LIFETIME_H
1053
+ if token_object.refresh_lifetime:
1054
+ extra_dict['refresh_lifetime'] = token_object.refresh_lifetime
1055
+ # if the token has been refreshed for time exceeding
1056
+ # the refresh_lifetime, the attempt will be aborted and refresh stopped
1057
+ if datetime.utcnow() - extra_dict['refresh_start'] > timedelta(hours=extra_dict['refresh_lifetime']):
1058
+ __change_refresh_state(token_object.token, refresh=False, session=session)
1059
+ return None
1060
+ oidc_dict = __get_init_oidc_client(token_object=token_object, token_type="refresh_token")
1061
+ oidc_client = oidc_dict['client']
1062
+ # getting a new refreshed set of tokens
1063
+ state = oidc_dict['state']
1064
+ oidc_tokens = oidc_client.do_access_token_refresh(state=state, skew=LEEWAY_SECS)
1065
+ if 'error' in oidc_tokens:
1066
+ raise CannotAuthorize(oidc_tokens['error'])
1067
+ METRICS.counter(name='IdP_authorization.refresh_token.refreshed').inc()
1068
+ # get audience and scope information
1069
+ if 'scope' in oidc_tokens and 'audience' in oidc_tokens:
1070
+ jwt_row_dict['authz_scope'] = val_to_space_sep_str(oidc_tokens['scope'])
1071
+ jwt_row_dict['audience'] = val_to_space_sep_str(oidc_tokens['audience'])
1072
+ elif 'access_token' in oidc_tokens:
1073
+ values = __get_keyvalues_from_claims(oidc_tokens['access_token'], ['scope', 'aud'])
1074
+ jwt_row_dict['authz_scope'] = values['scope']
1075
+ jwt_row_dict['audience'] = values['aud']
1076
+ # save new access and refresh tokens in the DB
1077
+ if 'refresh_token' in oidc_tokens and 'access_token' in oidc_tokens:
1078
+ # aborting refresh of the original token
1079
+ # (keeping it in place until it expires)
1080
+ __change_refresh_state(token_object.token, refresh=False, session=session)
1081
+
1082
+ # get access token expiry timestamp
1083
+ jwt_row_dict['lifetime'] = datetime.utcnow() + timedelta(seconds=oidc_tokens['expires_in'])
1084
+ extra_dict['refresh'] = True
1085
+ extra_dict['refresh_token'] = oidc_tokens['refresh_token']
1086
+ try:
1087
+ values = __get_keyvalues_from_claims(oidc_tokens['refresh_token'], ['exp'])
1088
+ extra_dict['refresh_expired_at'] = datetime.utcfromtimestamp(float(values['exp']))
1089
+ except Exception:
1090
+ # 4 day expiry period by default
1091
+ extra_dict['refresh_expired_at'] = datetime.utcnow() + timedelta(hours=REFRESH_LIFETIME_H)
1092
+ new_token = __save_validated_token(oidc_tokens['access_token'], jwt_row_dict, extra_dict=extra_dict, session=session)
1093
+ METRICS.counter(name='IdP_authorization.access_token.saved').inc()
1094
+ METRICS.counter(name='IdP_authorization.refresh_token.saved').inc()
1095
+ else:
1096
+ raise CannotAuthorize("OIDC identity '%s' of the '%s' account is did not " % (token_object.identity, token_object.account)
1097
+ + "succeed requesting a new access and refresh tokens.") # NOQA: W503
1098
+ return new_token
1099
+
1100
+ except Exception as error:
1101
+ METRICS.counter(name='IdP_authorization.refresh_token.exception').inc()
1102
+ raise CannotAuthorize(traceback.format_exc()) from error
1103
+
1104
+
1105
+ @transactional_session
1106
+ def delete_expired_oauthrequests(total_workers: int, worker_number: int, limit: int = 1000, *, session: "Session"):
1107
+ """
1108
+ Delete expired OAuth request parameters.
1109
+
1110
+ :param total_workers: Number of total workers.
1111
+ :param worker_number: id of the executing worker.
1112
+ :param limit: Maximum number of oauth request session parameters to delete.
1113
+ :param session: Database session in use.
1114
+
1115
+ :returns: number of deleted rows
1116
+ """
1117
+
1118
+ try:
1119
+ # get expired OAuth request parameters
1120
+ query = select(
1121
+ models.OAuthRequest.state
1122
+ ).where(
1123
+ models.OAuthRequest.expired_at < datetime.utcnow()
1124
+ ).order_by(
1125
+ models.OAuthRequest.expired_at
1126
+ )
1127
+ query = filter_thread_work(session=session, query=query, total_threads=total_workers, thread_id=worker_number, hash_variable='state')
1128
+ # limiting the number of oauth requests deleted at once
1129
+ query = query.limit(limit)
1130
+ # Oracle does not support chaining order_by(), limit(), and
1131
+ # with_for_update(). Use a nested query to overcome this.
1132
+ if session.bind.dialect.name == 'oracle':
1133
+ query = select(
1134
+ models.OAuthRequest.state
1135
+ ).where(
1136
+ models.OAuthRequest.state.in_(query)
1137
+ ).with_for_update(
1138
+ skip_locked=True
1139
+ )
1140
+ else:
1141
+ query = query.with_for_update(skip_locked=True)
1142
+
1143
+ ndeleted = 0
1144
+ for states in session.execute(query).scalars().partitions(10):
1145
+ query = delete(
1146
+ models.OAuthRequest
1147
+ ).where(
1148
+ models.OAuthRequest.state.in_(states)
1149
+ )
1150
+ ndeleted += session.execute(query).rowcount
1151
+ return ndeleted
1152
+ except Exception as error:
1153
+ raise RucioException(error.args) from error
1154
+
1155
+
1156
+ def __get_keyvalues_from_claims(token: str, keys=None):
1157
+ """
1158
+ Extracting claims from token, e.g. scope and audience.
1159
+ :param token: the JWT to be unpacked
1160
+ :param key: list of key names to extract from the token claims
1161
+
1162
+ :returns: The list of unicode values under the key, throws an exception otherwise.
1163
+ """
1164
+ resdict = {}
1165
+ try:
1166
+ claims = JWT().unpack(token).payload()
1167
+ if not keys:
1168
+ keys = claims.keys()
1169
+ for key in keys:
1170
+ value = ''
1171
+ if key in claims:
1172
+ value = val_to_space_sep_str(claims[key])
1173
+ resdict[key] = value
1174
+ return resdict
1175
+ except Exception as error:
1176
+ raise CannotAuthenticate(traceback.format_exc()) from error
1177
+
1178
+
1179
+ @read_session
1180
+ def __get_rucio_jwt_dict(jwt: str, account=None, *, session: "Session"):
1181
+ """
1182
+ Get a Rucio token dictionary from token claims.
1183
+ Check token expiration and find default Rucio
1184
+ account for token identity.
1185
+ :param jwt: JSON Web Token to be inspected
1186
+ :param session: DB session in use
1187
+
1188
+ :returns: Rucio token dictionary, None otherwise
1189
+ """
1190
+ try:
1191
+ # getting token paylod
1192
+ token_payload = __get_keyvalues_from_claims(jwt)
1193
+ identity_string = oidc_identity_string(token_payload['sub'], token_payload['iss'])
1194
+ expiry_date = datetime.utcfromtimestamp(float(token_payload['exp']))
1195
+ if expiry_date < datetime.utcnow(): # check if expired
1196
+ logging.debug("Token has already expired since: %s", str(expiry_date))
1197
+ return None
1198
+ scope = None
1199
+ audience = None
1200
+ if 'scope' in token_payload:
1201
+ scope = val_to_space_sep_str(token_payload['scope'])
1202
+ if 'aud' in token_payload:
1203
+ audience = val_to_space_sep_str(token_payload['aud'])
1204
+ if not account:
1205
+ # this assumes token has been previously looked up in DB
1206
+ # before to be sure that we do not have the right account already in the DB !
1207
+ account = get_default_account(identity_string, IdentityType.OIDC, True, session=session)
1208
+ else:
1209
+ if not exist_identity_account(identity_string, IdentityType.OIDC, account, session=session):
1210
+ logging.debug("No OIDC identity exists for account: %s", str(account))
1211
+ return None
1212
+ value = {'account': account,
1213
+ 'identity': identity_string,
1214
+ 'lifetime': expiry_date,
1215
+ 'audience': audience,
1216
+ 'authz_scope': scope}
1217
+ return value
1218
+ except Exception:
1219
+ logging.debug(traceback.format_exc())
1220
+ return None
1221
+
1222
+
1223
+ @transactional_session
1224
+ def __save_validated_token(token, valid_dict, extra_dict=None, *, session: "Session"):
1225
+ """
1226
+ Save JWT token to the Rucio DB.
1227
+
1228
+ :param token: Authentication token as a variable-length string.
1229
+ :param valid_dict: Validation Rucio dictionary as the output
1230
+ of the __get_rucio_jwt_dict function
1231
+ :raises RucioException: on any error
1232
+ :returns: A dict with token and expires_at entries.
1233
+ """
1234
+ try:
1235
+ if not extra_dict:
1236
+ extra_dict = {}
1237
+ new_token = models.Token(account=valid_dict.get('account', None),
1238
+ token=token,
1239
+ oidc_scope=valid_dict.get('authz_scope', None),
1240
+ expired_at=valid_dict.get('lifetime', None),
1241
+ audience=valid_dict.get('audience', None),
1242
+ identity=valid_dict.get('identity', None),
1243
+ refresh=extra_dict.get('refresh', False),
1244
+ refresh_token=extra_dict.get('refresh_token', None),
1245
+ refresh_expired_at=extra_dict.get('refresh_expired_at', None),
1246
+ refresh_lifetime=extra_dict.get('refresh_lifetime', None),
1247
+ refresh_start=extra_dict.get('refresh_start', None),
1248
+ ip=extra_dict.get('ip', None))
1249
+ new_token.save(session=session)
1250
+
1251
+ return token_dictionary(new_token)
1252
+
1253
+ except Exception as error:
1254
+ raise RucioException(error.args) from error
1255
+
1256
+
1257
+ @transactional_session
1258
+ def validate_jwt(json_web_token: str, *, session: "Session") -> dict[str, Any]:
1259
+ """
1260
+ Verifies signature and validity of a JSON Web Token.
1261
+ Gets the issuer public keys from the oidc_client
1262
+ and verifies the validity of the token.
1263
+ Used only for external tokens, not known to Rucio DB.
1264
+
1265
+ :param json_web_token: the JWT string to verify
1266
+
1267
+ :returns: dictionary { account: <account name>,
1268
+ identity: <identity>,
1269
+ lifetime: <token lifetime>,
1270
+ audience: <audience>,
1271
+ authz_scope: <authz_scope> }
1272
+ if successful.
1273
+ :raises: CannotAuthenticate if unsuccessful
1274
+ """
1275
+
1276
+ if not OIDC_CLIENTS:
1277
+ # retry once loading OIDC clients
1278
+ __initialize_oidc_clients()
1279
+ if not OIDC_CLIENTS:
1280
+ raise CannotAuthenticate(traceback.format_exc())
1281
+
1282
+ try:
1283
+
1284
+ # getting issuer from the token payload
1285
+ token_dict: Optional[dict[str, Any]] = __get_rucio_jwt_dict(json_web_token, session=session)
1286
+ if not token_dict:
1287
+ raise CannotAuthenticate(traceback.format_exc())
1288
+ issuer = token_dict['identity'].split(", ")[1].split("=")[1]
1289
+ oidc_client = OIDC_CLIENTS[issuer]
1290
+ issuer_keys = oidc_client.keyjar.get_issuer_keys(issuer)
1291
+ JWS().verify_compact(json_web_token, issuer_keys)
1292
+ # if there is no audience and scope information,
1293
+ # try to get it from IdP introspection endpoint
1294
+ # TO-BE-REMOVED - once all IdPs support scope and audience in token claims !!!
1295
+ if not token_dict['authz_scope'] or not token_dict['audience']:
1296
+ clprocess = subprocess.Popen(['curl', '-s', '-L', '-u', '%s:%s'
1297
+ % (oidc_client.client_id, oidc_client.client_secret),
1298
+ '-d', 'token=%s' % (json_web_token),
1299
+ oidc_client.introspection_endpoint],
1300
+ shell=False, stdout=subprocess.PIPE)
1301
+ inspect_claims = json.loads(clprocess.communicate()[0])
1302
+ try:
1303
+ token_dict['audience'] = inspect_claims['aud']
1304
+ token_dict['authz_scope'] = inspect_claims['scope']
1305
+ except:
1306
+ pass
1307
+ METRICS.counter(name='JSONWebToken.valid').inc()
1308
+ # if token is valid and coming from known issuer --> check aud and scope and save it if unknown
1309
+ if token_dict['authz_scope'] and token_dict['audience']:
1310
+ if all_oidc_req_claims_present(token_dict['authz_scope'], token_dict['audience'], EXPECTED_OIDC_SCOPE, EXPECTED_OIDC_AUDIENCE):
1311
+ # save the token in Rucio DB giving the permission to use it for Rucio operations
1312
+ __save_validated_token(json_web_token, token_dict, session=session)
1313
+ else:
1314
+ logging.debug("Token audience [%s] or scope [%s] verification failed.", token_dict['audience'], token_dict['authz_scope'])
1315
+ raise CannotAuthenticate(traceback.format_exc())
1316
+ else:
1317
+ logging.debug("Token audience or scope not present.")
1318
+ raise CannotAuthenticate(traceback.format_exc())
1319
+ METRICS.counter(name='JSONWebToken.saved').inc()
1320
+ return token_dict
1321
+ except Exception:
1322
+ METRICS.counter(name='JSONWebToken.invalid').inc()
1323
+ logging.debug(traceback.format_exc())
1324
+ raise CannotAuthenticate(traceback.format_exc())
1325
+
1326
+
1327
+ def oidc_identity_string(sub: str, iss: str):
1328
+ """
1329
+ Transform IdP sub claim and issuers url into users identity string.
1330
+ :param sub: users SUB claim from the Identity Provider
1331
+ :param iss: issuer (IdP) https url
1332
+
1333
+ :returns: OIDC identity string "SUB=<usersid>, ISS=https://iam-test.ch/"
1334
+ """
1335
+ return 'SUB=' + str(sub) + ', ISS=' + str(iss)
1336
+
1337
+
1338
+ def token_dictionary(token: models.Token):
1339
+ return {'token': token.token, 'expires_at': token.expired_at}