graphql-armory 0.1.0.pre13 → 0.1.0.pre14
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 +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +5 -5
- data/docs/authorization.md +4 -0
- data/docs/encrypted-ids.md +24 -2
- data/docs/persisted-queries.md +4 -0
- data/docs/rate-limiting.md +23 -0
- data/docs/response-caching.md +590 -0
- data/docs/subscriptions.md +261 -0
- data/docs/telemetry.md +39 -1
- data/docs/versioning.md +4 -0
- data/lib/graphql/armory/authorization/authorize.rb +3 -0
- data/lib/graphql/armory/authorization/scope.rb +2 -0
- data/lib/graphql/armory/caching/audit/command.rb +51 -0
- data/lib/graphql/armory/caching/audit/context_class.rb +117 -0
- data/lib/graphql/armory/caching/audit/declarations.rb +166 -0
- data/lib/graphql/armory/caching/audit/own_checks.rb +150 -0
- data/lib/graphql/armory/caching/audit/public_rules.rb +158 -0
- data/lib/graphql/armory/caching/audit/report.rb +113 -0
- data/lib/graphql/armory/caching/audit.rb +136 -0
- data/lib/graphql/armory/caching/base.rb +118 -0
- data/lib/graphql/armory/caching/canonical.rb +138 -0
- data/lib/graphql/armory/caching/charge.rb +86 -0
- data/lib/graphql/armory/caching/codec.rb +108 -0
- data/lib/graphql/armory/caching/context_methods.rb +88 -0
- data/lib/graphql/armory/caching/controls/app_schema.rb +70 -0
- data/lib/graphql/armory/caching/controls/context.rb +30 -0
- data/lib/graphql/armory/caching/controls/operations.rb +37 -0
- data/lib/graphql/armory/caching/controls/schema.rb +123 -0
- data/lib/graphql/armory/caching/controls/store.rb +16 -0
- data/lib/graphql/armory/caching/controls/walk_schema.rb +197 -0
- data/lib/graphql/armory/caching/controls.rb +19 -0
- data/lib/graphql/armory/caching/digester.rb +48 -0
- data/lib/graphql/armory/caching/errors.rb +16 -0
- data/lib/graphql/armory/caching/gate/admission.rb +67 -0
- data/lib/graphql/armory/caching/gate/signals.rb +65 -0
- data/lib/graphql/armory/caching/gate/store_check.rb +60 -0
- data/lib/graphql/armory/caching/gate/substitute.rb +61 -0
- data/lib/graphql/armory/caching/gate.rb +384 -0
- data/lib/graphql/armory/caching/integration/guard.rb +55 -0
- data/lib/graphql/armory/caching/integration/install.rb +262 -0
- data/lib/graphql/armory/caching/integration/query.rb +42 -0
- data/lib/graphql/armory/caching/integration.rb +141 -0
- data/lib/graphql/armory/caching/keyer.rb +208 -0
- data/lib/graphql/armory/caching/memory_store.rb +164 -0
- data/lib/graphql/armory/caching/nulls.rb +121 -0
- data/lib/graphql/armory/caching/purge.rb +98 -0
- data/lib/graphql/armory/caching/record_keys.rb +109 -0
- data/lib/graphql/armory/caching/recorder/context.rb +178 -0
- data/lib/graphql/armory/caching/recorder/log.rb +142 -0
- data/lib/graphql/armory/caching/recorder/null.rb +24 -0
- data/lib/graphql/armory/caching/recorder/shared_writes.rb +58 -0
- data/lib/graphql/armory/caching/recorder/substitute.rb +119 -0
- data/lib/graphql/armory/caching/recorder.rb +34 -0
- data/lib/graphql/armory/caching/redis_store/scripts.rb +159 -0
- data/lib/graphql/armory/caching/redis_store.rb +162 -0
- data/lib/graphql/armory/caching/rspec.rb +25 -0
- data/lib/graphql/armory/caching/schema_identity.rb +102 -0
- data/lib/graphql/armory/caching/store/null.rb +31 -0
- data/lib/graphql/armory/caching/store/substitute.rb +144 -0
- data/lib/graphql/armory/caching/store.rb +34 -0
- data/lib/graphql/armory/caching/tags.rb +39 -0
- data/lib/graphql/armory/caching/values/decision.rb +45 -0
- data/lib/graphql/armory/caching/values/declaration.rb +114 -0
- data/lib/graphql/armory/caching/values/entry.rb +108 -0
- data/lib/graphql/armory/caching/values/key.rb +39 -0
- data/lib/graphql/armory/caching/values/lookup.rb +35 -0
- data/lib/graphql/armory/caching/values/policy.rb +44 -0
- data/lib/graphql/armory/caching/viewer.rb +112 -0
- data/lib/graphql/armory/caching/walk/directives.rb +45 -0
- data/lib/graphql/armory/caching/walk/relay.rb +54 -0
- data/lib/graphql/armory/caching/walk/run.rb +130 -0
- data/lib/graphql/armory/caching/walk/tally.rb +38 -0
- data/lib/graphql/armory/caching/walk.rb +46 -0
- data/lib/graphql/armory/caching.rb +69 -0
- data/lib/graphql/armory/encoder/legacy.rb +14 -0
- data/lib/graphql/armory/encoder/null.rb +14 -1
- data/lib/graphql/armory/encoder/substitute.rb +54 -2
- data/lib/graphql/armory/encoder/versioned.rb +36 -0
- data/lib/graphql/armory/encoder.rb +80 -1
- data/lib/graphql/armory/persisted_queries.rb +8 -1
- data/lib/graphql/armory/rate_limits/cached_charge.rb +43 -0
- data/lib/graphql/armory/rate_limits/concurrency.rb +12 -5
- data/lib/graphql/armory/rate_limits/invalid_queries.rb +3 -2
- data/lib/graphql/armory/rate_limits/points.rb +21 -9
- data/lib/graphql/armory/rate_limits/time.rb +15 -8
- data/lib/graphql/armory/rate_limits.rb +9 -0
- data/lib/graphql/armory/subscriptions/ably/key.rb +49 -0
- data/lib/graphql/armory/subscriptions/ably/publisher.rb +132 -0
- data/lib/graphql/armory/subscriptions/ably/token_request.rb +42 -0
- data/lib/graphql/armory/subscriptions/ably/webhook.rb +96 -0
- data/lib/graphql/armory/subscriptions/ably.rb +126 -0
- data/lib/graphql/armory/subscriptions/audit.rb +92 -0
- data/lib/graphql/armory/subscriptions/controls/dependencies.rb +37 -0
- data/lib/graphql/armory/subscriptions/controls/event_log.rb +30 -0
- data/lib/graphql/armory/subscriptions/controls/id.rb +26 -0
- data/lib/graphql/armory/subscriptions/controls/record.rb +34 -0
- data/lib/graphql/armory/subscriptions/controls/schema.rb +187 -0
- data/lib/graphql/armory/subscriptions/controls/viewer.rb +99 -0
- data/lib/graphql/armory/subscriptions/controls.rb +19 -0
- data/lib/graphql/armory/subscriptions/core/channels.rb +93 -0
- data/lib/graphql/armory/subscriptions/core/clock.rb +40 -0
- data/lib/graphql/armory/subscriptions/core/controls.rb +29 -0
- data/lib/graphql/armory/subscriptions/core/delivery.rb +250 -0
- data/lib/graphql/armory/subscriptions/core/settings.rb +182 -0
- data/lib/graphql/armory/subscriptions/core/trigger.rb +63 -0
- data/lib/graphql/armory/subscriptions/core/viewer.rb +153 -0
- data/lib/graphql/armory/subscriptions/core/write.rb +172 -0
- data/lib/graphql/armory/subscriptions/core.rb +200 -0
- data/lib/graphql/armory/subscriptions/errors.rb +30 -0
- data/lib/graphql/armory/subscriptions/http/null.rb +17 -0
- data/lib/graphql/armory/subscriptions/http/substitute.rb +56 -0
- data/lib/graphql/armory/subscriptions/http.rb +93 -0
- data/lib/graphql/armory/subscriptions/memory_store.rb +115 -0
- data/lib/graphql/armory/subscriptions/pusher/signature.rb +45 -0
- data/lib/graphql/armory/subscriptions/pusher.rb +192 -0
- data/lib/graphql/armory/subscriptions/redis_store/scripts.rb +115 -0
- data/lib/graphql/armory/subscriptions/redis_store.rb +157 -0
- data/lib/graphql/armory/subscriptions/store/null.rb +23 -0
- data/lib/graphql/armory/subscriptions/store/substitute.rb +155 -0
- data/lib/graphql/armory/subscriptions/store.rb +38 -0
- data/lib/graphql/armory/subscriptions/topic.rb +253 -0
- data/lib/graphql/armory/subscriptions/transport/null.rb +28 -0
- data/lib/graphql/armory/subscriptions/transport/substitute.rb +150 -0
- data/lib/graphql/armory/subscriptions/transport.rb +41 -0
- data/lib/graphql/armory/subscriptions/values/capabilities.rb +50 -0
- data/lib/graphql/armory/subscriptions/values/message.rb +31 -0
- data/lib/graphql/armory/subscriptions/values/outcome.rb +42 -0
- data/lib/graphql/armory/subscriptions/values/record.rb +176 -0
- data/lib/graphql/armory/subscriptions/webhook_app.rb +31 -0
- data/lib/graphql/armory/subscriptions.rb +33 -0
- data/lib/graphql/armory/telemetry.rb +8 -4
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory/versioning/integration.rb +2 -1
- data/lib/graphql/armory.rb +2 -0
- data/lib/tasks/graphql_armory.rake +9 -0
- metadata +110 -1
|
@@ -14,6 +14,9 @@ module GraphQL
|
|
|
14
14
|
# releases every slot its multiplex took when the multiplex ends, even when it raises. A slot a crashed
|
|
15
15
|
# process never released expires after `stale_after` seconds. Soft mode (the default) records but refuses
|
|
16
16
|
# nothing. What happened is in `context[:rate_limit_concurrency]`.
|
|
17
|
+
#
|
|
18
|
+
# A response the response cache answers runs nothing and takes no slot. The key, limit and exemption lookups and
|
|
19
|
+
# the limiter's own record go through `Caching.neutral`.
|
|
17
20
|
class Concurrency
|
|
18
21
|
CONTEXT_KEY = :rate_limit_concurrency
|
|
19
22
|
NAMESPACE = :graphql_armory
|
|
@@ -54,19 +57,21 @@ module GraphQL
|
|
|
54
57
|
|
|
55
58
|
# Whether this query's client has no limit to charge.
|
|
56
59
|
def exempt?(query)
|
|
57
|
-
Settings.exempt?(@key, @limit, query)
|
|
60
|
+
Caching.neutral(query.context) { Settings.exempt?(@key, @limit, query) }
|
|
58
61
|
end
|
|
59
62
|
|
|
60
63
|
# Takes a slot for the query: the refusal when none is free, or nil to let it run.
|
|
61
64
|
def call(query)
|
|
65
|
+
return nil if Settings.delivery?(query)
|
|
66
|
+
|
|
62
67
|
unless query.current_trace.is_a?(Release)
|
|
63
68
|
raise ConfigurationError, "this query runs with its own trace (context[:trace]), so Concurrency could never release its slot: remove context[:trace] or add trace_with to that trace"
|
|
64
69
|
end
|
|
65
70
|
|
|
66
|
-
key = Settings.key_for(@key, query)
|
|
71
|
+
key = Caching.neutral(query.context) { Settings.key_for(@key, query) }
|
|
67
72
|
return call_several(query, key) if key.is_a?(Array)
|
|
68
73
|
|
|
69
|
-
limit = Settings.limit_for(@limit, key, query)
|
|
74
|
+
limit = Caching.neutral(query.context) { Settings.limit_for(@limit, key, query) }
|
|
70
75
|
if limit.nil?
|
|
71
76
|
record(query, key: key, limit: nil, active: nil, limited: false)
|
|
72
77
|
return nil
|
|
@@ -116,7 +121,7 @@ module GraphQL
|
|
|
116
121
|
# listed as it is taken, so a raise later on still lets `Release` give it back. A key whose store fails is left out,
|
|
117
122
|
# and the others still decide.
|
|
118
123
|
def call_several(query, keys)
|
|
119
|
-
limits = Settings.limits_for(@limit, keys, query)
|
|
124
|
+
limits = Caching.neutral(query.context) { Settings.limits_for(@limit, keys, query) }
|
|
120
125
|
if limits.empty?
|
|
121
126
|
record(query, key: keys.first, limit: nil, active: nil, limited: false, keys: [])
|
|
122
127
|
return nil
|
|
@@ -158,8 +163,10 @@ module GraphQL
|
|
|
158
163
|
refusal(full[:limit], full[:active])
|
|
159
164
|
end
|
|
160
165
|
|
|
166
|
+
# The limiter's write is not the query's: a resolver that reads the record reads an input of its answer.
|
|
161
167
|
def record(query, **info)
|
|
162
|
-
|
|
168
|
+
record = { **info.slice(:key, :limit, :active), soft: @soft, limited: info[:limited], **info.slice(:keys) }
|
|
169
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = record }
|
|
163
170
|
end
|
|
164
171
|
|
|
165
172
|
def refusal(limit, active)
|
|
@@ -11,9 +11,10 @@ module GraphQL
|
|
|
11
11
|
groups = {}
|
|
12
12
|
multiplex.queries.each do |query|
|
|
13
13
|
next unless invalid?(query, limiter)
|
|
14
|
+
next if Settings.delivery?(query)
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
limits = Settings.limits_for(limit, Array(
|
|
16
|
+
# Who pays does not change any answer: the response cache does not count these reads (Caching.neutral).
|
|
17
|
+
limits = Caching.neutral(query.context) { Settings.limits_for(limit, Array(Settings.key_for(reader, query)), query) }
|
|
17
18
|
(groups[limits] ||= []) << query unless limits.empty?
|
|
18
19
|
rescue StandardError => error
|
|
19
20
|
report(handler, error, query)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "graphql/armory/rate_limits/cached_charge"
|
|
4
|
+
|
|
3
5
|
module GraphQL
|
|
4
6
|
module Armory
|
|
5
7
|
module RateLimits
|
|
@@ -12,7 +14,13 @@ module GraphQL
|
|
|
12
14
|
# A query that costs more than the client has left is refused before any field resolves, and charged
|
|
13
15
|
# nothing. Soft mode (the default) charges and records but refuses nothing. What happened is in
|
|
14
16
|
# `context[:rate_limit_points]`. An invalid query, which no analyzer sees, costs `invalid_cost:`, charged by a trace.
|
|
17
|
+
# A response the cache answers is charged the points its run cost, through `charge_cached` (CachedCharge).
|
|
18
|
+
#
|
|
19
|
+
# The key, limit and exemption lookups and the limiter's own records go through `Caching.neutral`: who is charged
|
|
20
|
+
# does not change the answer, so the response cache does not count them as inputs of it.
|
|
15
21
|
class Points
|
|
22
|
+
include CachedCharge
|
|
23
|
+
|
|
16
24
|
CONTEXT_KEY = :rate_limit_points
|
|
17
25
|
NAMESPACE = :graphql_armory
|
|
18
26
|
|
|
@@ -101,15 +109,17 @@ module GraphQL
|
|
|
101
109
|
|
|
102
110
|
# Whether this query's client has no limit to charge.
|
|
103
111
|
def exempt?(query)
|
|
104
|
-
Settings.exempt?(@key, @limit, query)
|
|
112
|
+
Caching.neutral(query.context) { Settings.exempt?(@key, @limit, query) }
|
|
105
113
|
end
|
|
106
114
|
|
|
107
115
|
# The refusal for a query that costs `cost`, or nil to let it run.
|
|
108
116
|
def call(query, cost)
|
|
109
|
-
|
|
117
|
+
return nil if Settings.delivery?(query)
|
|
118
|
+
|
|
119
|
+
key = Caching.neutral(query.context) { Settings.key_for(@key, query) }
|
|
110
120
|
return call_several(query, key, cost) if key.is_a?(Array)
|
|
111
121
|
|
|
112
|
-
limit = Settings.limit_for(@limit, key, query)
|
|
122
|
+
limit = Caching.neutral(query.context) { Settings.limit_for(@limit, key, query) }
|
|
113
123
|
if limit.nil?
|
|
114
124
|
record(query, key: key, cost: cost, limit: nil, remaining: nil, limited: false)
|
|
115
125
|
return nil
|
|
@@ -119,7 +129,7 @@ module GraphQL
|
|
|
119
129
|
@store.take(key, cost: cost, limit: limit, window: @window, force: @soft)
|
|
120
130
|
rescue StandardError => error
|
|
121
131
|
# No record: a context hash shared with an earlier query must not keep its numbers.
|
|
122
|
-
query.context[CONTEXT_KEY] = nil
|
|
132
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = nil }
|
|
123
133
|
@on_store_error.call(error, query)
|
|
124
134
|
return nil
|
|
125
135
|
end
|
|
@@ -136,7 +146,7 @@ module GraphQL
|
|
|
136
146
|
# Two queries can race between the two steps: then the refused one leaves the other keys charged. A key whose store
|
|
137
147
|
# fails is left out, and the others still decide: an attacker who can pick a dead shard must not cancel a refusal.
|
|
138
148
|
def call_several(query, keys, cost)
|
|
139
|
-
limits = Settings.limits_for(@limit, keys, query)
|
|
149
|
+
limits = Caching.neutral(query.context) { Settings.limits_for(@limit, keys, query) }
|
|
140
150
|
if limits.empty?
|
|
141
151
|
record(query, key: keys.first, cost: cost, limit: nil, remaining: nil, limited: false, keys: [])
|
|
142
152
|
return nil
|
|
@@ -149,7 +159,7 @@ module GraphQL
|
|
|
149
159
|
takes(query, read ? read.to_h { |budget| budget.values_at(:key, :limit) } : limits, cost, @soft, charging: true)
|
|
150
160
|
end
|
|
151
161
|
if budgets.empty?
|
|
152
|
-
query.context[CONTEXT_KEY] = nil
|
|
162
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = nil }
|
|
153
163
|
return nil
|
|
154
164
|
end
|
|
155
165
|
|
|
@@ -187,11 +197,13 @@ module GraphQL
|
|
|
187
197
|
end
|
|
188
198
|
|
|
189
199
|
# In the context hash for instrumentation, and per query for `headers`: queries of a multiplex may share one
|
|
190
|
-
# context hash, where the last query's record wins.
|
|
200
|
+
# context hash, where the last query's record wins. The limiter's write is not the query's: a resolver that reads
|
|
201
|
+
# the record later reads an input of its answer, and the response cache must see that read.
|
|
191
202
|
def record(query, **info)
|
|
192
203
|
info = { **info.slice(:key, :cost, :limit, :remaining), window: @window, soft: @soft, limited: info[:limited], **info.slice(:keys, :invalid) }
|
|
193
204
|
query.context.namespace(NAMESPACE)[:points] = info
|
|
194
|
-
query.context[CONTEXT_KEY] = info
|
|
205
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = info }
|
|
206
|
+
info
|
|
195
207
|
end
|
|
196
208
|
|
|
197
209
|
def refusal(cost, limit, remaining)
|
|
@@ -223,7 +235,7 @@ module GraphQL
|
|
|
223
235
|
rescue GraphQL::Error => error
|
|
224
236
|
raise unless error.instance_of?(GraphQL::Error) && error.message.start_with?(UNSIZED_CONNECTION)
|
|
225
237
|
|
|
226
|
-
query.context[CONTEXT_KEY] = nil
|
|
238
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = nil }
|
|
227
239
|
return AnalysisGuard.answer(query, points) { GraphQL::AnalysisError.new("This connection needs first: or last:", extensions: { "code" => "PAGE_SIZE_REQUIRED" }) }
|
|
228
240
|
end
|
|
229
241
|
unless cost.is_a?(Numeric) && cost.finite? && !cost.negative?
|
|
@@ -15,6 +15,9 @@ module GraphQL
|
|
|
15
15
|
# that spends the last milliseconds finishes, and the next is refused. Soft mode (the default) charges and
|
|
16
16
|
# records but refuses nothing. What happened is in `context[:rate_limit_time]`. An invalid query, which no analyzer
|
|
17
17
|
# sees, takes its share of the time too.
|
|
18
|
+
#
|
|
19
|
+
# A response the response cache answers runs nothing, so Time does not bound it: only Points charges a hit. The
|
|
20
|
+
# key, limit and exemption lookups and the limiter's own records go through `Caching.neutral`.
|
|
18
21
|
class Time
|
|
19
22
|
CONTEXT_KEY = :rate_limit_time
|
|
20
23
|
NAMESPACE = :graphql_armory
|
|
@@ -86,19 +89,21 @@ module GraphQL
|
|
|
86
89
|
|
|
87
90
|
# Whether this query's client has no limit to charge.
|
|
88
91
|
def exempt?(query)
|
|
89
|
-
Settings.exempt?(@key, @limit, query)
|
|
92
|
+
Caching.neutral(query.context) { Settings.exempt?(@key, @limit, query) }
|
|
90
93
|
end
|
|
91
94
|
|
|
92
95
|
# The refusal for a client with no milliseconds left, or nil to let the operation run (and be charged).
|
|
93
96
|
def call(query)
|
|
97
|
+
return nil if Settings.delivery?(query)
|
|
98
|
+
|
|
94
99
|
unless query.current_trace.is_a?(@trace)
|
|
95
100
|
raise ConfigurationError, "this query runs with its own trace (context[:trace]), so Time could never charge it: remove context[:trace] or add trace_with to that trace"
|
|
96
101
|
end
|
|
97
102
|
|
|
98
|
-
key = Settings.key_for(@key, query)
|
|
103
|
+
key = Caching.neutral(query.context) { Settings.key_for(@key, query) }
|
|
99
104
|
return call_several(query, key) if key.is_a?(Array)
|
|
100
105
|
|
|
101
|
-
limit = Settings.limit_for(@limit, key, query)
|
|
106
|
+
limit = Caching.neutral(query.context) { Settings.limit_for(@limit, key, query) }
|
|
102
107
|
if limit.nil?
|
|
103
108
|
record(query, key: key, limit: nil, remaining: nil, limited: false)
|
|
104
109
|
return nil
|
|
@@ -108,7 +113,7 @@ module GraphQL
|
|
|
108
113
|
@store.take(self.class.bucket_key(key), cost: 0, limit: limit, window: @window, force: false)
|
|
109
114
|
rescue StandardError => error
|
|
110
115
|
# Fail open, and still measure: the charge may find the store back.
|
|
111
|
-
query.context[CONTEXT_KEY] = nil
|
|
116
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = nil }
|
|
112
117
|
@on_store_error.call(error, query)
|
|
113
118
|
owe(query, key, limit)
|
|
114
119
|
return nil
|
|
@@ -220,7 +225,7 @@ module GraphQL
|
|
|
220
225
|
# Reads every balance. A refusal charges none, as with one key; otherwise every key owes the whole time. A key whose
|
|
221
226
|
# store fails at the read still owes it, and the others still decide.
|
|
222
227
|
def call_several(query, keys)
|
|
223
|
-
limits = Settings.limits_for(@limit, keys, query)
|
|
228
|
+
limits = Caching.neutral(query.context) { Settings.limits_for(@limit, keys, query) }
|
|
224
229
|
if limits.empty?
|
|
225
230
|
record(query, key: keys.first, limit: nil, remaining: nil, limited: false, keys: [])
|
|
226
231
|
return nil
|
|
@@ -236,7 +241,7 @@ module GraphQL
|
|
|
236
241
|
{ key: key, limit: limit, remaining: Settings.checked(answer, :take, "the Integer milliseconds left").last }
|
|
237
242
|
end
|
|
238
243
|
if budgets.empty?
|
|
239
|
-
query.context[CONTEXT_KEY] = nil
|
|
244
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = nil }
|
|
240
245
|
first, *rest = limits.to_a
|
|
241
246
|
owe(query, first[0], first[1], others: rest.to_h)
|
|
242
247
|
return nil
|
|
@@ -263,7 +268,7 @@ module GraphQL
|
|
|
263
268
|
remaining = info[:remaining]
|
|
264
269
|
end
|
|
265
270
|
query.context.namespace(NAMESPACE)[:time] = info
|
|
266
|
-
query.context[CONTEXT_KEY] = info
|
|
271
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = info }
|
|
267
272
|
debt[:extension]&.merge!("limit" => info[:limit], "remaining" => remaining, "elapsed" => cost, "resetIn" => Points.reset_in(info))
|
|
268
273
|
end
|
|
269
274
|
|
|
@@ -274,10 +279,12 @@ module GraphQL
|
|
|
274
279
|
charges[self] = { key: key, limit: limit, info: info, extension: extension, others: others }
|
|
275
280
|
end
|
|
276
281
|
|
|
282
|
+
# The limiter's write is not the query's: a resolver that reads the record reads an input of its answer.
|
|
277
283
|
def record(query, **info)
|
|
278
284
|
info = base_record(**info)
|
|
279
285
|
query.context.namespace(NAMESPACE)[:time] = info
|
|
280
|
-
query.context[CONTEXT_KEY] = info
|
|
286
|
+
Caching.neutral(query.context) { query.context[CONTEXT_KEY] = info }
|
|
287
|
+
info
|
|
281
288
|
end
|
|
282
289
|
|
|
283
290
|
# `elapsed` is nil until the operation ends and is charged; it stays nil for a refused one.
|
|
@@ -33,10 +33,19 @@ module GraphQL
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def exempt?(reader, limit, query)
|
|
36
|
+
return true if delivery?(query)
|
|
37
|
+
|
|
36
38
|
key = key_for(reader, query)
|
|
37
39
|
key.is_a?(Array) ? limits_for(limit, key, query).empty? : limit_for(limit, key, query).nil?
|
|
38
40
|
end
|
|
39
41
|
|
|
42
|
+
# A subscription delivery is server work a trigger caused: no limiter charges it to the subscriber (the mark lives
|
|
43
|
+
# in a namespace no request can write; see Subscriptions.delivery?). Without the subscriptions module loaded,
|
|
44
|
+
# nothing can have marked it.
|
|
45
|
+
def delivery?(query)
|
|
46
|
+
defined?(Armory::Subscriptions.delivery?) ? Armory::Subscriptions.delivery?(query.context) : false
|
|
47
|
+
end
|
|
48
|
+
|
|
40
49
|
def several_keys?(keys)
|
|
41
50
|
keys.is_a?(Array) && keys.size.between?(1, MAX_KEYS) && keys.all? { |key| key.is_a?(String) && !key.empty? } && keys.map(&:b).uniq.size == keys.size
|
|
42
51
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "openssl"
|
|
5
|
+
|
|
6
|
+
module GraphQL
|
|
7
|
+
module Armory
|
|
8
|
+
module Subscriptions
|
|
9
|
+
class Ably
|
|
10
|
+
# An Ably API key, `"<keyName>:<keySecret>"` (keyName is `<appId>.<keyId>`). It signs and never shows the
|
|
11
|
+
# secret: `inspect` names nothing.
|
|
12
|
+
class Key
|
|
13
|
+
NAME = /\A[A-Za-z0-9_.-]+\z/
|
|
14
|
+
SECRET = /\A\S+\z/
|
|
15
|
+
|
|
16
|
+
attr_reader :name
|
|
17
|
+
|
|
18
|
+
def self.parse(text)
|
|
19
|
+
raise ConfigurationError, "key: must be \"<keyName>:<keySecret>\"" unless text.is_a?(String)
|
|
20
|
+
|
|
21
|
+
name, secret = text.split(":", 2)
|
|
22
|
+
unless name && secret && NAME.match?(name) && SECRET.match?(secret)
|
|
23
|
+
raise ConfigurationError, "key: must be \"<keyName>:<keySecret>\" (the Ably API key, no spaces)"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
new(name, secret)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize(name, secret)
|
|
30
|
+
@name = name.dup.freeze
|
|
31
|
+
@secret = secret.dup.freeze
|
|
32
|
+
freeze
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The key id (the part of the name after the app id): what `X-Ably-Key` may carry instead of the name.
|
|
36
|
+
def id = name.split(".", 2).last
|
|
37
|
+
|
|
38
|
+
def basic_auth = "Basic #{Base64.strict_encode64("#{name}:#{@secret}")}"
|
|
39
|
+
|
|
40
|
+
# base64(HMAC-SHA256(secret, text)): the `mac` of a TokenRequest and the `X-Ably-Signature` of a webhook.
|
|
41
|
+
def sign(text) = Base64.strict_encode64(OpenSSL::HMAC.digest("SHA256", @secret, text))
|
|
42
|
+
|
|
43
|
+
def inspect = "#<#{self.class.name}>"
|
|
44
|
+
alias_method :to_s, :inspect
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Subscriptions
|
|
8
|
+
class Ably
|
|
9
|
+
# Ably's REST batch publish: `POST https://<host>/messages` with an Array of `{channels, messages}` specs, one
|
|
10
|
+
# spec per message (https://ably.com/docs/api/rest-api). A 200 answers `[{channel, messageId}, ...]`; when some
|
|
11
|
+
# inner requests fail it answers 400 with `error.code` 40020 and a `batchResponse` of per-channel results,
|
|
12
|
+
# each `{channel, messageId}` or `{channel, error: {code, statusCode}}`.
|
|
13
|
+
#
|
|
14
|
+
# Every answer becomes an Outcome by message index; nothing a provider says raises. A failure to get an answer
|
|
15
|
+
# (`TransportError`) is `:retry` for the messages of that request only.
|
|
16
|
+
class Publisher
|
|
17
|
+
PARTIAL_FAILURE = 40_020
|
|
18
|
+
TOO_LARGE_CODE = 40_009
|
|
19
|
+
|
|
20
|
+
def initialize(key:, url:, http:, max_payload_bytes:, max_batch:)
|
|
21
|
+
@key = key
|
|
22
|
+
@url = url
|
|
23
|
+
@http = http
|
|
24
|
+
@max_payload_bytes = max_payload_bytes
|
|
25
|
+
@max_batch = max_batch
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call(messages)
|
|
29
|
+
raise ArgumentError, "publish takes an Array of Messages" unless messages.is_a?(Array) && messages.all?(Message)
|
|
30
|
+
|
|
31
|
+
outcomes = Array.new(messages.size)
|
|
32
|
+
sendable = []
|
|
33
|
+
messages.each_with_index do |message, index|
|
|
34
|
+
data = encode(message)
|
|
35
|
+
if data.is_a?(Symbol)
|
|
36
|
+
outcomes[index] = Outcome.new(status: :refused, index: index, reason: data)
|
|
37
|
+
else
|
|
38
|
+
sendable << [index, message, data]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
sendable.each_slice(@max_batch) { |chunk| send_chunk(chunk, outcomes) }
|
|
42
|
+
outcomes
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# The JSON text of the payload, or the reason it cannot be sent.
|
|
48
|
+
def encode(message)
|
|
49
|
+
data = JSON.generate(message.payload)
|
|
50
|
+
data.bytesize > @max_payload_bytes ? :too_large : data
|
|
51
|
+
rescue JSON::JSONError, EncodingError
|
|
52
|
+
:unencodable
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def send_chunk(chunk, outcomes)
|
|
56
|
+
verdict = verdict_for(chunk)
|
|
57
|
+
chunk.each do |index, message, _data|
|
|
58
|
+
outcomes[index] =
|
|
59
|
+
if verdict.first == :all
|
|
60
|
+
Outcome.new(status: verdict[1], index: index, reason: verdict[2])
|
|
61
|
+
else
|
|
62
|
+
outcome_for(verdict[1][message.channel]&.shift, index)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# [:all, status, reason] when one answer covers the whole request, or [:each, {channel => [entry, ...]}].
|
|
68
|
+
def verdict_for(chunk)
|
|
69
|
+
response = post(chunk)
|
|
70
|
+
return [:all, :retry, :transport] unless response
|
|
71
|
+
|
|
72
|
+
parsed = parse(response.body)
|
|
73
|
+
return success_verdict(parsed) if response.success?
|
|
74
|
+
|
|
75
|
+
error = parsed.is_a?(Hash) && parsed["error"].is_a?(Hash) ? parsed["error"] : {}
|
|
76
|
+
entries = parsed.is_a?(Hash) ? (parsed["batchResponse"] || error["batchResponse"]) : nil
|
|
77
|
+
return [:each, group(entries)] if error["code"] == PARTIAL_FAILURE && entries.is_a?(Array) && entries.all?(Hash)
|
|
78
|
+
|
|
79
|
+
[:all, *classify(error["code"], response.status)]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def success_verdict(parsed)
|
|
83
|
+
return [:all, :retry, :bad_response] unless parsed.is_a?(Array) && parsed.all?(Hash)
|
|
84
|
+
|
|
85
|
+
[:each, group(parsed)]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The response, or nil when none came.
|
|
89
|
+
def post(chunk)
|
|
90
|
+
specs = chunk.map do |_index, message, data|
|
|
91
|
+
{ channels: [message.channel], messages: [{ name: message.event, data: data, encoding: "json" }] }
|
|
92
|
+
end
|
|
93
|
+
@http.post(@url, JSON.generate(specs), "Authorization" => @key.basic_auth, "Content-Type" => "application/json", "Accept" => "application/json")
|
|
94
|
+
rescue TransportError
|
|
95
|
+
nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def parse(text)
|
|
99
|
+
JSON.parse(text)
|
|
100
|
+
rescue JSON::ParserError
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def group(entries)
|
|
105
|
+
entries.each_with_object({}) { |entry, groups| (groups[entry["channel"]] ||= []) << entry }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def outcome_for(entry, index)
|
|
109
|
+
return Outcome.new(status: :retry, index: index, reason: :missing_result) unless entry
|
|
110
|
+
return Outcome.new(status: :ok, index: index) unless entry["error"]
|
|
111
|
+
|
|
112
|
+
error = entry["error"].is_a?(Hash) ? entry["error"] : {}
|
|
113
|
+
status, reason = classify(error["code"], error["statusCode"])
|
|
114
|
+
Outcome.new(status: status, index: index, reason: reason)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# [status, reason] for an Ably error code (e.g. 40009) and HTTP status.
|
|
118
|
+
def classify(code, status)
|
|
119
|
+
code = code.is_a?(Integer) ? code : 0
|
|
120
|
+
status = status.is_a?(Integer) ? status : 0
|
|
121
|
+
if status == 401 || status == 403 || code.between?(40_100, 40_399) then [:refused, :unauthorized]
|
|
122
|
+
elsif status == 413 || code == TOO_LARGE_CODE then [:refused, :too_large]
|
|
123
|
+
elsif status == 429 || code.between?(42_900, 42_999) then [:retry, :rate_limited]
|
|
124
|
+
elsif status >= 500 || code >= 50_000 then [:retry, :provider_error]
|
|
125
|
+
else [:refused, :bad_request]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Subscriptions
|
|
8
|
+
class Ably
|
|
9
|
+
# A signed Ably TokenRequest (https://ably.com/docs/api/token-request-spec): the app hands it to the client's
|
|
10
|
+
# `authCallback`/`authUrl`, and the client trades it with Ably for a token. Fields: `keyName`, `ttl` (ms),
|
|
11
|
+
# `capability` (the JSON text), `clientId`, `timestamp` (ms), `nonce`, `mac`. The clientId is the viewer digest
|
|
12
|
+
# (or "anonymous"): Ably's presence, and so the leave webhook, needs one.
|
|
13
|
+
#
|
|
14
|
+
# The signing text is keyName, ttl, capability, clientId, timestamp and nonce, each followed by a newline;
|
|
15
|
+
# mac = base64(HMAC-SHA256(keySecret, text)).
|
|
16
|
+
class TokenRequest
|
|
17
|
+
OPERATIONS = %w[presence subscribe].freeze
|
|
18
|
+
|
|
19
|
+
def initialize(key:, prefix:, ttl_seconds:, clock:, nonce:)
|
|
20
|
+
@key = key
|
|
21
|
+
@prefix = prefix
|
|
22
|
+
@ttl_seconds = ttl_seconds
|
|
23
|
+
@clock = clock
|
|
24
|
+
@nonce = nonce
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Credentials for the channels `<prefix>:<viewer_digest>:*` and no others (operations sorted, no whitespace,
|
|
28
|
+
# as the spec's canonical form asks), as the client `viewer_digest`.
|
|
29
|
+
def call(viewer_digest)
|
|
30
|
+
capability = JSON.generate("#{@prefix}:#{viewer_digest}:*" => OPERATIONS)
|
|
31
|
+
ttl = @ttl_seconds * 1000
|
|
32
|
+
timestamp = (@clock.call.to_f * 1000).to_i
|
|
33
|
+
nonce = @nonce.call.to_s
|
|
34
|
+
text = [@key.name, ttl, capability, viewer_digest, timestamp, nonce].map { |field| "#{field}\n" }.join
|
|
35
|
+
{ "keyName" => @key.name, "ttl" => ttl, "capability" => capability, "clientId" => viewer_digest, "timestamp" => timestamp, "nonce" => nonce,
|
|
36
|
+
"mac" => @key.sign(text) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "openssl"
|
|
5
|
+
|
|
6
|
+
module GraphQL
|
|
7
|
+
module Armory
|
|
8
|
+
module Subscriptions
|
|
9
|
+
class Ably
|
|
10
|
+
# Verifies and reads an Ably presence webhook (https://ably.com/docs/platform/integrations/webhooks). Order
|
|
11
|
+
# matters: the key header, the declared length and the signature are decided before the body is parsed.
|
|
12
|
+
#
|
|
13
|
+
# X-Ably-Key our key name (or its key id part)
|
|
14
|
+
# X-Ably-Signature base64(HMAC-SHA256(keySecret, raw body)), compared in constant time
|
|
15
|
+
#
|
|
16
|
+
# A body over 1 MB, or with a presence event more than 5 minutes from now, is refused. The answer is
|
|
17
|
+
# `[:ok, channels]`: the subscription channels with a presence `leave` (action 3, or "leave") from any client.
|
|
18
|
+
class Webhook
|
|
19
|
+
MAX_BODY_BYTES = 1_048_576
|
|
20
|
+
MAX_AGE_MS = 300_000
|
|
21
|
+
PRESENCE = "channel.presence"
|
|
22
|
+
LEAVE = [3, "leave"].freeze
|
|
23
|
+
|
|
24
|
+
def initialize(key:, clock:, id_from:)
|
|
25
|
+
@key = key
|
|
26
|
+
@clock = clock
|
|
27
|
+
@id_from = id_from
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call(env)
|
|
31
|
+
return [:refused, :bad_signature] unless env.respond_to?(:[]) && env.respond_to?(:key?)
|
|
32
|
+
return [:refused, :unknown_key] unless known_key?(env["HTTP_X_ABLY_KEY"])
|
|
33
|
+
|
|
34
|
+
input = env["rack.input"]
|
|
35
|
+
length = env["CONTENT_LENGTH"].to_s
|
|
36
|
+
return [:refused, :too_large] if /\A\d+\z/.match?(length) && length.to_i > MAX_BODY_BYTES
|
|
37
|
+
return [:refused, :bad_signature] unless input.respond_to?(:read)
|
|
38
|
+
|
|
39
|
+
raw = input.read(MAX_BODY_BYTES + 1).to_s
|
|
40
|
+
return [:refused, :too_large] if raw.bytesize > MAX_BODY_BYTES
|
|
41
|
+
return [:refused, :bad_signature] unless signed?(raw, env["HTTP_X_ABLY_SIGNATURE"])
|
|
42
|
+
|
|
43
|
+
events = events_in(parse(raw))
|
|
44
|
+
return [:refused, :bad_body] unless events
|
|
45
|
+
|
|
46
|
+
read(events)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def known_key?(header)
|
|
52
|
+
header.is_a?(String) && (header == @key.name || header == @key.id)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def signed?(raw, signature)
|
|
56
|
+
return false unless signature.is_a?(String) && !signature.empty?
|
|
57
|
+
|
|
58
|
+
OpenSSL.fixed_length_secure_compare(OpenSSL::Digest::SHA256.digest(@key.sign(raw)), OpenSSL::Digest::SHA256.digest(signature))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def parse(raw)
|
|
62
|
+
JSON.parse(raw)
|
|
63
|
+
rescue JSON::ParserError
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The presence events of a batched (`items`) or single enveloped body, as [{channel:, presence: [...]}], or nil.
|
|
68
|
+
def events_in(body)
|
|
69
|
+
return nil unless body.is_a?(Hash)
|
|
70
|
+
|
|
71
|
+
items = body["items"].is_a?(Array) ? body["items"] : [body]
|
|
72
|
+
items.select { |item| item.is_a?(Hash) && item["source"] == PRESENCE }.map do |item|
|
|
73
|
+
data = item["data"].is_a?(Hash) ? item["data"] : item
|
|
74
|
+
{ channel: data["channelId"] || data["channel"], presence: Array(data["presence"]).grep(Hash), timestamp: item["timestamp"] }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def read(events)
|
|
79
|
+
now = (@clock.call.to_f * 1000).to_i
|
|
80
|
+
channels = []
|
|
81
|
+
events.each do |event|
|
|
82
|
+
event[:presence].each do |message|
|
|
83
|
+
timestamp = message["timestamp"] || event[:timestamp]
|
|
84
|
+
return [:refused, :missing_timestamp] unless timestamp.is_a?(Integer)
|
|
85
|
+
return [:refused, :stale] if (now - timestamp).abs > MAX_AGE_MS
|
|
86
|
+
|
|
87
|
+
channels << event[:channel] if LEAVE.include?(message["action"]) && event[:channel].is_a?(String) && @id_from.call(event[:channel])
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
[:ok, channels.uniq]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|