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

Remove the old Connection Validators (#18681)

Marcello Rochacefe0c43a8be
diff --git a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb b/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb
deleted file mode 100644
index 25f3a0941b1..00000000000
--- a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb
+++ /dev/null
@@ -1,307 +0,0 @@
-# frozen_string_literal:true
-
-#-- 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 Storages
- module Peripherals
- class NextcloudConnectionValidator
- include TaggedLogging
- include Dry::Monads[:maybe]
-
- using ServiceResultRefinements
-
- def initialize(storage:)
- @storage = storage
- @user = User.current
- end
-
- def validate
- with_tagged_logger do
- maybe_is_not_configured
- .or { has_base_configuration_error? }
- .or { has_ampf_configuration_error? }
- .value_or(ConnectionValidation.new(type: :healthy,
- error_code: :none,
- timestamp: Time.current,
- description: nil))
- end
- end
-
- private
-
- def sso_misconfigured
- return None() unless @storage.authenticate_via_idp?
-
- non_provisioned_user
- .or { non_oidc_provisioned_user }
- .or { token_usability }
- end
-
- def non_provisioned_user
- return None() if @user.identity_url.present?
-
- Some(ConnectionValidation.new(type: :warning,
- error_code: :oidc_non_provisioned_user,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.oidc_non_provisioned_user")))
- end
-
- def non_oidc_provisioned_user
- return None() if @user.authentication_provider.is_a?(OpenIDConnect::Provider)
-
- Some(ConnectionValidation.new(type: :warning,
- error_code: :oidc_non_oidc_user,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.oidc_non_oidc_user")))
- end
-
- def token_usability
- service = OpenIDConnect::UserTokens::FetchService.new(user: @user)
-
- result = service.access_token_for(audience: @storage.audience)
- return None() if result.success?
-
- error_code = case result.failure
- in { code: /token_exchange/ | :unable_to_exchange_token }
- :oidc_cant_exchange_token
- in { code: /token_refresh/ }
- :oidc_cant_refresh_token
- in { code: :no_token_for_audience }
- :oidc_cant_acquire_token
- else
- :unknown_error
- end
-
- Some(ConnectionValidation.new(type: :error,
- error_code:,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.#{error_code}")))
- end
-
- def has_base_configuration_error?
- host_url_not_found
- .or { missing_dependencies }
- .or { version_mismatch }
- .or { sso_misconfigured }
- .or { with_unexpected_content }
- .or { capabilities_request_failed_with_unknown_error }
- end
-
- def has_ampf_configuration_error?
- return None() unless @storage.automatic_management_enabled?
-
- userless_access_denied
- .or { group_folder_not_found }
- .or { files_request_failed_with_unknown_error }
- end
-
- def capabilities
- @capabilities ||= Peripherals::Registry
- .resolve("#{@storage}.queries.capabilities")
- .call(storage: @storage, auth_strategy: noop)
- end
-
- def files
- @files ||= Peripherals::Registry
- .resolve("#{@storage}.queries.files")
- .call(storage: @storage, auth_strategy: userless, folder: ParentFolder.new(@storage.group_folder))
- end
-
- def maybe_is_not_configured
- return None() if @storage.configured?
-
- Some(ConnectionValidation.new(type: :none,
- error_code: :wrn_not_configured,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.not_configured")))
- end
-
- def host_url_not_found
- return None() if capabilities.result != :not_found
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_host_not_found,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.host_not_found")))
- end
-
- # rubocop:disable Metrics/AbcSize
- def missing_dependencies
- return None() if capabilities.failure?
-
- capabilities_result = capabilities.result
-
- if !capabilities_result.app_enabled? ||
- (@storage.automatic_management_enabled? && !capabilities_result.group_folder_enabled?)
- app_name = if capabilities_result.app_enabled?
- I18n.t("storages.dependencies.nextcloud.group_folders_app")
- else
- I18n.t("storages.dependencies.nextcloud.integration_app")
- end
-
- Some(
- ConnectionValidation.new(
- type: :error,
- error_code: :err_missing_dependencies,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.missing_dependencies", dependency: app_name)
- )
- )
- else
- None()
- end
- end
-
- # rubocop:enable Metrics/AbcSize
-
- # rubocop:disable Metrics/AbcSize
- def version_mismatch
- return None() if capabilities.failure?
-
- config = YAML.load_file(path_to_config).deep_stringify_keys!
- min_app_version = SemanticVersion.parse(config.dig("dependencies", "integration_app", "min_version"))
- min_group_folder_version = SemanticVersion.parse(config.dig("dependencies", "group_folders_app", "min_version"))
-
- capabilities_result = capabilities.result
-
- if capabilities_result.app_version < min_app_version
- Some(
- ConnectionValidation.new(
- type: :error,
- error_code: :err_unexpected_version,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.app_version_mismatch",
- found: capabilities_result.app_version.to_s,
- expected: min_app_version.to_s)
- )
- )
- elsif @storage.automatic_management_enabled? &&
- capabilities_result.group_folder_version < min_group_folder_version
- Some(
- ConnectionValidation.new(
- type: :error,
- error_code: :err_unexpected_version,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.group_folder_version_mismatch",
- found: capabilities_result.group_folder_version.to_s,
- expected: min_group_folder_version.to_s)
- )
- )
- else
- None()
- end
- end
-
- # rubocop:enable Metrics/AbcSize
-
- def userless_access_denied
- return None() if files.result != :unauthorized
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_userless_access_denied,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.userless_access_denied")))
- end
-
- def group_folder_not_found
- return None() if files.result != :not_found
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_group_folder_not_found,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.group_folder_not_found")))
- end
-
- # rubocop:disable Metrics/AbcSize
- def with_unexpected_content
- return None() unless @storage.automatic_management_enabled?
- return None() if files.failure?
-
- expected_folder_ids = @storage.project_storages
- .where(project_folder_mode: "automatic")
- .map(&:project_folder_id)
-
- unexpected_files = files.result.files.reject { |file| expected_folder_ids.include?(file.id) }
- return None() if unexpected_files.empty?
-
- file_representation = unexpected_files.map do |file|
- "Name: #{file.name}, ID: #{file.id}, Location: #{file.location}"
- end
- warn "Unexpected files/folder found in group folder:\n\t#{file_representation.join("\n\t")}"
-
- Some(
- ConnectionValidation.new(
- type: :warning,
- error_code: :wrn_unexpected_content,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.nextcloud.unexpected_content")
- )
- )
- end
-
- # rubocop:enable Metrics/AbcSize
-
- def capabilities_request_failed_with_unknown_error
- return None() if capabilities.success?
-
- error "Connection validation failed with unknown error:\n\t" \
- "storage: ##{@storage.id} #{@storage.name}\n\t" \
- "request: Nextcloud capabilities\n\t" \
- "status: #{capabilities.result}\n\t" \
- "response: #{capabilities.error_payload}"
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_unknown,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.unknown_error")))
- end
-
- def files_request_failed_with_unknown_error
- return None() if files.success?
-
- error "Connection validation failed with unknown error:\n\t" \
- "storage: ##{@storage.id} #{@storage.name}\n\t" \
- "request: Group folder content\n\t" \
- "status: #{files.result}\n\t" \
- "response: #{files.error_payload}"
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_unknown,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.unknown_error")))
- end
-
- def noop = StorageInteraction::AuthenticationStrategies::Noop.strategy
-
- def userless = Peripherals::Registry.resolve("#{@storage.short_provider_type}.authentication.userless").call
-
- def path_to_config = Rails.root.join("modules/storages/config/nextcloud_dependencies.yml")
- end
- end
-end
diff --git a/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb b/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb
deleted file mode 100644
index 5c62084e37d..00000000000
--- a/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb
+++ /dev/null
@@ -1,190 +0,0 @@
-# frozen_string_literal:true
-
-#-- 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 Storages
- module Peripherals
- class OneDriveConnectionValidator
- include TaggedLogging
- include Dry::Monads[:maybe]
-
- using ServiceResultRefinements
-
- def initialize(storage:)
- @storage = storage
- end
-
- def validate
- with_tagged_logger do
- maybe_is_not_configured
- .or { tenant_id_wrong }
- .or { client_id_wrong }
- .or { client_secret_wrong }
- .or { drive_id_wrong }
- .or { request_failed_with_unknown_error }
- .or { drive_with_unexpected_content }
- .value_or(ConnectionValidation.new(type: :healthy,
- error_code: :none,
- timestamp: Time.current,
- description: nil))
- end
- end
-
- private
-
- def query
- @query ||= Peripherals::Registry
- .resolve("#{@storage}.queries.files")
- .call(storage: @storage, auth_strategy:, folder: root_folder)
- end
-
- def maybe_is_not_configured
- return None() if @storage.configured?
-
- Some(ConnectionValidation.new(type: :none,
- error_code: :wrn_not_configured,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.not_configured")))
- end
-
- # rubocop:disable Metrics/AbcSize
- def drive_id_wrong
- return None() if query.success?
-
- validation_for_invalid_drive = ConnectionValidation.new(
- type: :error,
- error_code: :err_drive_invalid,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.drive_id_wrong")
- )
-
- error_code = query.result
- return Some(validation_for_invalid_drive) if error_code == :not_found
-
- payload = query.error_payload
- return None() unless error_code == :error && payload.present? && payload.dig(:error, :code) == "invalidRequest"
-
- malformed_drive_id_string = "provided drive id appears to be malformed" # invalidRequest
- return None() unless payload.dig(:error, :message).include?(malformed_drive_id_string)
-
- Some(validation_for_invalid_drive)
- end
-
- # rubocop:enable Metrics/AbcSize
-
- def tenant_id_wrong
- return None() if query.result != :unauthorized
-
- payload = JSON.parse(query.error_payload)
- return None() if payload["error"] != "invalid_request"
-
- tenant_id_string = "Tenant '#{@storage.tenant_id}' not found."
- return None() unless payload["error_description"].include?(tenant_id_string)
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_tenant_invalid,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.tenant_id_wrong")))
- end
-
- def client_id_wrong
- return None() if query.result != :unauthorized
-
- payload = JSON.parse(query.error_payload)
- return None() if payload["error"] != "unauthorized_client"
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_client_invalid,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.client_id_wrong")))
- end
-
- def client_secret_wrong
- return None() if query.result != :unauthorized
-
- payload = JSON.parse(query.error_payload)
- return None() if payload["error"] != "invalid_client"
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_client_invalid,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.client_secret_wrong")))
- end
-
- # rubocop:disable Metrics/AbcSize
- def drive_with_unexpected_content
- return None() if query.failure?
- return None() unless @storage.automatic_management_enabled?
-
- expected_folder_ids = @storage.project_storages
- .where(project_folder_mode: "automatic")
- .map(&:project_folder_id)
-
- unexpected_files = query.result.files.reject { |file| expected_folder_ids.include?(file.id) }
- return None() if unexpected_files.empty?
-
- file_representation = unexpected_files.map do |file|
- "Name: #{file.name}, ID: #{file.id}, Location: #{file.location}"
- end
- warn "Unexpected files/folder found in group folder:\n\t#{file_representation.join("\n\t")}"
-
- Some(ConnectionValidation.new(type: :warning,
- error_code: :wrn_unexpected_content,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.unexpected_content.one_drive")))
- end
-
- # rubocop:enable Metrics/AbcSize
-
- def request_failed_with_unknown_error
- return None() if query.success?
-
- error "Connection validation failed with unknown error:\n\t" \
- "storage: ##{@storage.id} #{@storage.name}\n\t" \
- "status: #{query.result}\n\t" \
- "response: #{query.error_payload}"
-
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_unknown,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.unknown_error")))
- end
-
- def root_folder
- Peripherals::ParentFolder.new("/")
- end
-
- def auth_strategy
- Peripherals::Registry.resolve("#{@storage}.authentication.userless")
- .call
- .with_cache(false)
- end
- end
- end
-end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 319697f20cc..5a2bd35935a 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -201,7 +201,6 @@ en:
storage_delete_result_3: In case this storage has automatically managed project folders, those and their contained files will be deleted forever.
dependencies:
nextcloud:
- group_folders_app: Group folders
integration_app: Integration OpenProject
enabled_in_projects:
setup_incomplete_description: This storage has an incomplete setup. Please, complete the setup before enabling it in multiple projects.
diff --git a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb
deleted file mode 100644
index 824fc026b26..00000000000
--- a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb
+++ /dev/null
@@ -1,376 +0,0 @@
-# frozen_string_literal: true
-
-#-- 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.
-#++
-
-require "spec_helper"
-require_module_spec_helper
-
-RSpec.describe Storages::Peripherals::NextcloudConnectionValidator, :webmock do
- before do
- Storages::Peripherals::Registry.stub("#{storage}.queries.capabilities", ->(_) { capabilities_response })
- Storages::Peripherals::Registry.stub("#{storage}.queries.files", ->(_) { files_response })
- end
-
- subject { described_class.new(storage:).validate }
-
- context "if storage is not yet configured" do
- let(:storage) { create(:nextcloud_storage) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:none)
- expect(subject.error_code).to eq(:wrn_not_configured)
- expect(subject.description).to eq("The connection could not be validated. Please finish configuration first.")
- end
- end
-
- context "if storage is not configured for automatic folder management" do
- let(:storage) { create(:nextcloud_storage_configured, :as_not_automatically_managed) }
- let(:app_enabled) { true }
- let(:app_version) { Storages::SemanticVersion.parse("2.6.3") }
- let(:capabilities_response) do
- ServiceResult.success(result: Storages::NextcloudCapabilities.new(
- app_enabled?: app_enabled,
- app_version:,
- group_folder_enabled?: false,
- group_folder_version: nil
- ))
- end
-
- it "returns a healthy validation" do
- expect(subject.type).to eq(:healthy)
- expect(subject.error_code).to eq(:none)
- end
-
- context "if nextcloud host url could not be found" do
- let(:capabilities_response) { build_failure(code: :not_found, payload: nil) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_host_not_found)
- expect(subject.description)
- .to eq("No Nextcloud server found at the configured host url. Please check the configuration.")
- end
- end
-
- context "if nextcloud server instance is badly configured" do
- context "with missing integration app" do
- let(:app_enabled) { false }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_missing_dependencies)
- expect(subject.description).to eq("A required dependency is missing on the file storage. " \
- "Please add the following dependency: Integration OpenProject.")
- end
- end
-
- context "with outdated integration app" do
- let(:app_version) { Storages::SemanticVersion.parse("2.4.3") }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_unexpected_version)
- expect(subject.description)
- .to eq("The Integration OpenProject app version is not supported. Please update your Nextcloud server.")
- end
- end
-
- context "if capabilities query returns an unhandled error" do
- let(:capabilities_response) { build_failure(code: :error, payload: nil) }
-
- before do
- allow(Rails.logger).to receive(:error)
- end
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_unknown)
- expect(subject.description).to eq("The connection could not be validated. An unknown error occurred. " \
- "Please check the server logs for further information.")
- end
-
- it "logs the error message" do
- described_class.new(storage:).validate
- expect(Rails.logger).to have_received(:error).with(/Connection validation failed with unknown error/)
- end
- end
- end
- end
-
- context "if storage is configured for automatic folder management" do
- let(:storage) { create(:nextcloud_storage_configured, :as_automatically_managed) }
- let(:group_folder_enabled) { true }
- let(:group_folder_version) { Storages::SemanticVersion.parse("17.0.1") }
- let(:capabilities_response) do
- ServiceResult.success(result: Storages::NextcloudCapabilities.new(
- app_enabled?: true,
- app_version: Storages::SemanticVersion.parse("2.6.3"),
- group_folder_enabled?: group_folder_enabled,
- group_folder_version:
- ))
- end
- let(:project_folder_id) { "1337" }
- let(:project_storage) do
- create(:project_storage,
- :as_automatically_managed,
- project_folder_id:,
- storage:,
- project: create(:project))
- end
-
- before { project_storage }
-
- context "with disabled/not installed group folder app" do
- let(:group_folder_enabled) { false }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_missing_dependencies)
- expect(subject.description).to eq("A required dependency is missing on the file storage. " \
- "Please add the following dependency: Group folders.")
- end
- end
-
- context "with outdated group folder app" do
- let(:group_folder_version) { Storages::SemanticVersion.parse("11.0.1") }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_unexpected_version)
- expect(subject.description)
- .to eq("The Group Folder version is not supported. Please update your Nextcloud server.")
- end
- end
-
- context "if userless authentication fails" do
- let(:files_response) { build_failure(code: :unauthorized, payload: nil) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_userless_access_denied)
- expect(subject.description).to eq("The configured app password is invalid.")
- end
- end
-
- context "if the files request returns not_found" do
- let(:files_response) { build_failure(code: :not_found, payload: nil) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_group_folder_not_found)
- expect(subject.description).to eq("The group folder could not be found.")
- end
- end
-
- context "if the files request returns an unknown error" do
- let(:files_response) do
- Storages::Peripherals::StorageInteraction::Nextcloud::Util.error(:error)
- end
-
- before do
- allow(Rails.logger).to receive(:error)
- end
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_unknown)
- expect(subject.description).to eq("The connection could not be validated. An unknown error occurred. " \
- "Please check the server logs for further information.")
- end
-
- it "logs the error message" do
- described_class.new(storage:).validate
- expect(Rails.logger).to have_received(:error).with(/Connection validation failed with unknown error/)
- end
- end
-
- context "if the files request returns unexpected files" do
- let(:files_response) do
- ServiceResult.success(result: Storages::StorageFiles.new(
- [
- Storages::StorageFile.new(id: project_folder_id, name: "I am your father"),
- Storages::StorageFile.new(id: "noooooooooo", name: "testimony_of_luke_skywalker.md")
- ],
- Storages::StorageFile.new(id: "root", name: "root"),
- []
- ))
- end
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:warning)
- expect(subject.error_code).to eq(:wrn_unexpected_content)
- expect(subject.description).to eq("Unexpected content found in the managed group folder.")
- end
- end
- end
-
- describe "OpenID Connect support" do
- let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_enabled) }
- let(:app_version) { Storages::SemanticVersion.parse("2.6.3") }
- let(:capabilities_response) do
- ServiceResult.success(result: Storages::NextcloudCapabilities.new(
- app_enabled?: true,
- app_version:,
- group_folder_enabled?: false,
- group_folder_version: nil
- ))
- end
-
- let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER") }
- let!(:oidc_provider) { create(:oidc_provider) }
-
- before do
- User.current = user
-
- xml_response = Rails.root.join("modules/storages/spec/support/payloads/nextcloud_user_query_success.xml")
-
- stub_request(:get, "#{storage.uri}ocs/v1.php/cloud/user")
- .and_return(status: 200, body: File.read(xml_response), headers: { content_type: "text/xml" })
- end
-
- it "returns a success" do
- create(:oidc_user_token, user:, extra_audiences: storage.audience)
-
- expect(subject.type).to eq(:healthy)
- expect(subject.error_code).to eq(:none)
- end
-
- it "returns a validation warning if the current user isn't provisioned" do
- user.update!(identity_url: nil)
-
- expect(subject.type).to eq(:warning)
- expect(subject.error_code).to eq(:oidc_non_provisioned_user)
- expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_non_provisioned_user"))
- end
-
- it "returns a warning if the user is not provisioned by an oidc provider" do
- user.update!(identity_url: "ldap-provider:this-will-trigger-a-warning")
-
- expect(subject.type).to eq(:warning)
- expect(subject.error_code).to eq(:oidc_non_oidc_user)
- expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_non_oidc_user"))
- end
-
- describe "checks related to the token" do
- let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER") }
-
- context "when the token doesn't have the necessary audiences" do
- it "returns a validation failure in case the server does not support token exchange" do
- create(:oidc_user_token, user:)
-
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:oidc_cant_acquire_token)
- expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_cant_acquire_token"))
- end
- end
-
- describe "token refresh" do
- let(:expired_storage_token) do
- create(:oidc_user_token, user:, extra_audiences: storage.audience, expires_at: 10.hours.ago)
- end
-
- it "tries to refresh the token if it is expired" do
- refresh_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })
- .and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })
-
- expect(subject.type).to eq(:healthy)
- expect(refresh_request).to have_been_requested.once
- end
-
- it "returns a validation failure when the refresh response is invalid" do
- stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })
- .and_return_json(status: 200, body: { error: "this is a broken endpoint" })
-
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:oidc_cant_refresh_token)
- expect(subject.description)
- .to eq(I18n.t("storages.health.connection_validation.oidc_cant_refresh_token"))
- end
-
- it "returns validation failure about an unusable token if refresh fails" do
- stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })
- .and_return(status: 401)
-
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:oidc_cant_refresh_token)
- expect(subject.description)
- .to eq(I18n.t("storages.health.connection_validation.oidc_cant_refresh_token"))
- end
-
- context "when the server supports token exchange" do
- let(:oidc_provider) { create(:oidc_provider, :token_exchange_capable) }
- let(:exchangeable_token) { create(:oidc_user_token, user:, refresh_token: nil) }
-
- it "perform favors token exchange when refreshing" do
- exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
- .and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })
-
- expect(subject.type).to eq(:healthy)
- expect(exchange_request).to have_been_requested.once
- end
-
- it "returns a validation failure if the exchange fails with an unexpected body" do
- exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
- .and_return_json(status: 200, body: { error: "failed " })
-
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:oidc_cant_exchange_token)
- expect(exchange_request).to have_been_requested.once
- end
-
- it "returns a validation failure if the exchange fails" do
- exchange_request = stub_request(:post, oidc_provider.token_endpoint)
- .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,
- grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })
- .and_return(status: 401)
-
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:oidc_cant_exchange_token)
- expect(exchange_request).to have_been_requested.once
- end
- end
- end
- end
- end
-
- private
-
- def build_failure(code:, payload:)
- data = Storages::StorageErrorData.new(source: "query", payload:)
- error = Storages::StorageError.new(code:, data:)
- ServiceResult.failure(result: code, errors: error)
- end
-end
diff --git a/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb
deleted file mode 100644
index 26bdc47292b..00000000000
--- a/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb
+++ /dev/null
@@ -1,192 +0,0 @@
-# frozen_string_literal: true
-
-#-- 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.
-#++
-
-require "spec_helper"
-require_module_spec_helper
-
-RSpec.describe Storages::Peripherals::OneDriveConnectionValidator do
- let(:storage) { create(:one_drive_storage, oauth_client: create(:oauth_client)) }
-
- before do
- Storages::Peripherals::Registry.stub("#{storage}.queries.files", ->(_) { response })
- end
-
- subject { described_class.new(storage:).validate }
-
- context "if storage is not yet configured" do
- let(:storage) { create(:one_drive_storage) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:none)
- expect(subject.error_code).to eq(:wrn_not_configured)
- expect(subject.description).to eq("The connection could not be validated. Please finish configuration first.")
- end
- end
-
- context "if the storage's tenant id could not be found" do
- let(:error_payload) do
- {
- error: "invalid_request",
- error_description: "There is an error. Tenant '#{storage.tenant_id}' not found. This is VERY bad."
- }.to_json
- end
- let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_tenant_invalid)
- expect(subject.description)
- .to eq("The configured directory (tenant) id is invalid. Please check the configuration.")
- end
- end
-
- context "if the storage's client id could not be found" do
- let(:error_payload) { { error: "unauthorized_client" }.to_json }
- let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_client_invalid)
- expect(subject.description).to eq("The configured OAuth 2 client id is invalid. Please check the configuration.")
- end
- end
-
- context "if the storage's client secret is wrong" do
- let(:error_payload) { { error: "invalid_client" }.to_json }
- let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_client_invalid)
- expect(subject.description)
- .to eq("The configured OAuth 2 client secret is invalid. Please check the configuration.")
- end
- end
-
- context "if the storage's drive id could not be found" do
- let(:response) { build_failure(code: :not_found, payload: nil) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_drive_invalid)
- expect(subject.description).to eq("The configured drive id could not be found. Please check the configuration.")
- end
- end
-
- context "if the storage's drive id is malformed" do
- let(:error_payload) do
- {
- error: {
- code: "invalidRequest",
- message: "The provided drive id appears to be malformed, or does not represent a valid drive."
- }
- }
- end
- let(:response) { build_failure(code: :error, payload: error_payload) }
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_drive_invalid)
- expect(subject.description).to eq("The configured drive id could not be found. Please check the configuration.")
- end
- end
-
- context "if the request fails with an unknown error" do
- let(:response) { build_failure(code: :error, payload: nil) }
-
- before do
- allow(Rails.logger).to receive(:error)
- end
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:error)
- expect(subject.error_code).to eq(:err_unknown)
- expect(subject.description)
- .to eq("The connection could not be validated. An unknown error occurred. " \
- "Please check the server logs for further information.")
- end
-
- it "logs the error message" do
- described_class.new(storage:).validate
- expect(Rails.logger).to have_received(:error)
- end
- end
-
- context "if the request returns unexpected files" do
- let(:storage) { create(:one_drive_storage, :as_automatically_managed, oauth_client: create(:oauth_client)) }
- let(:project_folder_id) { "1337" }
- let(:project_storage) do
- create(:project_storage,
- :as_automatically_managed,
- project_folder_id:,
- storage:,
- project: create(:project))
- end
- let(:files_result) do
- Storages::StorageFiles.new(
- [
- Storages::StorageFile.new(id: project_folder_id, name: "I am your father"),
- Storages::StorageFile.new(id: "noooooooooo", name: "testimony_of_luke_skywalker.md")
- ],
- Storages::StorageFile.new(id: "root", name: "root"),
- []
- )
- end
- let(:response) { ServiceResult.success(result: files_result) }
-
- before do
- project_storage
- end
-
- it "returns a validation failure" do
- expect(subject.type).to eq(:warning)
- expect(subject.error_code).to eq(:wrn_unexpected_content)
- expect(subject.description).to eq("Unexpected content found in the drive.")
- end
- end
-
- context "if everything was fine" do
- let(:response) { ServiceResult.success }
-
- it "returns a validation success" do
- expect(subject.type).to eq(:healthy)
- expect(subject.error_code).to eq(:none)
- expect(subject.description).to be_nil
- end
- end
-
- private
-
- def build_failure(code:, payload:)
- data = Storages::StorageErrorData.new(source: "query", payload:)
- error = Storages::StorageError.new(code:, data:)
- ServiceResult.failure(result: code, errors: error)
- end
-end