class TShield::Configuration

Class for read configuration file

Attributes

domains[R]
request[R]

Configuration file

Possible attributes request:

timeout: wait time for real service in seconds
verify_ssl: ignores invalid ssl if false

domains:

'url':
  name: Name to identify the domain in the generated files
  headers: Object to translate received header in tshield to send to
           original service. Sinatra change keys. Example:
             HTTP_AUTHORIZATION should be mapped to Authorization
             (NEED IMPROVEMENT github-issue #https://github.com/diegorubin/tshield/issues/17)
  not_save_headers: List of headers that should be ignored in generated
                    file
  ignore_query_params: List of params that should be ignored in
                       generated directory
session_path[R]
tcp_servers[R]
windows_compatibility[R]

Public Class Methods

clear() click to toggle source
# File lib/tshield/configuration.rb, line 56
def self.clear
  @singleton = nil
end
get_url_for_domain_by_path(path, config) click to toggle source
# File lib/tshield/configuration.rb, line 121
def self.get_url_for_domain_by_path(path, config)
  config['paths'].select { |pattern| path =~ Regexp.new(pattern) }[0]
end
load_configuration() click to toggle source
# File lib/tshield/configuration.rb, line 130
def self.load_configuration
  configuration_file = TShield::Options.instance.configuration_file
  read_configuration_file(configuration_file)
rescue Errno::ENOENT => e
  TShield.logger.fatal(
    "Load configuration file #{configuration_file} failed!\n#{e}"
  )
  raise 'Startup aborted'
end
new(attributes) click to toggle source
# File lib/tshield/configuration.rb, line 37
def initialize(attributes)
  attributes.each { |key, value| instance_variable_set("@#{key}", value) }

  return unless File.exist?('filters')

  Dir.entries('filters').each do |entry|
    next if entry =~ /^\.\.?$/

    TShield.logger.info("loading filter #{entry}")
    entry.gsub!('.rb', '')

    require File.join('.', 'filters', entry)
  end
end
read_configuration_file(config_path) click to toggle source
# File lib/tshield/configuration.rb, line 125
def self.read_configuration_file(config_path)
  configs = YAML.safe_load(File.open(config_path).read)
  Configuration.new(configs)
end
singleton() click to toggle source
# File lib/tshield/configuration.rb, line 52
def self.singleton
  @singleton ||= load_configuration
end

Public Instance Methods

cache_request?(domain) click to toggle source
# File lib/tshield/configuration.rb, line 90
def cache_request?(domain)
  domains[domain]['cache_request'] || true
end
get_after_filters(domain) click to toggle source
# File lib/tshield/configuration.rb, line 85
def get_after_filters(domain)
  get_filters(domain)
    .select { |klass| klass.ancestors.include?(TShield::AfterFilter) }
end
get_before_filters(domain) click to toggle source
# File lib/tshield/configuration.rb, line 80
def get_before_filters(domain)
  get_filters(domain)
    .select { |klass| klass.ancestors.include?(TShield::BeforeFilter) }
end
get_delay(domain, path) click to toggle source
# File lib/tshield/configuration.rb, line 140
def get_delay(domain, path)
  ((domains[domain] || {})['delay'] || {})[path] || 0
end
get_domain_for(path) click to toggle source
# File lib/tshield/configuration.rb, line 60
def get_domain_for(path)
  domains.each do |url, config|
    result = self.class.get_url_for_domain_by_path(path, config)
    return url if result
  end
  nil
end
get_excluded_headers(domain) click to toggle source
# File lib/tshield/configuration.rb, line 99
def get_excluded_headers(domain)
  domains[domain]['excluded_headers'] || []
end
get_filters(domain) click to toggle source
# File lib/tshield/configuration.rb, line 94
def get_filters(domain)
  (domains[domain]['filters'] || [])
    .collect { |filter| Class.const_get(filter) }
end
get_headers(domain) click to toggle source
# File lib/tshield/configuration.rb, line 72
def get_headers(domain)
  (domains[domain] || {})['headers'] || {}
end
get_name(domain) click to toggle source
# File lib/tshield/configuration.rb, line 76
def get_name(domain)
  domains[domain]['name'] || domain.gsub(%r{.*://}, '')
end
grpc() click to toggle source
# File lib/tshield/configuration.rb, line 116
def grpc
  defaults = { 'port' => 5678, 'proto_dir' => 'proto', 'services' => {} }
  defaults.merge(@grpc || {})
end
not_save_headers(domain) click to toggle source
# File lib/tshield/configuration.rb, line 103
def not_save_headers(domain)
  domains[domain]['not_save_headers'] || []
end
read_session_path() click to toggle source
# File lib/tshield/configuration.rb, line 112
def read_session_path
  session_path || '/sessions'
end
send_header_content_type(domain) click to toggle source
# File lib/tshield/configuration.rb, line 107
def send_header_content_type(domain)
  return domains[domain]['send_header_content_type'] != false if domains[domain]
  true
end
windows_compatibility?() click to toggle source
# File lib/tshield/configuration.rb, line 68
def windows_compatibility?
  windows_compatibility || false
end