From 7ae2c3bc4b2d5485838432be973ac0cee7584b7f Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Fri, 1 Nov 2024 16:32:31 -0600 Subject: [PATCH 1/3] Add rake task for openid_connect / CILogon cleanup Intended to resolve this issue: https://github.com/portagenetwork/roadmap/issues/912 The following PR was used as a reference: https://github.com/portagenetwork/roadmap/pull/915 Co-Authored-By: Omar Rodriguez Arenas <1537909+lagoan@users.noreply.github.com> --- lib/tasks/dmp_assistant_upgrade.rake | 43 +++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/tasks/dmp_assistant_upgrade.rake b/lib/tasks/dmp_assistant_upgrade.rake index 150364010..0231f1e4b 100644 --- a/lib/tasks/dmp_assistant_upgrade.rake +++ b/lib/tasks/dmp_assistant_upgrade.rake @@ -12,7 +12,12 @@ namespace :dmp_assistant_upgrade do p '------------------------------------------------------------------------' handle_email_confirmations_for_existing_users p 'Task completed: Handle email confirmations for existing users' - p 'All tasks completed successfully' + p 'Beginning task: Update `openid_connect` IdentifierScheme and its identifers' + p '------------------------------------------------------------------------' + if openid_scheme_and_its_identifiers_updated? + p 'Task completed: Update `openid_connect` IdentifierScheme and its identifers' + p 'All tasks completed successfully' + end end # rubocop:enable Naming/VariableNumber @@ -53,4 +58,40 @@ namespace :dmp_assistant_upgrade do def super_admin_perm_ids [Perm.add_orgs.id, Perm.grant_api.id, Perm.change_affiliation.id] end + + # Returns a boolean indicating whether the task was executed successfully + def openid_scheme_and_its_identifiers_updated? + identifier_scheme = IdentifierScheme.includes(:identifiers) + .find_by!(name: 'openid_connect') + old_prefix = identifier_scheme.identifier_prefix + p "Updating identifier_prefix to '' for openid_connect IdentifierScheme" + p '------------------------------------------------------------------------' + begin + ensure_correct_identifier_prefix(old_prefix) + rescue StandardError => e + p "Error updating IdentifierScheme: #{e.message}" + return false + end + + # Update identifier_prefix to '' for openid_connect + identifier_scheme.update!(identifier_prefix: '') + p "identifier_prefix updated from #{old_prefix} to '' for #{identifier_scheme.name} IdentifierScheme" + p "Updating prefixed value for identifiers with multiple occurences of 'cilogon'" + p '------------------------------------------------------------------------' + # Find all identifiers having multiple occurences of 'cilogon' in their value + identifiers = identifier_scheme.identifiers.where('value ~* ?', "#{old_prefix}.+cilogon.+") + count = identifiers.count + p "(Found #{count} such identifiers)" + # identifier_scheme.identifier_prefix was used to prefix identifier.value + # Update the affected identifier values from `old_prefix` to '' + identifiers.each { |identifier| identifier.update!(value: identifier.value.delete_prefix(old_prefix)) } + p "Updated prefixed value from #{old_prefix} to '' for #{count} identifiers" + true + end + + def ensure_correct_identifier_prefix(prefix) + expected_prefix = 'http://cilogon.org/serverE/users/' + error_msg = "Unexpected identifier_prefix! Expected #{expected_prefix} but got #{prefix}." + raise error_msg unless prefix == expected_prefix + end end From 7f158a7acac49f8c5ec2e10aacf30ad82082154d Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Mon, 4 Nov 2024 14:27:08 -0700 Subject: [PATCH 2/3] Make Rubocop happy --- lib/tasks/dmp_assistant_upgrade.rake | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/tasks/dmp_assistant_upgrade.rake b/lib/tasks/dmp_assistant_upgrade.rake index 0231f1e4b..2447754cb 100644 --- a/lib/tasks/dmp_assistant_upgrade.rake +++ b/lib/tasks/dmp_assistant_upgrade.rake @@ -78,14 +78,7 @@ namespace :dmp_assistant_upgrade do p "identifier_prefix updated from #{old_prefix} to '' for #{identifier_scheme.name} IdentifierScheme" p "Updating prefixed value for identifiers with multiple occurences of 'cilogon'" p '------------------------------------------------------------------------' - # Find all identifiers having multiple occurences of 'cilogon' in their value - identifiers = identifier_scheme.identifiers.where('value ~* ?', "#{old_prefix}.+cilogon.+") - count = identifiers.count - p "(Found #{count} such identifiers)" - # identifier_scheme.identifier_prefix was used to prefix identifier.value - # Update the affected identifier values from `old_prefix` to '' - identifiers.each { |identifier| identifier.update!(value: identifier.value.delete_prefix(old_prefix)) } - p "Updated prefixed value from #{old_prefix} to '' for #{count} identifiers" + find_and_update_identifiers(identifier_scheme, old_prefix) true end @@ -94,4 +87,16 @@ namespace :dmp_assistant_upgrade do error_msg = "Unexpected identifier_prefix! Expected #{expected_prefix} but got #{prefix}." raise error_msg unless prefix == expected_prefix end + + def find_and_update_identifiers(identifier_scheme, old_prefix) + # `identifier_scheme.identifiers` == openid_connect-related identifiers + # Get identifiers where `.value` has both the old_prefix and multiple occurences of 'cilogon' + identifiers = identifier_scheme.identifiers.where('value ~* ?', "#{old_prefix}.+cilogon.+") + count = identifiers.count + p "(Found #{count} such identifiers)" + # identifier_scheme.identifier_prefix was initially used to prefix identifier.value + # Update by removing old_prefix from identifier.value + identifiers.each { |identifier| identifier.update!(value: identifier.value.delete_prefix(old_prefix)) } + p "Updated prefixed value from #{old_prefix} to '' for #{count} identifiers" + end end From 0a2b126c4a9901570129d6685c5897d3a4f14319 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Wed, 6 Nov 2024 11:38:40 -0700 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c480b67cd..be4139559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + + - Add rake task for openid_connect / CILogon cleanup [#944](https://github.com/portagenetwork/roadmap/pull/944) + ### Changed - Email Confirmation Changes [#923](https://github.com/portagenetwork/roadmap/pull/923)