2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-12-19 22:12:23 -06:00
|
|
|
class GlobalSetting
|
2014-01-01 17:46:09 -06:00
|
|
|
def self.register(key, default)
|
|
|
|
define_singleton_method(key) { provider.lookup(key, default) }
|
|
|
|
end
|
|
|
|
|
2023-01-20 12:52:49 -06:00
|
|
|
VALID_SECRET_KEY ||= /\A[0-9a-f]{128}\z/
|
2017-01-31 16:21:37 -06:00
|
|
|
# this is named SECRET_TOKEN as opposed to SECRET_KEY_BASE
|
|
|
|
# for legacy reasons
|
2017-06-22 05:55:58 -05:00
|
|
|
REDIS_SECRET_KEY ||= "SECRET_TOKEN"
|
2017-01-31 16:21:37 -06:00
|
|
|
|
2017-06-22 05:55:58 -05:00
|
|
|
REDIS_VALIDATE_SECONDS ||= 30
|
2017-03-13 09:19:02 -05:00
|
|
|
|
2017-01-31 16:21:37 -06:00
|
|
|
# In Rails secret_key_base is used to encrypt the cookie store
|
|
|
|
# the cookie store contains session data
|
|
|
|
# Discourse also uses this secret key to digest user auth tokens
|
|
|
|
# This method will
|
|
|
|
# - use existing token if already set in ENV or discourse.conf
|
|
|
|
# - generate a token on the fly if needed and cache in redis
|
|
|
|
# - enforce rules about token format falling back to redis if needed
|
|
|
|
def self.safe_secret_key_base
|
2017-03-13 09:19:02 -05:00
|
|
|
if @safe_secret_key_base && @token_in_redis &&
|
|
|
|
(@token_last_validated + REDIS_VALIDATE_SECONDS) < Time.now
|
2017-03-13 09:47:43 -05:00
|
|
|
@token_last_validated = Time.now
|
2019-12-03 03:05:53 -06:00
|
|
|
token = Discourse.redis.without_namespace.get(REDIS_SECRET_KEY)
|
|
|
|
Discourse.redis.without_namespace.set(REDIS_SECRET_KEY, @safe_secret_key_base) if token.nil?
|
2017-03-13 09:19:02 -05:00
|
|
|
end
|
|
|
|
|
2017-01-31 16:21:37 -06:00
|
|
|
@safe_secret_key_base ||=
|
|
|
|
begin
|
|
|
|
token = secret_key_base
|
|
|
|
if token.blank? || token !~ VALID_SECRET_KEY
|
2017-03-13 09:19:02 -05:00
|
|
|
@token_in_redis = true
|
|
|
|
@token_last_validated = Time.now
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2019-12-03 03:05:53 -06:00
|
|
|
token = Discourse.redis.without_namespace.get(REDIS_SECRET_KEY)
|
2017-01-31 16:21:37 -06:00
|
|
|
unless token && token =~ VALID_SECRET_KEY
|
|
|
|
token = SecureRandom.hex(64)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.without_namespace.set(REDIS_SECRET_KEY, token)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2017-01-31 16:21:37 -06:00
|
|
|
end
|
|
|
|
if !secret_key_base.blank? && token != secret_key_base
|
|
|
|
STDERR.puts "WARNING: DISCOURSE_SECRET_KEY_BASE is invalid, it was re-generated"
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
|
|
|
token
|
2017-01-31 16:21:37 -06:00
|
|
|
end
|
2017-08-02 00:32:01 -05:00
|
|
|
rescue Redis::CommandError => e
|
|
|
|
@safe_secret_key_base = SecureRandom.hex(64) if e.message =~ /READONLY/
|
2017-01-31 16:21:37 -06:00
|
|
|
end
|
|
|
|
|
2013-12-19 23:38:51 -06:00
|
|
|
def self.load_defaults
|
2013-12-20 00:41:12 -06:00
|
|
|
default_provider =
|
|
|
|
FileProvider.from(File.expand_path("../../../config/discourse_defaults.conf", __FILE__))
|
2014-01-01 17:46:09 -06:00
|
|
|
default_provider
|
|
|
|
.keys
|
|
|
|
.concat(@provider.keys)
|
|
|
|
.uniq
|
|
|
|
.each do |key|
|
|
|
|
default = default_provider.lookup(key, nil)
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2017-03-09 17:00:55 -06:00
|
|
|
instance_variable_set("@#{key}_cache", nil)
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2014-01-01 17:46:09 -06:00
|
|
|
define_singleton_method(key) do
|
2017-03-09 17:00:55 -06:00
|
|
|
val = instance_variable_get("@#{key}_cache")
|
2023-02-16 03:40:11 -06:00
|
|
|
if val.nil?
|
2017-03-09 17:00:55 -06:00
|
|
|
val = provider.lookup(key, default)
|
|
|
|
val = :missing if val.nil?
|
|
|
|
instance_variable_set("@#{key}_cache", val)
|
|
|
|
end
|
2023-02-16 03:40:11 -06:00
|
|
|
val == :missing ? nil : val
|
2017-03-09 17:00:55 -06:00
|
|
|
end
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-12 21:58:27 -05:00
|
|
|
def self.skip_db=(v)
|
|
|
|
@skip_db = v
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.skip_db?
|
|
|
|
@skip_db
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.skip_redis=(v)
|
|
|
|
@skip_redis = v
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.skip_redis?
|
|
|
|
@skip_redis
|
|
|
|
end
|
|
|
|
|
2023-12-06 06:19:09 -06:00
|
|
|
# rubocop:disable Lint/BooleanSymbol
|
2017-10-06 00:20:01 -05:00
|
|
|
def self.use_s3?
|
|
|
|
(
|
|
|
|
@use_s3 ||=
|
|
|
|
begin
|
|
|
|
if s3_bucket && s3_region &&
|
|
|
|
(s3_use_iam_profile || (s3_access_key_id && s3_secret_access_key))
|
|
|
|
:true
|
2023-01-09 06:20:10 -06:00
|
|
|
else
|
2017-10-06 00:20:01 -05:00
|
|
|
:false
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2017-10-06 00:20:01 -05:00
|
|
|
end
|
|
|
|
) == :true
|
|
|
|
end
|
2023-12-06 06:19:09 -06:00
|
|
|
# rubocop:enable Lint/BooleanSymbol
|
2017-10-06 00:20:01 -05:00
|
|
|
|
2018-05-16 15:10:15 -05:00
|
|
|
def self.s3_bucket_name
|
|
|
|
@s3_bucket_name ||= s3_bucket.downcase.split("/")[0]
|
|
|
|
end
|
|
|
|
|
2017-10-06 00:20:01 -05:00
|
|
|
# for testing
|
|
|
|
def self.reset_s3_cache!
|
|
|
|
@use_s3 = nil
|
|
|
|
end
|
|
|
|
|
2020-09-17 04:11:57 -05:00
|
|
|
def self.cdn_hostnames
|
|
|
|
hostnames = []
|
|
|
|
hostnames << URI.parse(cdn_url).host if cdn_url.present?
|
|
|
|
hostnames << cdn_origin_hostname if cdn_origin_hostname.present?
|
|
|
|
hostnames
|
|
|
|
end
|
|
|
|
|
2014-01-13 23:59:55 -06:00
|
|
|
def self.database_config
|
|
|
|
hash = { "adapter" => "postgresql" }
|
2018-03-08 20:22:29 -06:00
|
|
|
|
|
|
|
%w[
|
|
|
|
pool
|
|
|
|
connect_timeout
|
|
|
|
socket
|
|
|
|
host
|
|
|
|
backup_host
|
|
|
|
port
|
|
|
|
backup_port
|
|
|
|
username
|
|
|
|
password
|
|
|
|
replica_host
|
|
|
|
replica_port
|
|
|
|
].each do |s|
|
2019-05-06 20:27:05 -05:00
|
|
|
if val = self.public_send("db_#{s}")
|
2014-01-13 23:59:55 -06:00
|
|
|
hash[s] = val
|
|
|
|
end
|
|
|
|
end
|
2016-01-25 00:27:59 -06:00
|
|
|
|
2015-07-23 00:22:54 -05:00
|
|
|
hostnames = [hostname]
|
2015-07-23 00:33:38 -05:00
|
|
|
hostnames << backup_hostname if backup_hostname.present?
|
2015-07-23 00:22:54 -05:00
|
|
|
|
2018-03-27 14:20:22 -05:00
|
|
|
hostnames << URI.parse(cdn_url).host if cdn_url.present?
|
2020-05-12 10:34:12 -05:00
|
|
|
hostnames << cdn_origin_hostname if cdn_origin_hostname.present?
|
2018-03-27 14:20:22 -05:00
|
|
|
|
2015-07-23 00:22:54 -05:00
|
|
|
hash["host_names"] = hostnames
|
2014-01-13 23:59:55 -06:00
|
|
|
hash["database"] = db_name
|
2015-02-17 18:16:53 -06:00
|
|
|
hash["prepared_statements"] = !!self.db_prepared_statements
|
2020-05-21 01:32:41 -05:00
|
|
|
hash["idle_timeout"] = connection_reaper_age if connection_reaper_age.present?
|
|
|
|
hash["reaping_frequency"] = connection_reaper_interval if connection_reaper_interval.present?
|
2020-06-15 01:33:30 -05:00
|
|
|
hash["advisory_locks"] = !!self.db_advisory_locks
|
2015-02-17 18:16:53 -06:00
|
|
|
|
2021-04-14 10:34:28 -05:00
|
|
|
db_variables = provider.keys.filter { |k| k.to_s.starts_with? "db_variables_" }
|
|
|
|
if db_variables.length > 0
|
|
|
|
hash["variables"] = {}
|
|
|
|
db_variables.each do |k|
|
|
|
|
hash["variables"][k.slice(("db_variables_".length)..)] = self.public_send(k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-13 23:59:55 -06:00
|
|
|
{ "production" => hash }
|
|
|
|
end
|
|
|
|
|
2017-02-01 23:47:57 -06:00
|
|
|
# For testing purposes
|
|
|
|
def self.reset_redis_config!
|
|
|
|
@config = nil
|
2019-05-28 00:52:43 -05:00
|
|
|
@message_bus_config = nil
|
2017-02-01 23:47:57 -06:00
|
|
|
end
|
|
|
|
|
2020-12-22 20:14:19 -06:00
|
|
|
def self.get_redis_replica_host
|
|
|
|
return redis_replica_host if redis_replica_host.present?
|
|
|
|
redis_slave_host if respond_to?(:redis_slave_host) && redis_slave_host.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_redis_replica_port
|
|
|
|
return redis_replica_port if redis_replica_port.present?
|
|
|
|
redis_slave_port if respond_to?(:redis_slave_port) && redis_slave_port.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_message_bus_redis_replica_host
|
|
|
|
return message_bus_redis_replica_host if message_bus_redis_replica_host.present?
|
|
|
|
if respond_to?(:message_bus_redis_slave_host) && message_bus_redis_slave_host.present?
|
|
|
|
message_bus_redis_slave_host
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2020-12-22 20:14:19 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_message_bus_redis_replica_port
|
|
|
|
return message_bus_redis_replica_port if message_bus_redis_replica_port.present?
|
|
|
|
if respond_to?(:message_bus_redis_slave_port) && message_bus_redis_slave_port.present?
|
|
|
|
message_bus_redis_slave_port
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2020-12-22 20:14:19 -06:00
|
|
|
end
|
|
|
|
|
2015-06-25 01:51:48 -05:00
|
|
|
def self.redis_config
|
|
|
|
@config ||=
|
|
|
|
begin
|
|
|
|
c = {}
|
|
|
|
c[:host] = redis_host if redis_host
|
|
|
|
c[:port] = redis_port if redis_port
|
2017-02-01 23:47:57 -06:00
|
|
|
|
2020-12-22 20:14:19 -06:00
|
|
|
if get_redis_replica_host && get_redis_replica_port && defined?(RailsFailover)
|
|
|
|
c[:replica_host] = get_redis_replica_host
|
|
|
|
c[:replica_port] = get_redis_replica_port
|
2020-06-11 00:45:46 -05:00
|
|
|
c[:connector] = RailsFailover::Redis::Connector
|
2017-02-01 23:47:57 -06:00
|
|
|
end
|
|
|
|
|
2015-06-30 21:19:02 -05:00
|
|
|
c[:password] = redis_password if redis_password.present?
|
2015-06-25 01:51:48 -05:00
|
|
|
c[:db] = redis_db if redis_db != 0
|
|
|
|
c[:db] = 1 if Rails.env == "test"
|
2019-01-03 22:08:22 -06:00
|
|
|
c[:id] = nil if redis_skip_client_commands
|
2020-03-05 16:21:38 -06:00
|
|
|
c[:ssl] = true if redis_use_ssl
|
2017-02-01 23:47:57 -06:00
|
|
|
|
2015-06-25 01:51:48 -05:00
|
|
|
c.freeze
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-28 00:52:43 -05:00
|
|
|
def self.message_bus_redis_config
|
|
|
|
return redis_config unless message_bus_redis_enabled
|
|
|
|
@message_bus_config ||=
|
|
|
|
begin
|
|
|
|
c = {}
|
|
|
|
c[:host] = message_bus_redis_host if message_bus_redis_host
|
|
|
|
c[:port] = message_bus_redis_port if message_bus_redis_port
|
|
|
|
|
2020-12-22 20:14:19 -06:00
|
|
|
if get_message_bus_redis_replica_host && get_message_bus_redis_replica_port
|
|
|
|
c[:replica_host] = get_message_bus_redis_replica_host
|
|
|
|
c[:replica_port] = get_message_bus_redis_replica_port
|
2020-06-11 00:45:46 -05:00
|
|
|
c[:connector] = RailsFailover::Redis::Connector
|
2019-05-28 00:52:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
c[:password] = message_bus_redis_password if message_bus_redis_password.present?
|
|
|
|
c[:db] = message_bus_redis_db if message_bus_redis_db != 0
|
|
|
|
c[:db] = 1 if Rails.env == "test"
|
|
|
|
c[:id] = nil if message_bus_redis_skip_client_commands
|
2020-03-05 16:21:38 -06:00
|
|
|
c[:ssl] = true if redis_use_ssl
|
2019-05-28 00:52:43 -05:00
|
|
|
|
|
|
|
c.freeze
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 15:38:24 -06:00
|
|
|
def self.add_default(name, default)
|
|
|
|
define_singleton_method(name) { default } unless self.respond_to? name
|
|
|
|
end
|
|
|
|
|
2024-03-27 21:18:19 -05:00
|
|
|
def self.smtp_settings
|
|
|
|
if GlobalSetting.smtp_address
|
|
|
|
settings = {
|
|
|
|
address: GlobalSetting.smtp_address,
|
|
|
|
port: GlobalSetting.smtp_port,
|
|
|
|
domain: GlobalSetting.smtp_domain,
|
|
|
|
user_name: GlobalSetting.smtp_user_name,
|
|
|
|
password: GlobalSetting.smtp_password,
|
|
|
|
enable_starttls_auto: GlobalSetting.smtp_enable_start_tls,
|
|
|
|
open_timeout: GlobalSetting.smtp_open_timeout,
|
|
|
|
read_timeout: GlobalSetting.smtp_read_timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings[:password] || settings[:user_name]
|
|
|
|
settings[:authentication] = GlobalSetting.smtp_authentication
|
|
|
|
end
|
|
|
|
|
|
|
|
settings[
|
|
|
|
:openssl_verify_mode
|
|
|
|
] = GlobalSetting.smtp_openssl_verify_mode if GlobalSetting.smtp_openssl_verify_mode
|
|
|
|
|
|
|
|
settings[:tls] = true if GlobalSetting.smtp_force_tls
|
|
|
|
settings.compact
|
|
|
|
settings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 22:12:23 -06:00
|
|
|
class BaseProvider
|
2013-12-19 23:17:21 -06:00
|
|
|
def self.coerce(setting)
|
|
|
|
return setting == "true" if setting == "true" || setting == "false"
|
2023-01-20 12:52:49 -06:00
|
|
|
return $1.to_i if setting.to_s.strip =~ /\A([0-9]+)\z/
|
2013-12-19 23:17:21 -06:00
|
|
|
setting
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(current, default)
|
|
|
|
BaseProvider.coerce(
|
|
|
|
if current.present?
|
|
|
|
current
|
|
|
|
else
|
|
|
|
default.present? ? default : nil
|
|
|
|
end,
|
|
|
|
)
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 23:17:21 -06:00
|
|
|
class FileProvider < BaseProvider
|
2013-12-19 23:38:51 -06:00
|
|
|
attr_reader :data
|
2013-12-19 23:17:21 -06:00
|
|
|
def self.from(file)
|
2022-01-05 11:45:08 -06:00
|
|
|
parse(file) if File.exist?(file)
|
2013-12-19 23:17:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
@data = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
2014-02-14 15:35:30 -06:00
|
|
|
ERB
|
|
|
|
.new(File.read(@file))
|
|
|
|
.result()
|
|
|
|
.split("\n")
|
|
|
|
.each do |line|
|
2023-01-20 12:52:49 -06:00
|
|
|
if line =~ /\A\s*([a-z_]+[a-z0-9_]*)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
|
2013-12-19 23:17:21 -06:00
|
|
|
@data[$1.strip.to_sym] = ($4 || $3 || $2).strip
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2013-12-19 23:17:21 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lookup(key, default)
|
2013-12-20 00:09:51 -06:00
|
|
|
var = @data[key]
|
|
|
|
resolve(var, var.nil? ? default : "")
|
2013-12-19 23:17:21 -06:00
|
|
|
end
|
|
|
|
|
2014-01-01 17:46:09 -06:00
|
|
|
def keys
|
|
|
|
@data.keys
|
|
|
|
end
|
|
|
|
|
2013-12-19 23:17:21 -06:00
|
|
|
def self.parse(file)
|
|
|
|
provider = self.new(file)
|
|
|
|
provider.read
|
|
|
|
provider
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|
2014-10-03 22:07:20 -05:00
|
|
|
|
|
|
|
private_class_method :parse
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|
|
|
|
|
2013-12-19 23:17:21 -06:00
|
|
|
class EnvProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
2019-05-02 17:17:27 -05:00
|
|
|
var = ENV["DISCOURSE_" + key.to_s.upcase]
|
2013-12-20 00:07:08 -06:00
|
|
|
resolve(var, var.nil? ? default : nil)
|
2013-12-19 23:17:21 -06:00
|
|
|
end
|
2014-01-01 17:46:09 -06:00
|
|
|
|
|
|
|
def keys
|
2023-01-20 12:52:49 -06:00
|
|
|
ENV.keys.select { |k| k =~ /\ADISCOURSE_/ }.map { |k| k[10..-1].downcase.to_sym }
|
2014-01-01 17:46:09 -06:00
|
|
|
end
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|
|
|
|
|
2015-03-08 20:21:41 -05:00
|
|
|
class BlankProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
2019-04-01 03:27:49 -05:00
|
|
|
if key == :redis_port
|
|
|
|
return ENV["DISCOURSE_REDIS_PORT"] if ENV["DISCOURSE_REDIS_PORT"]
|
|
|
|
end
|
2015-03-08 20:21:41 -05:00
|
|
|
default
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 22:12:23 -06:00
|
|
|
class << self
|
|
|
|
attr_accessor :provider
|
|
|
|
end
|
|
|
|
|
2017-01-09 16:10:14 -06:00
|
|
|
def self.configure!
|
|
|
|
if Rails.env == "test"
|
|
|
|
@provider = BlankProvider.new
|
|
|
|
else
|
|
|
|
@provider =
|
|
|
|
FileProvider.from(File.expand_path("../../../config/discourse.conf", __FILE__)) ||
|
|
|
|
EnvProvider.new
|
|
|
|
end
|
2015-03-08 20:21:41 -05:00
|
|
|
end
|
2014-01-01 17:46:09 -06:00
|
|
|
|
2022-01-11 06:30:22 -06:00
|
|
|
def self.load_plugins?
|
|
|
|
if ENV["LOAD_PLUGINS"] == "1"
|
|
|
|
true
|
|
|
|
elsif ENV["LOAD_PLUGINS"] == "0"
|
|
|
|
false
|
|
|
|
elsif Rails.env.test?
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2013-12-19 22:12:23 -06:00
|
|
|
end
|