Class: Castle::Configuration

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

Overview

manages configuration variables

Direct Known Subclasses

SingletonConfiguration

Constant Summary collapse

BASE_URL =

API endpoint

'https://api.castle.io/v1'
REQUEST_TIMEOUT =

in milliseconds

1000
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_ALLOWLIST =
Note:

this value is not assigned as we don’t recommend using a allowlist. 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
  Dnt
  Host
  Origin
  Pragma
  Referer
  Sec-Fetch-Dest
  Sec-Fetch-Mode
  Sec-Fetch-Site
  Sec-Fetch-User
  Te
  Upgrade-Insecure-Requests
  User-Agent
  X-Requested-With
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



61
62
63
64
65
# File 'lib/castle/configuration.rb', line 61

def initialize
  @header_format = Castle::Headers::Format
  @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)



135
136
137
# File 'lib/castle/configuration.rb', line 135

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

Instance Attribute Details

#allowlistedObject

Returns the value of attribute allowlisted.



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

def allowlisted
  @allowlisted
end

#api_secretObject

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#denylistedObject

Returns the value of attribute denylisted.



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

def denylisted
  @denylisted
end

#failover_strategyObject

Returns the value of attribute failover_strategy.



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

def failover_strategy
  @failover_strategy
end

#ip_headersObject

Returns the value of attribute ip_headers.



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

def ip_headers
  @ip_headers
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#request_timeoutObject

Returns the value of attribute request_timeout.



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

def request_timeout
  @request_timeout
end

#trust_proxy_chainObject

Returns the value of attribute trust_proxy_chain.



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

def trust_proxy_chain
  @trust_proxy_chain
end

#trusted_proxiesObject

Returns the value of attribute trusted_proxies.



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

def trusted_proxies
  @trusted_proxies
end

#trusted_proxy_depthObject

Returns the value of attribute trusted_proxy_depth.



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

def trusted_proxy_depth
  @trusted_proxy_depth
end

Instance Method Details

#resetObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/castle/configuration.rb', line 67

def reset
  self.failover_strategy = Castle::Failover::Strategy::ALLOW
  self.base_url = BASE_URL
  self.allowlisted = [].freeze
  self.denylisted = [].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
  self.logger = nil
end

#valid?Boolean

Returns:

  • (Boolean)


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

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