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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#57052] Add optimistic locking to oauth_client_tokens (#16376)
Pavel Balashoucab6ecf92e8a
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex 5209ca058c9..1d8c61703a7 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -33,7 +33,6 @@ require "uri/http"module OAuthClientsclass ConnectionManager- # Nextcloud API endpoint to check if Bearer token is validTOKEN_IS_FRESH_DURATION = 10.seconds.freezedef initialize(user:, configuration:)diff --git a/db/migrate/20240808133947_add_lock_version_to_oauth_client_tokens.rb b/db/migrate/20240808133947_add_lock_version_to_oauth_client_tokens.rbnew file mode 100644index 00000000000..69d1f347f8d--- /dev/null+++ b/db/migrate/20240808133947_add_lock_version_to_oauth_client_tokens.rb@@ -0,0 +1,35 @@+# frozen_string_literal:true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#++++class AddLockVersionToOAuthClientTokens < ActiveRecord::Migration[7.1]+ def change+ add_column :oauth_client_tokens, :lock_version, :integer, default: 0, null: false+ end+enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rbindex 8f423f342ba..d4c55dc2ec4 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb@@ -39,6 +39,7 @@ module Storagesdef initialize(user)@user = user+ @retried_after_stale_object_update = falseend# rubocop:disable Metrics/AbcSize@@ -56,6 +57,12 @@ module Storagesrefresh_and_retry(httpx_oauth_config, http_options, token.result, &)end+ rescue ActiveRecord::StaleObjectError => e+ raise e if @retried_after_stale_object_update++ @retried_after_stale_object_update = true+ Rails.logger.error("#{e.inspect} happend for User ##{@user.id} #{@user.name}")+ retryend# rubocop:enable Metrics/AbcSizediff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_spec.rbindex ce4c0d6a4e1..3b46acfa343 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_spec.rb@@ -131,6 +131,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Authentication, :webmoendcontext "with invalid oauth refresh token", vcr: "auth/nextcloud/user_token_refresh_token_invalid" do+ before { storage }+it "must return unauthorized" doresult = described_class[auth_strategy].call(storage:, http_options:) { |http| make_request(http) }expect(result).to be_failure@@ -140,6 +142,23 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Authentication, :webmoexpect(error.data.source).to be(Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken)end++ it "logs, retries once, raises exception if race condition happens" do+ token = OAuthClientToken.last+ strategy = described_class[auth_strategy]++ allow(Rails.logger).to receive(:error)+ allow(strategy).to receive(:current_token).and_return(ServiceResult.success(result: token))+ allow(token).to receive(:destroy).and_raise(ActiveRecord::StaleObjectError).twice++ expect do+ strategy.call(storage:, http_options:) { |http| make_request(http) }+ end.to raise_error(ActiveRecord::StaleObjectError)++ expect(Rails.logger)+ .to have_received(:error)+ .with("#<ActiveRecord::StaleObjectError: Stale object error.> happend for User ##{user.id} #{user.name}").once+ endendcontext "with invalid oauth access token", vcr: "auth/nextcloud/user_token_access_token_invalid" dodiff --git a/spec/support/vcr.rb b/spec/support/vcr.rbindex d669f267c7f..e42f4cf626b 100644--- a/spec/support/vcr.rb+++ b/spec/support/vcr.rb@@ -68,7 +68,11 @@ VCR.configure do |config|endend- config.default_cassette_options = { record: ENV.fetch("VCR_RECORD_MODE", :once).to_sym, drop_unused_requests: true }+ config.default_cassette_options = {+ record: ENV.fetch("VCR_RECORD_MODE", :once).to_sym,+ allow_playback_repeats: true,+ drop_unused_requests: true+ }endVCR.turn_off!