Module: Labkit::RateLimit::Config::Schema

Defined in:
lib/labkit/rate_limit/config/schema.rb

Overview

Schema is the schema step: the checks that gitlab/ratelimit/config/v1 declares with protovalidate, mirrored in Ruby because there is no protovalidate runtime for Ruby. Each check keeps the protovalidate rule id, message and field path. Inside one message the order is protovalidate's: message rules, then the oneof, then fields in declaration order. A protobuf map keeps no document order and Go iterates it in an unspecified order, so Ruby reports map entries by sorted key and the corpus compares per entry.

FIELD_RULES and MESSAGE_RULES are copied from the proto by hand, and schema_spec.rb compares both with the vendored config.proto and constraint.proto field by field, and checks that the checker reports message rules in declaration order, so a PROTOS_REF bump that changes a cap, a flag, a rule id, a message or the rule order fails the suite until this file follows. The CEL expressions have no such check; the examples pin them.

The check is the strict reading of the schema. Lenient loading (Loader) drops a rule whose matcher has no known operator before this step, because such a matcher fails required here.

Defined Under Namespace

Classes: Violation

Constant Summary collapse

NAME =

CEL matches() is RE2, where ^ and $ anchor the whole text; Ruby's $ matches before a newline, so the mirror anchors with \A and \z.

/\A[a-z0-9_]{1,64}\z/
NAME_MESSAGE =
"invalid name; must match ^[a-z0-9_]{1,64}$"
PERIODS =
[1, 15, 30, 60, 180, 300, 600, 900, 1800, 3600, 28800, 86400].freeze
SKIP =
:ACTION_SKIP
FIELD_RULES =

The field rules of config.proto, keyed "Message.field". The keys of a map field and the items of a repeated field carry their rule under keys_* and items_*.

{
  "AvailableLimiters.available_limiters" => { min_pairs: 1, keys_is_name: true },
  "LimiterSpec.description" => { max_len: 1024 },
  "LimiterSpec.available_properties" => { unique: true, max_items: 100, items_is_name: true },
  "LimiterSpec.default_rules" => { max_items: 200 },
  "LimiterSpec.protected_rules" => { unique: true, max_items: 200, items_is_name: true },
  "RateLimits.rate_limits" => { keys_is_name: true },
  "RuleList.rules" => { max_items: 200 },
  "Rule.name" => { is_name: true },
  "Rule.description" => { max_len: 1024 },
  "Rule.action" => { required: true, defined_only: true },
  "Rule.characteristics" => { unique: true, max_items: 10, items_is_name: true },
  "Rule.match" => { max_pairs: 20, keys_is_name: true },
  "Rule.limit" => { gte: 1, lte: 1_000_000 },
  "Rule.period_s" => { in: PERIODS },
  "Rule.count_distinct" => { is_name: true, ignore_empty: true },
  "Rule.ban_for_s" => { gte: 1, lte: 86_400 },
  "PublishRulesRequest.rate_limits" => { keys_is_name: true },
  "PublishRulesRequest.remove" => { keys_is_name: true },
  "RuleNames.names" => { min_items: 1, max_items: 200, unique: true, items_is_name: true },
  "Matcher.eq" => { max_len: 1024 },
  "Matcher.ne" => { max_len: 1024 },
  "Matcher.re" => { max_len: 1024 },
  "Matcher.nre" => { max_len: 1024 },
  "StringList.values" => { min_items: 1, max_items: 100, unique: true, items_max_len: 1024 },
  "MatcherList.matchers" => { min_items: 1, max_items: 20 }
}.freeze
MESSAGE_RULES =

The message-level CEL rules of config.proto, by id.

{
  "limiter_spec.protected_rules_declared" => "every protected rule must be a default rule of this limiter",
  "limiter_spec.unique_default_names" => "default rule names must be unique within a limiter",
  "limiter_spec.no_position_in_defaults" => "position is not allowed on a default rule",
  "rule_list.unique_names" => "rule names must be unique within a limiter",
  "rule.limit_requires_threshold" => "limit and period_s are required unless action is skip",
  "rule.ban_excludes_count_distinct" => "ban_for_s cannot be combined with count_distinct",
  "rule.ban_not_on_skip" => "ban_for_s is not allowed on a skip rule",
  "rule.no_counter_on_skip" => "limit, period_s, characteristics and count_distinct are not allowed on a skip rule",
  "rule.count_distinct_not_characteristic" => "count_distinct cannot also be a characteristic",
  "publish.name_in_both" => "a rule name cannot be in both rate_limits and remove for one limiter",
  "matcher_list.no_nested_all" => "all cannot contain another all"
}.freeze
ESCAPES =

The ASCII characters that strconv.Quote prints with a backslash escape.

{
  '"' => '\\"', "\\" => "\\\\", "\a" => "\\a", "\b" => "\\b", "\f" => "\\f",
  "\n" => "\\n", "\r" => "\\r", "\t" => "\\t", "\v" => "\\v"
}.freeze

Class Method Summary collapse

Class Method Details

.check!(message) ⇒ Object

Returns the message.

Parameters:

Returns:

  • the message

Raises:

  • (Error) —

    step :schema, every violation joined with "; "



102
103
104
105
106
107
# File 'lib/labkit/rate_limit/config/schema.rb', line 102

def check!(message)
  found = violations(message)
  raise Error.new(:schema, found.join("; ")) unless found.empty?

  message
end

.quote(key) ⇒ Object

Quotes a map key for a path the way Go's strconv.Quote does, so the path reads the same in every SDK: backslash escapes for the ASCII controls that have one, \xNN for the other ASCII controls and DEL, \uNNNN or UNNNNNNNN for a non-printable rune, everything else as is.



126
127
128
129
130
# File 'lib/labkit/rate_limit/config/schema.rb', line 126

def quote(key)
  out = +'"'
  key.each_char { |c| out << (ESCAPES[c] || escape_rune(c)) }
  out << '"'
end

.violations(message) ⇒ Array<Violation>

Returns every violation, in the order described above.

Returns:

  • (Array<Violation>) —

    every violation, in the order described above



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/labkit/rate_limit/config/schema.rb', line 110

def violations(message)
  checker = Checker.new
  case message
  when Proto::AvailableLimiters then checker.available_limiters(message)
  when Proto::RateLimits then checker.rate_limits(message)
  when Proto::PublishRulesRequest then checker.publish_rules_request(message)
  else
    raise ArgumentError, "no schema for #{message.class}; expected AvailableLimiters, RateLimits or PublishRulesRequest"
  end
  checker.violations
end