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

Adds 403 handling to OneDrive commands/queries

Marcello Rocha230b204fa968
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb
index dec0178eb7b..efb9588f8ae 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb
@@ -59,6 +59,8 @@ module Storages
ServiceResult.success
in { status: 401 }
ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden, errors: ::Storages::StorageError.new(code: :forbidden))
else
data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
index a190bca43ab..3ea82367012 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
@@ -62,24 +62,22 @@ module Storages
end
def handle_response(response)
- data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
-
case response
in { status: 200..299 }
ServiceResult.success(result: file_info_for(MultiJson.load(response.body, symbolize_keys: true)),
message: "Folder was successfully created.")
in { status: 404 }
ServiceResult.failure(result: :not_found,
- errors: ::Storages::StorageError.new(code: :not_found, data:))
+ errors: Util.storage_error(code: :not_found, response:, source: self.class))
in { status: 401 }
ServiceResult.failure(result: :unauthorized,
- errors: ::Storages::StorageError.new(code: :unauthorized, data:))
+ errors: Util.storage_error(code: :unauthorized, response:, source: self.class))
in { status: 409 }
ServiceResult.failure(result: :already_exists,
- errors: ::Storages::StorageError.new(code: :conflict, data:))
+ errors: Util.storage_error(code: :conflict, response:, source: self.class))
else
ServiceResult.failure(result: :error,
- errors: ::Storages::StorageError.new(code: :error, data:))
+ errors: Util.storage_error(code: :error, response:, source: self.class))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
index 67786974dc5..6c1e50c3a26 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
@@ -45,7 +45,9 @@ module Storages
def call(auth_strategy:, file_link:)
if file_link.nil?
- return failure(code: :error, payload: nil, log_message: "File link can not be nil.")
+ return ServiceResult.failure(result: :error,
+ errors: Util.storage_error(code: :error, response: nil, source: self.class,
+ log_message: "File link can not be nil."))
end
Auth[auth_strategy].call(storage: @storage) do |http|
@@ -60,36 +62,27 @@ module Storages
in { status: 300..399 }
ServiceResult.success(result: response.headers["Location"])
in { status: 404 }
- failure(code: :not_found,
- payload: response.json(symbolize_keys: true),
- log_message: "Outbound request destination not found!")
+ ServiceResult.failure(result: :not_found,
+ errors: Util.storage_error(code: :not_found, response:, source: self.class,
+ log_message: "Outbound request destination not found!"))
in { status: 403 }
- failure(code: :forbidden,
- payload: response.json(symbolize_keys: true),
- log_message: "Outbound request forbidden!")
+ ServiceResult.failure(result: :forbidden,
+ errors: Util.storage_error(code: :forbidden, response:, source: self.class,
+ log_message: "Outbound request forbidden!"))
in { status: 401 }
- failure(code: :unauthorized,
- payload: response.json(symbolize_keys: true),
- log_message: "Outbound request not authorized!")
+ ServiceResult.failure(result: :unauthorized,
+ errors: Util.storage_error(code: :unauthorized, response:, source: self.class,
+ log_message: "Outbound request not authorized!"))
else
- failure(code: :error,
- payload: response.json(symbolize_keys: true),
- log_message: "Outbound request failed with unknown error!")
+ ServiceResult.failure(result: :error,
+ errors: Util.storage_error(code: :error, response:, source: self.class,
+ log_message: "Outbound request failed with unknown error!"))
end
end
def uri_path_for(file_id)
"/v1.0/drives/#{@storage.drive_id}/items/#{file_id}/content"
end
-
- def failure(code:, payload:, log_message:)
- ServiceResult.failure(
- result: code,
- errors: ::Storages::StorageError.new(code:,
- data: StorageErrorData.new(source: self.class, payload:),
- log_message:)
- )
- end
end
end
end
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 91088a4cda2..e070513df2d 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
@@ -68,10 +68,13 @@ module Storages
ServiceResult.success(result: response.json(symbolize_keys: true).fetch(map_value))
in { status: 404 }
ServiceResult.failure(result: :not_found,
- errors: Util.storage_error(response:, code: :not_found, source: self))
+ errors: Util.storage_error(response:, code: :not_found, source: self.class))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden,
+ errors: Util.storage_error(response:, code: :forbidden, source: self.class))
in { status: 401 }
ServiceResult.failure(result: :unauthorized,
- errors: Util.storage_error(response:, code: :unauthorized, source: self))
+ errors: Util.storage_error(response:, code: :unauthorized, source: self.class))
else
data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
index f49282ae860..8409576c9ac 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
@@ -48,37 +48,23 @@ module Storages
def call(auth_strategy:, file_id:, open_location: false)
Auth[auth_strategy].call(storage: @storage) do |http|
if open_location
- request_parent_id(http).call(file_id) >> request_web_url(http)
+ request_parent_id(http, file_id).on_success { |parent_id| return request_web_url(http, parent_id.result) }
else
- request_web_url(http).call(file_id)
+ request_web_url(http, file_id)
end
end
end
private
- def request_web_url(http)
- ->(file_id) do
- @delegate.call(http:, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)
- end
- end
-
- def request_parent_id(http)
- ->(file_id) do
- @delegate.call(http:, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)
- end
+ # rubocop:disable Rails/Pluck
+ def request_web_url(http, file_id)
+ @delegate.call(http:, drive_item_id: file_id, fields: %w[webUrl]).map { |json| json[:webUrl] }
end
+ # rubocop:enable Rails/Pluck
- def web_url
- ->(json) do
- json[:webUrl]
- end
- end
-
- def parent_id
- ->(json) do
- json.dig(:parentReference, :id)
- end
+ def request_parent_id(http, file_id)
+ @delegate.call(http:, drive_item_id: file_id, fields: %w[parentReference]).map { |json| json.dig(:parentReference, :id) }
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
index 498d2a8a8fc..8a7242e1da2 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
@@ -69,8 +69,8 @@ module Storages
ServiceResult.failure(result: :unauthorized,
errors: Util.storage_error(response:, code: :unauthorized, source: self.class))
else
- data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
- ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
+ ServiceResult.failure(result: :error,
+ errors: Util.storage_error(response:, code: :error, source: self.class))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
index fd0f18b07e3..5099b2b7251 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
@@ -53,23 +53,24 @@ module Storages
private
def handle_response(response)
- data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
-
case response
in { status: 200..299 }
ServiceResult.success(result: storage_file(response.json(symbolize_keys: true)))
in { status: 401 }
ServiceResult.failure(result: :unauthorized,
- errors: ::Storages::StorageError.new(code: :unauthorized, data:))
+ errors: Util.storage_error(response:, code: :unauthorized, source: self.class))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden,
+ errors: Util.storage_error(response:, code: :forbidden, source: self.class))
in { status: 404 }
ServiceResult.failure(result: :not_found,
- errors: ::Storages::StorageError.new(code: :not_found, data:))
+ errors: Util.storage_error(response:, code: :not_found, source: self.class))
in { status: 409 }
ServiceResult.failure(result: :conflict,
- errors: ::Storages::StorageError.new(code: :conflict, data:))
+ errors: Util.storage_error(response:, code: :conflict, source: self.class))
else
ServiceResult.failure(result: :error,
- errors: ::Storages::StorageError.new(code: :error, data:))
+ errors: Util.storage_error(response:, code: :error, source: self.class))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
index 2d60c31c52e..7fda3dd4992 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
@@ -131,23 +131,26 @@ module Storages
end
def handle_response(response)
- data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
-
case response
in { status: 200 }
ServiceResult.success(result: response.json(symbolize_keys: true))
in { status: 204 }
ServiceResult.success(result: response)
in { status: 400 }
- ServiceResult.failure(result: :bad_request, errors: ::Storages::StorageError.new(code: :bad_request, data:))
+ ServiceResult.failure(result: :bad_request,
+ errors: Util.storage_error(response:, code: :bad_request, source: self.class))
in { status: 401 }
- ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized, data:))
+ ServiceResult.failure(result: :unauthorized,
+ errors: Util.storage_error(response:, code: :unauthorized, source: self.class))
in { status: 403 }
- ServiceResult.failure(result: :forbidden, errors: ::Storages::StorageError.new(code: :forbidden, data:))
+ ServiceResult.failure(result: :forbidden,
+ errors: Util.storage_error(response:, code: :forbidden, source: self.class))
in { status: 404 }
- ServiceResult.failure(result: :not_found, errors: ::Storages::StorageError.new(code: :not_found, data:))
+ ServiceResult.failure(result: :not_found,
+ errors: Util.storage_error(response:, code: :not_found, source: self.class))
else
- ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
+ ServiceResult.failure(result: :error,
+ errors: Util.storage_error(response:, code: :error, source: self.class))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
index fcf1a4d1c74..6187c86413a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
@@ -72,21 +72,22 @@ module Storages
end
def handle_response(response)
- data = StorageErrorData.new(source: self.class, payload: response)
-
case response
in { status: 200..299 }
upload_url = response.json(symbolize_keys: true)[:uploadUrl]
ServiceResult.success(result: UploadLink.new(URI(upload_url), :put))
in { status: 404 | 400 } # not existent parent folder in request url is responded with 400
ServiceResult.failure(result: :not_found,
- errors: StorageError.new(code: :not_found, data:))
+ errors: Util.storage_error(code: :not_found, response:, source: self.class))
in { status: 401 }
ServiceResult.failure(result: :unauthorized,
- errors: StorageError.new(code: :unauthorized, data:))
+ errors: Util.storage_error(code: :unauthorized, response:, source: self.class))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden,
+ errors: Util.storage_error(code: :forbidden, response:, source: self.class))
else
ServiceResult.failure(result: :error,
- errors: StorageError.new(code: :error, data:))
+ errors: Util.storage_error(code: :error, response:, source: self.class))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
index 560caf5011c..f7666b8e78d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
@@ -69,10 +69,12 @@ module Storages
)
end
- def storage_error(response:, code:, source:)
- data = StorageErrorData.new(source:, payload: response.json(symbolize_keys: true))
+ def storage_error(response:, code:, source:, log_message: nil)
+ # Some errors, like timeouts, aren't json responses so we need to adapt
+ payload = response.respond_to?(:json) ? response.json(symbolize_keys: true) : response.to_s
+ data = StorageErrorData.new(source:, payload:)
- StorageError.new(code:, data:)
+ StorageError.new(code:, data:, log_message:)
end
def join_uri_path(uri, *)
diff --git a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
index 956abdefe9a..e025c018a53 100644
--- a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
+++ b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb
@@ -198,16 +198,9 @@ module Storages
end
def format_and_log_error(error, context = {})
- payload = error.data.payload
- data =
- case payload
- in { status: Integer }
- { status: payload.status, body: payload.body.to_s }
- else
- payload.error.to_s
- end
-
- error_message = context.merge({ command: error.data.source, message: error.log_message, data: })
+ error_message = context.merge({ command: error.data.source,
+ message: error.log_message,
+ data: { status: error.code, body: error.data.payload.to_s } })
OpenProject.logger.warn error_message
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
index 53225148061..3100d2208a8 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_query_spec.rb
@@ -181,7 +181,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
it "must return not found" do
result = described_class.call(storage:, auth_strategy:, folder:)
expect(result).to be_failure
- expect(result.error_source).to be_a(described_class)
+ expect(result.error_source).to eq(described_class)
result.match(
on_failure: ->(error) { expect(error.code).to eq(:not_found) },
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
index 9324d99f226..08ab9ca5459 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
@@ -38,7 +38,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenFileLink
let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }
let(:file_id) { "01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU" }
let(:auth_strategy) do
- Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)
+ Storages::Peripherals::Registry.resolve("one_drive.authentication.userbound").call(user:)
end
subject { described_class.new(storage) }
@@ -79,11 +79,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenFileLink
result = subject.call(auth_strategy:, file_id:)
expect(result).to be_failure
expect(result.error_source).to be(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)
-
- result.match(
- on_failure: ->(error) { expect(error.code).to eq(:not_found) },
- on_success: ->(file_infos) { fail "Expected failure, got #{file_infos}" }
- )
+ expect(result.result).to eq(:not_found)
end
end
end
diff --git a/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb b/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
index 329fa2e3bad..2b39643c58f 100644
--- a/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/one_drive_managed_folder_sync_service_spec.rb
@@ -266,7 +266,7 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
.to have_received(:warn)
.with(command: described_class,
message: nil,
- data: "timed out while waiting on select")
+ data: { body: /timed out while waiting on select/, status: nil })
end
end
@@ -288,7 +288,7 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
.with(folder_path: "[Sample] Project Name _ Ehuu (#{project.id})",
command: Storages::Peripherals::StorageInteraction::OneDrive::CreateFolderCommand,
message: nil,
- data: { status: 409, body: /nameAlreadyExists/ })
+ data: { status: :conflict, body: /nameAlreadyExists/ })
ensure
delete_folder(already_existing_folder.id)
end
@@ -308,7 +308,7 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
target: "[Sample] Project Name _ Ehuu (#{project.id})",
command: Storages::Peripherals::StorageInteraction::OneDrive::RenameFileCommand,
message: nil,
- data: { status: 409, body: /nameAlreadyExists/ })
+ data: { status: :conflict, body: /nameAlreadyExists/ })
ensure
delete_folder(already_existing_folder.result.id)
end
@@ -323,7 +323,7 @@ RSpec.describe Storages::OneDriveManagedFolderSyncService, :webmock do
.to have_received(:warn)
.with(command: Storages::Peripherals::StorageInteraction::OneDrive::SetPermissionsCommand,
message: nil,
- data: { body: /noResolvedUsers/, status: 400 }).twice
+ data: { body: /noResolvedUsers/, status: nil }).twice
end
end
end