Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
current_application_settings belongs on Gitlab::CurrentSettings
The initializers including this were doing so at the top level, so every object
loaded after them had a `current_application_settings` method. However, if
someone had rack-attack enabled (which was loaded before these initializers), it
would try to load the API, and fail, because `Gitlab::CurrentSettings` didn't
have that method.

To fix this:

1. Don't include `Gitlab::CurrentSettings` at the top level. We do not need
   `Object.new.current_application_settings` to work.
2. Make `Gitlab::CurrentSettings` explicitly `extend self`, as we already use it
   like that in several places.
3. Change the initializers to use that new form.
  • Loading branch information
smcgivern committed Aug 31, 2017
1 parent bf51ab8 commit 5883ce9
Show file tree
Hide file tree
Showing 46 changed files with 84 additions and 24 deletions.
@@ -1,5 +1,8 @@
module RequiresWhitelistedMonitoringClient
extend ActiveSupport::Concern

include Gitlab::CurrentSettings

included do
before_action :validate_ip_whitelisted_or_valid_token!
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Expand Up @@ -202,7 +202,7 @@ def promo_url
end

def support_url
current_application_settings.help_page_support_url.presence || promo_url + '/getting-help/'
Gitlab::CurrentSettings.current_application_settings.help_page_support_url.presence || promo_url + '/getting-help/'
end

def page_filter_path(options = {})
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_settings_helper.rb
@@ -1,5 +1,8 @@
module ApplicationSettingsHelper
extend self

include Gitlab::CurrentSettings

delegate :gravatar_enabled?,
:signup_enabled?,
:password_authentication_enabled?,
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/auth_helper.rb
@@ -1,4 +1,6 @@
module AuthHelper
include Gitlab::CurrentSettings

PROVIDERS_WITH_ICONS = %w(twitter github gitlab bitbucket google_oauth2 facebook azure_oauth2 authentiq).freeze
FORM_BASED_PROVIDERS = [/\Aldap/, 'crowd'].freeze

Expand Down
2 changes: 2 additions & 0 deletions app/helpers/projects_helper.rb
@@ -1,4 +1,6 @@
module ProjectsHelper
include Gitlab::CurrentSettings

def link_to_project(project)
link_to [project.namespace.becomes(Namespace), project], title: h(project.name) do
title = content_tag(:span, project.name, class: 'project-name')
Expand Down
4 changes: 3 additions & 1 deletion app/mailers/base_mailer.rb
@@ -1,11 +1,13 @@
class BaseMailer < ActionMailer::Base
include Gitlab::CurrentSettings

around_action :render_with_default_locale

helper ApplicationHelper
helper MarkupHelper

attr_accessor :current_user
helper_method :current_user, :can?
helper_method :current_user, :can?, :current_application_settings

default from: proc { default_sender_address.format }
default reply_to: proc { default_reply_to_address.format }
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/spammable.rb
Expand Up @@ -28,7 +28,7 @@ def submittable_as_spam_by?(current_user)

def submittable_as_spam?
if user_agent_detail
user_agent_detail.submittable? && current_application_settings.akismet_enabled
user_agent_detail.submittable? && Gitlab::CurrentSettings.current_application_settings.akismet_enabled
else
false
end
Expand Down
1 change: 1 addition & 0 deletions app/models/project.rb
Expand Up @@ -19,6 +19,7 @@ class Project < ActiveRecord::Base
include Routable

extend Gitlab::ConfigHelper
extend Gitlab::CurrentSettings

BoardLimitExceeded = Class.new(StandardError)

Expand Down
2 changes: 2 additions & 0 deletions app/models/protected_branch.rb
Expand Up @@ -2,6 +2,8 @@ class ProtectedBranch < ActiveRecord::Base
include Gitlab::ShellAdapter
include ProtectedRef

extend Gitlab::CurrentSettings

protected_ref_access_levels :merge, :push

# Check if branch name is marked as protected in the system
Expand Down
2 changes: 2 additions & 0 deletions app/models/snippet.rb
Expand Up @@ -10,6 +10,8 @@ class Snippet < ActiveRecord::Base
include Spammable
include Editable

