Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip rotation for read Apis #1254

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/api/connect/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ def authenticate_with_token
end
end

def system_token_header
headers[SYSTEM_TOKEN_HEADER] = @system.system_token
end

def refresh_system_token
@system.update(system_token: SecureRandom.uuid)
system_token_header
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Api::Connect::V3::Systems::ProductsController < Api::Connect::BaseControll
before_action :check_product_service_and_repositories, only: %i[show activate]
before_action :load_subscription, only: %i[activate upgrade]
before_action :check_base_product_dependencies, only: %i[activate upgrade show]
after_action :refresh_system_token, only: %i[activate upgrade], if: -> { request.headers.key?(SYSTEM_TOKEN_HEADER) }
digitaltom marked this conversation as resolved.
Show resolved Hide resolved

def activate
create_product_activation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Api::Connect::V3::Systems::SystemsController < Api::Connect::BaseController

before_action :authenticate_system
after_action :refresh_system_token, only: [:update], if: -> { request.headers.key?(SYSTEM_TOKEN_HEADER) }

def update
if params[:online_at].present?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Api::Connect::V4::Systems::ProductsController < Api::Connect::V3::Systems::ProductsController

after_action :refresh_system_token, only: %i[synchronize destroy], if: -> { request.headers.key?(SYSTEM_TOKEN_HEADER) }
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
def destroy
if @product.base?
raise ActionController::TranslatedError.new(N_('The product "%s" is a base product and cannot be deactivated'), @product.name)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def authenticate_system(skip_on_duplicated: false)
update_user_agent

# If SYSTEM_TOKEN_HEADER is present, RMT assumes the client uses a SUSEConnect version
# that supports this feature. In this case, refresh the token and include it in the response.
# that supports this feature.
if system_tokens_enabled? && request.headers.key?(SYSTEM_TOKEN_HEADER)
@system.update(last_seen_at: Time.zone.now, system_token: SecureRandom.uuid)
@system.update(last_seen_at: Time.zone.now)
headers[SYSTEM_TOKEN_HEADER] = @system.system_token
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
# only update last_seen_at each 3 minutes,
# only update last_seen_at each 3 minutes,
# so that a system that calls SCC every second doesn't write + lock the database row
elsif [email protected]_seen_at || @system.last_seen_at < 3.minutes.ago
@system.touch(:last_seen_at)
Expand Down
19 changes: 3 additions & 16 deletions spec/requests/api/connect/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ def require_product
end
end

shared_examples 'updates the system token' do
digitaltom marked this conversation as resolved.
Show resolved Hide resolved
it 'updates the system token' do
allow(SecureRandom).to receive(:uuid).and_return(new_system_token)

expect { get :service, params: { id: 1 } }
.to change { system.reload.system_token }
.from(current_system_token).to(new_system_token)
end
end

shared_examples "does not update the old system's token" do
it 'does not update the system token' do
Expand All @@ -74,7 +65,6 @@ def require_product

shared_examples 'creates a duplicate system' do
it 'creates a new System (duplicate)' do
allow(SecureRandom).to receive(:uuid).and_return(new_system_token)

expect { get :service, params: { id: 1 } }
.to change { System.get_by_credentials(system.login, system.password).count }
Expand All @@ -85,7 +75,6 @@ def require_product
expect(duplicate_system).not_to eq(system)
expect(duplicate_system.activations.count).to eq(system.activations.count)
expect(duplicate_system.system_token).not_to eq(system.system_token)
expect(duplicate_system.system_token).to eq(new_system_token)
end
end

Expand Down Expand Up @@ -182,8 +171,7 @@ def require_product
let(:system) { create(:system, hostname: 'system') }

include_examples 'does not create a duplicate system'
include_examples 'updates the system token'
include_examples 'responds with a new token'
include_examples "does not update the old system's token"
end

context 'when the system has a token and the header matches it' do
Expand All @@ -193,8 +181,8 @@ def require_product
let(:system) { create(:system, hostname: 'system', system_token: current_system_token) }

include_examples 'does not create a duplicate system'
include_examples 'updates the system token'
include_examples 'responds with a new token'
include_examples "does not update the old system's token"

end

context 'when the system has a token and the header is blank' do
Expand All @@ -208,7 +196,6 @@ def require_product

include_examples "does not update the old system's token"
include_examples 'creates a duplicate system'
include_examples 'responds with a new token'
end

context 'when the system has a token and the header does not match it' do
Expand Down
Loading