Skip to content

Commit

Permalink
Merge branch 'master' into depfu/update/group/rails-6.1.7.9
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom authored Nov 11, 2024
2 parents 8a993b1 + ac5c206 commit 3b5c2e0
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ SECRET_KEY_BASE=8ea53ad3bc6c03923e376c8bdd85059c1885524947a7efe53d5e9c9d4e398611
EXTERNAL_PORT=8080
SCC_USERNAME=
SCC_PASSWORD=
RMT_METRICS_ENABLED=
PROMETHEUS_JOB_NAME=
6 changes: 3 additions & 3 deletions .github/workflows/lint-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
- name: Run core tests
run: |
bundle exec rspec --format documentation
bundle exec rake test:core
- name: Run PubCloud engines tests
run: |
bundle exec rake test:engines
Expand All @@ -91,4 +91,4 @@ jobs:
run: |
echo "::group::Version verification checks"
ruby ci/check-version-matches.rb
echo "::endgroup::"
echo "::endgroup::"
4 changes: 4 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ unless ENV['NO_COVERAGE']
# omit registration sharing (removing systems using rmt-cli)
add_filter('engines/registration_sharing/lib/registration_sharing.rb')

add_filter('lib/rmt/db.rb')
add_filter('lib/rmt.rb')
add_filter('config')

track_files('app/**/*.rb')
track_files('lib/**/*.rb')
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ gem 'repomd_parser', '~> 1.1.0'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

# Prometheus Exporter:
gem 'yabeda'
gem 'yabeda-rails'
gem 'yabeda-puma-plugin'
gem 'yabeda-prometheus'

gem 'strong_migrations'

group :development, :test do
Expand Down
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ GEM
zeitwerk (~> 2.3)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
anyway_config (2.6.4)
ruby-next-core (~> 1.0)
ast (2.4.2)
awesome_print (1.9.2)
base32 (0.3.4)
Expand Down Expand Up @@ -173,6 +175,8 @@ GEM
prime (0.1.2)
forwardable
singleton
prometheus-client (4.2.3)
base64
pry (0.14.0)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -266,6 +270,7 @@ GEM
rubocop (~> 1.19)
rubocop-thread_safety (0.4.4)
rubocop (>= 0.53.0)
ruby-next-core (1.0.3)
ruby-progressbar (1.11.0)
ruby_parser (3.19.2)
sexp_processor (~> 4.16)
Expand Down Expand Up @@ -319,6 +324,23 @@ GEM
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
yabeda (0.13.1)
anyway_config (>= 1.0, < 3)
concurrent-ruby
dry-initializer
yabeda-prometheus (0.9.1)
prometheus-client (>= 3.0, < 5.0)
rack
yabeda (~> 0.10)
yabeda-puma-plugin (0.7.1)
json
puma
yabeda (~> 0.5)
yabeda-rails (0.9.0)
activesupport
anyway_config (>= 1.3, < 3)
railties
yabeda (~> 0.8)
yard (0.9.35)
zeitwerk (2.7.0)
zstd-ruby (1.5.6.1)
Expand Down Expand Up @@ -380,6 +402,10 @@ DEPENDENCIES
vcr (~> 6.0)
versionist
webmock
yabeda
yabeda-prometheus
yabeda-puma-plugin
yabeda-rails

BUNDLED WITH
1.17.3
38 changes: 38 additions & 0 deletions config/initializers/yabeda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# :nocov:
# frozen_string_literal: true

return unless Settings.dig(:scc, :metrics, :enabled)

# Configure prometheus client
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(
dir: './tmp/prometheus/'
)

# Configure yabeda
Yabeda.configure do
assign_labels = lambda {
default_tag :environment, Rails.env
default_tag :application, 'rmt'
}

group :rails, &assign_labels

group :rails do
counter :started_requests_total,
comment: 'A counter of the total number of HTTP requests rails has started to process.',
tags: %i[controller action format method]
end
end

# Instrument the request from the start
ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |*args|
# Match the same event as Yabeda
event = Yabeda::Rails::Event.new(*args)

Yabeda.rails.started_requests_total.tap do |metric|
labels = event.labels.slice(*metric.tags)

metric.increment(labels, by: 1)
end
end
# :nocov:
5 changes: 5 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rmt/config'
require_relative 'puma/prometheus'

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
Expand Down Expand Up @@ -36,3 +37,7 @@

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

if Settings.dig(:scc, :metrics, :enabled)
configure_prometheus!(self)
end
7 changes: 7 additions & 0 deletions config/puma/prometheus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def configure_prometheus!(puma)
ENV['STARTED_FROM_PUMA'] = '1'

puma.activate_control_app
puma.plugin :yabeda
puma.plugin :yabeda_prometheus
end
3 changes: 3 additions & 0 deletions config/rmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ scc:
username: <%= ENV['SCC_USERNAME'] %>
password: <%= ENV['SCC_PASSWORD'] %>
sync_systems: true
metrics:
enabled: <%= ENV.fetch('RMT_METRICS_ENABLED') { false } %>
job_name: <%= ENV.fetch('PROMETHEUS_JOB_NAME') { 'rmt-webserver' } %>

mirroring:
mirror_src: false
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
get '/v2/_catalog', to: 'registry/registry#catalog'
end

# For bin/rails s users: expose the metrics endpoint via rails instead of puma
if Rails.env.development? && ENV['STARTED_FROM_PUMA'].blank?
Rails.logger.info('Mounting Yabeda in Rails.routes')
mount Yabeda::Prometheus::Exporter, at: '/metrics'
end

