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,49 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# A client audience: a name flags `expose:` wire names to (`:web`,
|
|
3
|
+
# `:mobile`), and whether it is served before authentication.
|
|
4
|
+
#
|
|
5
|
+
# config.audiences = { web: {}, mobile: {}, pre_auth: { public: true, unit: :install } }
|
|
6
|
+
#
|
|
7
|
+
# A `public: true` audience is the only kind an `anonymous: true` OFREP
|
|
8
|
+
# controller serves. Its flags may target by client attributes, and by
|
|
9
|
+
# `unit:` (a declared unit) whose key the request's `targetingKey` names;
|
|
10
|
+
# every other unit resolves to nothing for it, and segments are skipped.
|
|
11
|
+
#
|
|
12
|
+
# Without `config.audiences`, every audience a flag exposes to is a
|
|
13
|
+
# private audience, which is how 0.2.x behaved.
|
|
14
|
+
Audience = Data.define(:name, :public, :unit) do
|
|
15
|
+
# `{ name => Audience }` from `config.audiences` (an Array of names or a
|
|
16
|
+
# Hash of name => options), or nil for nil.
|
|
17
|
+
def self.normalize(value)
|
|
18
|
+
return nil if value.nil?
|
|
19
|
+
|
|
20
|
+
pairs =
|
|
21
|
+
case value
|
|
22
|
+
when Hash then value
|
|
23
|
+
when Array then value.to_h { |name| [ name, {} ] }
|
|
24
|
+
else raise ArgumentError, "config.audiences must be an Array of names or a Hash of name => options"
|
|
25
|
+
end
|
|
26
|
+
pairs.to_h { |name, options| [ name.to_sym, build(name.to_sym, options) ] }.freeze
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.build(name, options)
|
|
30
|
+
raise ArgumentError, "audience :#{name} options must be a Hash" unless options.nil? || options.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
options = (options || {}).transform_keys(&:to_sym)
|
|
33
|
+
unknown = options.keys - %i[public unit]
|
|
34
|
+
raise ArgumentError, "audience :#{name} has unknown option(s): #{unknown.map(&:inspect).join(', ')}" unless unknown.empty?
|
|
35
|
+
|
|
36
|
+
public = options.fetch(:public, false)
|
|
37
|
+
raise ArgumentError, "audience :#{name} public: must be true or false" unless [ true, false ].include?(public)
|
|
38
|
+
|
|
39
|
+
unit = options[:unit]&.to_sym
|
|
40
|
+
raise ArgumentError, "audience :#{name} unit: only applies to a public: true audience" if unit && !public
|
|
41
|
+
|
|
42
|
+
new(name: name, public: public, unit: unit)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def public?
|
|
46
|
+
public
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
# The calling client: platform, version and build. A frozen Hash of the
|
|
3
|
+
# attributes as given (so `fetch`, `dig`, `key?`, `each` and `to_h` work as
|
|
4
|
+
# they did on 0.2.2's client Hash), plus typed readers:
|
|
5
|
+
#
|
|
6
|
+
# client = StandardFlags::Client.from_headers(request.headers)
|
|
7
|
+
# client.platform # => "android"
|
|
8
|
+
# client.version # => #<StandardFlags::Version 1.4.0>
|
|
9
|
+
# client.build # => 381
|
|
10
|
+
# client[:version] # => "1.4.0" (as received)
|
|
11
|
+
#
|
|
12
|
+
# `from_headers` reads `X-Client-Platform`, `X-Client-Version` and
|
|
13
|
+
# `X-Client-Build` (the names are `config.client_headers`). The platform is
|
|
14
|
+
# downcased and must be one of `config.client_platforms`; the version must
|
|
15
|
+
# parse as a StandardFlags::Version; the build must be 1 to 18 digits. A
|
|
16
|
+
# version or build that fails is dropped, as if the header were missing; a
|
|
17
|
+
# platform that fails becomes "unknown" (below). Either way it emits
|
|
18
|
+
# `client_invalid.standard_flags`; nothing raises, whatever bytes arrive,
|
|
19
|
+
# and nothing is reported as an error. A request with none of the headers
|
|
20
|
+
# gives an empty client.
|
|
21
|
+
#
|
|
22
|
+
# Runtime rules scope version and build ranges to a platform. An empty
|
|
23
|
+
# client (no platform) fails a rule's platform list but skips its
|
|
24
|
+
# per-platform ranges, as in 0.2.2. A client whose platform is "unknown"
|
|
25
|
+
# (sent but not recognised) fails every platform-scoped condition: the
|
|
26
|
+
# platform list and every version or build range. So a misspelt platform
|
|
27
|
+
# header can't step around a version floor.
|
|
28
|
+
#
|
|
29
|
+
# `Client.new(platform: "ios", version: "2.1.0")` (or a Context's
|
|
30
|
+
# `client:` Hash) keeps whatever it is given, the way 0.2.2 did, other keys
|
|
31
|
+
# included. The typed readers parse it with no allowlist and no
|
|
32
|
+
# notification: platform stripped and downcased, version as a Version (via
|
|
33
|
+
# `to_s`) or nil, build as an Integer or nil.
|
|
34
|
+
class Client < Hash
|
|
35
|
+
FIELDS = %i[platform version build].freeze
|
|
36
|
+
# The platform of a client that sent one this app doesn't recognise.
|
|
37
|
+
UNKNOWN_PLATFORM = "unknown".freeze
|
|
38
|
+
# The OpenFeature evaluation-context fields that carry the client, in
|
|
39
|
+
# order of preference (the OpenFeature provider and the OFREP body).
|
|
40
|
+
CONTEXT_FIELDS = { platform: %w[platform], version: %w[appVersion app_version], build: %w[build] }.freeze
|
|
41
|
+
# Longest header value read.
|
|
42
|
+
MAX_LENGTH = 128
|
|
43
|
+
|
|
44
|
+
class << self
|
|
45
|
+
def empty
|
|
46
|
+
@empty ||= new
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def [](*arguments)
|
|
50
|
+
new(Hash[*arguments])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# A Client from a Client, a Hash of attributes, or nil (empty).
|
|
54
|
+
def wrap(value)
|
|
55
|
+
case value
|
|
56
|
+
when Client then value
|
|
57
|
+
when nil then empty
|
|
58
|
+
when Hash then value.empty? ? empty : new(value)
|
|
59
|
+
else raise ArgumentError, "client: must be a StandardFlags::Client or a Hash, not a #{value.class}"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The client a request's headers describe. Takes anything that answers
|
|
64
|
+
# `[]` with a header name: ActionDispatch's headers, a Rack env (read
|
|
65
|
+
# as HTTP_X_CLIENT_PLATFORM and so on) or a plain Hash.
|
|
66
|
+
def from_headers(headers)
|
|
67
|
+
names = StandardFlags.config.client_headers
|
|
68
|
+
validated(FIELDS.each_with_object({}) do |field, found|
|
|
69
|
+
name = names[field]&.to_s
|
|
70
|
+
found[field] = [ header(headers, name), name ] if name
|
|
71
|
+
end)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The client an OpenFeature evaluation context's fields describe
|
|
75
|
+
# (`platform`, `appVersion` or `app_version`, `build`; see
|
|
76
|
+
# CONTEXT_FIELDS), checked exactly like headers: a bad version or build
|
|
77
|
+
# is dropped, an unrecognised platform becomes "unknown", and either
|
|
78
|
+
# emits `client_invalid.standard_flags`, whose `header` is the field
|
|
79
|
+
# name.
|
|
80
|
+
def from_context(fields)
|
|
81
|
+
validated(CONTEXT_FIELDS.each_with_object({}) do |(field, names), found|
|
|
82
|
+
name = names.find { |candidate| !fields[candidate].nil? }
|
|
83
|
+
found[field] = [ fields[name], name ] if name
|
|
84
|
+
end)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
# `{ field => [raw value, where it came from] }` to a Client.
|
|
90
|
+
def validated(raw_values)
|
|
91
|
+
config = StandardFlags.config
|
|
92
|
+
attributes = raw_values.each_with_object({}) do |(field, (raw, source)), found|
|
|
93
|
+
next if raw.nil?
|
|
94
|
+
|
|
95
|
+
text = Version.clean(raw)
|
|
96
|
+
next if text&.empty?
|
|
97
|
+
|
|
98
|
+
value = text && normalize(field, text, config)
|
|
99
|
+
next found[field] = value unless value.nil?
|
|
100
|
+
|
|
101
|
+
Reporting.instrument(:client_invalid, field: field, header: source)
|
|
102
|
+
found[field] = UNKNOWN_PLATFORM if field == :platform
|
|
103
|
+
end
|
|
104
|
+
attributes.empty? ? empty : new(attributes)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def header(headers, name)
|
|
108
|
+
[ name, name.downcase, "HTTP_#{name.upcase.tr('-', '_')}" ].each do |candidate|
|
|
109
|
+
value = headers[candidate]
|
|
110
|
+
return value unless value.nil?
|
|
111
|
+
end
|
|
112
|
+
nil
|
|
113
|
+
rescue StandardError
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def normalize(field, value, config)
|
|
118
|
+
return nil if value.length > MAX_LENGTH
|
|
119
|
+
|
|
120
|
+
case field
|
|
121
|
+
when :platform
|
|
122
|
+
platform = value.downcase
|
|
123
|
+
allowed = config.client_platforms.map { |name| name.to_s.downcase } - [ UNKNOWN_PLATFORM ]
|
|
124
|
+
platform if allowed.include?(platform)
|
|
125
|
+
when :version then value if Version.parse(value)
|
|
126
|
+
when :build then value if BuildRange.number(value)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def initialize(attributes = nil, **keywords)
|
|
132
|
+
super()
|
|
133
|
+
(attributes || {}).to_h.merge(keywords).each { |key, value| store(key.to_sym, value) }
|
|
134
|
+
freeze
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The platform, stripped and downcased, or nil when there is none or it
|
|
138
|
+
# is "unknown".
|
|
139
|
+
def platform
|
|
140
|
+
text = Version.clean(self[:platform])&.downcase
|
|
141
|
+
text unless text.nil? || text.empty? || text == UNKNOWN_PLATFORM
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Whether the client sent a platform that isn't recognised.
|
|
145
|
+
def unknown_platform?
|
|
146
|
+
Version.clean(self[:platform])&.downcase == UNKNOWN_PLATFORM
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def version
|
|
150
|
+
Version.parse(self[:version])
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def build
|
|
154
|
+
BuildRange.number(self[:build])
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
@@ -30,6 +30,17 @@ module StandardFlags
|
|
|
30
30
|
attr_accessor :controller_responder
|
|
31
31
|
# Whose wire name `requires_flag` reports in `meta.feature`.
|
|
32
32
|
attr_accessor :default_audience
|
|
33
|
+
# Client context. StandardFlags::Controller reads the calling client from
|
|
34
|
+
# these request headers (`{ platform:, version:, build: }` => header
|
|
35
|
+
# name) unless `read_client_headers` is false; a platform outside
|
|
36
|
+
# `client_platforms` is dropped.
|
|
37
|
+
attr_accessor :client_headers, :client_platforms, :read_client_headers
|
|
38
|
+
# The client audiences this app serves (see StandardFlags::Audience):
|
|
39
|
+
# an Array of names, or a Hash of name => `{ public:, unit: }`. nil (the
|
|
40
|
+
# default) means every audience a flag exposes to, none of them public.
|
|
41
|
+
# When set, every `expose:` and tombstone `serve:` audience must be
|
|
42
|
+
# declared here, or the registry refuses to build.
|
|
43
|
+
attr_reader :audiences
|
|
33
44
|
# Runtime rules (rules stored in the app's database, between the ENV pin
|
|
34
45
|
# and the code default). Off by default; when off, nothing touches a
|
|
35
46
|
# database and ActiveRecord is never loaded. Turn on only after running
|
|
@@ -73,6 +84,8 @@ module StandardFlags
|
|
|
73
84
|
# how often (seconds) it writes.
|
|
74
85
|
attr_accessor :track_evaluations, :evaluation_tracking_interval
|
|
75
86
|
|
|
87
|
+
DEFAULT_CLIENT_HEADERS = { platform: "X-Client-Platform", version: "X-Client-Version", build: "X-Client-Build" }.freeze
|
|
88
|
+
DEFAULT_CLIENT_PLATFORMS = %w[android ios web desktop].freeze
|
|
76
89
|
DEFAULT_AUTHORIZE = ->(actor, _scope, _key) { actor.is_a?(Actor) && actor.system? }
|
|
77
90
|
DEFAULT_ACTOR_RESOLVER = -> { ::Current.user if defined?(::Current) && ::Current.respond_to?(:user) }
|
|
78
91
|
|
|
@@ -92,6 +105,10 @@ module StandardFlags
|
|
|
92
105
|
@typescript_path = "app/javascript/types/standard_flags.d.ts"
|
|
93
106
|
@controller_responder = nil
|
|
94
107
|
@default_audience = :web
|
|
108
|
+
@client_headers = DEFAULT_CLIENT_HEADERS
|
|
109
|
+
@client_platforms = DEFAULT_CLIENT_PLATFORMS
|
|
110
|
+
@read_client_headers = true
|
|
111
|
+
@audiences = nil
|
|
95
112
|
@runtime_rules = false
|
|
96
113
|
@refresh_interval = 5
|
|
97
114
|
@runtime_store = nil
|
|
@@ -124,6 +141,10 @@ module StandardFlags
|
|
|
124
141
|
@env_source || ENV
|
|
125
142
|
end
|
|
126
143
|
|
|
144
|
+
def audiences=(value)
|
|
145
|
+
@audiences = Audience.normalize(value)
|
|
146
|
+
end
|
|
147
|
+
|
|
127
148
|
def error_reporter=(reporter)
|
|
128
149
|
raise ArgumentError, "error_reporter must respond to call(error, context)" unless reporter.nil? || reporter.respond_to?(:call)
|
|
129
150
|
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
module StandardFlags
|
|
2
2
|
# Who and what a flag is being evaluated for: one actor per unit, an
|
|
3
|
-
# optional environment override, and the calling client (
|
|
4
|
-
#
|
|
5
|
-
#
|
|
3
|
+
# optional environment override, and the calling client (a
|
|
4
|
+
# StandardFlags::Client: platform, version, build), which runtime rules'
|
|
5
|
+
# client conditions read.
|
|
6
6
|
#
|
|
7
7
|
# StandardFlags.on?(:new_checkout, for: account) # a single target
|
|
8
8
|
# StandardFlags.on?(:new_checkout, for: { account: account, organization: org })
|
|
9
9
|
# StandardFlags.on?(:new_checkout, for: StandardFlags::Context.new(env: "staging"))
|
|
10
|
+
# StandardFlags.on?(:new_checkout, for: StandardFlags::Context.wrap(account, client: client))
|
|
11
|
+
#
|
|
12
|
+
# `client:` takes a Client or a Hash (`{ platform:, version:, build: }`);
|
|
13
|
+
# `context.client[:platform]` reads the value as given either way.
|
|
10
14
|
#
|
|
11
15
|
# The memo key covers env, target, actors and client, so two clients in one
|
|
12
16
|
# unit of work never share an answer once rules target platform or version.
|
|
@@ -17,13 +21,18 @@ module StandardFlags
|
|
|
17
21
|
class Context
|
|
18
22
|
attr_reader :actors, :target, :env, :client, :cache_key
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
# A Context from a Context, a Hash of actors, a single target or nil.
|
|
25
|
+
# `client:` (a Client or a Hash) replaces the context's client when
|
|
26
|
+
# given and not empty; nil and `{}` both leave it as it is.
|
|
27
|
+
def self.wrap(value, client: nil)
|
|
28
|
+
context =
|
|
29
|
+
case value
|
|
30
|
+
when Context then value
|
|
31
|
+
when nil then empty
|
|
32
|
+
when Hash then new(actors: value)
|
|
33
|
+
else new(target: value)
|
|
34
|
+
end
|
|
35
|
+
client.nil? || (client.respond_to?(:empty?) && client.empty?) ? context : context.with_client(client)
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
def self.empty
|
|
@@ -34,16 +43,25 @@ module StandardFlags
|
|
|
34
43
|
@actors = actors.transform_keys(&:to_sym).freeze
|
|
35
44
|
@target = target
|
|
36
45
|
@env = env&.to_s
|
|
37
|
-
@client =
|
|
38
|
-
@cache_key =
|
|
39
|
-
@env,
|
|
40
|
-
identity(@target),
|
|
41
|
-
@actors.sort.map { |unit, actor| [ unit, identity(actor) ] },
|
|
42
|
-
@client.sort
|
|
43
|
-
].freeze
|
|
46
|
+
@client = Client.wrap(client)
|
|
47
|
+
@cache_key = build_cache_key.freeze
|
|
44
48
|
freeze
|
|
45
49
|
end
|
|
46
50
|
|
|
51
|
+
# Whether this is a request made before authentication (an
|
|
52
|
+
# AnonymousContext).
|
|
53
|
+
def anonymous?
|
|
54
|
+
false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The same context for another client.
|
|
58
|
+
def with_client(client)
|
|
59
|
+
client = Client.wrap(client)
|
|
60
|
+
return self if client == @client
|
|
61
|
+
|
|
62
|
+
self.class.new(actors: actors, target: target, env: env, client: client)
|
|
63
|
+
end
|
|
64
|
+
|
|
47
65
|
def actor(unit)
|
|
48
66
|
unit = unit.to_sym
|
|
49
67
|
return actors[unit] if actors.key?(unit)
|
|
@@ -63,6 +81,15 @@ module StandardFlags
|
|
|
63
81
|
|
|
64
82
|
private
|
|
65
83
|
|
|
84
|
+
def build_cache_key
|
|
85
|
+
[
|
|
86
|
+
@env,
|
|
87
|
+
identity(@target),
|
|
88
|
+
@actors.sort.map { |unit, actor| [ unit, identity(actor) ] },
|
|
89
|
+
@client.to_h.sort
|
|
90
|
+
]
|
|
91
|
+
end
|
|
92
|
+
|
|
66
93
|
# An unsaved record (nil id) falls back to object identity, so two of
|
|
67
94
|
# them never share a memoised answer.
|
|
68
95
|
def identity(object)
|
|
@@ -74,4 +101,30 @@ module StandardFlags
|
|
|
74
101
|
end
|
|
75
102
|
end
|
|
76
103
|
end
|
|
104
|
+
|
|
105
|
+
# The context of a request made before authentication (an `anonymous:
|
|
106
|
+
# true` OFREP controller, through Delivery.build_anonymous): no actors, no
|
|
107
|
+
# target, only the client. Runtime rules skip segment conditions for it,
|
|
108
|
+
# since an app's segment resolver may read the signed-in user. It never
|
|
109
|
+
# shares a memoised answer with an ordinary context.
|
|
110
|
+
class AnonymousContext < Context
|
|
111
|
+
def initialize(client: {})
|
|
112
|
+
super(client: client)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def anonymous?
|
|
116
|
+
true
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def with_client(client)
|
|
120
|
+
client = Client.wrap(client)
|
|
121
|
+
client == self.client ? self : self.class.new(client: client)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
def build_cache_key
|
|
127
|
+
[ :anonymous, *super ]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
77
130
|
end
|
|
@@ -29,6 +29,22 @@ module StandardFlags
|
|
|
29
29
|
# `config.controller_responder` replaces the response entirely, and a
|
|
30
30
|
# guard's own `with:` (a callable or the name of a controller method)
|
|
31
31
|
# replaces it for that guard only.
|
|
32
|
+
#
|
|
33
|
+
# A StandardFlags::FeatureDisabled raised in an action (or by
|
|
34
|
+
# `StandardFlags.require!`) is answered the same way, through
|
|
35
|
+
# `config.controller_responder` or the default response, even when the
|
|
36
|
+
# app declares `rescue_from StandardError` after including this module.
|
|
37
|
+
# Only an app's own `rescue_from StandardFlags::FeatureDisabled` (or a
|
|
38
|
+
# subclass) takes it over. One raised with `status: 404` answers like
|
|
39
|
+
# `error: :not_found`, with no meta.
|
|
40
|
+
#
|
|
41
|
+
# Flags are evaluated for `standard_flags_context` plus the calling client,
|
|
42
|
+
# read once per request from the X-Client-Platform, X-Client-Version and
|
|
43
|
+
# X-Client-Build headers (`standard_flags_client`). A context that already
|
|
44
|
+
# carries a client keeps it. `config.read_client_headers = false` turns
|
|
45
|
+
# the headers off. Inside an action, `standard_flags_reader` (or
|
|
46
|
+
# `StandardFlags.require!(key, for: standard_flags_request_context)`)
|
|
47
|
+
# evaluates for the same context the guards use.
|
|
32
48
|
module Controller
|
|
33
49
|
extend ActiveSupport::Concern
|
|
34
50
|
|
|
@@ -44,7 +60,10 @@ module StandardFlags
|
|
|
44
60
|
end
|
|
45
61
|
|
|
46
62
|
included do
|
|
47
|
-
|
|
63
|
+
if respond_to?(:helper_method)
|
|
64
|
+
helper_method :standard_flags_payload, :standard_flags_client, :standard_flags_request_context, :standard_flags_reader
|
|
65
|
+
end
|
|
66
|
+
rescue_from FeatureDisabled, with: :standard_flags_feature_disabled if respond_to?(:rescue_from)
|
|
48
67
|
end
|
|
49
68
|
|
|
50
69
|
class_methods do
|
|
@@ -65,9 +84,11 @@ module StandardFlags
|
|
|
65
84
|
end
|
|
66
85
|
end
|
|
67
86
|
|
|
68
|
-
# The payload for one audience, evaluated for this request's context
|
|
69
|
-
|
|
70
|
-
|
|
87
|
+
# The payload for one audience, evaluated for this request's context
|
|
88
|
+
# and client: flat by default, or `format: :ofrep` for an embed in a
|
|
89
|
+
# bootstrap response (see StandardFlags.payload).
|
|
90
|
+
def standard_flags_payload(audience, format: :flat)
|
|
91
|
+
StandardFlags.payload(audience, for: standard_flags_request_context, format: format)
|
|
71
92
|
end
|
|
72
93
|
|
|
73
94
|
private
|
|
@@ -77,10 +98,74 @@ module StandardFlags
|
|
|
77
98
|
nil
|
|
78
99
|
end
|
|
79
100
|
|
|
101
|
+
# The calling client, from this request's headers (memoised). Empty
|
|
102
|
+
# when `config.read_client_headers` is false, no header is sent, or the
|
|
103
|
+
# controller has no request (a console or a spec).
|
|
104
|
+
def standard_flags_client
|
|
105
|
+
@standard_flags_client ||=
|
|
106
|
+
if StandardFlags.config.read_client_headers && request
|
|
107
|
+
Client.from_headers(request.headers)
|
|
108
|
+
else
|
|
109
|
+
Client.empty
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# `standard_flags_context`, with the request's client unless it already
|
|
114
|
+
# names one: what guards and the payload evaluate for.
|
|
115
|
+
def standard_flags_request_context
|
|
116
|
+
context = Context.wrap(standard_flags_context)
|
|
117
|
+
context.client.empty? ? context.with_client(standard_flags_client) : context
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# A reader for this request's context, for reads inside an action:
|
|
121
|
+
# `standard_flags_reader.on?(:new_checkout)`.
|
|
122
|
+
def standard_flags_reader
|
|
123
|
+
StandardFlags.reader(for: standard_flags_request_context)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# FeatureDisabled is answered here even when an app `rescue_from`
|
|
127
|
+
# (StandardError, say) declared after the include would otherwise win,
|
|
128
|
+
# since Rails tries handlers last-declared first.
|
|
129
|
+
def rescue_with_handler(exception)
|
|
130
|
+
handler = standard_flags_handler(exception)
|
|
131
|
+
return super unless handler
|
|
132
|
+
|
|
133
|
+
handler.call(exception)
|
|
134
|
+
exception
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def handler_for_rescue(exception)
|
|
138
|
+
standard_flags_handler(exception) || super
|
|
139
|
+
end
|
|
140
|
+
public :rescue_with_handler, :handler_for_rescue
|
|
141
|
+
|
|
142
|
+
def standard_flags_handler(exception)
|
|
143
|
+
return unless exception.is_a?(FeatureDisabled)
|
|
144
|
+
|
|
145
|
+
targeted = self.class.rescue_handlers.any? do |name, handler|
|
|
146
|
+
klass = name.safe_constantize if handler != :standard_flags_feature_disabled
|
|
147
|
+
klass.is_a?(Class) && klass <= FeatureDisabled
|
|
148
|
+
end
|
|
149
|
+
method(:standard_flags_feature_disabled) unless targeted
|
|
150
|
+
end
|
|
151
|
+
|
|
80
152
|
def standard_flags_require!(key, error:, status:, responder: nil)
|
|
81
|
-
return if StandardFlags.reader(for:
|
|
153
|
+
return if StandardFlags.reader(for: standard_flags_request_context).enabled?(key)
|
|
82
154
|
|
|
83
|
-
|
|
155
|
+
standard_flags_deny(Denial.new(key: key.to_sym, error: error, status: status, feature: standard_flags_feature_name(key)),
|
|
156
|
+
responder)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# A 404 answers like `error: :not_found`, revealing no feature name.
|
|
160
|
+
# Nothing is rendered when the action already has.
|
|
161
|
+
def standard_flags_feature_disabled(exception)
|
|
162
|
+
return if performed?
|
|
163
|
+
|
|
164
|
+
error = exception.status == ERRORS[:not_found] ? :not_found : :feature_disabled
|
|
165
|
+
standard_flags_deny(Denial.new(key: exception.key, error: error, status: exception.status, feature: exception.feature), nil)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def standard_flags_deny(denial, responder)
|
|
84
169
|
responder ||= StandardFlags.config.controller_responder
|
|
85
170
|
case responder
|
|
86
171
|
when nil then standard_flags_render_denial(denial)
|
|
@@ -98,10 +183,7 @@ module StandardFlags
|
|
|
98
183
|
end
|
|
99
184
|
|
|
100
185
|
def standard_flags_feature_name(key)
|
|
101
|
-
|
|
102
|
-
return key.to_s unless definition
|
|
103
|
-
|
|
104
|
-
definition.wire_name(StandardFlags.config.default_audience) || definition.expose.values.first || key.to_s
|
|
186
|
+
FeatureDisabled.feature_name(key)
|
|
105
187
|
end
|
|
106
188
|
end
|
|
107
189
|
end
|
|
@@ -17,7 +17,7 @@ module StandardFlags
|
|
|
17
17
|
}.freeze
|
|
18
18
|
|
|
19
19
|
COMMON_ATTRIBUTES = %i[owner description default env inverted legacy_parser legacy_true forbidden_in fail
|
|
20
|
-
expose unit].freeze
|
|
20
|
+
expose min_client unit].freeze
|
|
21
21
|
ATTRIBUTES = {
|
|
22
22
|
flag: COMMON_ATTRIBUTES + %i[remove_by final permanent bucketing],
|
|
23
23
|
kill_switch: COMMON_ATTRIBUTES,
|
|
@@ -44,8 +44,8 @@ module StandardFlags
|
|
|
44
44
|
Bucketing = Data.define(:version, :salt)
|
|
45
45
|
|
|
46
46
|
attr_reader :key, :kind, :owner, :description, :env, :inverted, :forbidden_in,
|
|
47
|
-
:expose, :unit, :remove_by, :final, :values, :variants, :permanent,
|
|
48
|
-
:legacy_parser, :legacy_true
|
|
47
|
+
:expose, :exposure, :min_client, :unit, :remove_by, :final, :values, :variants, :permanent,
|
|
48
|
+
:bucketing, :legacy_parser, :legacy_true
|
|
49
49
|
|
|
50
50
|
def initialize(kind, key, attributes)
|
|
51
51
|
@kind = kind.to_sym
|
|
@@ -146,14 +146,40 @@ module StandardFlags
|
|
|
146
146
|
forbidden_in.include?(env_name.to_s)
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
+
# The wire name `audience` sees (the first one listed when it has
|
|
150
|
+
# aliases), or nil when the flag isn't exposed to it.
|
|
149
151
|
def wire_name(audience)
|
|
150
152
|
expose[audience.to_sym]
|
|
151
153
|
end
|
|
152
154
|
|
|
155
|
+
# Every wire name `audience` sees, in declared order: one, or several
|
|
156
|
+
# aliases that all serve the same evaluation. Empty when not exposed.
|
|
157
|
+
def wire_names(audience)
|
|
158
|
+
exposure.fetch(audience.to_sym, [])
|
|
159
|
+
end
|
|
160
|
+
|
|
153
161
|
def audiences
|
|
154
162
|
expose.keys
|
|
155
163
|
end
|
|
156
164
|
|
|
165
|
+
# Whether a client may receive this flag, given `min_client:` (never set
|
|
166
|
+
# on a kill switch). A flag
|
|
167
|
+
# without one goes to every client. With one, it goes to a client on a
|
|
168
|
+
# platform the declaration doesn't list, or on a listed platform at or
|
|
169
|
+
# above that minimum; an unknown platform, or a listed platform with an
|
|
170
|
+
# unknown version, never receives it.
|
|
171
|
+
def deliverable_to?(client)
|
|
172
|
+
return true if min_client.empty?
|
|
173
|
+
|
|
174
|
+
platform = client.platform
|
|
175
|
+
return false if platform.nil?
|
|
176
|
+
|
|
177
|
+
minimum = min_client[platform]
|
|
178
|
+
return true if minimum.nil?
|
|
179
|
+
|
|
180
|
+
!client.version.nil? && client.version >= minimum
|
|
181
|
+
end
|
|
182
|
+
|
|
157
183
|
# Permanent flags have no remove_by:, so they are never stale.
|
|
158
184
|
def stale?(today = Date.today)
|
|
159
185
|
!remove_by.nil? && remove_by < today
|
|
@@ -189,7 +215,9 @@ module StandardFlags
|
|
|
189
215
|
@legacy_parser = @legacy_parser.to_sym if @legacy_parser.respond_to?(:to_sym)
|
|
190
216
|
@legacy_true = normalize_legacy_true(attributes)
|
|
191
217
|
@forbidden_in = Array(attributes[:forbidden_in]).map(&:to_s).freeze
|
|
192
|
-
@
|
|
218
|
+
@exposure = normalize_expose(attributes[:expose])
|
|
219
|
+
@expose = @exposure.transform_values(&:first).freeze
|
|
220
|
+
@min_client = normalize_min_client(attributes[:min_client])
|
|
193
221
|
@unit = attributes[:unit]&.to_sym
|
|
194
222
|
@remove_by = parse_date(attributes[:remove_by], :remove_by)
|
|
195
223
|
@values = resolve_values(attributes)
|
|
@@ -313,6 +341,8 @@ module StandardFlags
|
|
|
313
341
|
Bucketing.new(version: version, salt: salt.dup.freeze)
|
|
314
342
|
end
|
|
315
343
|
|
|
344
|
+
# `{ audience => [wire, ...] }`. A Hash value may list several wire
|
|
345
|
+
# names (aliases) for one audience.
|
|
316
346
|
def normalize_expose(expose)
|
|
317
347
|
pairs =
|
|
318
348
|
case expose
|
|
@@ -321,13 +351,30 @@ module StandardFlags
|
|
|
321
351
|
else Array(expose).to_h { |audience| [ audience, key ] }
|
|
322
352
|
end
|
|
323
353
|
|
|
324
|
-
pairs.to_h do |audience,
|
|
325
|
-
|
|
326
|
-
fail!("expose:
|
|
327
|
-
|
|
354
|
+
pairs.to_h do |audience, wires|
|
|
355
|
+
wires = (wires.is_a?(Array) ? wires : [ wires ]).map(&:to_s)
|
|
356
|
+
fail!("expose: lists no wire name for :#{audience}") if wires.empty?
|
|
357
|
+
wires.each do |wire|
|
|
358
|
+
fail!("expose: wire name #{wire.inspect} must match #{WIRE_FORMAT.inspect}") unless wire.match?(WIRE_FORMAT)
|
|
359
|
+
end
|
|
360
|
+
fail!("expose: lists a wire name twice for :#{audience}") unless wires.uniq.size == wires.size
|
|
361
|
+
[ audience.to_sym, wires.freeze ]
|
|
328
362
|
end.freeze
|
|
329
363
|
end
|
|
330
364
|
|
|
365
|
+
# `{ "platform" => Version }`: the oldest client build per platform that
|
|
366
|
+
# may receive this flag.
|
|
367
|
+
def normalize_min_client(min_client)
|
|
368
|
+
return {}.freeze if min_client.nil?
|
|
369
|
+
fail!("min_client: must be a Hash of platform => version") unless min_client.is_a?(Hash)
|
|
370
|
+
fail!("min_client: only applies to a flag with expose:") if @exposure.empty?
|
|
371
|
+
# A withheld entry means the client's code default, which for a kill
|
|
372
|
+
# switch is "available": an old client would never see it pulled.
|
|
373
|
+
fail!("min_client: can't be used on a kill switch; a kill switch must reach every client") if kill_switch?
|
|
374
|
+
|
|
375
|
+
Definition.client_versions(min_client) { |message| fail!("min_client: #{message}") }
|
|
376
|
+
end
|
|
377
|
+
|
|
331
378
|
# Casts a declared value to this kind's type, raising for anything outside
|
|
332
379
|
# the declared set.
|
|
333
380
|
def coerce(value, attribute)
|
|
@@ -353,5 +400,21 @@ module StandardFlags
|
|
|
353
400
|
def fail!(message)
|
|
354
401
|
raise DefinitionError, "#{kind} :#{key} #{message}"
|
|
355
402
|
end
|
|
403
|
+
|
|
404
|
+
# `{ platform => version }` (a min_client: or until_clients: Hash) as
|
|
405
|
+
# `{ "platform" => Version }`. Platforms must be in
|
|
406
|
+
# `config.client_platforms`; versions must parse. Yields a message and
|
|
407
|
+
# expects the block to raise.
|
|
408
|
+
def self.client_versions(pairs)
|
|
409
|
+
platforms = StandardFlags.config.client_platforms.map { |name| name.to_s.downcase }
|
|
410
|
+
yield "must name at least one platform" if pairs.empty?
|
|
411
|
+
pairs.to_h do |platform, version|
|
|
412
|
+
name = platform.to_s.downcase
|
|
413
|
+
yield "platform #{platform.inspect} is not one of config.client_platforms #{platforms.inspect}" unless platforms.include?(name)
|
|
414
|
+
parsed = version.is_a?(String) ? Version.parse(version) : nil
|
|
415
|
+
yield "#{version.inspect} for #{name} is not a version (\"0.4.0\")" if parsed.nil?
|
|
416
|
+
[ name.freeze, parsed ]
|
|
417
|
+
end.freeze
|
|
418
|
+
end
|
|
356
419
|
end
|
|
357
420
|
end
|