extend Gitlab::CurrentSettings

cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
cache_markdown_field :content
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Expand Up @@ -2,6 +2,7 @@

class User < ActiveRecord::Base
extend Gitlab::ConfigHelper
extend Gitlab::CurrentSettings

include Gitlab::ConfigHelper
include Gitlab::CurrentSettings
Expand Down
4 changes: 1 addition & 3 deletions app/policies/base_policy.rb
@@ -1,8 +1,6 @@
require_dependency 'declarative_policy'

class BasePolicy < DeclarativePolicy::Base
include Gitlab::CurrentSettings

desc "User is an instance admin"
with_options scope: :user, score: 0
condition(:admin) { @user&.admin? }
Expand All @@ -15,6 +13,6 @@ class BasePolicy < DeclarativePolicy::Base

desc "The application is restricted from public visibility"
condition(:restricted_public_level, scope: :global) do
current_application_settings.restricted_visibility_levels.include?(Gitlab::VisibilityLevel::PUBLIC)
Gitlab::CurrentSettings.current_application_settings.restricted_visibility_levels.include?(Gitlab::VisibilityLevel::PUBLIC)
end
end
2 changes: 2 additions & 0 deletions app/services/akismet_service.rb
@@ -1,4 +1,6 @@
class AkismetService
include Gitlab::CurrentSettings

attr_accessor :owner, :text, :options

def initialize(owner, text, options = {})
Expand Down
@@ -1,6 +1,6 @@
module Auth
class ContainerRegistryAuthenticationService < BaseService
include Gitlab::CurrentSettings
extend Gitlab::CurrentSettings

AUDIENCE = 'container_registry'.freeze

Expand Down
2 changes: 2 additions & 0 deletions app/services/projects/update_pages_service.rb
@@ -1,5 +1,7 @@
module Projects
class UpdatePagesService < BaseService
include Gitlab::CurrentSettings

BLOCK_SIZE = 32.kilobytes
MAX_SIZE = 1.terabyte
SITE_PATH = 'public/'.freeze
Expand Down
2 changes: 2 additions & 0 deletions app/services/upload_service.rb
@@ -1,4 +1,6 @@
class UploadService
include Gitlab::CurrentSettings

def initialize(model, file, uploader_class = FileUploader)
@model, @file, @uploader_class = model, file, uploader_class
end
Expand Down
2 changes: 2 additions & 0 deletions app/services/users/build_service.rb
@@ -1,5 +1,7 @@
module Users
class BuildService < BaseService
include Gitlab::CurrentSettings

def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
Expand Down
5 changes: 2 additions & 3 deletions config/initializers/sentry.rb
@@ -1,19 +1,18 @@
# Be sure to restart your server when you modify this file.

require 'gitlab/current_settings'
include Gitlab::CurrentSettings

if Rails.env.production?
# allow it to fail: it may do so when create_from_defaults is executed before migrations are actually done
begin
sentry_enabled = current_application_settings.sentry_enabled
sentry_enabled = Gitlab::CurrentSettings.current_application_settings.sentry_enabled
rescue
sentry_enabled = false
end

if sentry_enabled
Raven.configure do |config|
config.dsn = current_application_settings.sentry_dsn
config.dsn = Gitlab::CurrentSettings.current_application_settings.sentry_dsn
config.release = Gitlab::REVISION

# Sanitize fields based on those sanitized from Rails.
Expand Down
3 changes: 1 addition & 2 deletions config/initializers/session_store.rb
@@ -1,11 +1,10 @@
# Be sure to restart your server when you modify this file.

require 'gitlab/current_settings'
include Gitlab::CurrentSettings

# allow it to fail: it may do so when create_from_defaults is executed before migrations are actually done
begin
Settings.gitlab['session_expire_delay'] = current_application_settings.session_expire_delay || 10080
Settings.gitlab['session_expire_delay'] = Gitlab::CurrentSettings.current_application_settings.session_expire_delay || 10080
rescue
Settings.gitlab['session_expire_delay'] ||= 10080
end
Expand Down
2 changes: 2 additions & 0 deletions lib/api/helpers/runner.rb
@@ -1,6 +1,8 @@
module API
module Helpers
module Runner
include Gitlab::CurrentSettings

