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

Implements the BaseConfigurationValidator (#18316)

Marcello Rochacc0cace0142a
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb b/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
new file mode 100644
index 00000000000..79d39939bb8
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
@@ -0,0 +1,53 @@
+# 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
+ module ConnectionValidators
+ CheckResult = Data.define(:key, :state, :message, :timestamp) do
+ private_class_method :new
+ def self.skipped(key)
+ new(key:, state: :skipped, message: nil, timestamp: nil)
+ end
+
+ def self.failure(key, message)
+ new(key:, state: :failure, message: message, timestamp: Time.zone.now)
+ end
+
+ def self.success(key)
+ new(key:, state: :success, message: nil, timestamp: Time.zone.now)
+ end
+
+ def success? = state == :success
+ def failure? = state == :failure
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb
new file mode 100644
index 00000000000..909a575439a
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb
@@ -0,0 +1,134 @@
+# 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
+ module ConnectionValidators
+ module Nextcloud
+ class BaseConfigurationValidator
+ def initialize(storage)
+ @storage = storage
+ @results = build_result_list
+ end
+
+ def call
+ catch :interrupted do
+ capabilities_request_failed
+ host_url_not_found
+ missing_dependencies
+ version_mismatch
+ end
+
+ @results
+ end
+
+ private
+
+ def capabilities_request_failed
+ if capabilities.failure? && capabilities.result != :not_found
+ fail_check(__method__, message(:error))
+ else
+ pass_check(__method__)
+ end
+ end
+
+ def version_mismatch
+ min_app_version = SemanticVersion.parse(nextcloud_dependencies.dig("dependencies", "integration_app", "min_version"))
+ capabilities_result = capabilities.result
+
+ if capabilities_result.app_version < min_app_version
+ fail_check(__method__, message(:app_version_mismatch))
+ else
+ pass_check(__method__)
+ end
+ end
+
+ def missing_dependencies
+ capabilities_result = capabilities.result
+ app_name = I18n.t("storages.dependencies.nextcloud.integration_app")
+
+ if capabilities_result.app_disabled?
+ fail_check(__method__, message(:missing_dependencies, dependency: app_name))
+ else
+ pass_check(__method__)
+ end
+ end
+
+ def host_url_not_found
+ if capabilities.result == :not_found
+ fail_check(__method__, message(:host_not_found))
+ else
+ pass_check(__method__)
+ end
+ end
+
+ def message(key, context = {})
+ I18n.t("storages.health.connection_validation.#{key}", **context)
+ end
+
+ def noop = StorageInteraction::AuthenticationStrategies::Noop.strategy
+
+ def capabilities
+ @capabilities ||= Peripherals::Registry.resolve("#{@storage}.queries.capabilities")
+ .call(storage: @storage, auth_strategy: noop)
+ end
+
+ def nextcloud_dependencies
+ @nextcloud_dependencies ||= YAML.load_file(path_to_config).deep_stringify_keys!
+ end
+
+ def path_to_config = Rails.root.join("modules/storages/config/nextcloud_dependencies.yml")
+
+ def fail_check(key, message)
+ update_result(key, CheckResult.failure(key, message))
+ throw :interrupted
+ end
+
+ def pass_check(key)
+ update_result(key, CheckResult.success(key))
+ end
+
+ def update_result(method, value)
+ @results[method.to_sym] = value
+ end
+
+ def build_result_list
+ {
+ capabilities_request_failed: CheckResult.skipped(:capabilities_request_failed),
+ host_url_not_found: CheckResult.skipped(:host_url_not_found),
+ missing_dependencies: CheckResult.skipped(:missing_dependencies),
+ version_mismatch: CheckResult.skipped(:version_mismatch)
+ }
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query.rb
index 46ac6efabba..6466ac2de1b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query.rb
@@ -34,7 +34,7 @@ module Storages
module Nextcloud
class CapabilitiesQuery
include Dry::Monads[:result]
- include Dry::Monads::Do.for(:call, :parse_capabilities)
+ include Dry::Monads::Do.for(:parse_capabilities)
def self.call(storage:, auth_strategy:)
new(storage).call(auth_strategy:)
@@ -47,9 +47,7 @@ module Storages
def call(auth_strategy:)
http_options = Util.ocs_api_request.deep_merge(Util.accept_json)
result = Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|
- json = yield handle_response(http.get(url))
-
- parse_capabilities(json)
+ handle_response(http.get(url))
end
to_service_result(result)
@@ -64,7 +62,8 @@ module Storages
case response
in { status: 200..299 }
- Success(response.json(symbolize_keys: true))
+ json = response.json(symbolize_keys: true)
+ parse_capabilities(json)
in { status: 404 }
Failure(StorageError.new(code: :not_found,
log_message: "Outbound request destination not found!",
@@ -98,7 +97,8 @@ module Storages
# rubocop:enable Metrics/AbcSize
def version(str)
- failure = Failure(StorageError.new(code: :error, log_message: "'#{str}' is not a valid version string"))
+ failure = Failure(StorageError.new(code: :invalid_version_number,
+ log_message: "'#{str}' is not a valid version string"))
return failure if str.nil?
diff --git a/modules/storages/app/models/storages/nextcloud_capabilities.rb b/modules/storages/app/models/storages/nextcloud_capabilities.rb
index ae40a5fc8b1..b9b658c997f 100644
--- a/modules/storages/app/models/storages/nextcloud_capabilities.rb
+++ b/modules/storages/app/models/storages/nextcloud_capabilities.rb
@@ -29,14 +29,13 @@
#++
module Storages
- NextcloudCapabilities = Data.define(
- :app_enabled?,
- :app_version,
- :group_folder_enabled?,
- :group_folder_version
- ) do
+ NextcloudCapabilities = Data.define(:app_enabled?, :app_version, :group_folder_enabled?, :group_folder_version) do
def self.empty
new(app_enabled?: false, group_folder_enabled?: false, app_version: nil, group_folder_version: nil)
end
+
+ def group_folder_disabled? = !group_folder_enabled?
+
+ def app_disabled? = !app_enabled?
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb
new file mode 100644
index 00000000000..3fbe341dd3d
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb
@@ -0,0 +1,85 @@
+# 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
+
+module Storages
+ module Peripherals
+ module ConnectionValidators
+ module Nextcloud
+ RSpec.describe BaseConfigurationValidator, :webmock do
+ let(:storage) { create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed) }
+
+ subject(:validator) { described_class.new(storage) }
+
+ it "returns a hash with check 'key' as key and a CheckResult as value", vcr: "nextcloud/capabilities_success" do
+ results = validator.call
+
+ expect(results).to be_a(Hash)
+ expect(results.values).to all(be_success)
+ end
+
+ describe "when errors occurs" do
+ it "base url could not be reached" do
+ stub_request(:get, UrlBuilder.url(storage.uri, "/ocs/v2.php/cloud/capabilities"))
+ .to_return(status: 404, body: "Not Found")
+
+ results = validator.call
+ expect(results[:host_url_not_found]).to be_a_failure
+ expect(results[:host_url_not_found].message).to eq(I18n.t(i18n_key(:host_not_found)))
+ end
+
+ it "integration app version mismatch", vcr: "nextcloud/capabilities_success" do
+ absurd_version = { dependencies: { integration_app: { min_version: "2099.10.138" } } }.deep_stringify_keys
+ allow(subject).to receive(:nextcloud_dependencies).and_return(absurd_version)
+
+ results = validator.call
+ expect(results[:version_mismatch]).to be_a_failure
+ expect(results[:version_mismatch].message).to eq(I18n.t(i18n_key(:app_version_mismatch)))
+ end
+
+ it "integration app disabled / missing", vcr: "nextcloud/capabilities_success_app_disabled" do
+ results = validator.call
+
+ expect(results[:missing_dependencies]).to be_a_failure
+ expect(results[:missing_dependencies].message)
+ .to eq(I18n.t(i18n_key(:missing_dependencies), dependency: "Integration OpenProject"))
+ end
+ end
+
+ private
+
+ def i18n_key(key) = "storages.health.connection_validation.#{key}"
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query_spec.rb
index 8ae0ebeb4da..be54a814c0b 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/capabilities_query_spec.rb
@@ -110,7 +110,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::Capabilitie
error = result.errors
expect(error).to be_a(Storages::StorageError)
- expect(error.code).to eq(:error)
+ expect(error.code).to eq(:invalid_version_number)
expect(error.log_message).to include("not a valid version string")
end
end