From 3a828bd1fbb2e900aa0ea74350f1b818007e48c9 Mon Sep 17 00:00:00 2001 From: Martin Emde Date: Tue, 3 Dec 2024 19:46:20 -0800 Subject: [PATCH] Reduce the spread of application config across the app --- app/avo/actions/create_user.rb | 2 +- app/models/gem_download.rb | 2 +- config/application.rb | 5 +++-- config/deploy/web.yaml.erb | 4 ++++ config/initializers/avo.rb | 4 ++-- config/initializers/content_security_policy.rb | 2 +- config/initializers/datadog.rb | 2 +- config/initializers/rack_attack.rb | 2 +- config/initializers/semantic_logger.rb | 2 +- config/initializers/statsd.rb | 2 +- test/helpers/es_helper.rb | 2 +- test/models/deletion_test.rb | 3 +-- test/models/pusher_test.rb | 4 ++-- test/test_helper.rb | 2 +- 14 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/avo/actions/create_user.rb b/app/avo/actions/create_user.rb index 80ec4ed66e5..791c0a25ed6 100644 --- a/app/avo/actions/create_user.rb +++ b/app/avo/actions/create_user.rb @@ -1,7 +1,7 @@ class Avo::Actions::CreateUser < Avo::Actions::ApplicationAction self.name = "Create User" self.visible = lambda { - current_user.team_member?("rubygems-org") && view == :index && !Rails.env.production? + current_user.team_member?("rubygems-org") && view == :index && !Clearance.configuration.allow_sign_up? } self.standalone = true diff --git a/app/models/gem_download.rb b/app/models/gem_download.rb index 8462392ec01..237f3e98441 100644 --- a/app/models/gem_download.rb +++ b/app/models/gem_download.rb @@ -135,7 +135,7 @@ def downloads_by_gem(rubygem_ids) end def update_query(id, downloads, version_downloads) - { update: { _index: "rubygems-#{Rails.env}", + { update: { _index: Gemcutter::SEARCH_INDEX_NAME, _id: id, data: { doc: { downloads: downloads, version_downloads: version_downloads } } } } end diff --git a/config/application.rb b/config/application.rb index 1328ecd5bd4..36ce09432d6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -84,11 +84,12 @@ def self.config Rails.application.config.rubygems end + DEPLOYMENT_ENV_NAME = Rails.env.to_s.freeze DEFAULT_PAGE = 1 DEFAULT_PAGINATION = 20 EMAIL_TOKEN_EXPIRES_AFTER = 3.hours HOST = config["host"].freeze - HOST_DISPLAY = Rails.env.production? || Rails.env.development? || Rails.env.test? ? "RubyGems.org" : "RubyGems.org #{Rails.env}" + HOST_DISPLAY = ENV.fetch("HOST_DISPLAY") { Rails.env.production? || Rails.env.development? || Rails.env.test? ? "RubyGems.org" : "RubyGems.org #{Rails.env}" } NEWS_DAYS_LIMIT = 7.days NEWS_MAX_PAGES = 10 NEWS_PER_PAGE = 10 @@ -98,7 +99,7 @@ def self.config POPULAR_DAYS_LIMIT = 70.days PROTOCOL = config["protocol"] REMEMBER_FOR = 2.weeks - SEARCH_INDEX_NAME = "rubygems-#{Rails.env}".freeze + SEARCH_INDEX_NAME = ENV.fetch("SEARCH_INDEX_NAME", "rubygems-#{Rails.env}".freeze) SEARCH_NUM_REPLICAS = ENV.fetch("SEARCH_NUM_REPLICAS", 1).to_i SEARCH_MAX_PAGES = 100 # Limit max page as ES result window is upper bounded by 10_000 records STATS_MAX_PAGES = 10 diff --git a/config/deploy/web.yaml.erb b/config/deploy/web.yaml.erb index c9e4f42feef..4fa0940409b 100644 --- a/config/deploy/web.yaml.erb +++ b/config/deploy/web.yaml.erb @@ -67,6 +67,8 @@ spec: value: "<%= environment == 'production' ? 8 : 2 %>" - name: RAILS_MAX_THREADS value: "5" + - name: HOST_DISPLAY + value: "RubyGems.org<%= environment == 'production' ? '' : " #{environment.capitalize}" %>" - name: DD_AGENT_HOST valueFrom: fieldRef: @@ -139,6 +141,8 @@ spec: key: elasticsearch_url - name: SEARCH_NUM_REPLICAS value: "<%= environment == 'production' ? 2 : 1 %>" + - name: SEARCH_INDEX_NAME + value: "rubygems-<%= environment %>" - name: MEMCACHED_ENDPOINT valueFrom: secretKeyRef: diff --git a/config/initializers/avo.rb b/config/initializers/avo.rb index a7c018b0454..97f7fb53a8e 100644 --- a/config/initializers/avo.rb +++ b/config/initializers/avo.rb @@ -18,7 +18,7 @@ config.current_user_method = :admin_user config.authenticate_with do if !Rails.env.local? && !(Avo.license.valid? && Avo.license.advanced?) - raise "Avo::Pro is missing in #{Rails.env}." \ + raise "Avo::Pro is missing on #{Gemcutter::HOST} (env:#{Rails.env})." \ "\nRails.groups=#{Rails.groups.inspect}" \ "\nAvo.license=#{Avo.license.inspect}" \ "\nAvo.configuration.license=#{Avo.configuration.license.inspect}" @@ -66,7 +66,7 @@ # config.cache_resource_filters = ->(current_user:, resource:) { current_user.cache_resource_filters?} ## == Customization == - config.app_name = "RubyGems.org (#{Rails.env})" + config.app_name = Gemcutter::HOST_DISPLAY # config.timezone = 'UTC' # config.currency = 'USD' # config.hide_layout_when_printing = false diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb index df1cf833606..1bf848e6667 100644 --- a/config/initializers/content_security_policy.rb +++ b/config/initializers/content_security_policy.rb @@ -35,7 +35,7 @@ ddtags: { service: "rubygems.org", version: AppRevision.version, - env: Rails.env, + env: Gemcutter::DEPLOYMENT_ENV_NAME, trace_id: Datadog::Tracing.correlation&.trace_id, "gemcutter.user.id": (current_user.id if respond_to?(:signed_in?) && signed_in?) }.compact.map { |k, v| "#{k}:#{v}" }.join(',') diff --git a/config/initializers/datadog.rb b/config/initializers/datadog.rb index 9681e9c18aa..8e39df7e8c6 100644 --- a/config/initializers/datadog.rb +++ b/config/initializers/datadog.rb @@ -7,7 +7,7 @@ c.service = "rubygems.org" c.version = AppRevision.version - c.env = Rails.env + c.env = Gemcutter::DEPLOYMENT_ENV_NAME # Enabling datadog functionality diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index ccf01536ad4..2d7078c74cc 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -263,7 +263,7 @@ def self.api_key_owner_id(req) event = { timestamp: ::Time.now.utc, - env: Rails.env, + env: Gemcutter::DEPLOYMENT_ENV_NAME, message: "[429] #{method} #{request.env['REQUEST_PATH']}", http: { request_id: request.env["action_dispatch.request_id"], diff --git a/config/initializers/semantic_logger.rb b/config/initializers/semantic_logger.rb index 52e3cf17a36..7f67ddf4464 100644 --- a/config/initializers/semantic_logger.rb +++ b/config/initializers/semantic_logger.rb @@ -4,7 +4,7 @@ def append_info_to_payload(payload) payload.merge!( timestamp: Time.now.utc, - env: Rails.env, + env: Gemcutter::DEPLOYMENT_ENV_NAME, network: { client: { ip: request.ip diff --git a/config/initializers/statsd.rb b/config/initializers/statsd.rb index a3721b165bb..a956c36c8dc 100644 --- a/config/initializers/statsd.rb +++ b/config/initializers/statsd.rb @@ -1,7 +1,7 @@ StatsD.logger = SemanticLogger[StatsD] StatsD.singleton_client = StatsD.singleton_client.clone_with_options( - default_tags: ["env:#{Rails.env}"] + default_tags: ["env:#{Gemcutter::DEPLOYMENT_ENV_NAME}"] ) ActiveSupport::Notifications.subscribe(/process_action.action_controller/) do |event| diff --git a/test/helpers/es_helper.rb b/test/helpers/es_helper.rb index 47552fd0548..396ccb7f67d 100644 --- a/test/helpers/es_helper.rb +++ b/test/helpers/es_helper.rb @@ -18,6 +18,6 @@ def es_version_downloads(id) def get_response(id) Rubygem.searchkick_index.refresh - Searchkick.client.get index: "rubygems-#{Rails.env}", id: id + Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: id end end diff --git a/test/models/deletion_test.rb b/test/models/deletion_test.rb index 032f82c923c..81c5363d88f 100644 --- a/test/models/deletion_test.rb +++ b/test/models/deletion_test.rb @@ -160,8 +160,7 @@ class DeletionTest < ActiveSupport::TestCase perform_enqueued_jobs - response = Searchkick.client.get index: "rubygems-#{Rails.env}", - id: @version.rubygem_id + response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: @version.rubygem_id assert response["_source"]["yanked"] end diff --git a/test/models/pusher_test.rb b/test/models/pusher_test.rb index 14fd53443cd..67b8d25c0f8 100644 --- a/test/models/pusher_test.rb +++ b/test/models/pusher_test.rb @@ -627,7 +627,7 @@ def two_cert_chain(signing_key:, root_not_before: Time.current, cert_not_before: should "create rubygem index" do @rubygem.update_column("updated_at", Date.new(2016, 07, 04)) perform_enqueued_jobs only: ReindexRubygemJob - response = Searchkick.client.get index: "rubygems-#{Rails.env}", + response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: @rubygem.id expected_response = { "name" => "gemsgemsgems", @@ -733,7 +733,7 @@ def two_cert_chain(signing_key:, root_not_before: Time.current, cert_not_before: should "update rubygem index" do perform_enqueued_jobs only: ReindexRubygemJob - response = Searchkick.client.get index: "rubygems-#{Rails.env}", + response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: @rubygem.id assert_equal "new summary", response["_source"]["summary"] diff --git a/test/test_helper.rb b/test/test_helper.rb index a1da1d4b86f..bc25ae046c5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -117,7 +117,7 @@ def requires_avo_pro return if Avo.configuration.license == "advanced" && defined?(Avo::Pro) if ActiveRecord::Type::Boolean.new.cast(ENV["REQUIRE_AVO_PRO"]) - raise "REQUIRE_AVO_PRO is set but Avo::Pro is missing in #{Rails.env}." \ + raise "REQUIRE_AVO_PRO is set but Avo::Pro is missing on #{Gemcutter::HOST} (env:#{Rails.env})." \ "\nRAILS_GROUPS=#{ENV['RAILS_GROUPS'].inspect}\nAvo.license=#{Avo.license.inspect}" end skip "avo pro is not present but was required for this test"