if defined?(SccSumaApi::Engine)
mount SccSumaApi::Engine, at: '/api/scc'

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240821114908_change_local_path_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ChangeLocalPathType < ActiveRecord::Migration[6.1]
def up
safety_assured do
change_column :repositories, :local_path, :string, limit: 512
change_column :downloaded_files, :local_path, :string, limit: 512
end
end

def down
change_column :repositories, :local_path, :string
change_column :downloaded_files, :local_path, :string
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_29_103525) do
ActiveRecord::Schema.define(version: 2024_08_21_114908) do

create_table "activations", charset: "utf8", force: :cascade do |t|
t.bigint "service_id", null: false
Expand All @@ -34,7 +34,7 @@
create_table "downloaded_files", charset: "utf8", force: :cascade do |t|
t.string "checksum_type"
t.string "checksum"
t.string "local_path"
t.string "local_path", limit: 512
t.bigint "file_size", unsigned: true
t.index ["checksum_type", "checksum"], name: "index_downloaded_files_on_checksum_type_and_checksum"
t.index ["local_path"], name: "index_downloaded_files_on_local_path", unique: true
Expand Down Expand Up @@ -104,7 +104,7 @@
t.string "auth_token"
t.boolean "installer_updates", default: false, null: false
t.boolean "mirroring_enabled", default: false, null: false
t.string "local_path", null: false
t.string "local_path", limit: 512, null: false
t.datetime "last_mirrored_at"
t.string "friendly_id"
t.index ["external_url"], name: "index_repositories_on_external_url", unique: true
Expand Down
4 changes: 2 additions & 2 deletions engines/registry/app/models/access_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def allowed_paths(system = nil)
}
allowed_non_free_product_classes.each do |non_free_prod_class|
activation_state = SccProxy.scc_check_subscription_expiration(
auth_header, system.login, system.system_token, Rails.logger, system.proxy_byos_mode, non_free_prod_class
)
auth_header, system, non_free_prod_class
)
unless activation_state[:is_active]
Rails.logger.info(
"Access to #{non_free_prod_class} from system #{system.login} denied: #{activation_state[:message]}"
Expand Down
12 changes: 2 additions & 10 deletions engines/registry/spec/app/models/access_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,15 @@
allow(SccProxy).to receive(:scc_check_subscription_expiration)
.with(
header_expected,
system.login,
system.system_token,
Rails.logger,
system.proxy_byos_mode,
system,
'SLES15-SP4-LTSS-X86'
).and_return(scc_response)
end

# rubocop:disable RSpec/ExampleLength
it 'returns no actions allowed' do
expect(SccProxy).to receive(:scc_check_subscription_expiration).with(
header_expected,
system.login,
system.system_token,
Rails.logger,
system.proxy_byos_mode,
system,
'SLES15-SP4-LTSS-X86'
)
yaml_string = access_policy_content
Expand All @@ -250,7 +243,6 @@
}
)
end
# rubocop:enable RSpec/ExampleLength
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include_context 'version header', 3

describe '#activations' do
let(:system) { FactoryBot.create(:system, :with_activated_product) }
let(:system) { FactoryBot.create(:system, :payg, :with_activated_product) }
let(:headers) { auth_header.merge(version_header) }

context 'without valid repository cache' do
Expand Down Expand Up @@ -57,5 +57,68 @@
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end

context 'system is hybrid' do
let(:system) { FactoryBot.create(:system, :hybrid, :with_activated_product) }
let(:plugin_double) { instance_double('InstanceVerification::Providers::Example') }
let(:cache_name) { "repo/cache/127.0.0.1-#{system.login}-#{system.products.first.id}" }
let(:scc_systems_activations_url) { 'https://scc.suse.com/connect/systems/activations' }
let(:body_active) do
{
id: 1,
regcode: '631dc51f',
name: 'Subscription 1',
type: 'FULL',
status: 'ACTIVE',
starts_at: 'null',
expires_at: '2014-03-14T13:10:21.164Z',
system_limit: 6,
systems_count: 1,
service: {
product: {
id: system.activations.first.product.id
}
}
}
end

before do
allow(InstanceVerification::Providers::Example).to receive(:new).and_return(plugin_double)

allow(plugin_double).to(
receive(:instance_valid?).and_return(true)
)
allow(File).to receive(:join).and_call_original
allow(InstanceVerification).to receive(:update_cache)
allow(ZypperAuth).to receive(:verify_instance).and_call_original
stub_request(:get, scc_systems_activations_url).to_return(status: 200, body: [body_active].to_json, headers: {})
headers['X-Instance-Data'] = 'IMDS'
end

context 'no registry' do
it 'refreshes registry cache key only' do
FileUtils.mkdir_p('repo/cache')
expect(InstanceVerification).to receive(:update_cache).with('127.0.0.1', system.login, system.activations.first.product.id)
get '/connect/systems/activations', headers: headers
FileUtils.rm_rf('repo/cache')
data = JSON.parse(response.body)
expect(SccProxy).not_to receive(:product_path_access)
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end

context 'registry' do
it 'refreshes registry cache key only' do
FileUtils.mkdir_p('repo/cache')
FileUtils.touch(cache_name)
expect(InstanceVerification).to receive(:update_cache).with('127.0.0.1', system.login, nil, registry: true)
get '/connect/systems/activations', headers: headers
FileUtils.rm_rf('repo/cache')
data = JSON.parse(response.body)
expect(SccProxy).not_to receive(:product_path_access)
expect(data[0]['service']['url']).to match(%r{^plugin:/susecloud})
end
end
end
end
end
Loading

0 comments on commit 3b5c2e0

Please sign in to comment.