flipper 0.24.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/dependabot.yml +6 -0
  4. data/.github/workflows/ci.yml +45 -14
  5. data/.github/workflows/examples.yml +39 -16
  6. data/Changelog.md +2 -443
  7. data/Gemfile +19 -11
  8. data/README.md +31 -27
  9. data/Rakefile +6 -4
  10. data/benchmark/enabled_ips.rb +10 -0
  11. data/benchmark/enabled_multiple_actors_ips.rb +20 -0
  12. data/benchmark/enabled_profile.rb +20 -0
  13. data/benchmark/instrumentation_ips.rb +21 -0
  14. data/benchmark/typecast_ips.rb +27 -0
  15. data/docs/images/banner.jpg +0 -0
  16. data/docs/images/flipper_cloud.png +0 -0
  17. data/examples/api/basic.ru +3 -4
  18. data/examples/api/custom_memoized.ru +3 -4
  19. data/examples/api/memoized.ru +3 -4
  20. data/examples/cloud/app.ru +12 -0
  21. data/examples/cloud/backoff_policy.rb +13 -0
  22. data/examples/cloud/basic.rb +22 -0
  23. data/examples/cloud/cloud_setup.rb +20 -0
  24. data/examples/cloud/forked.rb +36 -0
  25. data/examples/cloud/import.rb +17 -0
  26. data/examples/cloud/threaded.rb +33 -0
  27. data/examples/dsl.rb +1 -15
  28. data/examples/enabled_for_actor.rb +4 -2
  29. data/examples/expressions.rb +213 -0
  30. data/examples/instrumentation.rb +1 -0
  31. data/examples/instrumentation_last_accessed_at.rb +1 -0
  32. data/examples/mirroring.rb +59 -0
  33. data/examples/strict.rb +18 -0
  34. data/exe/flipper +5 -0
  35. data/flipper-cloud.gemspec +19 -0
  36. data/flipper.gemspec +10 -6
  37. data/lib/flipper/actor.rb +6 -3
  38. data/lib/flipper/adapter.rb +33 -7
  39. data/lib/flipper/adapter_builder.rb +44 -0
  40. data/lib/flipper/adapters/actor_limit.rb +28 -0
  41. data/lib/flipper/adapters/cache_base.rb +143 -0
  42. data/lib/flipper/adapters/dual_write.rb +1 -3
  43. data/lib/flipper/adapters/failover.rb +0 -4
  44. data/lib/flipper/adapters/failsafe.rb +72 -0
  45. data/lib/flipper/adapters/http/client.rb +44 -20
  46. data/lib/flipper/adapters/http/error.rb +1 -1
  47. data/lib/flipper/adapters/http.rb +31 -16
  48. data/lib/flipper/adapters/instrumented.rb +25 -6
  49. data/lib/flipper/adapters/memoizable.rb +33 -21
  50. data/lib/flipper/adapters/memory.rb +81 -46
  51. data/lib/flipper/adapters/operation_logger.rb +17 -78
  52. data/lib/flipper/adapters/poll/poller.rb +2 -0
  53. data/lib/flipper/adapters/poll.rb +37 -0
  54. data/lib/flipper/adapters/pstore.rb +17 -11
  55. data/lib/flipper/adapters/read_only.rb +8 -41
  56. data/lib/flipper/adapters/strict.rb +45 -0
  57. data/lib/flipper/adapters/sync/feature_synchronizer.rb +10 -1
  58. data/lib/flipper/adapters/sync.rb +0 -4
  59. data/lib/flipper/adapters/wrapper.rb +54 -0
  60. data/lib/flipper/cli.rb +263 -0
  61. data/lib/flipper/cloud/configuration.rb +263 -0
  62. data/lib/flipper/cloud/dsl.rb +27 -0
  63. data/lib/flipper/cloud/message_verifier.rb +95 -0
  64. data/lib/flipper/cloud/middleware.rb +63 -0
  65. data/lib/flipper/cloud/routes.rb +14 -0
  66. data/lib/flipper/cloud/telemetry/backoff_policy.rb +93 -0
  67. data/lib/flipper/cloud/telemetry/instrumenter.rb +22 -0
  68. data/lib/flipper/cloud/telemetry/metric.rb +39 -0
  69. data/lib/flipper/cloud/telemetry/metric_storage.rb +30 -0
  70. data/lib/flipper/cloud/telemetry/submitter.rb +98 -0
  71. data/lib/flipper/cloud/telemetry.rb +191 -0
  72. data/lib/flipper/cloud.rb +53 -0
  73. data/lib/flipper/configuration.rb +25 -4
  74. data/lib/flipper/dsl.rb +46 -45
  75. data/lib/flipper/engine.rb +102 -0
  76. data/lib/flipper/errors.rb +3 -20
  77. data/lib/flipper/export.rb +26 -0
  78. data/lib/flipper/exporter.rb +17 -0
  79. data/lib/flipper/exporters/json/export.rb +32 -0
  80. data/lib/flipper/exporters/json/v1.rb +33 -0
  81. data/lib/flipper/expression/builder.rb +73 -0
  82. data/lib/flipper/expression/constant.rb +25 -0
  83. data/lib/flipper/expression.rb +71 -0
  84. data/lib/flipper/expressions/all.rb +11 -0
  85. data/lib/flipper/expressions/any.rb +9 -0
  86. data/lib/flipper/expressions/boolean.rb +9 -0
  87. data/lib/flipper/expressions/comparable.rb +13 -0
  88. data/lib/flipper/expressions/duration.rb +28 -0
  89. data/lib/flipper/expressions/equal.rb +9 -0
  90. data/lib/flipper/expressions/greater_than.rb +9 -0
  91. data/lib/flipper/expressions/greater_than_or_equal_to.rb +9 -0
  92. data/lib/flipper/expressions/less_than.rb +9 -0
  93. data/lib/flipper/expressions/less_than_or_equal_to.rb +9 -0
  94. data/lib/flipper/expressions/not_equal.rb +9 -0
  95. data/lib/flipper/expressions/now.rb +9 -0
  96. data/lib/flipper/expressions/number.rb +9 -0
  97. data/lib/flipper/expressions/percentage.rb +9 -0
  98. data/lib/flipper/expressions/percentage_of_actors.rb +12 -0
  99. data/lib/flipper/expressions/property.rb +9 -0
  100. data/lib/flipper/expressions/random.rb +9 -0
  101. data/lib/flipper/expressions/string.rb +9 -0
  102. data/lib/flipper/expressions/time.rb +9 -0
  103. data/lib/flipper/feature.rb +87 -26
  104. data/lib/flipper/feature_check_context.rb +10 -6
  105. data/lib/flipper/gate.rb +13 -11
  106. data/lib/flipper/gate_values.rb +5 -18
  107. data/lib/flipper/gates/actor.rb +10 -17
  108. data/lib/flipper/gates/boolean.rb +1 -1
  109. data/lib/flipper/gates/expression.rb +75 -0
  110. data/lib/flipper/gates/group.rb +5 -7
  111. data/lib/flipper/gates/percentage_of_actors.rb +10 -13
  112. data/lib/flipper/gates/percentage_of_time.rb +1 -2
  113. data/lib/flipper/identifier.rb +2 -2
  114. data/lib/flipper/instrumentation/log_subscriber.rb +34 -6
  115. data/lib/flipper/instrumentation/statsd_subscriber.rb +2 -4
  116. data/lib/flipper/instrumentation/subscriber.rb +8 -1
  117. data/lib/flipper/metadata.rb +7 -1
  118. data/lib/flipper/middleware/memoizer.rb +28 -22
  119. data/lib/flipper/model/active_record.rb +23 -0
  120. data/lib/flipper/poller.rb +118 -0
  121. data/lib/flipper/serializers/gzip.rb +22 -0
  122. data/lib/flipper/serializers/json.rb +17 -0
  123. data/lib/flipper/spec/shared_adapter_specs.rb +105 -63
  124. data/lib/flipper/test/shared_adapter_test.rb +101 -58
  125. data/lib/flipper/test_help.rb +43 -0
  126. data/lib/flipper/typecast.rb +59 -18
  127. data/lib/flipper/types/actor.rb +13 -13
  128. data/lib/flipper/types/group.rb +4 -4
  129. data/lib/flipper/types/percentage.rb +1 -1
  130. data/lib/flipper/version.rb +11 -1
  131. data/lib/flipper.rb +50 -11
  132. data/lib/generators/flipper/setup_generator.rb +63 -0
  133. data/lib/generators/flipper/templates/update/migrations/01_create_flipper_tables.rb.erb +22 -0
  134. data/lib/generators/flipper/templates/update/migrations/02_change_flipper_gates_value_to_text.rb.erb +18 -0
  135. data/lib/generators/flipper/update_generator.rb +35 -0
  136. data/package-lock.json +41 -0
  137. data/package.json +10 -0
  138. data/spec/fixtures/environment.rb +1 -0
  139. data/spec/fixtures/flipper_pstore_1679087600.json +46 -0
  140. data/spec/flipper/adapter_builder_spec.rb +72 -0
  141. data/spec/flipper/adapter_spec.rb +30 -2
  142. data/spec/flipper/adapters/actor_limit_spec.rb +20 -0
  143. data/spec/flipper/adapters/dual_write_spec.rb +2 -2
  144. data/spec/flipper/adapters/failsafe_spec.rb +58 -0
  145. data/spec/flipper/adapters/http/client_spec.rb +61 -0
  146. data/spec/flipper/adapters/http_spec.rb +137 -55
  147. data/spec/flipper/adapters/instrumented_spec.rb +29 -11
  148. data/spec/flipper/adapters/memoizable_spec.rb +51 -31
  149. data/spec/flipper/adapters/memory_spec.rb +14 -3
  150. data/spec/flipper/adapters/operation_logger_spec.rb +31 -12
  151. data/spec/flipper/adapters/read_only_spec.rb +32 -17
  152. data/spec/flipper/adapters/strict_spec.rb +64 -0
  153. data/spec/flipper/adapters/sync/feature_synchronizer_spec.rb +27 -0
  154. data/spec/flipper/cli_spec.rb +164 -0
  155. data/spec/flipper/cloud/configuration_spec.rb +251 -0
  156. data/spec/flipper/cloud/dsl_spec.rb +82 -0
  157. data/spec/flipper/cloud/message_verifier_spec.rb +104 -0
  158. data/spec/flipper/cloud/middleware_spec.rb +289 -0
  159. data/spec/flipper/cloud/telemetry/backoff_policy_spec.rb +107 -0
  160. data/spec/flipper/cloud/telemetry/metric_spec.rb +87 -0
  161. data/spec/flipper/cloud/telemetry/metric_storage_spec.rb +58 -0
  162. data/spec/flipper/cloud/telemetry/submitter_spec.rb +145 -0
  163. data/spec/flipper/cloud/telemetry_spec.rb +208 -0
  164. data/spec/flipper/cloud_spec.rb +181 -0
  165. data/spec/flipper/configuration_spec.rb +17 -0
  166. data/spec/flipper/dsl_spec.rb +54 -73
  167. data/spec/flipper/engine_spec.rb +373 -0
  168. data/spec/flipper/export_spec.rb +13 -0
  169. data/spec/flipper/exporter_spec.rb +16 -0
  170. data/spec/flipper/exporters/json/export_spec.rb +60 -0
  171. data/spec/flipper/exporters/json/v1_spec.rb +33 -0
  172. data/spec/flipper/expression/builder_spec.rb +248 -0
  173. data/spec/flipper/expression_spec.rb +188 -0
  174. data/spec/flipper/expressions/all_spec.rb +15 -0
  175. data/spec/flipper/expressions/any_spec.rb +15 -0
  176. data/spec/flipper/expressions/boolean_spec.rb +15 -0
  177. data/spec/flipper/expressions/duration_spec.rb +43 -0
  178. data/spec/flipper/expressions/equal_spec.rb +24 -0
  179. data/spec/flipper/expressions/greater_than_or_equal_to_spec.rb +28 -0
  180. data/spec/flipper/expressions/greater_than_spec.rb +28 -0
  181. data/spec/flipper/expressions/less_than_or_equal_to_spec.rb +28 -0
  182. data/spec/flipper/expressions/less_than_spec.rb +32 -0
  183. data/spec/flipper/expressions/not_equal_spec.rb +15 -0
  184. data/spec/flipper/expressions/now_spec.rb +11 -0
  185. data/spec/flipper/expressions/number_spec.rb +21 -0
  186. data/spec/flipper/expressions/percentage_of_actors_spec.rb +20 -0
  187. data/spec/flipper/expressions/percentage_spec.rb +15 -0
  188. data/spec/flipper/expressions/property_spec.rb +13 -0
  189. data/spec/flipper/expressions/random_spec.rb +9 -0
  190. data/spec/flipper/expressions/string_spec.rb +11 -0
  191. data/spec/flipper/expressions/time_spec.rb +13 -0
  192. data/spec/flipper/feature_check_context_spec.rb +17 -17
  193. data/spec/flipper/feature_spec.rb +436 -33
  194. data/spec/flipper/gate_values_spec.rb +2 -33
  195. data/spec/flipper/gates/boolean_spec.rb +1 -1
  196. data/spec/flipper/gates/expression_spec.rb +108 -0
  197. data/spec/flipper/gates/group_spec.rb +2 -3
  198. data/spec/flipper/gates/percentage_of_actors_spec.rb +61 -5
  199. data/spec/flipper/gates/percentage_of_time_spec.rb +2 -2
  200. data/spec/flipper/identifier_spec.rb +4 -5
  201. data/spec/flipper/instrumentation/log_subscriber_spec.rb +23 -6
  202. data/spec/flipper/instrumentation/statsd_subscriber_spec.rb +25 -1
  203. data/spec/flipper/middleware/memoizer_spec.rb +74 -24
  204. data/spec/flipper/model/active_record_spec.rb +61 -0
  205. data/spec/flipper/poller_spec.rb +47 -0
  206. data/spec/flipper/serializers/gzip_spec.rb +13 -0
  207. data/spec/flipper/serializers/json_spec.rb +13 -0
  208. data/spec/flipper/typecast_spec.rb +121 -6
  209. data/spec/flipper/types/actor_spec.rb +63 -46
  210. data/spec/flipper/types/group_spec.rb +2 -2
  211. data/spec/flipper_integration_spec.rb +168 -58
  212. data/spec/flipper_spec.rb +93 -29
  213. data/spec/spec_helper.rb +8 -14
  214. data/spec/support/actor_names.yml +1 -0
  215. data/spec/support/fail_on_output.rb +8 -0
  216. data/spec/support/fake_backoff_policy.rb +15 -0
  217. data/spec/support/skippable.rb +18 -0
  218. data/spec/support/spec_helpers.rb +23 -8
  219. data/test/adapters/actor_limit_test.rb +20 -0
  220. data/test_rails/generators/flipper/setup_generator_test.rb +64 -0
  221. data/test_rails/generators/flipper/update_generator_test.rb +96 -0
  222. data/test_rails/helper.rb +19 -2
  223. data/test_rails/system/test_help_test.rb +51 -0
  224. metadata +223 -19
  225. data/lib/flipper/railtie.rb +0 -47
  226. data/spec/flipper/railtie_spec.rb +0 -73