JOB_TOKEN_HEADER = 'HTTP_JOB_TOKEN'.freeze
JOB_TOKEN_PARAM = :token
UPDATE_RUNNER_EVERY = 10 * 60
Expand Down
2 changes: 1 addition & 1 deletion lib/email_template_interceptor.rb
@@ -1,6 +1,6 @@
# Read about interceptors in http://guides.rubyonrails.org/action_mailer_basics.html#intercepting-emails
class EmailTemplateInterceptor
include Gitlab::CurrentSettings
extend Gitlab::CurrentSettings

def self.delivering_email(message)
# Remove HTML part if HTML emails are disabled.
Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/asciidoc.rb
Expand Up @@ -6,6 +6,8 @@ module Gitlab
# Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
# the resulting HTML through HTML pipeline filters.
module Asciidoc
extend Gitlab::CurrentSettings

DEFAULT_ADOC_ATTRS = [
'showtitle', 'idprefix=user-content-', 'idseparator=-', 'env=gitlab',
'env-gitlab', 'source-highlighter=html-pipeline', 'icons=font'
Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/auth.rb
Expand Up @@ -19,6 +19,8 @@ module Auth
OPTIONAL_SCOPES = (AVAILABLE_SCOPES + OPENID_SCOPES - DEFAULT_SCOPES).freeze

class << self
include Gitlab::CurrentSettings

def find_for_git_client(login, password, project:, ip:)
raise "Must provide an IP for rate limiting" if ip.nil?

Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/current_settings.rb
@@ -1,5 +1,7 @@
module Gitlab
module CurrentSettings
extend self

def current_application_settings
if RequestStore.active?
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
Expand Down
1 change: 1 addition & 0 deletions lib/gitlab/gon_helper.rb
Expand Up @@ -3,6 +3,7 @@
module Gitlab
module GonHelper
include WebpackHelper
include Gitlab::CurrentSettings

def add_gon_variables
gon.api_version = 'v4'
Expand Down
2 changes: 1 addition & 1 deletion lib/gitlab/metrics/influx_db.rb
@@ -1,7 +1,7 @@
module Gitlab
module Metrics
module InfluxDb
extend Gitlab::CurrentSettings
include Gitlab::CurrentSettings
extend self

MUTEX = Mutex.new
Expand Down
2 changes: 1 addition & 1 deletion lib/gitlab/performance_bar.rb
@@ -1,6 +1,6 @@
module Gitlab
module PerformanceBar
include Gitlab::CurrentSettings
extend Gitlab::CurrentSettings

ALLOWED_USER_IDS_KEY = 'performance_bar_allowed_user_ids:v2'.freeze
EXPIRY_TIME = 5.minutes
Expand Down
2 changes: 1 addition & 1 deletion lib/gitlab/polling_interval.rb
@@ -1,6 +1,6 @@
module Gitlab
class PollingInterval
include Gitlab::CurrentSettings
extend Gitlab::CurrentSettings

HEADER_NAME = 'Poll-Interval'.freeze

Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/protocol_access.rb
@@ -1,5 +1,7 @@
module Gitlab
module ProtocolAccess
extend Gitlab::CurrentSettings

def self.allowed?(protocol)
if protocol == 'web'
true
Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/recaptcha.rb
@@ -1,5 +1,7 @@
module Gitlab
module Recaptcha
extend Gitlab::CurrentSettings

def self.load_configurations!
if current_application_settings.recaptcha_enabled
::Recaptcha.configure do |config|
Expand Down
2 changes: 2 additions & 0 deletions lib/gitlab/sentry.rb
@@ -1,5 +1,7 @@
module Gitlab
module Sentry
extend Gitlab::CurrentSettings

def self.enabled?
Rails.env.production? && current_application_settings.sentry_enabled?
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gitlab/usage_data.rb
@@ -1,8 +1,8 @@
module Gitlab
class UsageData
include Gitlab::CurrentSettings

class << self
include Gitlab::CurrentSettings

def data(force_refresh: false)
Rails.cache.fetch('usage_data', force: force_refresh, expires_in: 2.weeks) { uncached_data }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/import.rake
Expand Up @@ -80,7 +80,7 @@ class GithubImport
end

def visibility_level
@repo['private'] ? Gitlab::VisibilityLevel::PRIVATE : current_application_settings.default_project_visibility
@repo['private'] ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::CurrentSettings.current_application_settings.default_project_visibility
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/version_check_helper_spec.rb
Expand Up @@ -4,15 +4,15 @@
describe '#version_status_badge' do
it 'should return nil if not dev environment and not enabled' do
allow(Rails.env).to receive(:production?) { false }
allow(current_application_settings).to receive(:version_check_enabled) { false }
allow(helper.current_application_settings).to receive(:version_check_enabled) { false }

expect(helper.version_status_badge).to be(nil)
end

context 'when production and enabled' do
before do
allow(Rails.env).to receive(:production?) { true }
allow(current_application_settings).to receive(:version_check_enabled) { true }
allow(helper.current_application_settings).to receive(:version_check_enabled) { true }
allow_any_instance_of(VersionCheck).to receive(:url) { 'https://version.host.com/check.svg?gitlab_info=xxx' }

@image_tag = helper.version_status_badge
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/gitlab/auth/unique_ips_limiter_spec.rb
Expand Up @@ -41,7 +41,7 @@ def operation

context 'allow 2 unique ips' do
before do
current_application_settings.update!(unique_ips_limit_per_user: 2)
Gitlab::CurrentSettings.current_application_settings.update!(unique_ips_limit_per_user: 2)
end

it 'blocks user trying to login from third ip' do
Expand Down
2 changes: 2 additions & 0 deletions spec/support/stub_env.rb
@@ -1,5 +1,7 @@
# Inspired by https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb
module StubENV
include Gitlab::CurrentSettings

def stub_env(key_or_hash, value = nil)
init_stub unless env_stubbed?
if key_or_hash.is_a? Hash
Expand Down
1 change: 1 addition & 0 deletions spec/views/admin/dashboard/index.html.haml_spec.rb
Expand Up @@ -9,6 +9,7 @@
assign(:groups, create_list(:group, 1))

allow(view).to receive(:admin?).and_return(true)
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
end

it "shows version of GitLab Workhorse" do
Expand Down
1 change: 1 addition & 0 deletions spec/views/devise/shared/_signin_box.html.haml_spec.rb
Expand Up @@ -5,6 +5,7 @@
before do
stub_devise
assign(:ldap_servers, [])
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
end

it 'is shown when Crowd is enabled' do
Expand Down
1 change: 1 addition & 0 deletions spec/views/help/index.html.haml_spec.rb
Expand Up @@ -37,5 +37,6 @@ def stub_version(version, revision)
def stub_helpers
allow(view).to receive(:markdown).and_return('')
allow(view).to receive(:version_status_badge).and_return('')
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
end
end
4 changes: 4 additions & 0 deletions spec/views/layouts/_head.html.haml_spec.rb
@@ -1,6 +1,10 @@
require 'spec_helper'

describe 'layouts/_head' do
before do
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
end

it 'escapes HTML-safe strings in page_title' do
stub_helper_with_safe_string(:page_title)

Expand Down
4 changes: 4 additions & 0 deletions spec/views/projects/commits/_commit.html.haml_spec.rb
@@ -1,6 +1,10 @@
require 'spec_helper'

describe 'projects/commits/_commit.html.haml' do
before do
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
end

context 'with a singed commit' do
let(:project) { create(:project, :repository) }
let(:repository) { project.repository }
Expand Down
4 changes: 3 additions & 1 deletion spec/views/projects/edit.html.haml_spec.rb
Expand Up @@ -10,7 +10,9 @@
assign(:project, project)

allow(controller).to receive(:current_user).and_return(user)
allow(view).to receive_messages(current_user: user, can?: true)
allow(view).to receive_messages(current_user: user,
can?: true,
current_application_settings: Gitlab::CurrentSettings.current_application_settings)
end

context 'LFS enabled setting' do
Expand Down

0 comments on commit 5883ce9

Please sign in to comment.