Class: Castle::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/castle/configuration.rb

Overview

manages configuration variables

Constant Summary collapse

HOST =
'api.castle.io'
PORT =
443
URL_PREFIX =
'/v1'
FAILOVER_STRATEGY =
:allow
REQUEST_TIMEOUT =

in milliseconds

500
FAILOVER_STRATEGIES =
%i[allow deny challenge throw].freeze
TRUSTED_PROXIES =

regexp of trusted proxies which is always appended to the trusted proxy list

[/
  \A127\.0\.0\.1\Z|
  \A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|
  \A::1\Z|\Afd[0-9a-f]{2}:.+|
  \Alocalhost\Z|
  \Aunix\Z|
  \Aunix:
/ix].freeze
DEFAULT_WHITELIST =
Note:

this value is not assigned as we don’t recommend using a whitelist. If you need to use one, this constant is provided as a good default.

%w[
  Accept
  Accept-Charset
  Accept-Datetime
  Accept-Encoding
  Accept-Language
  Cache-Control
  Connection
  Content-Length
  Content-Type
  Host
  Origin
  Pragma
  Referer
  TE
  Upgrade-Insecure-Requests
  X-Castle-Client-Id
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



51
52
53
54
55
# File 'lib/castle/configuration.rb', line 51

def initialize
  @formatter = Castle::HeadersFormatter
  @request_timeout = REQUEST_TIMEOUT
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(setting, *_args) ⇒ Object (private)



119
120
121
# File 'lib/castle/configuration.rb', line 119

def method_missing(setting, *_args)
  raise Castle::ConfigurationError, "there is no such a config #{setting}"
end

Instance Attribute Details

#api_secretObject

Returns the value of attribute api_secret.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def api_secret
  @api_secret
end

#blacklistedObject

Returns the value of attribute blacklisted.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def blacklisted
  @blacklisted
end

#failover_strategyObject

Returns the value of attribute failover_strategy.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def failover_strategy
  @failover_strategy
end

#hostObject

Returns the value of attribute host.



47
48
49
# File 'lib/castle/configuration.rb', line 47

def host
  @host
end

#ip_headersObject

Returns the value of attribute ip_headers.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def ip_headers
  @ip_headers
end

#portObject

Returns the value of attribute port.



47
48
49
# File 'lib/castle/configuration.rb', line 47

def port
  @port
end

#request_timeoutObject

Returns the value of attribute request_timeout.



47
48
49
# File 'lib/castle/configuration.rb', line 47

def request_timeout
  @request_timeout
end

#trust_proxy_chainObject

Returns the value of attribute trust_proxy_chain.



47
48
49
# File 'lib/castle/configuration.rb', line 47

def trust_proxy_chain
  @trust_proxy_chain
end

#trusted_proxiesObject

Returns the value of attribute trusted_proxies.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def trusted_proxies
  @trusted_proxies
end

#trusted_proxy_depthObject

Returns the value of attribute trusted_proxy_depth.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def trusted_proxy_depth
  @trusted_proxy_depth
end

#url_prefixObject

Returns the value of attribute url_prefix.



47
48
49
# File 'lib/castle/configuration.rb', line 47

def url_prefix
  @url_prefix
end

#whitelistedObject

Returns the value of attribute whitelisted.



48
49
50
# File 'lib/castle/configuration.rb', line 48

def whitelisted
  @whitelisted
end

Instance Method Details

#resetObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/castle/configuration.rb', line 57

def reset
  self.failover_strategy = FAILOVER_STRATEGY
  self.host = HOST
  self.port = PORT
  self.url_prefix = URL_PREFIX
  self.whitelisted = [].freeze
  self.blacklisted = [].freeze
  self.api_secret = ENV.fetch('CASTLE_API_SECRET', '')
  self.ip_headers = [].freeze
  self.trusted_proxies = [].freeze
  self.trust_proxy_chain = false
  self.trusted_proxy_depth = nil
end

#valid?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/castle/configuration.rb', line 104

def valid?
  !api_secret.to_s.empty? && !host.to_s.empty? && !port.to_s.empty?
end