standard_flags 0.2.2 → 0.3.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 +4 -4
- data/CHANGELOG.md +158 -0
- data/README.md +566 -35
- data/conformance/README.md +166 -0
- data/conformance/bucket_vectors.json +46 -0
- data/conformance/guards_and_errors.json +35 -0
- data/conformance/legacy_env.json +38 -0
- data/conformance/legacy_exact.json +90 -0
- data/conformance/operations/write_path.json +723 -0
- data/conformance/permanent_flags.json +38 -0
- data/conformance/resolution.json +68 -0
- data/conformance/runtime/client.json +1431 -0
- data/conformance/runtime/rules.json +1305 -0
- data/conformance/schema/fixture.schema.json +1340 -0
- data/conformance/versions/legacy_ranges.json +183 -0
- data/conformance/versions/version_ranges.json +503 -0
- data/conformance/wire/client_state.json +1905 -0
- data/conformance/wire/delivery.json +1541 -0
- data/lib/standard_flags/audience.rb +49 -0
- data/lib/standard_flags/client.rb +157 -0
- data/lib/standard_flags/config.rb +21 -0
- data/lib/standard_flags/context.rb +70 -17
- data/lib/standard_flags/controller.rb +92 -10
- data/lib/standard_flags/definition.rb +71 -8
- data/lib/standard_flags/delivery.rb +195 -0
- data/lib/standard_flags/errors.rb +33 -0
- data/lib/standard_flags/job.rb +12 -1
- data/lib/standard_flags/mcp/flags.rb +3 -3
- data/lib/standard_flags/mcp/gating.rb +151 -0
- data/lib/standard_flags/mcp.rb +64 -5
- data/lib/standard_flags/notifiers/logger.rb +4 -0
- data/lib/standard_flags/ofrep/controller.rb +211 -0
- data/lib/standard_flags/ofrep.rb +115 -0
- data/lib/standard_flags/open_feature/provider.rb +1 -1
- data/lib/standard_flags/operations/rules.rb +17 -4
- data/lib/standard_flags/operations/validation.rb +25 -4
- data/lib/standard_flags/operations.rb +0 -2
- data/lib/standard_flags/payload.rb +8 -3
- data/lib/standard_flags/registry.rb +157 -12
- data/lib/standard_flags/runtime/cache.rb +1 -0
- data/lib/standard_flags/runtime/resolver.rb +26 -17
- data/lib/standard_flags/runtime/values.rb +8 -1
- data/lib/standard_flags/runtime.rb +42 -1
- data/lib/standard_flags/semantic_version.rb +141 -0
- data/lib/standard_flags/subscribers.rb +3 -0
- data/lib/standard_flags/testing.rb +4 -2
- data/lib/standard_flags/tooling.rb +25 -3
- data/lib/standard_flags/version.rb +1 -1
- data/lib/standard_flags/version_range.rb +309 -0
- data/lib/standard_flags.rb +38 -4
- metadata +24 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
require "active_support/core_ext/class/attribute"
|
|
3
|
+
|
|
4
|
+
module StandardFlags
|
|
5
|
+
module OFREP
|
|
6
|
+
# The OFREP endpoints as a controller mixin. The app draws the routes and
|
|
7
|
+
# authenticates the request before they run, exactly as for its own API:
|
|
8
|
+
#
|
|
9
|
+
# # config/routes.rb
|
|
10
|
+
# post "ofrep/v1/evaluate/flags", to: "flags#ofrep_evaluate_flags"
|
|
11
|
+
# post "ofrep/v1/evaluate/flags/:key", to: "flags#ofrep_evaluate_flag"
|
|
12
|
+
#
|
|
13
|
+
# class FlagsController < Api::BaseController # ActionController::API
|
|
14
|
+
# include StandardFlags::OFREP::Controller
|
|
15
|
+
# standard_flags_ofrep audience: :mobile
|
|
16
|
+
#
|
|
17
|
+
# private
|
|
18
|
+
#
|
|
19
|
+
# def standard_flags_context = current_account
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# On an ActionController::Base controller with forgery protection, say
|
|
23
|
+
# how the endpoints treat it: `csrf: :skip` (skip it for these two
|
|
24
|
+
# actions; safe because they only accept `Content-Type: application/json`,
|
|
25
|
+
# which a cross-site form can't send without a CORS preflight, and right
|
|
26
|
+
# for bearer-token APIs) or `csrf: :protect` (keep Rails' check; the
|
|
27
|
+
# client sends the token). Leaving it out raises
|
|
28
|
+
# StandardFlags::ConfigurationError, when the class is declared or on
|
|
29
|
+
# the first request, instead of answering every request 422.
|
|
30
|
+
#
|
|
31
|
+
# * Units come from `standard_flags_context` (the authenticated caller),
|
|
32
|
+
# never from the body: its `targetingKey` is ignored. The body only adds
|
|
33
|
+
# client attributes (`platform`, `appVersion` / `app_version`,
|
|
34
|
+
# `build`), and an X-Client-* header wins over the same attribute in the
|
|
35
|
+
# body. With `config.read_client_headers` off, the request contributes
|
|
36
|
+
# no client at all.
|
|
37
|
+
# * The bulk endpoint answers `{ "flags": [...], "metadata": { "version":
|
|
38
|
+
# <etag> } }` with an ETag; If-None-Match naming it gets 304 and no
|
|
39
|
+
# body. Both endpoints send `Cache-Control: no-cache, private` and a
|
|
40
|
+
# Vary on Authorization and the client headers. A single-flag 200 also
|
|
41
|
+
# carries a strong ETag over its one entry, and honours If-None-Match.
|
|
42
|
+
# * Before anything else runs (Rails' parameter parsing, the app's
|
|
43
|
+
# filters, forgery protection), the mixin checks the request itself: a
|
|
44
|
+
# Content-Type other than application/json gets 415, and a body over
|
|
45
|
+
# OFREP::MAX_BODY_BYTES (read with a bounded read), not UTF-8, not JSON
|
|
46
|
+
# or not an evaluation request gets 400, both in OFREP's error shape
|
|
47
|
+
# (INVALID_CONTEXT). These answers come before authentication and say
|
|
48
|
+
# nothing about the caller. An app's `rescue_from StandardError` can't
|
|
49
|
+
# turn them into 5xx.
|
|
50
|
+
# * A flag the audience isn't told about, or whose min_client: the client
|
|
51
|
+
# doesn't meet, gets 404 FLAG_NOT_FOUND, so server-only keys stay
|
|
52
|
+
# invisible; one whose evaluation failed gets 400 with its error code.
|
|
53
|
+
# 401 and 403 stay the app's. Nothing answers 5xx on purpose.
|
|
54
|
+
#
|
|
55
|
+
# `standard_flags_ofrep audience: :pre_auth, anonymous: true` serves a
|
|
56
|
+
# controller with no authentication: only a `public: true` audience (see
|
|
57
|
+
# StandardFlags::Audience), evaluated with no actors and without calling
|
|
58
|
+
# `standard_flags_context`; the body's `targetingKey` becomes the key of
|
|
59
|
+
# the audience's `unit:`, when it declares one.
|
|
60
|
+
module Controller
|
|
61
|
+
extend ActiveSupport::Concern
|
|
62
|
+
include StandardFlags::Controller
|
|
63
|
+
|
|
64
|
+
ACTIONS = %w[ofrep_evaluate_flags ofrep_evaluate_flag].freeze
|
|
65
|
+
CSRF_CHOICES = [ nil, :skip, :protect ].freeze
|
|
66
|
+
# Longest key looked up; a longer one is a 404 without a lookup.
|
|
67
|
+
MAX_KEY_LENGTH = 256
|
|
68
|
+
# Longest requested key echoed back in an error.
|
|
69
|
+
MAX_KEY_ECHO = 200
|
|
70
|
+
|
|
71
|
+
included do
|
|
72
|
+
class_attribute :standard_flags_ofrep_options, instance_writer: false, default: nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
class_methods do
|
|
76
|
+
def standard_flags_ofrep(audience:, anonymous: false, csrf: nil)
|
|
77
|
+
raise ArgumentError, "anonymous: must be true or false" unless [ true, false ].include?(anonymous)
|
|
78
|
+
raise ArgumentError, "csrf: must be :skip or :protect" unless CSRF_CHOICES.include?(csrf)
|
|
79
|
+
|
|
80
|
+
self.standard_flags_ofrep_options = { audience: audience.to_sym, anonymous: anonymous, csrf: csrf }.freeze
|
|
81
|
+
wrap_parameters false if respond_to?(:wrap_parameters)
|
|
82
|
+
skip_forgery_protection(only: ACTIONS.map(&:to_sym)) if csrf == :skip && respond_to?(:skip_forgery_protection)
|
|
83
|
+
standard_flags_ofrep_check_csrf!
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Whether Rails' forgery protection runs before this controller's
|
|
87
|
+
# actions (whatever `allow_forgery_protection` says right now).
|
|
88
|
+
def standard_flags_forgery_protected?
|
|
89
|
+
respond_to?(:_process_action_callbacks) &&
|
|
90
|
+
_process_action_callbacks.any? { |callback| callback.filter == :verify_authenticity_token }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def standard_flags_ofrep_check_csrf!
|
|
94
|
+
options = standard_flags_ofrep_options
|
|
95
|
+
return if options.nil? || !options[:csrf].nil? || !standard_flags_forgery_protected?
|
|
96
|
+
|
|
97
|
+
raise ConfigurationError,
|
|
98
|
+
"#{name || 'this controller'} has forgery protection: declare `standard_flags_ofrep ..., csrf: :skip` " \
|
|
99
|
+
"(bearer-token APIs; the endpoints accept only application/json) or `csrf: :protect` (clients send the " \
|
|
100
|
+
"CSRF token)"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# POST /ofrep/v1/evaluate/flags
|
|
105
|
+
def ofrep_evaluate_flags
|
|
106
|
+
result = standard_flags_ofrep_result(standard_flags_ofrep_request)
|
|
107
|
+
standard_flags_ofrep_headers(result.etag_header)
|
|
108
|
+
if result.fresh?(request.headers["If-None-Match"])
|
|
109
|
+
head :not_modified
|
|
110
|
+
else
|
|
111
|
+
render json: result.to_h
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# POST /ofrep/v1/evaluate/flags/:key, where :key is a wire name.
|
|
116
|
+
def ofrep_evaluate_flag
|
|
117
|
+
key = request.path_parameters[:key].to_s
|
|
118
|
+
result = standard_flags_ofrep_result(standard_flags_ofrep_request, only: key) if key.length <= MAX_KEY_LENGTH
|
|
119
|
+
entry = result&.[](key)
|
|
120
|
+
if entry.nil?
|
|
121
|
+
standard_flags_ofrep_headers
|
|
122
|
+
echo = key[0, MAX_KEY_ECHO]
|
|
123
|
+
render json: { key: echo, errorCode: ErrorCode::FLAG_NOT_FOUND, errorDetails: "flag #{echo} was not found" },
|
|
124
|
+
status: :not_found
|
|
125
|
+
elsif entry.error?
|
|
126
|
+
standard_flags_ofrep_headers
|
|
127
|
+
render json: entry.to_h, status: :bad_request
|
|
128
|
+
else
|
|
129
|
+
standard_flags_ofrep_single(result, entry)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Checks the request before Rails' instrumentation (which decodes a
|
|
134
|
+
# JSON body), the app's filters and forgery protection run, and answers
|
|
135
|
+
# a bad one itself, outside the rescue chain.
|
|
136
|
+
def process_action(*)
|
|
137
|
+
return super unless ACTIONS.include?(action_name.to_s)
|
|
138
|
+
|
|
139
|
+
self.class.standard_flags_ofrep_check_csrf!
|
|
140
|
+
@standard_flags_ofrep_request = OFREP.read(request)
|
|
141
|
+
super
|
|
142
|
+
rescue OFREP::InvalidRequest, OFREP::UnsupportedMediaType => e
|
|
143
|
+
raise unless @standard_flags_ofrep_request.nil?
|
|
144
|
+
|
|
145
|
+
standard_flags_ofrep_refuse(e)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
# A deliberate strong ETag over the one entry (so Rack::ETag adds no
|
|
151
|
+
# weak one), with If-None-Match honoured as on the bulk endpoint.
|
|
152
|
+
def standard_flags_ofrep_single(result, entry)
|
|
153
|
+
etag = Delivery::Result.new(audience: result.audience, entries: [ entry ].freeze,
|
|
154
|
+
etag: Delivery.etag(result.audience, [ entry ]))
|
|
155
|
+
standard_flags_ofrep_headers(etag.etag_header)
|
|
156
|
+
etag.fresh?(request.headers["If-None-Match"]) ? head(:not_modified) : render(json: entry.to_h)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def standard_flags_ofrep_request
|
|
160
|
+
@standard_flags_ofrep_request or raise Error, "#{self.class.name}: the OFREP request was not read"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def standard_flags_ofrep_result(parsed, only: nil)
|
|
164
|
+
options = standard_flags_ofrep_options
|
|
165
|
+
raise Error, "#{self.class.name} needs `standard_flags_ofrep audience: ...`" unless options
|
|
166
|
+
|
|
167
|
+
client = standard_flags_ofrep_client(parsed.client)
|
|
168
|
+
if options[:anonymous]
|
|
169
|
+
Delivery.build_anonymous(options[:audience], client: client, targeting_key: parsed.targeting_key, only: only)
|
|
170
|
+
else
|
|
171
|
+
context = Context.wrap(standard_flags_context)
|
|
172
|
+
context = context.with_client(client) if context.client.empty?
|
|
173
|
+
Delivery.build(options[:audience], context, only: only)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# The request's X-Client-* headers, with body attributes filling the
|
|
178
|
+
# fields the headers don't carry; nothing at all when
|
|
179
|
+
# `config.read_client_headers` is off.
|
|
180
|
+
def standard_flags_ofrep_client(body_client)
|
|
181
|
+
return Client.empty unless StandardFlags.config.read_client_headers
|
|
182
|
+
|
|
183
|
+
merged = body_client.to_h.merge(standard_flags_client.to_h)
|
|
184
|
+
merged.empty? ? Client.empty : Client.new(**merged)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def standard_flags_ofrep_headers(etag = nil)
|
|
188
|
+
response.headers["ETag"] = etag if etag
|
|
189
|
+
# Through Rails' own Cache-Control handling, which would rewrite a
|
|
190
|
+
# raw "private, no-cache" header to "no-cache": this gives
|
|
191
|
+
# "no-cache, private".
|
|
192
|
+
response.cache_control.merge!(no_cache: true, extras: [ "private" ])
|
|
193
|
+
vary = response.headers["Vary"].to_s.split(",").map(&:strip).reject(&:empty?)
|
|
194
|
+
vary += [ "Authorization", *StandardFlags.config.client_headers.values.compact.map(&:to_s) ]
|
|
195
|
+
response.headers["Vary"] = vary.uniq(&:downcase).join(", ")
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def standard_flags_ofrep_refuse(exception)
|
|
199
|
+
body = { errorCode: ErrorCode::INVALID_CONTEXT, errorDetails: exception.message }
|
|
200
|
+
key = request.path_parameters[:key]
|
|
201
|
+
body = { key: key.to_s[0, MAX_KEY_ECHO] }.merge(body) if key
|
|
202
|
+
standard_flags_ofrep_headers
|
|
203
|
+
# Set directly rather than through `render`, whose Vary handling
|
|
204
|
+
# reads params, which would parse the body being refused.
|
|
205
|
+
self.status = exception.is_a?(OFREP::UnsupportedMediaType) ? 415 : 400
|
|
206
|
+
self.content_type = "application/json"
|
|
207
|
+
self.response_body = JSON.generate(body)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
# OpenFeature Remote Evaluation Protocol (OFREP) support: the request
|
|
5
|
+
# side. StandardFlags::Delivery builds the responses, and
|
|
6
|
+
# StandardFlags::OFREP::Controller (actionpack) serves them.
|
|
7
|
+
#
|
|
8
|
+
# Validated against the OFREP OpenAPI document 0.4.0
|
|
9
|
+
# (open-feature/protocol, service/openapi.yaml at 98c4e0de55).
|
|
10
|
+
module OFREP
|
|
11
|
+
autoload :Controller, "standard_flags/ofrep/controller"
|
|
12
|
+
|
|
13
|
+
# Longest request body read, in bytes.
|
|
14
|
+
MAX_BODY_BYTES = 64 * 1024
|
|
15
|
+
# Longest `targetingKey` an anonymous request may name.
|
|
16
|
+
MAX_TARGETING_KEY = 256
|
|
17
|
+
|
|
18
|
+
# The one media type the endpoints accept (any charset parameter).
|
|
19
|
+
MEDIA_TYPE = "application/json".freeze
|
|
20
|
+
|
|
21
|
+
# A body OFREP can't accept. The message is safe to return to the client.
|
|
22
|
+
class InvalidRequest < Error; end
|
|
23
|
+
|
|
24
|
+
# A request whose Content-Type isn't application/json (answered 415).
|
|
25
|
+
class UnsupportedMediaType < Error; end
|
|
26
|
+
|
|
27
|
+
# What a request body says: the client attributes it carries, and its
|
|
28
|
+
# `targetingKey` (used only before authentication, for a public
|
|
29
|
+
# audience's unit).
|
|
30
|
+
Request = Data.define(:client, :targeting_key)
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
# Reads and parses a Rack request's evaluation body before Rails
|
|
35
|
+
# touches it: the Content-Type must be application/json, and at most
|
|
36
|
+
# MAX_BODY_BYTES are read (a declared Content-Length over the limit is
|
|
37
|
+
# refused without reading). The bytes read become the request's
|
|
38
|
+
# RAW_POST_DATA, so Rails' own parameter parsing, if anything asks for
|
|
39
|
+
# it later, sees the same (already checked) body.
|
|
40
|
+
def read(request)
|
|
41
|
+
unless request.media_type.to_s.downcase == MEDIA_TYPE
|
|
42
|
+
raise UnsupportedMediaType, "Content-Type must be #{MEDIA_TYPE}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
length = request.content_length.to_i
|
|
46
|
+
raise InvalidRequest, too_large if length > MAX_BODY_BYTES
|
|
47
|
+
|
|
48
|
+
body = bounded_body(request)
|
|
49
|
+
raise InvalidRequest, too_large if body.bytesize > MAX_BODY_BYTES
|
|
50
|
+
|
|
51
|
+
request.set_header("RAW_POST_DATA", body)
|
|
52
|
+
parse(body)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def bounded_body(request)
|
|
56
|
+
io = request.get_header("rack.input")
|
|
57
|
+
return "".b if io.nil?
|
|
58
|
+
|
|
59
|
+
io.rewind if io.respond_to?(:rewind)
|
|
60
|
+
body = io.read(MAX_BODY_BYTES + 1) || "".b
|
|
61
|
+
io.rewind if io.respond_to?(:rewind)
|
|
62
|
+
body
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def too_large
|
|
66
|
+
"the request body is larger than #{MAX_BODY_BYTES} bytes"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Parses an evaluation request body (`{ "context": { ... } }`). Raises
|
|
70
|
+
# InvalidRequest for anything that isn't one. Client attributes
|
|
71
|
+
# (`platform`, `appVersion` / `app_version`, `build`) are checked like
|
|
72
|
+
# the X-Client-* headers: a value that fails is dropped, not refused.
|
|
73
|
+
def parse(body)
|
|
74
|
+
body = body.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
75
|
+
raise InvalidRequest, too_large if body.bytesize > MAX_BODY_BYTES
|
|
76
|
+
raise InvalidRequest, "the request body is not valid UTF-8" unless body.valid_encoding?
|
|
77
|
+
|
|
78
|
+
document = parse_json(body)
|
|
79
|
+
context = document["context"] if document.is_a?(Hash)
|
|
80
|
+
raise InvalidRequest, "the request body must be a JSON object with a context object" unless context.is_a?(Hash)
|
|
81
|
+
|
|
82
|
+
Request.new(client: Client.from_context(check_fields!(context)), targeting_key: targeting_key(context))
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def parse_json(body)
|
|
86
|
+
JSON.parse(body)
|
|
87
|
+
rescue JSON::ParserError
|
|
88
|
+
raise InvalidRequest, "the request body is not valid JSON"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Checks the property each client field is read from (the first one
|
|
92
|
+
# present, as Client.from_context reads it).
|
|
93
|
+
def check_fields!(context)
|
|
94
|
+
Client::CONTEXT_FIELDS.each do |field, names|
|
|
95
|
+
name = names.find { |candidate| !context[candidate].nil? }
|
|
96
|
+
next if name.nil?
|
|
97
|
+
|
|
98
|
+
value = context[name]
|
|
99
|
+
next if value.is_a?(String) || (field == :build && value.is_a?(Integer))
|
|
100
|
+
|
|
101
|
+
raise InvalidRequest, "context property '#{name}' must be a string"
|
|
102
|
+
end
|
|
103
|
+
context
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def targeting_key(context)
|
|
107
|
+
value = context["targetingKey"]
|
|
108
|
+
return nil if value.nil?
|
|
109
|
+
raise InvalidRequest, "context property 'targetingKey' must be a string" unless value.is_a?(String)
|
|
110
|
+
raise InvalidRequest, "context property 'targetingKey' is longer than #{MAX_TARGETING_KEY} characters" if value.length > MAX_TARGETING_KEY
|
|
111
|
+
|
|
112
|
+
value.strip.empty? ? nil : value
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -71,7 +71,7 @@ module StandardFlags
|
|
|
71
71
|
object: []
|
|
72
72
|
}.freeze
|
|
73
73
|
|
|
74
|
-
CLIENT_FIELDS =
|
|
74
|
+
CLIENT_FIELDS = Client::CONTEXT_FIELDS
|
|
75
75
|
|
|
76
76
|
# A context field that can't be a unit key (a Hash, say).
|
|
77
77
|
class InvalidContext < StandardError; end
|
|
@@ -145,17 +145,23 @@ module StandardFlags
|
|
|
145
145
|
|
|
146
146
|
# Replaces the rule's conditions: `platforms:` (client platforms that
|
|
147
147
|
# may get the percentage and variants), `versions:` (a version range per
|
|
148
|
-
# platform)
|
|
149
|
-
# `
|
|
148
|
+
# platform), `builds:` (a build-number range per platform) and
|
|
149
|
+
# `segments:` (`{ name => value }`, asked of `config.segment_resolver`,
|
|
150
|
+
# in order). Omitted ones are cleared.
|
|
151
|
+
#
|
|
152
|
+
# Version ranges are stored in the neutral syntax (`">=1.4.0 <2.0.0"`);
|
|
153
|
+
# a RubyGems requirement (`"~> 1.4"`, `">= 1.4, < 2"`) is accepted and
|
|
154
|
+
# stored translated.
|
|
150
155
|
class SetConditions < Base
|
|
151
|
-
arguments optional: %i[platforms versions segments]
|
|
156
|
+
arguments optional: %i[platforms versions builds segments]
|
|
152
157
|
|
|
153
158
|
private
|
|
154
159
|
|
|
155
160
|
def event = Events::FLAG_EVENTS[:conditions_set]
|
|
156
161
|
|
|
157
162
|
def perform
|
|
158
|
-
conditions = { "platforms" => platforms, "versions" => versions, "
|
|
163
|
+
conditions = { "platforms" => platforms, "versions" => versions, "builds" => builds, "segments" => segments }
|
|
164
|
+
.reject { |_, v| v.empty? }
|
|
159
165
|
validate.conditions_size!(conditions, config)
|
|
160
166
|
save!(conditions: conditions)
|
|
161
167
|
nil
|
|
@@ -177,6 +183,13 @@ module StandardFlags
|
|
|
177
183
|
ranges.to_h { |platform, range| [ platform.to_s.downcase, validate.version_range!(range) ] }
|
|
178
184
|
end
|
|
179
185
|
|
|
186
|
+
def builds
|
|
187
|
+
ranges = arg(:builds) || {}
|
|
188
|
+
raise Failure.new(:invalid, "builds must be a Hash of platform => range") unless ranges.is_a?(Hash)
|
|
189
|
+
|
|
190
|
+
ranges.to_h { |platform, range| [ platform.to_s.downcase, validate.build_range!(range) ] }
|
|
191
|
+
end
|
|
192
|
+
|
|
180
193
|
def segments
|
|
181
194
|
given = arg(:segments) || {}
|
|
182
195
|
pairs = given.is_a?(Hash) ? given.to_a : Array(given).map { |s| s.to_h.transform_keys(&:to_s).values_at("name", "value") }
|
|
@@ -54,13 +54,33 @@ module StandardFlags
|
|
|
54
54
|
unit
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
# The stored form of a version range: as given when it is already in
|
|
58
|
+
# the neutral syntax, else the neutral translation of a RubyGems
|
|
59
|
+
# requirement (`"~> 1.4"` → `">=1.4.0 <2.0.0-0"`), else (a RubyGems
|
|
60
|
+
# requirement the neutral syntax can't express, such as
|
|
61
|
+
# `">= 1.4.0.1"`) as given, which evaluates through Gem::Requirement.
|
|
57
62
|
def version_range!(range)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
text = range.to_s
|
|
64
|
+
return text if VersionRange.neutral(text)
|
|
65
|
+
|
|
66
|
+
translated = VersionRange.translate(text)
|
|
67
|
+
return translated if translated
|
|
68
|
+
return text if VersionRange.parse(text)
|
|
69
|
+
|
|
61
70
|
invalid!("version range #{range.inspect} does not parse")
|
|
62
71
|
end
|
|
63
72
|
|
|
73
|
+
# A stored version range the read path can evaluate, in either syntax.
|
|
74
|
+
def stored_version_range!(range)
|
|
75
|
+
VersionRange.parse(range.to_s) || invalid!("version range #{range.inspect} does not parse")
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def build_range!(range)
|
|
79
|
+
return range.to_s if BuildRange.parse(range.to_s)
|
|
80
|
+
|
|
81
|
+
invalid!("build range #{range.inspect} does not parse")
|
|
82
|
+
end
|
|
83
|
+
|
|
64
84
|
def conditions_size!(conditions, config)
|
|
65
85
|
bytes = JSON.generate(conditions).bytesize
|
|
66
86
|
return if bytes <= config.max_conditions_bytes
|
|
@@ -108,7 +128,8 @@ module StandardFlags
|
|
|
108
128
|
invalid!(":#{definition.key} has a percentage but no rollout value or variant weights to serve")
|
|
109
129
|
end
|
|
110
130
|
rule.platforms
|
|
111
|
-
rule.versions.each_value { |range|
|
|
131
|
+
rule.versions.each_value { |range| stored_version_range!(range) }
|
|
132
|
+
rule.builds.each_value { |range| build_range!(range) }
|
|
112
133
|
rule.segments.each { |segment| value!(definition, segment["value"], "segment #{segment['name']}'s value") }
|
|
113
134
|
rule.cohort_attachments.each do |key, value|
|
|
114
135
|
cohort!(definition, key, cohorts[key])
|
|
@@ -22,8 +22,6 @@ module StandardFlags
|
|
|
22
22
|
# Each is called as `.call(key_or_cohort, actor:, reason:, expected_revision: nil, **args)` and
|
|
23
23
|
# returns a StandardFlags::Result. See Base for the steps every one takes.
|
|
24
24
|
module Operations
|
|
25
|
-
SCOPES = %w[flags:read flags:write flags:ramp].freeze
|
|
26
|
-
|
|
27
25
|
# Names a cohort where an operation takes a flag key or a cohort, for
|
|
28
26
|
# example `AddMembers.call(Operations.cohort(:staff), ...)`. The string
|
|
29
27
|
# "cohort:staff" works too.
|
|
@@ -5,14 +5,19 @@ module StandardFlags
|
|
|
5
5
|
# The capability payload a client audience sees: `{ wire_name => value }`
|
|
6
6
|
# for flags that declare `expose:` for that audience, and nothing else.
|
|
7
7
|
# Unexposed keys never appear, and wire names are decoupled from keys so a
|
|
8
|
-
# renamed or removed flag doesn't break a shipped client.
|
|
8
|
+
# renamed or removed flag doesn't break a shipped client. Each alias of a
|
|
9
|
+
# flag carries the same value.
|
|
10
|
+
#
|
|
11
|
+
# This flat format is for pages the server renders (the Inertia prop): it
|
|
12
|
+
# applies neither `min_client:` nor tombstones' static wires. Clients with
|
|
13
|
+
# builds of their own use StandardFlags::Delivery (`format: :ofrep`).
|
|
9
14
|
module Payload
|
|
10
15
|
module_function
|
|
11
16
|
|
|
12
17
|
def build(registry, audience, context)
|
|
13
18
|
reader = Reader.new(registry, context)
|
|
14
|
-
registry.
|
|
15
|
-
[
|
|
19
|
+
registry.wires(audience).to_h do |wire, definition|
|
|
20
|
+
[ wire, wire_value(definition, reader.evaluate(definition.key)) ]
|
|
16
21
|
end
|
|
17
22
|
end
|
|
18
23
|
|