omg-activesupport 8.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (289) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +86 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support/actionable_error.rb +50 -0
  6. data/lib/active_support/all.rb +5 -0
  7. data/lib/active_support/array_inquirer.rb +50 -0
  8. data/lib/active_support/backtrace_cleaner.rb +163 -0
  9. data/lib/active_support/benchmark.rb +21 -0
  10. data/lib/active_support/benchmarkable.rb +53 -0
  11. data/lib/active_support/broadcast_logger.rb +251 -0
  12. data/lib/active_support/builder.rb +8 -0
  13. data/lib/active_support/cache/coder.rb +153 -0
  14. data/lib/active_support/cache/entry.rb +134 -0
  15. data/lib/active_support/cache/file_store.rb +244 -0
  16. data/lib/active_support/cache/mem_cache_store.rb +290 -0
  17. data/lib/active_support/cache/memory_store.rb +262 -0
  18. data/lib/active_support/cache/null_store.rb +62 -0
  19. data/lib/active_support/cache/redis_cache_store.rb +492 -0
  20. data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
  21. data/lib/active_support/cache/strategy/local_cache.rb +201 -0
  22. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  23. data/lib/active_support/cache.rb +1104 -0
  24. data/lib/active_support/callbacks.rb +944 -0
  25. data/lib/active_support/class_attribute.rb +26 -0
  26. data/lib/active_support/code_generator.rb +79 -0
  27. data/lib/active_support/concern.rb +217 -0
  28. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
  29. data/lib/active_support/concurrency/null_lock.rb +13 -0
  30. data/lib/active_support/concurrency/share_lock.rb +225 -0
  31. data/lib/active_support/configurable.rb +159 -0
  32. data/lib/active_support/configuration_file.rb +60 -0
  33. data/lib/active_support/core_ext/array/access.rb +100 -0
  34. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  35. data/lib/active_support/core_ext/array/extract.rb +21 -0
  36. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  37. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  38. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  39. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  40. data/lib/active_support/core_ext/array.rb +9 -0
  41. data/lib/active_support/core_ext/benchmark.rb +13 -0
  42. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  43. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  44. data/lib/active_support/core_ext/class/attribute.rb +122 -0
  45. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  46. data/lib/active_support/core_ext/class/subclasses.rb +24 -0
  47. data/lib/active_support/core_ext/class.rb +4 -0
  48. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  49. data/lib/active_support/core_ext/date/blank.rb +18 -0
  50. data/lib/active_support/core_ext/date/calculations.rb +161 -0
  51. data/lib/active_support/core_ext/date/conversions.rb +98 -0
  52. data/lib/active_support/core_ext/date/zones.rb +8 -0
  53. data/lib/active_support/core_ext/date.rb +7 -0
  54. data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
  55. data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
  56. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  57. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  58. data/lib/active_support/core_ext/date_time/blank.rb +18 -0
  59. data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
  60. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  61. data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
  62. data/lib/active_support/core_ext/date_time.rb +7 -0
  63. data/lib/active_support/core_ext/digest/uuid.rb +76 -0
  64. data/lib/active_support/core_ext/digest.rb +3 -0
  65. data/lib/active_support/core_ext/enumerable.rb +267 -0
  66. data/lib/active_support/core_ext/erb/util.rb +201 -0
  67. data/lib/active_support/core_ext/file/atomic.rb +72 -0
  68. data/lib/active_support/core_ext/file.rb +3 -0
  69. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  70. data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
  71. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  72. data/lib/active_support/core_ext/hash/except.rb +12 -0
  73. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  74. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  75. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  76. data/lib/active_support/core_ext/hash/slice.rb +27 -0
  77. data/lib/active_support/core_ext/hash.rb +10 -0
  78. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  79. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  80. data/lib/active_support/core_ext/integer/time.rb +22 -0
  81. data/lib/active_support/core_ext/integer.rb +5 -0
  82. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  83. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  84. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  85. data/lib/active_support/core_ext/kernel.rb +5 -0
  86. data/lib/active_support/core_ext/load_error.rb +9 -0
  87. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  88. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  89. data/lib/active_support/core_ext/module/attr_internal.rb +49 -0
  90. data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
  91. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
  92. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  93. data/lib/active_support/core_ext/module/delegation.rb +225 -0
  94. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  95. data/lib/active_support/core_ext/module/introspection.rb +62 -0
  96. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  97. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  98. data/lib/active_support/core_ext/module.rb +13 -0
  99. data/lib/active_support/core_ext/name_error.rb +59 -0
  100. data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
  101. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  102. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  103. data/lib/active_support/core_ext/numeric.rb +5 -0
  104. data/lib/active_support/core_ext/object/acts_like.rb +45 -0
  105. data/lib/active_support/core_ext/object/blank.rb +199 -0
  106. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  107. data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
  108. data/lib/active_support/core_ext/object/duplicable.rb +69 -0
  109. data/lib/active_support/core_ext/object/inclusion.rb +37 -0
  110. data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
  111. data/lib/active_support/core_ext/object/json.rb +260 -0
  112. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  113. data/lib/active_support/core_ext/object/to_query.rb +87 -0
  114. data/lib/active_support/core_ext/object/try.rb +158 -0
  115. data/lib/active_support/core_ext/object/with.rb +46 -0
  116. data/lib/active_support/core_ext/object/with_options.rb +101 -0
  117. data/lib/active_support/core_ext/object.rb +17 -0
  118. data/lib/active_support/core_ext/pathname/blank.rb +20 -0
  119. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  120. data/lib/active_support/core_ext/pathname.rb +4 -0
  121. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  122. data/lib/active_support/core_ext/range/conversions.rb +62 -0
  123. data/lib/active_support/core_ext/range/each.rb +24 -0
  124. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  125. data/lib/active_support/core_ext/range.rb +6 -0
  126. data/lib/active_support/core_ext/regexp.rb +14 -0
  127. data/lib/active_support/core_ext/securerandom.rb +41 -0
  128. data/lib/active_support/core_ext/string/access.rb +95 -0
  129. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  130. data/lib/active_support/core_ext/string/conversions.rb +60 -0
  131. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  132. data/lib/active_support/core_ext/string/filters.rb +151 -0
  133. data/lib/active_support/core_ext/string/indent.rb +45 -0
  134. data/lib/active_support/core_ext/string/inflections.rb +300 -0
  135. data/lib/active_support/core_ext/string/inquiry.rb +16 -0
  136. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  137. data/lib/active_support/core_ext/string/output_safety.rb +228 -0
  138. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/string/strip.rb +27 -0
  140. data/lib/active_support/core_ext/string/zones.rb +16 -0
  141. data/lib/active_support/core_ext/string.rb +15 -0
  142. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  143. data/lib/active_support/core_ext/symbol.rb +3 -0
  144. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  145. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  146. data/lib/active_support/core_ext/time/calculations.rb +386 -0
  147. data/lib/active_support/core_ext/time/compatibility.rb +32 -0
  148. data/lib/active_support/core_ext/time/conversions.rb +75 -0
  149. data/lib/active_support/core_ext/time/zones.rb +97 -0
  150. data/lib/active_support/core_ext/time.rb +7 -0
  151. data/lib/active_support/core_ext.rb +5 -0
  152. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  153. data/lib/active_support/current_attributes.rb +233 -0
  154. data/lib/active_support/deep_mergeable.rb +53 -0
  155. data/lib/active_support/delegation.rb +202 -0
  156. data/lib/active_support/dependencies/autoload.rb +72 -0
  157. data/lib/active_support/dependencies/interlock.rb +49 -0
  158. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  159. data/lib/active_support/dependencies.rb +98 -0
  160. data/lib/active_support/deprecation/behaviors.rb +148 -0
  161. data/lib/active_support/deprecation/constant_accessor.rb +74 -0
  162. data/lib/active_support/deprecation/deprecators.rb +104 -0
  163. data/lib/active_support/deprecation/disallowed.rb +54 -0
  164. data/lib/active_support/deprecation/method_wrappers.rb +68 -0
  165. data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
  166. data/lib/active_support/deprecation/reporting.rb +179 -0
  167. data/lib/active_support/deprecation.rb +81 -0
  168. data/lib/active_support/deprecator.rb +7 -0
  169. data/lib/active_support/descendants_tracker.rb +112 -0
  170. data/lib/active_support/digest.rb +22 -0
  171. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  172. data/lib/active_support/duration/iso8601_serializer.rb +64 -0
  173. data/lib/active_support/duration.rb +520 -0
  174. data/lib/active_support/encrypted_configuration.rb +126 -0
  175. data/lib/active_support/encrypted_file.rb +133 -0
  176. data/lib/active_support/environment_inquirer.rb +40 -0
  177. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  178. data/lib/active_support/error_reporter.rb +265 -0
  179. data/lib/active_support/evented_file_update_checker.rb +182 -0
  180. data/lib/active_support/execution_context/test_helper.rb +13 -0
  181. data/lib/active_support/execution_context.rb +53 -0
  182. data/lib/active_support/execution_wrapper.rb +150 -0
  183. data/lib/active_support/executor/test_helper.rb +7 -0
  184. data/lib/active_support/executor.rb +8 -0
  185. data/lib/active_support/file_update_checker.rb +164 -0
  186. data/lib/active_support/fork_tracker.rb +43 -0
  187. data/lib/active_support/gem_version.rb +17 -0
  188. data/lib/active_support/gzip.rb +40 -0
  189. data/lib/active_support/hash_with_indifferent_access.rb +445 -0
  190. data/lib/active_support/html_safe_translation.rb +56 -0
  191. data/lib/active_support/i18n.rb +17 -0
  192. data/lib/active_support/i18n_railtie.rb +138 -0
  193. data/lib/active_support/inflections.rb +72 -0
  194. data/lib/active_support/inflector/inflections.rb +273 -0
  195. data/lib/active_support/inflector/methods.rb +387 -0
  196. data/lib/active_support/inflector/transliterate.rb +149 -0
  197. data/lib/active_support/inflector.rb +9 -0
  198. data/lib/active_support/isolated_execution_state.rb +75 -0
  199. data/lib/active_support/json/decoding.rb +76 -0
  200. data/lib/active_support/json/encoding.rb +120 -0
  201. data/lib/active_support/json.rb +4 -0
  202. data/lib/active_support/key_generator.rb +66 -0
  203. data/lib/active_support/lazy_load_hooks.rb +107 -0
  204. data/lib/active_support/locale/en.rb +33 -0
  205. data/lib/active_support/locale/en.yml +141 -0
  206. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  207. data/lib/active_support/log_subscriber.rb +192 -0
  208. data/lib/active_support/logger.rb +55 -0
  209. data/lib/active_support/logger_silence.rb +21 -0
  210. data/lib/active_support/logger_thread_safe_level.rb +47 -0
  211. data/lib/active_support/message_encryptor.rb +374 -0
  212. data/lib/active_support/message_encryptors.rb +141 -0
  213. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  214. data/lib/active_support/message_pack/extensions.rb +305 -0
  215. data/lib/active_support/message_pack/serializer.rb +63 -0
  216. data/lib/active_support/message_pack.rb +50 -0
  217. data/lib/active_support/message_verifier.rb +368 -0
  218. data/lib/active_support/message_verifiers.rb +135 -0
  219. data/lib/active_support/messages/codec.rb +65 -0
  220. data/lib/active_support/messages/metadata.rb +146 -0
  221. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  222. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  223. data/lib/active_support/messages/rotator.rb +59 -0
  224. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  225. data/lib/active_support/multibyte/chars.rb +178 -0
  226. data/lib/active_support/multibyte/unicode.rb +42 -0
  227. data/lib/active_support/multibyte.rb +23 -0
  228. data/lib/active_support/notifications/fanout.rb +446 -0
  229. data/lib/active_support/notifications/instrumenter.rb +240 -0
  230. data/lib/active_support/notifications.rb +281 -0
  231. data/lib/active_support/number_helper/number_converter.rb +190 -0
  232. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  233. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  234. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  235. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  236. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  237. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  238. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  239. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  240. data/lib/active_support/number_helper.rb +479 -0
  241. data/lib/active_support/option_merger.rb +38 -0
  242. data/lib/active_support/ordered_hash.rb +50 -0
  243. data/lib/active_support/ordered_options.rb +147 -0
  244. data/lib/active_support/parameter_filter.rb +157 -0
  245. data/lib/active_support/proxy_object.rb +20 -0
  246. data/lib/active_support/rails.rb +26 -0
  247. data/lib/active_support/railtie.rb +161 -0
  248. data/lib/active_support/reloader.rb +138 -0
  249. data/lib/active_support/rescuable.rb +176 -0
  250. data/lib/active_support/secure_compare_rotator.rb +58 -0
  251. data/lib/active_support/security_utils.rb +38 -0
  252. data/lib/active_support/string_inquirer.rb +35 -0
  253. data/lib/active_support/subscriber.rb +146 -0
  254. data/lib/active_support/syntax_error_proxy.rb +60 -0
  255. data/lib/active_support/tagged_logging.rb +152 -0
  256. data/lib/active_support/test_case.rb +304 -0
  257. data/lib/active_support/testing/assertions.rb +332 -0
  258. data/lib/active_support/testing/autorun.rb +5 -0
  259. data/lib/active_support/testing/constant_lookup.rb +51 -0
  260. data/lib/active_support/testing/constant_stubbing.rb +54 -0
  261. data/lib/active_support/testing/declarative.rb +28 -0
  262. data/lib/active_support/testing/deprecation.rb +82 -0
  263. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  264. data/lib/active_support/testing/file_fixtures.rb +38 -0
  265. data/lib/active_support/testing/isolation.rb +121 -0
  266. data/lib/active_support/testing/method_call_assertions.rb +69 -0
  267. data/lib/active_support/testing/parallelization/server.rb +85 -0
  268. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  269. data/lib/active_support/testing/parallelization.rb +55 -0
  270. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  271. data/lib/active_support/testing/setup_and_teardown.rb +57 -0
  272. data/lib/active_support/testing/stream.rb +41 -0
  273. data/lib/active_support/testing/strict_warnings.rb +43 -0
  274. data/lib/active_support/testing/tagged_logging.rb +27 -0
  275. data/lib/active_support/testing/tests_without_assertions.rb +19 -0
  276. data/lib/active_support/testing/time_helpers.rb +269 -0
  277. data/lib/active_support/time.rb +20 -0
  278. data/lib/active_support/time_with_zone.rb +609 -0
  279. data/lib/active_support/values/time_zone.rb +614 -0
  280. data/lib/active_support/version.rb +10 -0
  281. data/lib/active_support/xml_mini/jdom.rb +175 -0
  282. data/lib/active_support/xml_mini/libxml.rb +80 -0
  283. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  284. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  285. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  286. data/lib/active_support/xml_mini/rexml.rb +137 -0
  287. data/lib/active_support/xml_mini.rb +211 -0
  288. data/lib/active_support.rb +144 -0
  289. metadata +526 -0
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/messages/rotation_coordinator"
4
+
5
+ module ActiveSupport
6
+ class MessageEncryptors < Messages::RotationCoordinator
7
+ ##
8
+ # :attr_accessor: transitional
9
+ #
10
+ # If true, the first two rotation option sets are swapped when building
11
+ # message encryptors. For example, with the following configuration, message
12
+ # encryptors will encrypt messages using <tt>serializer: Marshal, url_safe: true</tt>,
13
+ # and will able to decrypt messages that were encrypted using any of the
14
+ # three option sets:
15
+ #
16
+ # encryptors = ActiveSupport::MessageEncryptors.new { ... }
17
+ # encryptors.rotate(serializer: JSON, url_safe: true)
18
+ # encryptors.rotate(serializer: Marshal, url_safe: true)
19
+ # encryptors.rotate(serializer: Marshal, url_safe: false)
20
+ # encryptors.transitional = true
21
+ #
22
+ # This can be useful when performing a rolling deploy of an application,
23
+ # wherein servers that have not yet been updated must still be able to
24
+ # decrypt messages from updated servers. In such a scenario, first perform a
25
+ # rolling deploy with the new rotation (e.g. <tt>serializer: JSON, url_safe: true</tt>)
26
+ # as the first rotation and <tt>transitional = true</tt>. Then, after all
27
+ # servers have been updated, perform a second rolling deploy with
28
+ # <tt>transitional = false</tt>.
29
+
30
+ ##
31
+ # :method: initialize
32
+ # :call-seq: initialize(&secret_generator)
33
+ #
34
+ # Initializes a new instance. +secret_generator+ must accept a salt and a
35
+ # +secret_length+ kwarg, and return a suitable secret (string) or secrets
36
+ # (array of strings). +secret_generator+ may also accept other arbitrary
37
+ # kwargs. If #rotate is called with any options matching those kwargs, those
38
+ # options will be passed to +secret_generator+ instead of to the message
39
+ # encryptor.
40
+ #
41
+ # encryptors = ActiveSupport::MessageEncryptors.new do |salt, secret_length:, base:|
42
+ # MySecretGenerator.new(base).generate(salt, secret_length)
43
+ # end
44
+ #
45
+ # encryptors.rotate(base: "...")
46
+
47
+ ##
48
+ # :method: []
49
+ # :call-seq: [](salt)
50
+ #
51
+ # Returns a MessageEncryptor configured with a secret derived from the
52
+ # given +salt+, and options from #rotate. MessageEncryptor instances will
53
+ # be memoized, so the same +salt+ will return the same instance.
54
+
55
+ ##
56
+ # :method: []=
57
+ # :call-seq: []=(salt, encryptor)
58
+ #
59
+ # Overrides a MessageEncryptor instance associated with a given +salt+.
60
+
61
+ ##
62
+ # :method: rotate
63
+ # :call-seq:
64
+ # rotate(**options)
65
+ # rotate(&block)
66
+ #
67
+ # Adds +options+ to the list of option sets. Messages will be encrypted
68
+ # using the first set in the list. When decrypting, however, each set will
69
+ # be tried, in order, until one succeeds.
70
+ #
71
+ # Notably, the +:secret_generator+ option can specify a different secret
72
+ # generator than the one initially specified. The secret generator must
73
+ # respond to +call+, accept a salt and a +secret_length+ kwarg, and return
74
+ # a suitable secret (string) or secrets (array of strings). The secret
75
+ # generator may also accept other arbitrary kwargs.
76
+ #
77
+ # If any options match the kwargs of the operative secret generator, those
78
+ # options will be passed to the secret generator instead of to the message
79
+ # encryptor.
80
+ #
81
+ # For fine-grained per-salt rotations, a block form is supported. The block
82
+ # will receive the salt, and should return an appropriate options Hash. The
83
+ # block may also return +nil+ to indicate that the rotation does not apply
84
+ # to the given salt. For example:
85
+ #
86
+ # encryptors = ActiveSupport::MessageEncryptors.new { ... }
87
+ #
88
+ # encryptors.rotate do |salt|
89
+ # case salt
90
+ # when :foo
91
+ # { serializer: JSON, url_safe: true }
92
+ # when :bar
93
+ # { serializer: Marshal, url_safe: true }
94
+ # end
95
+ # end
96
+ #
97
+ # encryptors.rotate(serializer: Marshal, url_safe: false)
98
+ #
99
+ # # Uses `serializer: JSON, url_safe: true`.
100
+ # # Falls back to `serializer: Marshal, url_safe: false`.
101
+ # encryptors[:foo]
102
+ #
103
+ # # Uses `serializer: Marshal, url_safe: true`.
104
+ # # Falls back to `serializer: Marshal, url_safe: false`.
105
+ # encryptors[:bar]
106
+ #
107
+ # # Uses `serializer: Marshal, url_safe: false`.
108
+ # encryptors[:baz]
109
+
110
+ ##
111
+ # :method: rotate_defaults
112
+ # :call-seq: rotate_defaults
113
+ #
114
+ # Invokes #rotate with the default options.
115
+
116
+ ##
117
+ # :method: clear_rotations
118
+ # :call-seq: clear_rotations
119
+ #
120
+ # Clears the list of option sets.
121
+
122
+ ##
123
+ # :method: on_rotation
124
+ # :call-seq: on_rotation(&callback)
125
+ #
126
+ # Sets a callback to invoke when a message is decrypted using an option set
127
+ # other than the first.
128
+ #
129
+ # For example, this callback could log each time it is called, and thus
130
+ # indicate whether old option sets are still in use or can be removed from
131
+ # rotation.
132
+
133
+ ##
134
+ private
135
+ def build(salt, secret_generator:, secret_generator_options:, **options)
136
+ secret_length = MessageEncryptor.key_len(*options[:cipher])
137
+ secret = secret_generator.call(salt, secret_length: secret_length, **secret_generator_options)
138
+ MessageEncryptor.new(*Array(secret), **options)
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "serializer"
4
+
5
+ module ActiveSupport
6
+ module MessagePack
7
+ module CacheSerializer
8
+ include Serializer
9
+ extend self
10
+
11
+ def load(dumped)
12
+ super
13
+ rescue ActiveSupport::MessagePack::MissingClassError
14
+ # Treat missing class as cache miss => return nil
15
+ end
16
+
17
+ private
18
+ def install_unregistered_type_handler
19
+ Extensions.install_unregistered_type_fallback(message_pack_factory)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,305 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "date"
5
+ require "ipaddr"
6
+ require "pathname"
7
+ require "uri/generic"
8
+ require "msgpack/bigint"
9
+ require "active_support/hash_with_indifferent_access"
10
+ require "active_support/time"
11
+
12
+ module ActiveSupport
13
+ module MessagePack
14
+ class UnserializableObjectError < StandardError; end
15
+ class MissingClassError < StandardError; end # :nodoc:
16
+
17
+ module Extensions # :nodoc:
18
+ extend self
19
+
20
+ def install(registry)
21
+ registry.register_type 0, Symbol,
22
+ packer: :to_msgpack_ext,
23
+ unpacker: :from_msgpack_ext,
24
+ optimized_symbols_parsing: true
25
+
26
+ registry.register_type 1, Integer,
27
+ packer: ::MessagePack::Bigint.method(:to_msgpack_ext),
28
+ unpacker: ::MessagePack::Bigint.method(:from_msgpack_ext),
29
+ oversized_integer_extension: true
30
+
31
+ registry.register_type 2, BigDecimal,
32
+ packer: :_dump,
33
+ unpacker: :_load
34
+
35
+ registry.register_type 3, Rational,
36
+ packer: method(:write_rational),
37
+ unpacker: method(:read_rational),
38
+ recursive: true
39
+
40
+ registry.register_type 4, Complex,
41
+ packer: method(:write_complex),
42
+ unpacker: method(:read_complex),
43
+ recursive: true
44
+
45
+ registry.register_type 5, DateTime,
46
+ packer: method(:write_datetime),
47
+ unpacker: method(:read_datetime),
48
+ recursive: true
49
+
50
+ registry.register_type 6, Date,
51
+ packer: method(:write_date),
52
+ unpacker: method(:read_date),
53
+ recursive: true
54
+
55
+ registry.register_type 7, Time,
56
+ packer: method(:write_time),
57
+ unpacker: method(:read_time),
58
+ recursive: true
59
+
60
+ registry.register_type 8, ActiveSupport::TimeWithZone,
61
+ packer: method(:write_time_with_zone),
62
+ unpacker: method(:read_time_with_zone),
63
+ recursive: true
64
+
65
+ registry.register_type 9, ActiveSupport::TimeZone,
66
+ packer: method(:dump_time_zone),
67
+ unpacker: method(:load_time_zone)
68
+
69
+ registry.register_type 10, ActiveSupport::Duration,
70
+ packer: method(:write_duration),
71
+ unpacker: method(:read_duration),
72
+ recursive: true
73
+
74
+ registry.register_type 11, Range,
75
+ packer: method(:write_range),
76
+ unpacker: method(:read_range),
77
+ recursive: true
78
+
79
+ registry.register_type 12, Set,
80
+ packer: method(:write_set),
81
+ unpacker: method(:read_set),
82
+ recursive: true
83
+
84
+ registry.register_type 13, URI::Generic,
85
+ packer: :to_s,
86
+ unpacker: URI.method(:parse)
87
+
88
+ registry.register_type 14, IPAddr,
89
+ packer: method(:write_ipaddr),
90
+ unpacker: method(:read_ipaddr),
91
+ recursive: true
92
+
93
+ registry.register_type 15, Pathname,
94
+ packer: :to_s,
95
+ unpacker: :new
96
+
97
+ registry.register_type 16, Regexp,
98
+ packer: :to_s,
99
+ unpacker: :new
100
+
101
+ registry.register_type 17, ActiveSupport::HashWithIndifferentAccess,
102
+ packer: method(:write_hash_with_indifferent_access),
103
+ unpacker: method(:read_hash_with_indifferent_access),
104
+ recursive: true
105
+ end
106
+
107
+ def install_unregistered_type_error(registry)
108
+ registry.register_type 127, Object,
109
+ packer: method(:raise_unserializable),
110
+ unpacker: method(:raise_invalid_format)
111
+ end
112
+
113
+ def install_unregistered_type_fallback(registry)
114
+ registry.register_type 127, Object,
115
+ packer: method(:write_object),
116
+ unpacker: method(:read_object),
117
+ recursive: true
118
+ end
119
+
120
+ def write_rational(rational, packer)
121
+ packer.write(rational.numerator)
122
+ packer.write(rational.denominator) unless rational.numerator.zero?
123
+ end
124
+
125
+ def read_rational(unpacker)
126
+ numerator = unpacker.read
127
+ Rational(numerator, numerator.zero? ? 1 : unpacker.read)
128
+ end
129
+
130
+ def write_complex(complex, packer)
131
+ packer.write(complex.real)
132
+ packer.write(complex.imaginary)
133
+ end
134
+
135
+ def read_complex(unpacker)
136
+ Complex(unpacker.read, unpacker.read)
137
+ end
138
+
139
+ def write_datetime(datetime, packer)
140
+ packer.write(datetime.jd)
141
+ packer.write(datetime.hour)
142
+ packer.write(datetime.min)
143
+ packer.write(datetime.sec)
144
+ write_rational(datetime.sec_fraction, packer)
145
+ write_rational(datetime.offset, packer)
146
+ end
147
+
148
+ def read_datetime(unpacker)
149
+ DateTime.jd(unpacker.read, unpacker.read, unpacker.read, unpacker.read + read_rational(unpacker), read_rational(unpacker))
150
+ end
151
+
152
+ def write_date(date, packer)
153
+ packer.write(date.jd)
154
+ end
155
+
156
+ def read_date(unpacker)
157
+ Date.jd(unpacker.read)
158
+ end
159
+
160
+ def write_time(time, packer)
161
+ packer.write(time.tv_sec)
162
+ packer.write(time.tv_nsec)
163
+ packer.write(time.utc_offset)
164
+ end
165
+
166
+ def read_time(unpacker)
167
+ Time.at_without_coercion(unpacker.read, unpacker.read, :nanosecond, in: unpacker.read)
168
+ end
169
+
170
+ def write_time_with_zone(twz, packer)
171
+ write_time(twz.utc, packer)
172
+ write_time_zone(twz.time_zone, packer)
173
+ end
174
+
175
+ def read_time_with_zone(unpacker)
176
+ ActiveSupport::TimeWithZone.new(read_time(unpacker), read_time_zone(unpacker))
177
+ end
178
+
179
+ def dump_time_zone(time_zone)
180
+ time_zone.name
181
+ end
182
+
183
+ def load_time_zone(name)
184
+ ActiveSupport::TimeZone[name]
185
+ end
186
+
187
+ def write_time_zone(time_zone, packer)
188
+ packer.write(dump_time_zone(time_zone))
189
+ end
190
+
191
+ def read_time_zone(unpacker)
192
+ load_time_zone(unpacker.read)
193
+ end
194
+
195
+ def write_duration(duration, packer)
196
+ packer.write(duration.value)
197
+ packer.write(duration._parts.values_at(*ActiveSupport::Duration::PARTS))
198
+ end
199
+
200
+ def read_duration(unpacker)
201
+ value = unpacker.read
202
+ parts = ActiveSupport::Duration::PARTS.zip(unpacker.read).to_h
203
+ parts.compact!
204
+ ActiveSupport::Duration.new(value, parts)
205
+ end
206
+
207
+ def write_range(range, packer)
208
+ packer.write(range.begin)
209
+ packer.write(range.end)
210
+ packer.write(range.exclude_end?)
211
+ end
212
+
213
+ def read_range(unpacker)
214
+ Range.new(unpacker.read, unpacker.read, unpacker.read)
215
+ end
216
+
217
+ def write_set(set, packer)
218
+ packer.write(set.to_a)
219
+ end
220
+
221
+ def read_set(unpacker)
222
+ Set.new(unpacker.read)
223
+ end
224
+
225
+ def write_ipaddr(ipaddr, packer)
226
+ if ipaddr.prefix < 32 || (ipaddr.ipv6? && ipaddr.prefix < 128)
227
+ packer.write("#{ipaddr}/#{ipaddr.prefix}")
228
+ else
229
+ packer.write(ipaddr.to_s)
230
+ end
231
+ end
232
+
233
+ def read_ipaddr(unpacker)
234
+ IPAddr.new(unpacker.read)
235
+ end
236
+
237
+ def write_hash_with_indifferent_access(hwia, packer)
238
+ packer.write(hwia.to_h)
239
+ end
240
+
241
+ def read_hash_with_indifferent_access(unpacker)
242
+ ActiveSupport::HashWithIndifferentAccess.new(unpacker.read)
243
+ end
244
+
245
+ def raise_unserializable(object, *)
246
+ raise UnserializableObjectError, "Unsupported type #{object.class} for object #{object.inspect}"
247
+ end
248
+
249
+ def raise_invalid_format(*)
250
+ raise "Invalid format"
251
+ end
252
+
253
+ def dump_class(klass)
254
+ raise UnserializableObjectError, "Cannot serialize anonymous class" unless klass.name
255
+ klass.name
256
+ end
257
+
258
+ def load_class(name)
259
+ Object.const_get(name)
260
+ rescue NameError => error
261
+ if error.name.to_s == name
262
+ raise MissingClassError, "Missing class: #{name}"
263
+ else
264
+ raise
265
+ end
266
+ end
267
+
268
+ def write_class(klass, packer)
269
+ packer.write(dump_class(klass))
270
+ end
271
+
272
+ def read_class(unpacker)
273
+ load_class(unpacker.read)
274
+ end
275
+
276
+ LOAD_WITH_MSGPACK_EXT = 0
277
+ LOAD_WITH_JSON_CREATE = 1
278
+
279
+ def write_object(object, packer)
280
+ if object.class.respond_to?(:from_msgpack_ext)
281
+ packer.write(LOAD_WITH_MSGPACK_EXT)
282
+ write_class(object.class, packer)
283
+ packer.write(object.to_msgpack_ext)
284
+ elsif object.class.respond_to?(:json_create)
285
+ packer.write(LOAD_WITH_JSON_CREATE)
286
+ write_class(object.class, packer)
287
+ packer.write(object.as_json)
288
+ else
289
+ raise_unserializable(object)
290
+ end
291
+ end
292
+
293
+ def read_object(unpacker)
294
+ case unpacker.read
295
+ when LOAD_WITH_MSGPACK_EXT
296
+ read_class(unpacker).from_msgpack_ext(unpacker.read)
297
+ when LOAD_WITH_JSON_CREATE
298
+ read_class(unpacker).json_create(unpacker.read)
299
+ else
300
+ raise_invalid_format
301
+ end
302
+ end
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "extensions"
4
+
5
+ module ActiveSupport
6
+ module MessagePack
7
+ module Serializer # :nodoc:
8
+ SIGNATURE = "\xCC\x80".b.freeze # == 128.to_msgpack
9
+ SIGNATURE_INT = 128
10
+
11
+ def dump(object)
12
+ message_pack_pool.packer do |packer|
13
+ packer.write(SIGNATURE_INT)
14
+ packer.write(object)
15
+ packer.full_pack
16
+ end
17
+ end
18
+
19
+ def load(dumped)
20
+ message_pack_pool.unpacker do |unpacker|
21
+ unpacker.feed_reference(dumped)
22
+ raise "Invalid serialization format" unless unpacker.read == SIGNATURE_INT
23
+ unpacker.full_unpack
24
+ end
25
+ end
26
+
27
+ def signature?(dumped)
28
+ dumped.getbyte(0) == SIGNATURE.getbyte(0) && dumped.getbyte(1) == SIGNATURE.getbyte(1)
29
+ end
30
+
31
+ def message_pack_factory
32
+ @message_pack_factory ||= ::MessagePack::Factory.new
33
+ end
34
+
35
+ def message_pack_factory=(factory)
36
+ @message_pack_pool = nil
37
+ @message_pack_factory = factory
38
+ end
39
+
40
+ delegate :register_type, to: :message_pack_factory
41
+
42
+ def warmup
43
+ message_pack_pool # eagerly compute
44
+ end
45
+
46
+ private
47
+ def message_pack_pool
48
+ @message_pack_pool ||= begin
49
+ unless message_pack_factory.frozen?
50
+ Extensions.install(message_pack_factory)
51
+ install_unregistered_type_handler
52
+ message_pack_factory.freeze
53
+ end
54
+ message_pack_factory.pool(ENV.fetch("RAILS_MAX_THREADS", 5).to_i)
55
+ end
56
+ end
57
+
58
+ def install_unregistered_type_handler
59
+ Extensions.install_unregistered_type_error(message_pack_factory)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ gem "msgpack", ">= 1.7.0"
5
+ require "msgpack"
6
+ rescue LoadError => error
7
+ warn "ActiveSupport::MessagePack requires the msgpack gem, version 1.7.0 or later. " \
8
+ "Please add it to your Gemfile: `gem \"msgpack\", \">= 1.7.0\"`"
9
+ raise error
10
+ end
11
+
12
+ require_relative "message_pack/cache_serializer"
13
+ require_relative "message_pack/serializer"
14
+
15
+ module ActiveSupport
16
+ module MessagePack
17
+ extend Serializer
18
+
19
+ ##
20
+ # :singleton-method: dump
21
+ # :call-seq: dump(object)
22
+ #
23
+ # Dumps an object. Raises ActiveSupport::MessagePack::UnserializableObjectError
24
+ # if the object type is not supported.
25
+ #
26
+ #--
27
+ # Implemented by Serializer#dump.
28
+
29
+ ##
30
+ # :singleton-method: load
31
+ # :call-seq: load(dumped)
32
+ #
33
+ # Loads an object dump created by ::dump.
34
+ #
35
+ #--
36
+ # Implemented by Serializer#load.
37
+
38
+ ##
39
+ # :singleton-method: signature?
40
+ # :call-seq: signature?(dumped)
41
+ #
42
+ # Returns true if the given dump begins with an +ActiveSupport::MessagePack+
43
+ # signature.
44
+ #
45
+ #--
46
+ # Implemented by Serializer#signature?.
47
+
48
+ ActiveSupport.run_load_hooks(:message_pack, self)
49
+ end
50
+ end