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

Implemetns the AMPF group checks (#18435)

Marcello Rochadc9eda5e2b00
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
index b489a816e6f..0d7aaae19b8 100644
--- a/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
@@ -38,7 +38,7 @@ module Storages
end
def self.failure(key, message)
- new(key:, state: :failure, message: message, timestamp: Time.zone.now)
+ new(key:, state: :failure, message:, timestamp: Time.zone.now)
end
def self.success(key)
@@ -46,7 +46,7 @@ module Storages
end
def self.warning(key, message)
- new(key:, state: :warning, message: message, timestamp: Time.zone.now)
+ new(key:, state: :warning, message:, timestamp: Time.zone.now)
end
def success? = state == :success
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rb
new file mode 100644
index 00000000000..3bdebff3dac
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rb
@@ -0,0 +1,113 @@
+# 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 AmpfConnectionValidator < BaseValidator
+ using ServiceResultRefinements
+
+ private
+
+ def validate
+ register_checks(
+ :userless_access, :group_folder_presence, :files_request, :group_folder_contents
+ )
+
+ userless_access_denied
+ group_folder_not_found
+ files_request_failed_with_unknown_error
+ with_unexpected_content
+ end
+
+ def userless_access_denied
+ if files.result == :unauthorized
+ fail_check(:userless_access, message(:userless_access_denied))
+ else
+ pass_check(:userless_access)
+ end
+ end
+
+ def group_folder_not_found
+ if files.result == :not_found
+ fail_check(:group_folder_presence, message(:group_folder_not_found))
+ else
+ pass_check(:group_folder_presence)
+ end
+ end
+
+ def files_request_failed_with_unknown_error
+ if files.result == :error
+ error "Connection validation failed with unknown error:\n" \
+ "\tstorage: ##{@storage.id} #{@storage.name}\n" \
+ "\trequest: Group folder content\n" \
+ "\tstatus: #{files.result}\n" \
+ "\tresponse: #{files.error_payload}"
+
+ fail_check(:files_request, message(:unknown_error))
+ else
+ pass_check(:files_request)
+ end
+ end
+
+ def with_unexpected_content
+ unexpected_files = files.result.files.reject { managed_project_folder_ids.include?(it.id) }
+ return pass_check(:group_folder_contents) if unexpected_files.empty?
+
+ log_extraneous_files(unexpected_files)
+ warn_check(:group_folder_contents, message(:unexpected_content))
+ end
+
+ def log_extraneous_files(unexpected_files)
+ 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")}"
+ end
+
+ def auth_strategy = Registry["nextcloud.authentication.userless"].call
+
+ def managed_project_folder_ids
+ @managed_project_folder_ids ||= ProjectStorage.automatic.where(storage: @storage)
+ .pluck(:project_folder_id).to_set
+ end
+
+ def files
+ @files ||= Peripherals::Registry
+ .resolve("#{@storage}.queries.files")
+ .call(storage: @storage, auth_strategy:, folder: ParentFolder.new(@storage.group_folder))
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb
index bd8c3be4d1e..34afb6f8842 100644
--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb
@@ -33,6 +33,8 @@ module Storages
module ConnectionValidators
module Nextcloud
class BaseValidator
+ include TaggedLogging
+
def initialize(storage)
@storage = storage
end
diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rb
new file mode 100644
index 00000000000..c5ffae86fa5
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rb
@@ -0,0 +1,138 @@
+# 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 AmpfConnectionValidator, :webmock do
+ let(:storage) { create(:nextcloud_storage_configured, :as_automatically_managed) }
+ 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_response) do
+ ServiceResult.success(result: StorageFiles.new(
+ [StorageFile.new(id: project_folder_id, name: project_storage.managed_project_folder_name)],
+ StorageFile.new(id: "root", name: "root"),
+ []
+ ))
+ end
+
+ subject(:validator) { described_class.new(storage) }
+
+ before do
+ Registry.stub("nextcloud.queries.files", ->(*) { files_response })
+ end
+
+ it "pass all checks" do
+ results = validator.call
+
+ expect(results.values).to all(be_success)
+ end
+
+ context "if userless authentication fails" do
+ let(:files_response) { build_failure(code: :unauthorized, payload: nil) }
+
+ it "fails and skips the next checks" do
+ results = validator.call
+
+ states = results.values.map { it.state }.tally
+ expect(states).to eq({ failure: 1, skipped: 3 })
+ expect(results[:userless_access]).to be_failure
+ expect(results[:userless_access].message).to eq(i18n_message(:userless_access_denied))
+ end
+ end
+
+ context "if the files request returns not_found" do
+ let(:files_response) { build_failure(code: :not_found, payload: nil) }
+
+ it "fails the check" do
+ results = validator.call
+
+ expect(results[:group_folder_presence]).to be_failure
+ expect(results[:group_folder_presence].message).to eq(i18n_message(:group_folder_not_found))
+ end
+ end
+
+ context "if the files request returns an unknown error" do
+ let(:files_response) { StorageInteraction::Nextcloud::Util.error(:error) }
+
+ before { allow(Rails.logger).to receive(:error) }
+
+ it "fails the check and logs the error" do
+ results = validator.call
+
+ expect(results[:files_request]).to be_failure
+ expect(results[:files_request].message)
+ .to eq(i18n_message(:unknown_error))
+
+ 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: StorageFiles.new(
+ [
+ StorageFile.new(id: project_folder_id, name: "I am your father"),
+ StorageFile.new(id: "noooooooooo", name: "testimony_of_luke_skywalker.md")
+ ],
+ StorageFile.new(id: "root", name: "root"),
+ []
+ ))
+ end
+
+ it "warns the user about extraneous folders" do
+ results = validator.call
+
+ expect(results[:group_folder_contents]).to be_a_warning
+ expect(results[:group_folder_contents].message).to eq(i18n_message(:unexpected_content))
+ end
+ end
+
+ private
+
+ def i18n_message(key, context = {}) = I18n.t("storages.health.connection_validation.#{key}", **context)
+
+ def build_failure(code:, payload:)
+ data = StorageErrorData.new(source: "query", payload:)
+ error = StorageError.new(code:, data:)
+ ServiceResult.failure(result: code, errors: error)
+ end
+ end
+ end
+ end
+ end
+end