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

[#56030] improve detection of wrong drive ids

Eric Schubert68950d0a5d07
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
index 70f749b328a..4f97eea30f8 100644
--- a/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb
+++ b/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb
@@ -70,15 +70,31 @@ module Storages
description: I18n.t("storages.health.connection_validation.not_configured")))
end
+ # rubocop:disable Metrics/AbcSize
def drive_id_wrong
- return None() if query.result != :not_found
+ return None() if query.success?
- Some(ConnectionValidation.new(type: :error,
- error_code: :err_drive_invalid,
- timestamp: Time.current,
- description: I18n.t("storages.health.connection_validation.drive_id_wrong")))
+ 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
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index 4c0d64d7cca..51a7896a2db 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -41,12 +41,11 @@ module Storages
def initialize(storage)
@storage = storage
- @uri = storage.uri
end
def call(auth_strategy:, folder:)
Authentication[auth_strategy].call(storage: @storage) do |http|
- call = http.get(Util.join_uri_path(@uri, children_uri_path_for(folder) + FIELDS))
+ call = http.get(Util.join_uri_path(@storage.uri, children_uri_path_for(folder) + FIELDS))
response = handle_response(call, :value)
if response.result.empty?
@@ -59,10 +58,14 @@ module Storages
private
+ # rubocop:disable Metrics/AbcSize
def handle_response(response, map_value)
case response
in { status: 200..299 }
ServiceResult.success(result: response.json(symbolize_keys: true).fetch(map_value))
+ in { status: 400 }
+ ServiceResult.failure(result: :error,
+ errors: Util.storage_error(response:, code: :error, source: self.class))
in { status: 404 }
ServiceResult.failure(result: :not_found,
errors: Util.storage_error(response:, code: :not_found, source: self.class))
@@ -78,6 +81,8 @@ module Storages
end
end
+ # rubocop:enable Metrics/AbcSize
+
def storage_files(json_files)
files = json_files.map { |json| Util.storage_file_from_json(json) }
@@ -86,7 +91,7 @@ module Storages
end
def empty_response(http, folder)
- response = http.get(Util.join_uri_path(@uri, location_uri_path_for(folder) + FIELDS))
+ response = http.get(Util.join_uri_path(@storage.uri, location_uri_path_for(folder) + FIELDS))
handle_response(response, :id).map do |parent_location_id|
empty_storage_files(folder.path, parent_location_id)
end
diff --git a/modules/storages/app/contracts/storages/storages/one_drive_contract.rb b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
index f83fccf6307..40dafef17e3 100644
--- a/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
@@ -35,6 +35,8 @@ module Storages::Storages
attribute :tenant_id
validates :tenant_id, format: { with: /\A(?:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}|consumers)\z/i }
attribute :drive_id
- validates :drive_id, presence: true, allow_nil: true
+ # GRAPH API considers drive ids of 16 characters or shorter as personal drive ids. Those are not supported,
+ # and allowing them lead to unexpected behavior.
+ validates :drive_id, presence: true, allow_nil: true, length: { minimum: 17 }
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
index 6aed3e2cf69..861b616571b 100644
--- 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
@@ -98,6 +98,24 @@ RSpec.describe Storages::Peripherals::OneDriveConnectionValidator do
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) }
diff --git a/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb
index b40600e4aa9..6f5b28e3e9b 100644
--- a/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb
@@ -49,13 +49,33 @@ RSpec.describe Storages::Storages::OneDriveContract, :storage_server_helpers, :w
end
end
+ context "with tenant that is no UUID" do
+ let(:storage) { build(:one_drive_storage, tenant_id: "123") }
+
+ it "is invalid" do
+ expect(contract).not_to be_valid
+
+ expect(contract.errors[:tenant_id]).to eq(["is invalid."])
+ end
+ end
+
context "with blank Drive ID" do
let(:storage) { build(:one_drive_storage, drive_id: "") }
it "is invalid" do
expect(contract).not_to be_valid
- expect(contract.errors[:drive_id]).to eq(["can't be blank."])
+ expect(contract.errors[:drive_id]).to eq(["can't be blank.", "is too short (minimum is 17 characters)."])
+ end
+ end
+
+ context "with short Drive ID" do
+ let(:storage) { build(:one_drive_storage, drive_id: "1234567890") }
+
+ it "is invalid" do
+ expect(contract).not_to be_valid
+
+ expect(contract.errors[:drive_id]).to eq(["is too short (minimum is 17 characters)."])
end
end
end