gitlab-labkit 5.16.0 → 5.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7d7c780bc76825cbcfb8107fd3a710e98c9b2cb12590acebd6d396472b1ce1c7
|
|
4
|
+
data.tar.gz: 8e9bb660d30c509222626623c88915f040733f2651d88dbadec83cb53896c314
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a3ce28ee38f7525339311cb93085215255303a219075ee1fa59cd596b99d29f8ca6824f81bf25059504775da3cc39303a8b846e9e82e1f9e2f02432e4ba5b268
|
|
7
|
+
data.tar.gz: 043eba89e255788dfb0e1abc80e8de1e4c51cf39f07827c4f613f6b97256e158a9fafedad76d6c06b89d9d944d82170d9000053d5516448e87ad4431be5280c8
|
|
@@ -808,6 +808,40 @@ Names are compared as they will run, so an override replaces a code rule that `L
|
|
|
808
808
|
`Labkit::RateLimit.check`, the throw-away helper, runs the rules it is given and reads no document.
|
|
809
809
|
A limiter built with no document, or with `Document.empty`, has today's rule list unchanged.
|
|
810
810
|
|
|
811
|
+
### Rule store
|
|
812
|
+
|
|
813
|
+
A rule store is where a host publishes override rules at runtime.
|
|
814
|
+
`Labkit::RateLimit::RuleStore` names the three methods a store implements, and `Labkit::RateLimit.rule_store` is the configured one.
|
|
815
|
+
|
|
816
|
+
```ruby
|
|
817
|
+
proto = Labkit::RateLimit::Config::Proto
|
|
818
|
+
request = proto::PublishRulesRequest.new
|
|
819
|
+
request.rate_limits["rack_request"] = proto::RuleList.new(rules: [
|
|
820
|
+
proto::Rule.new(name: "free", action: :ACTION_LIMIT, limit: 50, period_s: 60, position: 0)
|
|
821
|
+
])
|
|
822
|
+
request.remove["rack_request"] = proto::RuleNames.new(names: ["old_rule"])
|
|
823
|
+
|
|
824
|
+
Labkit::RateLimit.rule_store.publish(request)
|
|
825
|
+
Labkit::RateLimit.rule_store.override_rules("rack_request") # => the published rules
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
`publish` runs the schema, contract and rule steps on the request, against the configured document or the one passed to `MemoryRuleStore.new(document:)`.
|
|
829
|
+
A failure raises `Config::Error` with its step and writes nothing, also for the valid rules in the same request.
|
|
830
|
+
A published name replaces the rule of that name, position included, and a rule that a request does not name stays published.
|
|
831
|
+
`remove` drops a rule by name, and a name in both fields of one request fails the schema step.
|
|
832
|
+
A request that names a limiter that the contract does not declare fails the contract step, also when it only removes rules or has an empty rule list.
|
|
833
|
+
One case passes: a `remove` for a limiter that still has published rules, so a host can remove the rules of a limiter that a newer contract dropped.
|
|
834
|
+
A limiter holds at most `RuleStore::MAX_RULES` published rules: a request that leaves it with more fails the contract step.
|
|
835
|
+
`override_rules(name)` returns the published rules by `position`, then by name; a rule without a position sorts after every rule that has one.
|
|
836
|
+
`default_rules(name)` returns the contract defaults of the document.
|
|
837
|
+
A rule is checked when it is published.
|
|
838
|
+
`publish` logs no warning for a rule that has fewer `match` keys than the default it replaces, which a rate limits file does.
|
|
839
|
+
`MemoryRuleStore` does not check it again when the host loads another document, so a rule that the new contract rejects stays published.
|
|
840
|
+
|
|
841
|
+
The default store is a `MemoryRuleStore`, which holds the rules of one process, so it is for specs and for a host with one process.
|
|
842
|
+
Set another store with `Labkit::RateLimit.configure { |c| c.rule_store = store }`.
|
|
843
|
+
No limiter reads the store yet: a host that wants published rules to apply passes them to its limiters itself.
|
|
844
|
+
|
|
811
845
|
## Dev/test vs production guards
|
|
812
846
|
|
|
813
847
|
`Limiter.new` and `Rule.new` validate names and configuration. In
|
|
@@ -37,7 +37,7 @@ module Labkit
|
|
|
37
37
|
# @param logger [#warn, nil] receives each warning as it is recorded
|
|
38
38
|
def initialize(strict: false, logger: nil)
|
|
39
39
|
@strict = strict
|
|
40
|
-
@logger = logger
|
|
40
|
+
@logger = logger
|
|
41
41
|
# load keeps the run's warnings on the instance, so two threads
|
|
42
42
|
# sharing one Loader would mix them.
|
|
43
43
|
@mutex = Mutex.new
|
|
@@ -51,10 +51,34 @@ module Labkit
|
|
|
51
51
|
@mutex.synchronize { load_locked(contracts, rate_limits) }
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# The contract and rule steps for one rule a host publishes to a rule
|
|
55
|
+
# store. A failure rejects in both modes: a host must know that its
|
|
56
|
+
# publish did nothing.
|
|
57
|
+
#
|
|
58
|
+
# @param name [String] limiter name
|
|
59
|
+
# @param rule [Proto::Rule]
|
|
60
|
+
# @param document [Document, nil]
|
|
61
|
+
# @return [Rule]
|
|
62
|
+
# @raise [Error] step :contract or :rule
|
|
63
|
+
def published_rule(name, rule, document)
|
|
64
|
+
contract_error("limiter #{name} is not in the contract") unless document&.limiter?(name)
|
|
65
|
+
contract_error("limiter #{name}: rule #{rule.name} is a protected rule; the contract alone sets its limit") if document.protected_rules(name).include?(rule.name)
|
|
66
|
+
|
|
67
|
+
unknown = (used_properties(rule) - document.available_properties(name)).first
|
|
68
|
+
contract_error("limiter #{name}: rule #{rule.name}: property #{unknown} is not listed for limiter #{name}") if unknown
|
|
69
|
+
|
|
70
|
+
convert(name, rule)
|
|
71
|
+
end
|
|
72
|
+
|
|
54
73
|
private
|
|
55
74
|
|
|
56
75
|
attr_reader :strict
|
|
57
76
|
|
|
77
|
+
# Resolved on first use: a publish builds a Loader and never logs.
|
|
78
|
+
def logger
|
|
79
|
+
@logger ||= RateLimit.config.logger || Labkit::Logging::JsonLogger.new($stdout)
|
|
80
|
+
end
|
|
81
|
+
|
|
58
82
|
def load_locked(contracts, rate_limits)
|
|
59
83
|
@warnings = []
|
|
60
84
|
specs = merge_contracts(contracts)
|
|
@@ -234,7 +258,7 @@ module Labkit
|
|
|
234
258
|
@warnings << warning
|
|
235
259
|
# The warning's own :message is the detail; the log line keeps the
|
|
236
260
|
# fixed message every rate_limit log line has.
|
|
237
|
-
|
|
261
|
+
logger.warn(message: "rate_limit_config_warning", step: step, limiter: limiter, rule: rule, detail: message)
|
|
238
262
|
end
|
|
239
263
|
|
|
240
264
|
def contract_error(message)
|
|
@@ -8,6 +8,15 @@ module Labkit
|
|
|
8
8
|
# It stays nil until the host loads one, so a host without config files
|
|
9
9
|
# never loads Config and its vendored protobuf schema.
|
|
10
10
|
attr_accessor :redis, :logger, :rule_document
|
|
11
|
+
|
|
12
|
+
# rule_store is where the host publishes override rules at runtime.
|
|
13
|
+
# Built here and not on first use: two threads that built it together
|
|
14
|
+
# would each publish to their own store.
|
|
15
|
+
attr_accessor :rule_store
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@rule_store = MemoryRuleStore.new
|
|
19
|
+
end
|
|
11
20
|
end
|
|
12
21
|
end
|
|
13
22
|
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Labkit
|
|
4
|
+
module RateLimit
|
|
5
|
+
# RuleStore is where a host publishes override rules at runtime and where
|
|
6
|
+
# a reader takes them from. A store checks a request against the contract
|
|
7
|
+
# the host loaded, so a published rule passes the same schema, contract
|
|
8
|
+
# and rule steps as a rule in a rate limits file.
|
|
9
|
+
module RuleStore
|
|
10
|
+
# The schema's cap on one request's list, held for the rules a limiter
|
|
11
|
+
# has after the request. A literal, so a store does not load Config.
|
|
12
|
+
MAX_RULES = 200
|
|
13
|
+
|
|
14
|
+
# Publishes and removes rules in one step: either every change in the
|
|
15
|
+
# request is applied or none is. A published name replaces the rule of
|
|
16
|
+
# that name, position included. A rule that a request does not name
|
|
17
|
+
# stays published. A request that leaves a limiter with more than
|
|
18
|
+
# MAX_RULES published rules fails.
|
|
19
|
+
#
|
|
20
|
+
# @param request [Config::Proto::PublishRulesRequest]
|
|
21
|
+
# @raise [Config::Error] with the step that failed
|
|
22
|
+
# @raise [ArgumentError] for a message of another type
|
|
23
|
+
def publish(_request)
|
|
24
|
+
raise NotImplementedError, "#{self.class} must implement publish"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @param name [String, Symbol] limiter name
|
|
28
|
+
# @return [Array<Rule>] the published rules, by position and then by
|
|
29
|
+
# name; a rule without a position sorts after every rule that has one
|
|
30
|
+
def override_rules(_name)
|
|
31
|
+
raise NotImplementedError, "#{self.class} must implement override_rules"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @param name [String, Symbol] limiter name
|
|
35
|
+
# @return [Array<Rule>] the contract's default rules, document order
|
|
36
|
+
def default_rules(_name)
|
|
37
|
+
raise NotImplementedError, "#{self.class} must implement default_rules"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# MemoryRuleStore holds the published rules of one process. It is for
|
|
42
|
+
# specs and for a host with a single process: a rule published in one
|
|
43
|
+
# process does not reach another.
|
|
44
|
+
class MemoryRuleStore
|
|
45
|
+
include RuleStore
|
|
46
|
+
|
|
47
|
+
Entry = Data.define(:rule, :position)
|
|
48
|
+
private_constant :Entry
|
|
49
|
+
|
|
50
|
+
# @param document [Config::Document, nil] the contract to check a
|
|
51
|
+
# request against. Without one the store reads the configured
|
|
52
|
+
# document on each call, so a store built before the host loads its
|
|
53
|
+
# contract still sees it.
|
|
54
|
+
def initialize(document: nil)
|
|
55
|
+
@document = document
|
|
56
|
+
@published = {}
|
|
57
|
+
@mutex = Mutex.new
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def publish(request)
|
|
61
|
+
# RateLimits has the same shape and passes the schema step, so the
|
|
62
|
+
# wrong message would fail later, inside the store.
|
|
63
|
+
raise ArgumentError, "expected a Config::Proto::PublishRulesRequest, got #{request.class}" unless request.is_a?(Config::Proto::PublishRulesRequest)
|
|
64
|
+
|
|
65
|
+
Config::Schema.check!(request)
|
|
66
|
+
# Read once, so one request is checked against one document.
|
|
67
|
+
contract = document
|
|
68
|
+
check_limiters!(request, contract)
|
|
69
|
+
entries = entries_for(request, contract)
|
|
70
|
+
|
|
71
|
+
@mutex.synchronize do
|
|
72
|
+
merged = merged_entries(entries, request.remove)
|
|
73
|
+
merged.each { |limiter, by_name| check_cap!(limiter, by_name.size) }
|
|
74
|
+
@published.merge!(merged)
|
|
75
|
+
end
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def override_rules(name)
|
|
80
|
+
entries = @mutex.synchronize { @published.fetch(name.to_s, {}).values }
|
|
81
|
+
entries.sort_by { |entry| [entry.position ? 0 : 1, entry.position.to_i, entry.rule.name] }.map(&:rule)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def default_rules(name)
|
|
85
|
+
document&.default_rules(name) || []
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def document
|
|
91
|
+
@document || RateLimit.config.rule_document
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def merged_entries(entries, remove)
|
|
95
|
+
(entries.keys | remove.keys).sort.to_h do |limiter|
|
|
96
|
+
by_name = @published.fetch(limiter, {}).merge(entries.fetch(limiter, {}))
|
|
97
|
+
[limiter, by_name.except(*remove[limiter]&.names)]
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A limiter the contract does not declare is a mistake in the name, and
|
|
102
|
+
# it must not pass as a request that had nothing to do. A removal from a
|
|
103
|
+
# limiter that still has rules is no mistake: the contract dropped the
|
|
104
|
+
# limiter after the publish. Sorted, so every SDK reports the same limiter.
|
|
105
|
+
def check_limiters!(request, contract)
|
|
106
|
+
with_rules = @mutex.synchronize { @published.reject { |_, by_name| by_name.empty? }.keys }
|
|
107
|
+
unknown = (request.rate_limits.keys | (request.remove.keys - with_rules)).sort.find { |limiter| !contract&.limiter?(limiter) }
|
|
108
|
+
raise Config::Error.new(:contract, "limiter #{unknown} is not in the contract") if unknown
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def check_cap!(limiter, count)
|
|
112
|
+
return if count <= MAX_RULES
|
|
113
|
+
|
|
114
|
+
raise Config::Error.new(:contract, "limiter #{limiter} would have #{count} published rules; at most #{MAX_RULES} are allowed")
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Converts every rule before any is stored, so a request that fails
|
|
118
|
+
# leaves the store as it was. Limiters in sorted order, as in
|
|
119
|
+
# check_limiters!.
|
|
120
|
+
def entries_for(request, contract)
|
|
121
|
+
loader = Config::Loader.new
|
|
122
|
+
entries = {}
|
|
123
|
+
request.rate_limits.keys.sort.each do |limiter|
|
|
124
|
+
entries[limiter] = request.rate_limits[limiter].rules.to_h do |rule|
|
|
125
|
+
[rule.name, Entry.new(loader.published_rule(limiter, rule, contract), rule.has_position? ? rule.position : nil)]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
entries
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
data/lib/labkit/rate_limit.rb
CHANGED
|
@@ -27,6 +27,12 @@ module Labkit
|
|
|
27
27
|
autoload :Metrics, "labkit/rate_limit/metrics"
|
|
28
28
|
autoload :BlockSummary, "labkit/rate_limit/block_summary"
|
|
29
29
|
autoload :Config, "labkit/rate_limit/config"
|
|
30
|
+
autoload :RuleStore, "labkit/rate_limit/rule_store"
|
|
31
|
+
autoload :MemoryRuleStore, "labkit/rate_limit/rule_store"
|
|
32
|
+
|
|
33
|
+
# Built at load and not on first use, for the same reason as
|
|
34
|
+
# Configuration#rule_store.
|
|
35
|
+
@config = Configuration.new
|
|
30
36
|
|
|
31
37
|
class << self
|
|
32
38
|
def configure
|
|
@@ -37,6 +43,11 @@ module Labkit
|
|
|
37
43
|
@config ||= Configuration.new
|
|
38
44
|
end
|
|
39
45
|
|
|
46
|
+
# @return [RuleStore] the store the host publishes override rules to
|
|
47
|
+
def rule_store
|
|
48
|
+
config.rule_store
|
|
49
|
+
end
|
|
50
|
+
|
|
40
51
|
# Convenience wrapper - creates a throw-away Limiter.
|
|
41
52
|
# Prefer Limiter for call sites that can cache the object.
|
|
42
53
|
#
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gitlab-labkit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.17.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrew Newdigate
|
|
@@ -687,6 +687,7 @@ files:
|
|
|
687
687
|
- lib/labkit/rate_limit/metrics.rb
|
|
688
688
|
- lib/labkit/rate_limit/result.rb
|
|
689
689
|
- lib/labkit/rate_limit/rule.rb
|
|
690
|
+
- lib/labkit/rate_limit/rule_store.rb
|
|
690
691
|
- lib/labkit/redis.rb
|
|
691
692
|
- lib/labkit/redis/script.rb
|
|
692
693
|
- lib/labkit/rspec/README.md
|