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-Castle-Client-Id
  X-Requested-With
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



53
54
55
56
57
# File 'lib/castle/configuration.rb', line 53

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)



126
127
128
# File 'lib/castle/configuration.rb', line 126

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.



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

def allowlisted
  @allowlisted
end

#api_secretObject

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#denylistedObject

Returns the value of attribute denylisted.



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

def denylisted
  @denylisted
end

#failover_strategyObject

Returns the value of attribute failover_strategy.



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

def failover_strategy
  @failover_strategy
end

#ip_headersObject

Returns the value of attribute ip_headers.



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

def ip_headers
  @ip_headers
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#request_timeoutObject

Returns the value of attribute request_timeout.



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

def request_timeout
  @request_timeout
end

#trust_proxy_chainObject

Returns the value of attribute trust_proxy_chain.



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

def trust_proxy_chain
  @trust_proxy_chain
end

#trusted_proxiesObject

Returns the value of attribute trusted_proxies.



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

def trusted_proxies
  @trusted_proxies
end

#trusted_proxy_depthObject

Returns the value of attribute trusted_proxy_depth.



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

def trusted_proxy_depth
  @trusted_proxy_depth
end

Instance Method Details

#resetObject



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

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)


109
110
111
# File 'lib/castle/configuration.rb', line 109

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