2013-12-20 12:12:23 +08:00
|
|
|
class GlobalSetting
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
def self.register(key, default)
|
|
|
|
define_singleton_method(key) do
|
|
|
|
provider.lookup(key, default)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 18:55:58 +08:00
|
|
|
VALID_SECRET_KEY ||= /^[0-9a-f]{128}$/
|
2017-02-01 06:21:37 +08:00
|
|
|
# this is named SECRET_TOKEN as opposed to SECRET_KEY_BASE
|
|
|
|
# for legacy reasons
|
2017-06-22 18:55:58 +08:00
|
|
|
REDIS_SECRET_KEY ||= 'SECRET_TOKEN'
|
2017-02-01 06:21:37 +08:00
|
|
|
|
2017-06-22 18:55:58 +08:00
|
|
|
REDIS_VALIDATE_SECONDS ||= 30
|
2017-03-13 22:19:02 +08:00
|
|
|
|
2017-02-01 06:21:37 +08: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 22:19:02 +08:00
|
|
|
|
|
|
|
if @safe_secret_key_base && @token_in_redis && (@token_last_validated + REDIS_VALIDATE_SECONDS) < Time.now
|
2017-03-13 22:47:43 +08:00
|
|
|
@token_last_validated = Time.now
|
2017-03-13 22:19:02 +08:00
|
|
|
token = $redis.without_namespace.get(REDIS_SECRET_KEY)
|
|
|
|
if token.nil?
|
|
|
|
$redis.without_namespace.set(REDIS_SECRET_KEY, @safe_secret_key_base)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
@safe_secret_key_base ||= begin
|
|
|
|
token = secret_key_base
|
|
|
|
if token.blank? || token !~ VALID_SECRET_KEY
|
2017-03-13 22:19:02 +08:00
|
|
|
|
|
|
|
@token_in_redis = true
|
|
|
|
@token_last_validated = Time.now
|
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
token = $redis.without_namespace.get(REDIS_SECRET_KEY)
|
|
|
|
unless token && token =~ VALID_SECRET_KEY
|
|
|
|
token = SecureRandom.hex(64)
|
2017-07-28 09:20:09 +08:00
|
|
|
$redis.without_namespace.set(REDIS_SECRET_KEY, token)
|
2017-02-01 06:21:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if !secret_key_base.blank? && token != secret_key_base
|
|
|
|
STDERR.puts "WARNING: DISCOURSE_SECRET_KEY_BASE is invalid, it was re-generated"
|
|
|
|
end
|
|
|
|
token
|
|
|
|
end
|
2017-08-02 13:32:01 +08:00
|
|
|
rescue Redis::CommandError => e
|
|
|
|
@safe_secret_key_base = SecureRandom.hex(64) if e.message =~ /READONLY/
|
2017-02-01 06:21:37 +08:00
|
|
|
end
|
|
|
|
|
2013-12-20 13:38:51 +08:00
|
|
|
def self.load_defaults
|
2013-12-20 14:41:12 +08:00
|
|
|
default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__))
|
2014-01-02 07:46:09 +08:00
|
|
|
default_provider.keys.concat(@provider.keys).uniq.each do |key|
|
|
|
|
default = default_provider.lookup(key, nil)
|
2017-03-10 07:00:55 +08:00
|
|
|
|
|
|
|
instance_variable_set("@#{key}_cache", nil)
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
define_singleton_method(key) do
|
2017-03-10 07:00:55 +08:00
|
|
|
val = instance_variable_get("@#{key}_cache")
|
|
|
|
unless val.nil?
|
|
|
|
val == :missing ? nil : val
|
|
|
|
else
|
|
|
|
val = provider.lookup(key, default)
|
|
|
|
if val.nil?
|
|
|
|
val = :missing
|
|
|
|
end
|
|
|
|
instance_variable_set("@#{key}_cache", val)
|
|
|
|
val == :missing ? nil : val
|
|
|
|
end
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
def self.use_s3?
|
|
|
|
(@use_s3 ||=
|
|
|
|
begin
|
|
|
|
s3_bucket &&
|
|
|
|
s3_region && (
|
|
|
|
s3_use_iam_profile || (s3_access_key_id && s3_secret_access_key)
|
|
|
|
) ? :true : :false
|
|
|
|
end) == :true
|
|
|
|
end
|
|
|
|
|
2018-05-17 04:10:15 +08:00
|
|
|
def self.s3_bucket_name
|
|
|
|
@s3_bucket_name ||= s3_bucket.downcase.split("/")[0]
|
|
|
|
end
|
|
|
|
|
2017-10-06 13:20:01 +08:00
|
|
|
# for testing
|
|
|
|
def self.reset_s3_cache!
|
|
|
|
@use_s3 = nil
|
|
|
|
end
|
|
|
|
|
2014-01-14 13:59:55 +08:00
|
|
|
def self.database_config
|
2017-07-28 09:20:09 +08:00
|
|
|
hash = { "adapter" => "postgresql" }
|
2018-03-09 10:22:29 +08:00
|
|
|
|
|
|
|
%w{
|
|
|
|
pool
|
|
|
|
connect_timeout
|
|
|
|
timeout
|
|
|
|
socket
|
|
|
|
host
|
|
|
|
backup_host
|
|
|
|
port
|
|
|
|
backup_port
|
|
|
|
username
|
|
|
|
password
|
|
|
|
replica_host
|
|
|
|
replica_port
|
|
|
|
}.each do |s|
|
2014-01-14 13:59:55 +08:00
|
|
|
if val = self.send("db_#{s}")
|
|
|
|
hash[s] = val
|
|
|
|
end
|
|
|
|
end
|
2016-01-25 14:27:59 +08:00
|
|
|
|
|
|
|
hash["adapter"] = "postgresql_fallback" if hash["replica_host"]
|
|
|
|
|
2015-07-23 13:22:54 +08:00
|
|
|
hostnames = [ hostname ]
|
2015-07-23 13:33:38 +08:00
|
|
|
hostnames << backup_hostname if backup_hostname.present?
|
2015-07-23 13:22:54 +08:00
|
|
|
|
2018-03-28 03:20:22 +08:00
|
|
|
hostnames << URI.parse(cdn_url).host if cdn_url.present?
|
|
|
|
|
2015-07-23 13:22:54 +08:00
|
|
|
hash["host_names"] = hostnames
|
2014-01-14 13:59:55 +08:00
|
|
|
hash["database"] = db_name
|
|
|
|
|
2015-02-18 08:16:53 +08:00
|
|
|
hash["prepared_statements"] = !!self.db_prepared_statements
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
{ "production" => hash }
|
2014-01-14 13:59:55 +08:00
|
|
|
end
|
|
|
|
|
2017-02-02 13:47:57 +08:00
|
|
|
# For testing purposes
|
|
|
|
def self.reset_redis_config!
|
|
|
|
@config = nil
|
|
|
|
end
|
|
|
|
|
2015-06-25 14:51:48 +08:00
|
|
|
def self.redis_config
|
|
|
|
@config ||=
|
|
|
|
begin
|
|
|
|
c = {}
|
|
|
|
c[:host] = redis_host if redis_host
|
|
|
|
c[:port] = redis_port if redis_port
|
2017-02-02 13:47:57 +08:00
|
|
|
|
|
|
|
if redis_slave_host && redis_slave_port
|
|
|
|
c[:slave_host] = redis_slave_host
|
|
|
|
c[:slave_port] = redis_slave_port
|
|
|
|
c[:connector] = DiscourseRedis::Connector
|
|
|
|
end
|
|
|
|
|
2015-07-01 10:19:02 +08:00
|
|
|
c[:password] = redis_password if redis_password.present?
|
2015-06-25 14:51:48 +08:00
|
|
|
c[:db] = redis_db if redis_db != 0
|
|
|
|
c[:db] = 1 if Rails.env == "test"
|
2017-02-02 13:47:57 +08:00
|
|
|
|
2015-06-25 14:51:48 +08:00
|
|
|
c.freeze
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-16 05:38:24 +08:00
|
|
|
def self.add_default(name, default)
|
|
|
|
unless self.respond_to? name
|
|
|
|
define_singleton_method(name) do
|
|
|
|
default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 12:12:23 +08:00
|
|
|
class BaseProvider
|
2013-12-20 13:17:21 +08:00
|
|
|
def self.coerce(setting)
|
|
|
|
return setting == "true" if setting == "true" || setting == "false"
|
|
|
|
return $1.to_i if setting.to_s.strip =~ /^([0-9]+)$/
|
|
|
|
setting
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(current, default)
|
|
|
|
BaseProvider.coerce(
|
|
|
|
if current.present?
|
|
|
|
current
|
|
|
|
else
|
|
|
|
default.present? ? default : nil
|
|
|
|
end
|
|
|
|
)
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
class FileProvider < BaseProvider
|
2013-12-20 13:38:51 +08:00
|
|
|
attr_reader :data
|
2013-12-20 13:17:21 +08:00
|
|
|
def self.from(file)
|
|
|
|
if File.exists?(file)
|
|
|
|
parse(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
@data = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
2014-02-15 05:35:30 +08:00
|
|
|
ERB.new(File.read(@file)).result().split("\n").each do |line|
|
2015-08-14 15:01:06 +08:00
|
|
|
if line =~ /^\s*([a-z_]+[a-z0-9_]*)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
|
2013-12-20 13:17:21 +08:00
|
|
|
@data[$1.strip.to_sym] = ($4 || $3 || $2).strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def lookup(key, default)
|
2013-12-20 14:09:51 +08:00
|
|
|
var = @data[key]
|
|
|
|
resolve(var, var.nil? ? default : "")
|
2013-12-20 13:17:21 +08:00
|
|
|
end
|
|
|
|
|
2014-01-02 07:46:09 +08:00
|
|
|
def keys
|
|
|
|
@data.keys
|
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
def self.parse(file)
|
|
|
|
provider = self.new(file)
|
|
|
|
provider.read
|
|
|
|
provider
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
2014-10-04 11:07:20 +08:00
|
|
|
|
|
|
|
private_class_method :parse
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
|
2013-12-20 13:17:21 +08:00
|
|
|
class EnvProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
2013-12-20 14:07:08 +08:00
|
|
|
var = ENV["DISCOURSE_" << key.to_s.upcase]
|
|
|
|
resolve(var , var.nil? ? default : nil)
|
2013-12-20 13:17:21 +08:00
|
|
|
end
|
2014-01-02 07:46:09 +08:00
|
|
|
|
|
|
|
def keys
|
2017-07-28 09:20:09 +08:00
|
|
|
ENV.keys.select { |k| k =~ /^DISCOURSE_/ }.map { |k| k[10..-1].downcase.to_sym }
|
2014-01-02 07:46:09 +08:00
|
|
|
end
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|
|
|
|
|
2015-03-09 09:21:41 +08:00
|
|
|
class BlankProvider < BaseProvider
|
|
|
|
def lookup(key, default)
|
|
|
|
default
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 12:12:23 +08:00
|
|
|
class << self
|
|
|
|
attr_accessor :provider
|
|
|
|
end
|
|
|
|
|
2017-01-10 06:10:14 +08: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-09 09:21:41 +08:00
|
|
|
end
|
2014-01-02 07:46:09 +08:00
|
|
|
|
2013-12-20 12:12:23 +08:00
|
|
|
end
|