Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Create migration to sync settings to SAML provider

Oliver Günther4ce3c933b694
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index 36b35cc7943..950a140f5d4 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -35,6 +35,7 @@ class ServiceResult
attr_accessor :success,
:result,
:errors,
+ :message,
:dependent_results
attr_writer :state
diff --git a/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
index 29f9926c6c9..4c68fdad45e 100644
--- a/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
+++ b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
@@ -31,16 +31,12 @@ module EnvData
def seed_data!
provider_configuration.each do |name, options|
print_status " ↳ Creating or Updating SAML provider #{name}" do
- provider = ::Saml::Provider.find_by(slug: name)
- params = ::Saml::ConfigurationMapper.new(options).call!
- params["options"]["seeded_from_env"] = true
+ call = ::Saml::SyncService.new(name, options).call
- if provider
- print_status " - Updating existing SAML provider '#{name}' from ENV"
- update(name, provider, params)
+ if call.success
+ print_status " - #{call.message}"
else
- print_status " - Creating new SAML provider '#{name}' from ENV"
- create(name, params)
+ raise call.message
end
end
end
@@ -81,22 +77,6 @@ module EnvData
end
end
end
-
- def create(name, params)
- ::Saml::Providers::CreateService
- .new(user: User.system)
- .call(params)
- .on_success { print_status " - Successfully saved SAML provider #{name}." }
- .on_failure { |call| raise "Failed to create SAML provider: #{call.message}" }
- end
-
- def update(name, provider, params)
- ::Saml::Providers::UpdateService
- .new(model: provider, user: User.system)
- .call(params)
- .on_success { print_status " - Successfully updated SAML provider #{name}." }
- .on_failure { |call| raise "Failed to update SAML provider: #{call.message}" }
- end
end
end
end
diff --git a/modules/auth_saml/app/services/saml/sync_service.rb b/modules/auth_saml/app/services/saml/sync_service.rb
new file mode 100644
index 00000000000..ed7217fb8d6
--- /dev/null
+++ b/modules/auth_saml/app/services/saml/sync_service.rb
@@ -0,0 +1,69 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Saml
+ ##
+ # Synchronize a configuration from ENV or legacy settings to a SAML provider record
+ class SyncService
+ attr_reader :name, :configuration
+
+ def initialize(name, configuration)
+ @name = name
+ @configuration = configuration
+ end
+
+ def call
+ params = ::Saml::ConfigurationMapper.new(configuration).call!
+ provider = ::Saml::Provider.find_by(slug: name)
+
+ if provider
+ update(name, provider, params)
+ else
+ create(name, params)
+ end
+ end
+
+ private
+
+ def create(name, params)
+ ::Saml::Providers::CreateService
+ .new(user: User.system)
+ .call(params)
+ .on_success { |call| call.message = "Successfully saved SAML provider #{name}." }
+ .on_failure { |call| call.message = "Failed to create SAML provider: #{call.message}" }
+ end
+
+ def update(name, provider, params)
+ ::Saml::Providers::UpdateService
+ .new(model: provider, user: User.system)
+ .call(params)
+ .on_success { |call| call.message = "Successfully updated SAML provider #{name}." }
+ .on_failure { |call| call.message = "Failed to update SAML provider: #{call.message}" }
+ end
+ end
+end
diff --git a/modules/auth_saml/db/migrate/20240821121856_migrate_saml_settings_to_providers.rb b/modules/auth_saml/db/migrate/20240821121856_migrate_saml_settings_to_providers.rb
new file mode 100644
index 00000000000..ae82aba4de2
--- /dev/null
+++ b/modules/auth_saml/db/migrate/20240821121856_migrate_saml_settings_to_providers.rb
@@ -0,0 +1,43 @@
+class MigrateSamlSettingsToProviders < ActiveRecord::Migration[7.1]
+ def up
+ providers = Hash(Setting.plugin_openproject_auth_saml).with_indifferent_access[:providers]
+ return if providers.blank?
+
+ providers.each do |name, options|
+ migrate_provider!(name, options)
+ end
+ end
+
+ def down
+ # This migration does not yet remove Setting.plugin_openproject_auth_saml
+ # so it can be retried.
+ end
+
+ private
+
+ def migrate_provider!(name, options)
+ puts "Trying to migrate SAML provider #{name} from previous settings format..."
+ call = ::Saml::SyncService.new(name, options).call
+
+ if call.success
+ puts <<~SUCCESS
+ Successfully migrated SAML provider #{name} from previous settings format.
+ You can now manage this provider in the new administrative UI within OpenProject under
+ the "Administration -> Authentication -> SAML providers" section.
+ SUCCESS
+ else
+ raise <<~ERROR
+ Failed to create or update SAML provider #{name} from previous settings format.
+ The error message was: #{call.message}
+
+ Please check the logs for more information and open a bug report in our community:
+ https://www.openproject.org/docs/development/report-a-bug/
+
+ If you would like to skip migrating the SAML setting and discard them instead, you can use our documentation
+ to unset any previous SAML settings:
+
+ https://www.openproject.org/docs/system-admin-guide/authentication/saml/
+ ERROR
+ end
+ end
+end
diff --git a/modules/auth_saml/lib/open_project/auth_saml/engine.rb b/modules/auth_saml/lib/open_project/auth_saml/engine.rb
index 1c0ed182539..5477eb21da9 100644
--- a/modules/auth_saml/lib/open_project/auth_saml/engine.rb
+++ b/modules/auth_saml/lib/open_project/auth_saml/engine.rb
@@ -2,26 +2,15 @@ require "omniauth-saml"
module OpenProject
module AuthSaml
def self.configuration
- RequestStore.fetch(:openproject_omniauth_saml_provider) do
- settings_from_db
- .deep_merge(settings_from_providers)
- end
- end
+ providers = Saml::Provider.where(available: true)
- def self.settings_from_providers
- Saml::Provider
- .where(available: true)
- .each_with_object({}) do |provider, hash|
- hash[provider.slug] = provider.to_h
+ OpenProject::Cache.fetch(providers.cache_key) do
+ providers.each_with_object({}) do |provider, hash|
+ hash[provider.slug] = provider.to_h
+ end
end
end
- def self.settings_from_db
- value = Hash(Setting.plugin_openproject_auth_saml).with_indifferent_access[:providers]
-
- value.is_a?(Hash) ? value : {}
- end
-
class Engine < ::Rails::Engine
engine_name :openproject_auth_saml