Class: Aikido::Zen::RuntimeSettings::Endpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/runtime_settings/endpoints.rb

Overview

Wraps the list of endpoint protection settings, providing an interface for checking the settings for any given route. If the route has no configured settings, that will return the singleton ProtectionSettings.none.

Examples:

endpoint = runtime_settings.endpoints[request.route]
block_request unless endpoint.allows?(request.client_ip)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoints = {}) ⇒ Aikido::Zen::RuntimeSettings::Endpoints

Parameters:

  • endpoints (Hash) (defaults to: {})

    the endpoints in wildcard matching order



35
36
37
38
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 35

def initialize(endpoints = {})
  @endpoints = endpoints
  @endpoints.default = RuntimeSettings::ProtectionSettings.none
end

Class Method Details

.from_json(data) ⇒ Aikido::Zen::RuntimeSettings::Endpoints

Parameters:

  • data (Array<Hash>)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 18

def self.from_json(data)
  endpoint_pairs = Array(data).map do |value|
    route = Route.new(verb: value["method"], path: value["route"])
    settings = RuntimeSettings::ProtectionSettings.from_json(value)
    [route, settings]
  end

  # Sort endpoints by wildcard matching order
  endpoint_pairs.sort_by! do |route, settings|
    route.sort_key
  end

  new(endpoint_pairs.to_h)
end

Instance Method Details

#[](route) ⇒ Aikido::Zen::RuntimeSettings::ProtectionSettings



106
107
108
109
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 106

def [](route)
  result = match(route) { |_pattern, settings| settings }
  result || @endpoints.default
end

#match(route) {|pattern, settings| ... } ⇒ Array<Aikido::Zen::Route, Aikido::Zen::RuntimeSettings::ProtectionSettings>, ...

Match the route against the endpoints, and return the first match.

Parameters:

Yields:

  • (pattern, settings)

    the optional block to call when a match is found

Yield Parameters:

Yield Returns:

  • Object

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 52

def match(route)
  if @endpoints.key?(route)
    if block_given?
      return yield(route, @endpoints[route])
    else
      return [route, @endpoints[route]]
    end
  end

  # Wildcard endpoint matching

  @endpoints.each do |pattern, settings|
    if pattern.match?(route)
      if block_given?
        return yield(pattern, settings)
      else
        return [pattern, settings]
      end
    end
  end

  nil
end

#matched_settings(route) ⇒ Array<Aikido::Zen::RuntimeSettings::ProtectionSettings>



113
114
115
116
117
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 113

def matched_settings(route)
  results = matches(route) { |_pattern, settings| settings }
  results << @endpoints.default if results.empty?
  results
end

#matches(route) {|pattern, settings| ... } ⇒ Array<Aikido::Zen::Route, Aikido::Zen::RuntimeSettings::ProtectionSettings>, Array<Object>

Match the route against the endpoints, and return all matches.

Parameters:

Yields:

  • (pattern, settings)

    the optional block to call when a match is found

Yield Parameters:

Yield Returns:

  • Object

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aikido/zen/runtime_settings/endpoints.rb', line 87

def matches(route)
  results = []

  @endpoints.each do |pattern, settings|
    if pattern.match?(route)
      results <<
        if block_given?
          yield(pattern, settings)
        else
          [pattern, settings]
        end
    end
  end

  results
end