@@ -0,0 +1,263 @@
1
+ require "logger"
2
+ require "socket"
3
+ require "flipper/adapters/http"
4
+ require "flipper/adapters/poll"
5
+ require "flipper/poller"
6
+ require "flipper/adapters/memory"
7
+ require "flipper/adapters/dual_write"
8
+ require "flipper/adapters/sync/synchronizer"
9
+ require "flipper/cloud/telemetry"
10
+ require "flipper/cloud/telemetry/instrumenter"
11
+ require "flipper/cloud/telemetry/submitter"
12
+
13
+ module Flipper
14
+ module Cloud
15
+ class Configuration
16
+ # The set of valid ways that syncing can happpen.
17
+ VALID_SYNC_METHODS = Set[
18
+ :poll,
19
+ :webhook,
20
+ ].freeze
21
+
22
+ DEFAULT_URL = "https://www.flippercloud.io/adapter".freeze
23
+
24
+ # Public: The token corresponding to an environment on flippercloud.io.
25
+ attr_accessor :token
26
+
27
+ # Public: The url for http adapter. Really should only be customized for
28
+ # development work if you are me and you are not me. Feel free to
29
+ # forget you ever saw this.
30
+ attr_accessor :url
31
+
32
+ # Public: net/http read timeout for all http requests (default: 5).
33
+ attr_accessor :read_timeout
34
+
35
+ # Public: net/http open timeout for all http requests (default: 5).
36
+ attr_accessor :open_timeout
37
+
38
+ # Public: net/http write timeout for all http requests (default: 5).
39
+ attr_accessor :write_timeout
40
+
41
+ # Public: IO stream to send debug output too. Off by default.
42
+ #
43
+ # # for example, this would send all http request information to STDOUT
44
+ # configuration = Flipper::Cloud::Configuration.new
45
+ # configuration.debug_output = STDOUT
46
+ attr_accessor :debug_output
47
+
48
+ # Public: Instrumenter to use for the Flipper instance returned by
49
+ # Flipper::Cloud.new (default: Flipper::Instrumenters::Noop).
50
+ #
51
+ # # for example, to use active support notifications you could do:
52
+ # configuration = Flipper::Cloud::Configuration.new
53
+ # configuration.instrumenter = ActiveSupport::Notifications
54
+ attr_accessor :instrumenter
55
+
56
+ # Public: Local adapter that all reads should go to in order to ensure
57
+ # latency is low and resiliency is high. This adapter is automatically
58
+ # kept in sync with cloud.
59
+ #
60
+ # # for example, to use active record you could do:
61
+ # configuration = Flipper::Cloud::Configuration.new
62
+ # configuration.local_adapter = Flipper::Adapters::ActiveRecord.new
63
+ attr_accessor :local_adapter
64
+
65
+ # Public: The Integer or Float number of seconds between attempts to bring
66
+ # the local in sync with cloud (default: 10).
67
+ attr_accessor :sync_interval
68
+
69
+ # Public: The secret used to verify if syncs in the middleware should
70
+ # occur or not.
71
+ attr_accessor :sync_secret
72
+
73
+ # Public: The logger to use for debugging inner workings.
74
+ attr_accessor :logger
75
+
76
+ # Public: Should the logger log or not (default: true).
77
+ attr_accessor :logging_enabled
78
+
79
+ # Public: The telemetry instance to use for tracking feature usage.
80
+ attr_accessor :telemetry
81
+
82
+ # Public: Should telemetry be enabled or not (default: false).
83
+ attr_accessor :telemetry_enabled
84
+
85
+ def initialize(options = {})
86
+ setup_auth options
87
+ setup_log options
88
+ setup_http options
89
+ setup_sync options
90
+ setup_adapter options
91
+ setup_telemetry options
92
+ end
93
+
94
+ # Public: Read or customize the http adapter. Calling without a block will
95
+ # perform a read. Calling with a block yields the cloud adapter
96
+ # for customization.
97
+ #
98
+ # # for example, to instrument the http calls, you can wrap the http
99
+ # # adapter with the intsrumented adapter
100
+ # configuration = Flipper::Cloud::Configuration.new
101
+ # configuration.adapter do |adapter|
102
+ # Flipper::Adapters::Instrumented.new(adapter)
103
+ # end
104
+ #
105
+ def adapter(&block)
106
+ if block_given?
107
+ @adapter_block = block
108
+ else
109
+ @adapter_block.call app_adapter
110
+ end
111
+ end
112
+
113
+ # Public: Force a sync.
114
+ def sync
115
+ Flipper::Adapters::Sync::Synchronizer.new(local_adapter, http_adapter, {
116
+ instrumenter: instrumenter,
117
+ }).call
118
+ end
119
+
120
+ # Public: The method that will be used to synchronize local adapter with
121
+ # cloud. (default: :poll, will be :webhook if sync_secret is set).
122
+ def sync_method
123
+ sync_secret ? :webhook : :poll
124
+ end
125
+
126
+ # Internal: The http client used by the http adapter. Exposed so we can
127
+ # use the same client for posting telemetry.
128
+ def http_client
129
+ http_adapter.client
130
+ end
131
+
132
+ # Internal: Logs message if logging is enabled.
133
+ def log(message, level: :debug)
134
+ return unless logging_enabled
135
+ logger.send(level, "name=flipper_cloud #{message}")
136
+ end
137
+
138
+ private
139
+
140
+ def app_adapter
141
+ read_adapter = sync_method == :webhook ? local_adapter : poll_adapter
142
+ Flipper::Adapters::DualWrite.new(read_adapter, http_adapter)
143
+ end
144
+
145
+ def poller
146
+ Flipper::Poller.get(@url + @token, {
147
+ interval: sync_interval,
148
+ remote_adapter: http_adapter,
149
+ instrumenter: instrumenter,
150
+ }).tap(&:start)
151
+ end
152
+
153
+ def poll_adapter
154
+ Flipper::Adapters::Poll.new(poller, local_adapter)
155
+ end
156
+
157
+ def http_adapter
158
+ Flipper::Adapters::Http.new({
159
+ url: @url,
160
+ read_timeout: @read_timeout,
161
+ open_timeout: @open_timeout,
162
+ write_timeout: @write_timeout,
163
+ max_retries: 0, # we'll handle retries ourselves
164
+ debug_output: @debug_output,
165
+ headers: {
166
+ "flipper-cloud-token" => @token,
167
+ "accept-encoding" => "gzip",
168
+ },
169
+ })
170
+ end
171
+
172
+ def setup_auth(options)
173
+ set_option :token, options, required: true
174
+ end
175
+
176
+ def setup_log(options)
177
+ set_option :logging_enabled, options, default: false, typecast: :boolean
178
+ set_option :logger, options, from_env: false, default: -> {
179
+ if logging_enabled
180
+ Logger.new(STDOUT)
181
+ else
182
+ Logger.new("/dev/null")
183
+ end
184
+ }
185
+ end
186
+
187
+ def setup_http(options)
188
+ set_option :url, options, default: DEFAULT_URL
189
+ set_option :debug_output, options, from_env: false
190
+
191
+ if @debug_output.nil? && Flipper::Typecast.to_boolean(ENV["FLIPPER_CLOUD_DEBUG_OUTPUT_STDOUT"])
192
+ @debug_output = STDOUT
193
+ end
194
+
195
+ set_option :read_timeout, options, default: 5, typecast: :float, minimum: 0.1
196
+ set_option :open_timeout, options, default: 2, typecast: :float, minimum: 0.1
197
+ set_option :write_timeout, options, default: 5, typecast: :float, minimum: 0.1
198
+ end
199
+
200
+ def setup_sync(options)
201
+ set_option :sync_interval, options, default: 10, typecast: :float, minimum: 10
202
+ set_option :sync_secret, options
203
+ end
204
+
205
+ def setup_adapter(options)
206
+ set_option :local_adapter, options, default: -> { Adapters::Memory.new }, from_env: false
207
+ @adapter_block = ->(adapter) { adapter }
208
+ end
209
+
210
+ def setup_telemetry(options)
211
+ # Needs to be after url and token assignments because they are used for
212
+ # uniqueness in Telemetry.instance_for.
213
+ set_option :telemetry, options, from_env: false, default: -> {
214
+ Telemetry.instance_for(self)
215
+ }
216
+
217
+ set_option :telemetry_enabled, options, default: true, typecast: :boolean
218
+ instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
219
+ @instrumenter = if telemetry_enabled
220
+ Telemetry::Instrumenter.new(self, instrumenter)
221
+ else
222
+ instrumenter
223
+ end
224
+ end
225
+
226
+ # Internal: Super helper for defining an option that can be set via
227
+ # options hash or ENV with defaults, typecasting and minimums.
228
+ def set_option(name, options, default: nil, typecast: nil, minimum: nil, from_env: true, required: false)
229
+ env_var = "FLIPPER_CLOUD_#{name.to_s.upcase}"
230
+ value = options.fetch(name) {
231
+ default_value = default.respond_to?(:call) ? default.call : default
232
+ if from_env
233
+ ENV.fetch(env_var, default_value)
234
+ else
235
+ default_value
236
+ end
237
+ }
238
+ value = Flipper::Typecast.send("to_#{typecast}", value) if typecast
239
+ send("#{name}=", value)
240
+ enforce_minimum(name, minimum) if minimum
241
+
242
+ if required
243
+ option_value = send(name)
244
+ if option_value.nil? || option_value.empty?
245
+ message = "Flipper::Cloud #{name} is missing. Please "
246
+ message << "set #{env_var} or " if from_env
247
+ message << "provide #{name} (e.g. Flipper::Cloud.new(#{name}: value))."
248
+ raise ArgumentError, message
249
+ end
250
+ end
251
+ end
252
+
253
+ # Enforce minimum interval for tasks that run on a timer.
254
+ def enforce_minimum(name, minimum)
255
+ provided = send(name)
256
+ if provided && provided < minimum
257
+ warn "Flipper::Cloud##{name} must be at least #{minimum} seconds but was #{provided}. Using #{minimum} seconds."
258
+ send(:instance_variable_set, "@#{name}", minimum)
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,27 @@
1
+ require 'forwardable'
2
+
3
+ module Flipper
4
+ module Cloud
5
+ class DSL < SimpleDelegator
6
+ attr_reader :cloud_configuration
7
+
8
+ def initialize(cloud_configuration)
9
+ @cloud_configuration = cloud_configuration
10
+ super Flipper.new(@cloud_configuration.adapter, instrumenter: @cloud_configuration.instrumenter)
11
+ end
12
+
13
+ def sync
14
+ @cloud_configuration.sync
15
+ end
16
+
17
+ def sync_secret
18
+ @cloud_configuration.sync_secret
19
+ end
20
+
21
+ def inspect
22
+ inspect_id = ::Kernel::format "%x", (object_id * 2)
23
+ %(#<#{self.class}:0x#{inspect_id} @cloud_configuration=#{cloud_configuration.inspect}, flipper=#{__getobj__.inspect}>)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,95 @@
1
+ require "openssl"
2
+ require "digest/sha2"
3
+
4
+ module Flipper
5
+ module Cloud
6
+ class MessageVerifier
7
+ class InvalidSignature < StandardError; end
8
+
9
+ DEFAULT_VERSION = "v1"
10
+
11
+ def self.header(signature, timestamp, version = DEFAULT_VERSION)
12
+ raise ArgumentError, "timestamp should be an instance of Time" unless timestamp.is_a?(Time)
13
+ raise ArgumentError, "signature should be a string" unless signature.is_a?(String)
14
+ "t=#{timestamp.to_i},#{version}=#{signature}"
15
+ end
16
+
17
+ def initialize(secret:, version: DEFAULT_VERSION)
18
+ @secret = secret
19
+ @version = version || DEFAULT_VERSION
20
+
21
+ raise ArgumentError, "secret should be a string" unless @secret.is_a?(String)
22
+ raise ArgumentError, "version should be a string" unless @version.is_a?(String)
23
+ end
24
+
25
+ def generate(payload, timestamp)
26
+ raise ArgumentError, "timestamp should be an instance of Time" unless timestamp.is_a?(Time)
27
+ raise ArgumentError, "payload should be a string" unless payload.is_a?(String)
28
+
29
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), @secret, "#{timestamp.to_i}.#{payload}")
30
+ end
31
+
32
+ def header(signature, timestamp)
33
+ self.class.header(signature, timestamp, @version)
34
+ end
35
+
36
+ # Public: Verifies the signature header for a given payload.
37
+ #
38
+ # Raises a InvalidSignature in the following cases:
39
+ # - the header does not match the expected format
40
+ # - no signatures found with the expected scheme
41
+ # - no signatures matching the expected signature
42
+ # - a tolerance is provided and the timestamp is not within the
43
+ # tolerance
44
+ #
45
+ # Returns true otherwise.
46
+ def verify(payload, header, tolerance: nil)
47
+ begin
48
+ timestamp, signatures = get_timestamp_and_signatures(header)
49
+ rescue StandardError
50
+ raise InvalidSignature, "Unable to extract timestamp and signatures from header"
51
+ end
52
+
53
+ if signatures.empty?
54
+ raise InvalidSignature, "No signatures found with expected version #{@version}"
55
+ end
56
+
57
+ expected_sig = generate(payload, timestamp)
58
+ unless signatures.any? { |s| secure_compare(expected_sig, s) }
59
+ raise InvalidSignature, "No signatures found matching the expected signature for payload"
60
+ end
61
+
62
+ if tolerance && timestamp < Time.now - tolerance
63
+ raise InvalidSignature, "Timestamp outside the tolerance zone (#{Time.at(timestamp)})"
64
+ end
65
+
66
+ true
67
+ end
68
+
69
+ private
70
+
71
+ # Extracts the timestamp and the signature(s) with the desired version
72
+ # from the header
73
+ def get_timestamp_and_signatures(header)
74
+ list_items = header.split(/,\s*/).map { |i| i.split("=", 2) }
75
+ timestamp = Integer(list_items.select { |i| i[0] == "t" }[0][1])
76
+ signatures = list_items.select { |i| i[0] == @version }.map { |i| i[1] }
77
+ [Time.at(timestamp), signatures]
78
+ end
79
+
80
+ # Private
81
+ def fixed_length_secure_compare(a, b)
82
+ raise ArgumentError, "string length mismatch." unless a.bytesize == b.bytesize
83
+ l = a.unpack "C#{a.bytesize}"
84
+ res = 0
85
+ b.each_byte { |byte| res |= byte ^ l.shift }
86
+ res == 0
87
+ end
88
+
89
+ # Private
90
+ def secure_compare(a, b)
91
+ fixed_length_secure_compare(::Digest::SHA256.digest(a), ::Digest::SHA256.digest(b)) && a == b
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flipper/cloud/message_verifier"
4
+
5
+ module Flipper
6
+ module Cloud
7
+ class Middleware
8
+ # Internal: The path to match for webhook requests.
9
+ WEBHOOK_PATH = %r{\A/webhooks\/?\Z}
10
+ # Internal: The root path to match for requests.
11
+ ROOT_PATH = %r{\A/\Z}
12
+
13
+ def initialize(app, options = {})
14
+ @app = app
15
+ @env_key = options.fetch(:env_key, 'flipper')
16
+ end
17
+
18
+ def call(env)
19
+ dup.call!(env)
20
+ end
21
+
22
+ def call!(env)
23
+ request = Rack::Request.new(env)
24
+ if request.post? && (request.path_info.match(ROOT_PATH) || request.path_info.match(WEBHOOK_PATH))
25
+ status = 200
26
+ headers = {
27
+ Rack::CONTENT_TYPE => "application/json",
28
+ }
29
+ body = "{}"
30
+ payload = request.body.read
31
+ signature = request.env["HTTP_FLIPPER_CLOUD_SIGNATURE"]
32
+ flipper = env.fetch(@env_key)
33
+
34
+ begin
35
+ message_verifier = MessageVerifier.new(secret: flipper.sync_secret)
36
+ if message_verifier.verify(payload, signature)
37
+ begin
38
+ flipper.sync
39
+ body = JSON.generate({
40
+ groups: Flipper.group_names.map { |name| {name: name}}
41
+ })
42
+ rescue Flipper::Adapters::Http::Error => error
43
+ status = error.response.code.to_i == 402 ? 402 : 500
44
+ headers["flipper-cloud-response-error-class"] = error.class.name
45
+ headers["flipper-cloud-response-error-message"] = error.message
46
+ rescue => error
47
+ status = 500
48
+ headers["flipper-cloud-response-error-class"] = error.class.name
49
+ headers["flipper-cloud-response-error-message"] = error.message
50
+ end
51
+ end
52
+ rescue MessageVerifier::InvalidSignature
53
+ status = 400
54
+ end
55
+
56
+ [status, headers, [body]]
57
+ else
58
+ @app.call(env)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ # Default routes loaded by Flipper::Cloud::Engine
2
+ Rails.application.routes.draw do
3
+ if ENV["FLIPPER_CLOUD_TOKEN"] && ENV["FLIPPER_CLOUD_SYNC_SECRET"]
4
+ require 'flipper/cloud'
5
+ config = Rails.application.config.flipper
6
+
7
+ cloud_app = Flipper::Cloud.app(nil,
8
+ env_key: config.env_key,
9
+ memoizer_options: { preload: config.preload }
10
+ )
11
+
12
+ mount cloud_app, at: config.cloud_path
13
+ end
14
+ end
@@ -0,0 +1,93 @@
1
+ module Flipper
2
+ module Cloud
3
+ class Telemetry
4
+ class BackoffPolicy
5
+ # Private: The default minimum timeout between intervals in milliseconds.
6
+ MIN_TIMEOUT_MS = 1_000
7
+
8
+ # Private: The default maximum timeout between intervals in milliseconds.
9
+ MAX_TIMEOUT_MS = 30_000
10
+
11
+ # Private: The value to multiply the current interval with for each
12
+ # retry attempt.
13
+ MULTIPLIER = 1.5
14
+
15
+ # Private: The randomization factor to use to create a range around the
16
+ # retry interval.
17
+ RANDOMIZATION_FACTOR = 0.5
18
+
19
+ # Private
20
+ attr_reader :min_timeout_ms, :max_timeout_ms, :multiplier, :randomization_factor
21
+
22
+ # Private
23
+ attr_reader :attempts
24
+
25
+ # Public: Create new instance of backoff policy.
26
+ #
27
+ # options - The Hash of options.
28
+ # :min_timeout_ms - The minimum backoff timeout.
29
+ # :max_timeout_ms - The maximum backoff timeout.
30
+ # :multiplier - The value to multiply the current interval with for each
31
+ # retry attempt.
32
+ # :randomization_factor - The randomization factor to use to create a range
33
+ # around the retry interval.
34
+ def initialize(options = {})
35
+ @min_timeout_ms = options.fetch(:min_timeout_ms) {
36
+ ENV.fetch("FLIPPER_BACKOFF_MIN_TIMEOUT_MS", MIN_TIMEOUT_MS).to_i
37
+ }
38
+ @max_timeout_ms = options.fetch(:max_timeout_ms) {
39
+ ENV.fetch("FLIPPER_BACKOFF_MAX_TIMEOUT_MS", MAX_TIMEOUT_MS).to_i
40
+ }
41
+ @multiplier = options.fetch(:multiplier) {
42
+ ENV.fetch("FLIPPER_BACKOFF_MULTIPLIER", MULTIPLIER).to_f
43
+ }
44
+ @randomization_factor = options.fetch(:randomization_factor) {
45
+ ENV.fetch("FLIPPER_BACKOFF_RANDOMIZATION_FACTOR", RANDOMIZATION_FACTOR).to_f
46
+ }
47
+
48
+ unless @min_timeout_ms >= 0
49
+ raise ArgumentError, ":min_timeout_ms must be >= 0 but was #{@min_timeout_ms.inspect}"
50
+ end
51
+
52
+ unless @max_timeout_ms >= 0
53
+ raise ArgumentError, ":max_timeout_ms must be >= 0 but was #{@max_timeout_ms.inspect}"
54
+ end
55
+
56
+ unless @min_timeout_ms <= max_timeout_ms
57
+ raise ArgumentError, ":min_timeout_ms (#{@min_timeout_ms.inspect}) must be <= :max_timeout_ms (#{@max_timeout_ms.inspect})"
58
+ end
59
+
60
+ @attempts = 0
61
+ end
62
+
63
+ # Public: Returns the next backoff interval in milliseconds.
64
+ def next_interval
65
+ interval = @min_timeout_ms * (@multiplier**@attempts)
66
+ interval = add_jitter(interval, @randomization_factor)
67
+
68
+ @attempts += 1
69
+
70
+ [interval, @max_timeout_ms].min
71
+ end
72
+
73
+ def reset
74
+ @attempts = 0
75
+ end
76
+
77
+ private
78
+
79
+ def add_jitter(base, randomization_factor)
80
+ random_number = rand
81
+ max_deviation = base * randomization_factor
82
+ deviation = random_number * max_deviation
83
+
84
+ if random_number < 0.5
85
+ base - deviation
86
+ else
87
+ base + deviation
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,22 @@
1
+ require "delegate"
2
+
3
+ module Flipper
4
+ module Cloud
5
+ class Telemetry
6
+ class Instrumenter
7
+ attr_reader :instrumenter
8
+
9
+ def initialize(cloud_configuration, instrumenter)
10
+ @instrumenter = instrumenter
11
+ @cloud_configuration = cloud_configuration
12
+ end
13
+
14
+ def instrument(name, payload = {}, &block)
15
+ return_value = instrumenter.instrument(name, payload, &block)
16
+ @cloud_configuration.telemetry.record(name, payload)
17
+ return_value
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module Flipper
2
+ module Cloud
3
+ class Telemetry
4
+ class Metric
5
+ attr_reader :key, :time, :result
6
+
7
+ def initialize(key, result, time = Time.now)
8
+ @key = key
9
+ @result = result
10
+ @time = time.to_i / 60 * 60
11
+ end
12
+
13
+ def as_json(options = {})
14
+ data = {
15
+ "key" => key.to_s,
16
+ "time" => time,
17
+ "result" => result,
18
+ }
19
+
20
+ if options[:with]
21
+ data.merge!(options[:with])
22
+ end
23
+
24
+ data
25
+ end
26
+
27
+ def eql?(other)
28
+ self.class.eql?(other.class) &&
29
+ @key == other.key && @time == other.time && @result == other.result
30
+ end
31
+ alias :== :eql?
32
+
33
+ def hash
34
+ [self.class, @key, @time, @result].hash
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require 'concurrent/map'
2
+ require 'concurrent/atomic/atomic_fixnum'
3
+
4
+ module Flipper
5
+ module Cloud
6
+ class Telemetry
7
+ class MetricStorage
8
+ def initialize
9
+ @storage = Concurrent::Map.new { |h, k| h[k] = Concurrent::AtomicFixnum.new(0) }
10
+ end
11
+
12
+ def increment(metric)
13
+ @storage[metric].increment
14
+ end
15
+
16
+ def drain
17
+ metrics = {}
18
+ @storage.keys.each do |metric|
19
+ metrics[metric] = @storage.delete(metric).value
20
+ end
21
+ metrics.freeze
22
+ end
23
+
24
+ def empty?
25
+ @storage.empty?
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end