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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
seed providers from env
Oliver Günthercecdd6ce2de1
diff --git a/modules/auth_saml/app/components/saml/providers/sections/show_component.html.erb b/modules/auth_saml/app/components/saml/providers/sections/show_component.html.erbindex 40ad76cc1e1..e3d6adb05cc 100644--- a/modules/auth_saml/app/components/saml/providers/sections/show_component.html.erb+++ b/modules/auth_saml/app/components/saml/providers/sections/show_component.html.erb@@ -13,7 +13,7 @@endend- if provider.persisted? && @view_mode == :show+ if show_edit?grid.with_area(:action) doflex_layout(justify_content: :flex_end) do |icons_container|icons_container.with_column dodiff --git a/modules/auth_saml/app/components/saml/providers/sections/show_component.rb b/modules/auth_saml/app/components/saml/providers/sections/show_component.rbindex a630a78d535..434ebd19820 100644--- a/modules/auth_saml/app/components/saml/providers/sections/show_component.rb+++ b/modules/auth_saml/app/components/saml/providers/sections/show_component.rb@@ -41,5 +41,11 @@ module Saml::Providers::Sections@label = label@label_scheme = label_schemeend++ def show_edit?+ return false if provider.seeded_from_env?++ provider.persisted? && @view_mode == :show+ endendenddiff --git a/modules/auth_saml/app/components/saml/providers/view_component.html.erb b/modules/auth_saml/app/components/saml/providers/view_component.html.erbindex 69078ba90f0..0c2ac92d501 100644--- a/modules/auth_saml/app/components/saml/providers/view_component.html.erb+++ b/modules/auth_saml/app/components/saml/providers/view_component.html.erb@@ -1,3 +1,11 @@+<% if provider.seeded_from_env? %>+ <%=+ render(Primer::Alpha::Banner.new(mb: 2, scheme: :default, icon: :bell, spacious: true)) do+ I18n.t("saml.providers.seeded_from_env")+ end+ %>+<% end %>+<%=render(border_box_container) do |component|component.with_header(color: :muted) dodiff --git a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb b/modules/auth_saml/app/contracts/saml/providers/base_contract.rbindex 2ddd4762aa7..f0169df9e8f 100644--- a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb+++ b/modules/auth_saml/app/contracts/saml/providers/base_contract.rb@@ -35,6 +35,7 @@ module Samlendattribute :display_name+ attribute :slugattribute :optionsattribute :metadata_urlvalidates :metadata_url,diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rbindex 7189d988b02..c3c9f6d87a4 100644--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb@@ -6,6 +6,7 @@ module Samlbefore_action :require_adminbefore_action :check_eebefore_action :find_provider, only: %i[show edit import_metadata update destroy]+ before_action :check_provider_writable, only: %i[update import_metadata]def index@providers = Saml::Provider.order(display_name: :asc)@@ -163,5 +164,12 @@ module Samlrescue ActiveRecord::RecordNotFoundrender_404end++ def check_provider_writable+ if @provider.seeded_from_env?+ flash[:error] = I18n.t(:label_seeded_from_env_warning)+ redirect_to saml_provider_path(@provider)+ end+ endendenddiff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rbindex e3840842ce9..e2544a5028e 100644--- a/modules/auth_saml/app/models/saml/provider.rb+++ b/modules/auth_saml/app/models/saml/provider.rb@@ -4,6 +4,7 @@ module Samlstore_attribute :options, :icon, :stringstore_attribute :options, :sp_entity_id, :string+ store_attribute :options, :seeded_from_env, :booleanstore_attribute :options, :name_identifier_format, :stringstore_attribute :options, :metadata_url, :stringstore_attribute :options, :metadata_xml, :string@@ -46,6 +47,10 @@ module Samldef self.slug_fragment = "saml"+ def seeded_from_env?+ seeded_from_env == true+ end+def has_metadata?metadata_xml.present? || metadata_url.present?enddiff --git a/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rbnew file mode 100644index 00000000000..e64dbbef110--- /dev/null+++ b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb@@ -0,0 +1,125 @@+#-- copyright++# OpenProject is an open source project management software.+# Copyright (C) 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 EnvData+ module Saml+ class ProviderSeeder < Seeder+ def seed_data!+ Setting.seed_saml_provider.each do |name, options|+ print_status " ↳ Creating or Updating SAML provider #{name}" do+ provider = ::Saml::Provider.find_by(slug: "saml-env-#{name}")+ options = mapped_options(options)+ params = {+ slug: name,+ display_name: options.delete("display_name") || "SAML",+ available: true,+ options:+ }++ if provider+ print_status " - Updating existing SAML provider '#{name}' from ENV"+ update(name, provider, params)+ else+ print_status " - Creating new SAML provider '#{name}' from ENV"+ create(name, params)+ end+ end+ end+ end++ def applicable?+ Setting.seed_saml_provider.present?+ end++ private++ 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++ def mapped_options(options)+ options["seeded_from_env"] = true+ options["idp_sso_service_url"] ||= options.delete("idp_sso_target_url")+ options["idp_slo_service_url"] ||= options.delete("idp_slo_target_url")+ options["sp_entity_id"] ||= options.delete("issuer")++ build_idp_cert(options)+ extract_security_options(options)+ extract_mapping(options)++ options.compact+ end++ def extract_mapping(options)+ nil unless options["attribute_statements"]++ options["mapping_login"] = extract_mapping_attribute(options, "login")+ options["mapping_mail"] = extract_mapping_attribute(options, "email")+ options["mapping_firstname"] = extract_mapping_attribute(options, "first_name")+ options["mapping_lastname"] = extract_mapping_attribute(options, "last_name")+ options["mapping_uid"] = extract_mapping_attribute(options, "uid")+ end++ def extract_mapping_attribute(options, key)+ value = options["attribute_statements"][key]++ if value.present?+ Array(value).join("\n")+ end+ end++ def build_idp_cert(options)+ if options["idp_cert"]+ options["idp_cert"] = OneLogin::RubySaml::Utils.format_cert(options["idp_cert"])+ elsif options["idp_cert_multi"]+ options["idp_cert"] = options["idp_cert_multi"]["signing"]+ .map { |cert| OneLogin::RubySaml::Utils.format_cert(cert) }+ .join("\n")+ end+ end++ def extract_security_options(options)+ return unless options["security"]++ options.merge! options["security"].slice("authn_requests_signed", "want_assertions_signed",+ "want_assertions_encrypted", "digest_method", "signature_method")+ end+ end+ end+enddiff --git a/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb b/modules/auth_saml/app/services/saml/providers/set_attributes_service.rbindex 0da8fa8b962..e01adf1291d 100644--- a/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb+++ b/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb@@ -48,6 +48,8 @@ module Samldef update_options(options)update_idp_cert(options.delete(:idp_cert)) if options.key?(:idp_cert)+ update_certificate(options.delete(:certificate)) if options.key?(:certificate)+ update_private_key(options.delete(:private_key)) if options.key?(:private_key)options.select { |key, _| Saml::Provider.stored_attributes[:options].include?(key.to_s) }@@ -94,6 +96,14 @@ module Samlendend+ def update_certificate(cert)+ model.certificate = OneLogin::RubySaml::Utils.format_cert(cert)+ end++ def update_private_key(private_key)+ model.private_key = OneLogin::RubySaml::Utils.format_private_key(private_key)+ end+### Clean up provided mapping, reducing whitespacedef update_mapping(params)diff --git a/modules/auth_saml/app/views/saml/providers/show.html.erb b/modules/auth_saml/app/views/saml/providers/show.html.erbindex 42d188b0f6b..64d09eaad67 100644--- a/modules/auth_saml/app/views/saml/providers/show.html.erb+++ b/modules/auth_saml/app/views/saml/providers/show.html.erb@@ -3,23 +3,25 @@<%= render(Primer::OpenProject::PageHeader.new) do |header| %><% header.with_title { @provider.display_name } %>- <% header.with_breadcrumbs([{ href: admin_index_path, text: t("label_administration")},+ <% header.with_breadcrumbs([{ href: admin_index_path, text: t("label_administration") },{ href: saml_providers_path, text: t('saml.providers.plural') },@provider.display_name]) %><%- header.with_action_button(tag: :a,- scheme: :danger,- mobile_icon: :trash,- mobile_label: t(:button_delete),- size: :medium,- href: saml_provider_path(@provider),- aria: { label: I18n.t(:button_delete) },- data: {- confirm: t(:text_are_you_sure),- method: :delete,- },- title: I18n.t(:button_delete)) do |button|+ header.with_action_button(+ tag: :a,+ scheme: :danger,+ mobile_icon: :trash,+ mobile_label: t(:button_delete),+ size: :medium,+ href: saml_provider_path(@provider),+ aria: { label: I18n.t(:button_delete) },+ data: {+ confirm: t(:text_are_you_sure),+ method: :delete,+ },+ title: I18n.t(:button_delete)+ ) do |button|button.with_leading_visual_icon(icon: :trash)t(:button_delete)enddiff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.ymlindex 55a8eaa7153..6665fed787f 100644--- a/modules/auth_saml/config/locales/en.yml+++ b/modules/auth_saml/config/locales/en.yml@@ -81,6 +81,7 @@ en:encryption_form: "You may optionally want to encrypt the assertion response, or have requests from OpenProject signed."mapping: "Manually adjust the mapping between the SAML response and user attributes in OpenProject."requested_attributes: "Define the set of attributes to be requested in the SAML request sent to your identity provider."+ seeded_from_env: "This provider was seeded from the environment configuration. It cannot be edited."settings:metadata_none: "I don't have metadata"metadata_url: "Metadata URL"diff --git a/modules/auth_saml/lib/open_project/auth_saml/engine.rb b/modules/auth_saml/lib/open_project/auth_saml/engine.rbindex 92e445ce864..766279aea31 100644--- a/modules/auth_saml/lib/open_project/auth_saml/engine.rb+++ b/modules/auth_saml/lib/open_project/auth_saml/engine.rb@@ -21,7 +21,9 @@ module OpenProjectenddef self.settings_from_providers- Saml::Provider.all.each_with_object({}) do |provider, hash|+ Saml::Provider+ .where(available: true)+ .each_with_object({}) do |provider, hash|hash[provider.slug] = provider.to_hendend@@ -96,10 +98,12 @@ module OpenProjectendinitializer "auth_saml.configuration" do- ::Settings::Definition.add "saml",+ ::Settings::Definition.add :seed_saml_provider,+ description: "Provide a SAML provider and sync its settings through ENV",+ env_alias: "OPENPROJECT_SAML",+ writable: false,default: nil,- format: :hash,- writable: false+ format: :hashendendend