aws-sessionstore-dynamodb 2.2.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/LICENSE.txt +202 -0
  4. data/VERSION +1 -1
  5. data/lib/aws/session_store/dynamo_db/configuration.rb +119 -204
  6. data/lib/aws/session_store/dynamo_db/errors/base_handler.rb +2 -0
  7. data/lib/aws/session_store/dynamo_db/errors/default_handler.rb +13 -12
  8. data/lib/aws/session_store/dynamo_db/errors.rb +27 -0
  9. data/lib/aws/session_store/dynamo_db/garbage_collection.rb +84 -93
  10. data/lib/aws/session_store/dynamo_db/locking/base.rb +31 -32
  11. data/lib/aws/session_store/dynamo_db/locking/null.rb +4 -3
  12. data/lib/aws/session_store/dynamo_db/locking/pessimistic.rb +32 -20
  13. data/lib/aws/session_store/dynamo_db/rack_middleware.rb +45 -49
  14. data/lib/aws/session_store/dynamo_db/table.rb +64 -67
  15. data/lib/aws-sessionstore-dynamodb.rb +14 -14
  16. metadata +18 -64
  17. data/.github/PULL_REQUEST_TEMPLATE.md +0 -6
  18. data/.github/workflows/ci.yml +0 -39
  19. data/.gitignore +0 -12
  20. data/.gitmodules +0 -3
  21. data/.yardopts +0 -4
  22. data/CODE_OF_CONDUCT.md +0 -4
  23. data/CONTRIBUTING.md +0 -61
  24. data/Gemfile +0 -24
  25. data/LICENSE +0 -12
  26. data/README.md +0 -125
  27. data/Rakefile +0 -35
  28. data/aws-sessionstore-dynamodb.gemspec +0 -24
  29. data/doc-src/templates/default/layout/html/footer.erb +0 -6
  30. data/doc-src/templates/default/layout/html/layout.erb +0 -31
  31. data/lib/aws/session_store/dynamo_db/invalid_id_error.rb +0 -7
  32. data/lib/aws/session_store/dynamo_db/lock_wait_timeout_error.rb +0 -7
  33. data/lib/aws/session_store/dynamo_db/missing_secret_key_error.rb +0 -7
  34. data/lib/aws/session_store/dynamo_db/version.rb +0 -7
  35. data/spec/aws/session_store/dynamo_db/app_config.yml +0 -16
  36. data/spec/aws/session_store/dynamo_db/configuration_spec.rb +0 -81
  37. data/spec/aws/session_store/dynamo_db/error/default_error_handler_spec.rb +0 -64
  38. data/spec/aws/session_store/dynamo_db/garbage_collection_spec.rb +0 -158
  39. data/spec/aws/session_store/dynamo_db/locking/threaded_sessions_spec.rb +0 -96
  40. data/spec/aws/session_store/dynamo_db/rack_middleware_database_spec.rb +0 -130
  41. data/spec/aws/session_store/dynamo_db/rack_middleware_spec.rb +0 -148
  42. data/spec/aws/session_store/dynamo_db/table_spec.rb +0 -48
  43. data/spec/spec_helper.rb +0 -65
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee27fa01f65650a13304791dc345e3ee4b0c9ae10b734a54f57acb72706f4963
4
- data.tar.gz: 269c66dbd952cd68d76fc3e1fcec8559fc945f4c745cc4987fb33e42c3c0bd1c
3
+ metadata.gz: 36b57007b9df541572bec17e8f0a2c5d262dec1e3d3de5e48b30fbe16ac096a4
4
+ data.tar.gz: 007b5941ae32f468df86712404ac6ba0a8f833096154f1365a98cc2cd800c56b
5
5
  SHA512:
6
- metadata.gz: 68ff2d325413b24ef06bfe8a40f83f209afc93858af6322c648f4f82daac0096b90f7b2521bc5eac4e0d562636db2eea30d7931f97990d69b1f346866005506c
7
- data.tar.gz: 5653d32a3a00b7bfbfaee3ddd9449d9a92cb3c369dd31a7fc887194f5a3b12dea141ac6d12f82db552ee82087956c8c0b88803f9bf25c460ea8fc894a58bd0fc
6
+ metadata.gz: ab11030761f3ee4fcfe4531c6739d38c829d46404f0b9d7e9586681c13f7489acbbaa53700a198b6729b60d87a4f70ab9e87e686a17481b689fedd7bcac35e01
7
+ data.tar.gz: e9040852ec704e117244b747914065a9b5bf07cdad378f40a0011e1dde42c447e296183b10eb2b5f2821291d8c78b3bba6f9fb95dd4330fbfa370e9aaead4c10
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ 3.0.0 (2024-10-29)
2
+ ------------------
3
+
4
+ * Feature - Uses `rack ~> 3` as the minimum.
5
+
6
+ * Feature - Drop support for Ruby 2.5 and 2.6.
7
+
8
+ * Feature - Support additional configuration options through ENV.
9
+
10
+ * Feature - Moves error classes into the `Errors` module.
11
+
12
+ * Issue - Set `RackMiddleware`'s `#find_session`, `#write_session`, and `#delete_session` as public.
13
+
14
+ * Issue - Validate `Configuration` has a secret key on `RackMiddleware#initialize` instead of on `#find_session`.
15
+
1
16
  2.2.0 (2024-01-25)
2
17
  ------------------
3
18
 
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.0
1
+ 3.0.0
@@ -1,264 +1,179 @@
1
- require 'yaml'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'aws-sdk-dynamodb'
3
4
 
4
5
  module Aws::SessionStore::DynamoDB
5
- # This class provides a Configuration object for all DynamoDB transactions
6
- # by pulling configuration options from Runtime, a YAML file, the ENV and
7
- # default settings.
6
+ # This class provides a Configuration object for all DynamoDB session store operations
7
+ # by pulling configuration options from Runtime, a YAML file, the ENV, and default
8
+ # settings, in that order.
8
9
  #
9
10
  # == Environment Variables
10
- # The Configuration object can load default values from your environment. An example
11
- # of setting and environment variable is below:
11
+ # The Configuration object can load default values from your environment. All configuration
12
+ # keys are supported except for `:dynamo_db_client` and `:error_handler`. The keys take the form
13
+ # of DYNAMO_DB_SESSION_<KEY_NAME>. Example:
12
14
  #
13
15
  # export DYNAMO_DB_SESSION_TABLE_NAME='Sessions'
16
+ # export DYNAMO_DB_SESSION_TABLE_KEY='id'
17
+ #
18
+ # == Locking Strategy
19
+ # By default, locking is disabled for session store access. To enable locking, set the
20
+ # `:enable_locking` option to true. The locking strategy is pessimistic, meaning that only one
21
+ # read can be made on a session at once. While the session is being read by the process with the
22
+ # lock, other processes may try to obtain a lock on the same session but will be blocked.
23
+ # See the initializer for how to configure the pessimistic locking strategy to your needs.
14
24
  #
15
25
  # == Handling Errors
16
- # There are two configurable options for error handling: :raise_errors and :error_handler.
26
+ # There are two configurable options for error handling: `:raise_errors` and `:error_handler`.
17
27
  #
18
- # If you would like to use the Default Error Handler, you can decide to set :raise_errors
19
- # to true or false depending on whether you want all errors, regadless of class, to be raised
28
+ # If you would like to use the Default Error Handler, you can decide to set `:raise_errors`
29
+ # to true or false depending on whether you want all errors, regardless of class, to be raised
20
30
  # up the stack and essentially throw a 500.
21
31
  #
22
- # If you decide to use your own Error Handler. You may pass it in for the value of the key
23
- # :error_handler as a cofniguration object. You must implement the BaseErrorHandler class.
32
+ # If you decide to use your own Error Handler, you must implement the `BaseErrorHandler`
33
+ # class and pass it into the `:error_handler` option.
24
34
  # @see BaseHandler Interface for Error Handling for DynamoDB Session Store.
25
35
  #
26
- # == Locking Strategy
27
- # By default, locking is not implemented for the session store. You must trigger the
28
- # locking strategy through the configuration of the session store. Pessimistic locking,
29
- # in this case, means that only one read can be made on a session at once. While the session
30
- # is being read by the process with the lock, other processes may try to obtain a lock on
31
- # the same session but will be blocked. See the accessors with lock in their name for
32
- # how to configure the pessimistic locking strategy to your needs.
33
- #
34
36
  # == DynamoDB Specific Options
35
37
  # You may configure the table name and table hash key value of your session table with
36
- # the :table_name and :table_key options. You may also configure performance options for
37
- # your table with the :consistent_read, :read_capacity, write_capacity. For more information
38
- # about these configurations see CreateTable method for Amazon DynamoDB.
38
+ # the `:table_name` and `:table_key` options. You may also configure performance options for
39
+ # your table with the `:consistent_read`, `:read_capacity`, `:write_capacity`. For more information
40
+ # about these configurations see
41
+ # {https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#create_table-instance_method CreateTable }
42
+ # method for Amazon DynamoDB.
39
43
  #
40
44
  class Configuration
41
-
42
- # Default configuration options
43
- DEFAULTS = {
44
- :table_name => "sessions",
45
- :table_key => "session_id",
46
- :consistent_read => true,
47
- :read_capacity => 10,
48
- :write_capacity => 5,
49
- :raise_errors => false,
50
- # :max_age => 7*3600*24,
51
- # :max_stale => 3600*5,
52
- :enable_locking => false,
53
- :lock_expiry_time => 500,
54
- :lock_retry_delay => 500,
55
- :lock_max_wait_time => 1,
56
- :secret_key => nil
57
- }
58
-
59
- ### Feature options
60
-
61
- # @return [String] Session table name.
62
- attr_reader :table_name
63
-
64
- # @return [String] Session table hash key name.
65
- attr_reader :table_key
66
-
67
- # @return [true] If a strongly consistent read is used
68
- # @return [false] If an eventually consistent read is used.
69
- # See AWS DynamoDB documentation for table consistent_read for more
70
- # information on this setting.
71
- attr_reader :consistent_read
72
-
73
- # @return [Integer] Maximum number of reads consumed per second before
74
- # DynamoDB returns a ThrottlingException. See AWS DynamoDB documentation
75
- # for table read_capacity for more information on this setting.
76
- attr_reader :read_capacity
77
-
78
- # @return [Integer] Maximum number of writes consumed per second before
79
- # DynamoDB returns a ThrottlingException. See AWS DynamoDB documentation
80
- # for table write_capacity for more information on this setting.
81
- attr_reader :write_capacity
82
-
83
- # @return [true] All errors are raised up the stack when default ErrorHandler
84
- # is used.
85
- # @return [false] Only specified errors are raised up the stack when default
86
- # ErrorHandler is used.
87
- attr_reader :raise_errors
88
-
89
- # @return [Integer] Maximum number of seconds earlier
90
- # from the current time that a session was created.
91
- attr_reader :max_age
92
-
93
- # @return [Integer] Maximum number of seconds
94
- # before the current time that the session was last accessed.
95
- attr_reader :max_stale
96
-
97
- # @return [true] Pessimistic locking strategy will be implemented for
98
- # all session accesses.
99
- # @return [false] No locking strategy will be implemented for
100
- # all session accesses.
101
- attr_reader :enable_locking
102
-
103
- # @return [Integer] Time in milleseconds after which lock will expire.
104
- attr_reader :lock_expiry_time
105
-
106
- # @return [Integer] Time in milleseconds to wait before retrying to obtain
107
- # lock once an attempt to obtain lock has been made and has failed.
108
- attr_reader :lock_retry_delay
109
-
110
- # @return [Integer] Maximum time in seconds to wait to acquire lock
111
- # before giving up.
112
- attr_reader :lock_max_wait_time
113
-
114
- # @return [String] The secret key for HMAC encryption.
115
- attr_reader :secret_key
116
-
117
- # @return [String,Pathname]
118
- attr_reader :config_file
119
-
120
- ### Client and Error Handling options
121
-
122
- # @return [DynamoDB Client] DynamoDB client.
123
- attr_reader :dynamo_db_client
124
-
125
- # @return [Error Handler] An error handling object that handles all exceptions
126
- # thrown during execution of the AWS DynamoDB Session Store Rack Middleware.
127
- # For more information see the Handling Errors Section.
128
- attr_reader :error_handler
45
+ # @api private
46
+ MEMBERS = {
47
+ table_name: 'sessions',
48
+ table_key: 'session_id',
49
+ secret_key: nil,
50
+ consistent_read: true,
51
+ read_capacity: 10,
52
+ write_capacity: 5,
53
+ raise_errors: false,
54
+ error_handler: nil,
55
+ max_age: nil,
56
+ max_stale: nil,
57
+ enable_locking: false,
58
+ lock_expiry_time: 500,
59
+ lock_retry_delay: 500,
60
+ lock_max_wait_time: 1,
61
+ config_file: nil,
62
+ dynamo_db_client: nil
63
+ }.freeze
129
64
 
130
65
  # Provides configuration object that allows access to options defined
131
- # during Runtime, in a YAML file, in the ENV and by default.
66
+ # during Runtime, in a YAML file, in the ENV, and by default.
132
67
  #
133
- # @option options [String] :table_name ("Sessions") Name of the session
134
- # table.
135
- # @option options [String] :table_key ("id") The hash key of the sesison
136
- # table.
137
- # @option options [Boolean] :consistent_read (true) If true, a strongly
138
- # consistent read is used. If false, an eventually consistent read is
139
- # used.
140
- # @option options [Integer] :read_capacity (10) The maximum number of
141
- # strongly consistent reads consumed per second before
142
- # DynamoDB raises a ThrottlingException. See AWS DynamoDB documentation
143
- # for table read_capacity for more information on this setting.
68
+ # @option options [String] :table_name ("sessions") Name of the session table.
69
+ # @option options [String] :table_key ("session_id") The hash key of the session table.
70
+ # @option options [String] :secret_key Secret key for HMAC encryption.
71
+ # @option options [Boolean] :consistent_read (true) If true, a strongly consistent read is used.
72
+ # If false, an eventually consistent read is used.
73
+ # @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
74
+ # @option options [Integer] :read_capacity (10) The maximum number of strongly consistent reads
75
+ # consumed per second before DynamoDB raises a ThrottlingException.
76
+ # @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/read-write-operations.html
144
77
  # @option options [Integer] :write_capacity (5) The maximum number of writes
145
78
  # consumed per second before DynamoDB returns a ThrottlingException.
146
- # See AWS DynamoDB documentation for table write_capacity for more
147
- # information on this setting.
148
- # @option options [DynamoDB Client] :dynamo_db_client
149
- # (Aws::DynamoDB::Client) DynamoDB client used to perform database
150
- # operations inside of middleware application.
151
- # @option options [Boolean] :raise_errors (false) If true, all errors are
152
- # raised up the stack when default ErrorHandler. If false, Only specified
153
- # errors are raised up the stack when default ErrorHandler is used.
154
- # @option options [Error Handler] :error_handler (DefaultErrorHandler)
155
- # An error handling object that handles all exceptions thrown during
156
- # execution of the AWS DynamoDB Session Store Rack Middleware.
157
- # For more information see the Handling Errors Section.
79
+ # @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/read-write-operations.html
80
+ # @option options [Boolean] :raise_errors (false) If true, all errors are raised up the stack
81
+ # when default ErrorHandler. If false, Only specified errors are raised up the stack when
82
+ # the default ErrorHandler is used.
83
+ # @option options [#handle_error] :error_handler (Errors::DefaultHandler) An error handling object
84
+ # that handles all exceptions thrown during execution of the rack application.
158
85
  # @option options [Integer] :max_age (nil) Maximum number of seconds earlier
159
86
  # from the current time that a session was created.
160
87
  # @option options [Integer] :max_stale (nil) Maximum number of seconds
161
88
  # before current time that session was last accessed.
162
- # @option options [String] :secret_key (nil) Secret key for HMAC encription.
163
- # @option options [Integer] :enable_locking (false) If true, a pessimistic
164
- # locking strategy will be implemented for all session accesses.
165
- # If false, no locking strategy will be implemented for all session
166
- # accesses.
167
- # @option options [Integer] :lock_expiry_time (500) Time in milliseconds
168
- # after which lock expires on session.
169
- # @option options [Integer] :lock_retry_delay (500) Time in milleseconds to
170
- # wait before retrying to obtain lock once an attempt to obtain lock
171
- # has been made and has failed.
172
- # @option options [Integer] :lock_max_wait_time (500) Maximum time
173
- # in seconds to wait to acquire lock before giving up.
174
- # @option options [String] :secret_key (SecureRandom.hex(64))
175
- # Secret key for HMAC encription.
89
+ # @option options [Integer] :enable_locking (false) If true, a pessimistic locking strategy will be
90
+ # used for all session accesses.
91
+ # @option options [Integer] :lock_expiry_time (500) Time in milliseconds after which the lock
92
+ # expires on session.
93
+ # @option options [Integer] :lock_retry_delay (500) Time in milliseconds to wait before retrying
94
+ # to obtain lock once an attempt to obtain the lock has been made and has failed.
95
+ # @option options [Integer] :lock_max_wait_time (500) Maximum time in seconds to wait to acquire the
96
+ # lock before giving up.
97
+ # @option options [String, Pathname] :config_file
98
+ # Path to a YAML file that contains configuration options.
99
+ # @option options [Aws::DynamoDB::Client] :dynamo_db_client (Aws::DynamoDB::Client.new)
100
+ # DynamoDB client used to perform database operations inside of the rack application.
176
101
  def initialize(options = {})
177
- @options = default_options.merge(
178
- env_options.merge(
179
- file_options(options).merge(symbolize_keys(options))
180
- )
181
- )
182
- @options = client_error.merge(@options)
183
- set_attributes(@options)
102
+ opts = file_options(options).merge(options)
103
+ opts = env_options.merge(opts)
104
+ MEMBERS.each_pair do |opt_name, default_value|
105
+ opts[opt_name] = default_value unless opts.key?(opt_name)
106
+ end
107
+ opts = opts.merge(dynamo_db_client: default_dynamo_db_client(opts))
108
+ opts = opts.merge(error_handler: default_error_handler(opts)) unless opts[:error_handler]
109
+
110
+ set_attributes(opts)
111
+ end
112
+
113
+ MEMBERS.each_key do |attr_name|
114
+ attr_reader(attr_name)
184
115
  end
185
116
 
186
117
  # @return [Hash] The merged configuration hash.
187
118
  def to_hash
188
- @options.dup
119
+ MEMBERS.each_with_object({}) do |(key, _), hash|
120
+ hash[key] = send(key)
121
+ end
189
122
  end
190
123
 
191
124
  private
192
125
 
193
- # @return [Hash] DDB client.
194
- def gen_dynamo_db_client
195
- dynamo_db_client = @options[:dynamo_db_client] || Aws::DynamoDB::Client.new
196
- # this used to be aws-sessionstore/version on user_agent_suffix
197
- dynamo_db_client.config.user_agent_frameworks << "aws-sessionstore-dynamodb"
198
- {:dynamo_db_client => dynamo_db_client}
126
+ def default_dynamo_db_client(options)
127
+ dynamo_db_client = options[:dynamo_db_client] || Aws::DynamoDB::Client.new
128
+ dynamo_db_client.config.user_agent_frameworks << 'aws-sessionstore-dynamodb'
129
+ dynamo_db_client
199
130
  end
200
131
 
201
- # @return [Hash] Default Error Handler
202
- def gen_error_handler
203
- default_handler = Aws::SessionStore::DynamoDB::Errors::DefaultHandler
204
- error_handler = @options[:error_handler] ||
205
- default_handler.new(@options[:raise_errors])
206
- {:error_handler => error_handler}
132
+ def default_error_handler(options)
133
+ Aws::SessionStore::DynamoDB::Errors::DefaultHandler.new(options[:raise_errors])
207
134
  end
208
135
 
209
- # @return [Hash] Client and error objects in hash.
210
- def client_error
211
- gen_error_handler.merge(gen_dynamo_db_client)
212
- end
136
+ # @return [Hash] Environment options.
137
+ def env_options
138
+ unsupported_keys = %i[dynamo_db_client error_handler]
139
+ (MEMBERS.keys - unsupported_keys).each_with_object({}) do |opt_name, opts|
140
+ key = "DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
141
+ next unless ENV.key?(key)
213
142
 
214
- # @return [Hash] Default Session table options.
215
- def default_options
216
- DEFAULTS
143
+ opts[opt_name] = parse_env_value(key)
144
+ end
217
145
  end
218
146
 
219
- # @return [Hash] Environment options that are useful for Session Handler.
220
- def env_options
221
- default_options.keys.inject({}) do |opts, opt_name|
222
- env_var = "DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
223
- opts[opt_name] = ENV[env_var] if ENV.key?(env_var)
224
- opts
147
+ def parse_env_value(key)
148
+ Integer(ENV.fetch(key, nil))
149
+ rescue ArgumentError
150
+ if ENV[key] == 'true' || ENV[key] == 'false'
151
+ ENV[key] == 'true'
152
+ else
153
+ ENV.fetch(key, nil)
225
154
  end
226
155
  end
227
156
 
228
157
  # @return [Hash] File options.
229
158
  def file_options(options = {})
230
- file_path = config_file_path(options)
231
- if file_path
232
- load_from_file(file_path)
159
+ if options[:config_file]
160
+ load_from_file(options[:config_file])
233
161
  else
234
162
  {}
235
163
  end
236
164
  end
237
165
 
238
- # Load options from YAML file
166
+ # Load options from the YAML file.
239
167
  def load_from_file(file_path)
240
- require "erb"
241
- opts = YAML.load(ERB.new(File.read(file_path)).result) || {}
242
- symbolize_keys(opts)
168
+ require 'erb'
169
+ require 'yaml'
170
+ opts = YAML.safe_load(ERB.new(File.read(file_path)).result) || {}
171
+ opts.transform_keys(&:to_sym)
243
172
  end
244
173
 
245
- # @return [String] Configuration path found in environment or YAML file.
246
- def config_file_path(options)
247
- options[:config_file] || ENV["DYNAMO_DB_SESSION_CONFIG_FILE"]
248
- end
249
-
250
- # Set accessible attributes after merged options.
251
174
  def set_attributes(options)
252
- @options.keys.each do |opt_name|
253
- instance_variable_set("@#{opt_name}", options[opt_name])
254
- end
255
- end
256
-
257
- # @return [Hash] Hash with all symbolized keys.
258
- def symbolize_keys(options)
259
- options.inject({}) do |opts, (opt_name, opt_value)|
260
- opts[opt_name.to_sym] = opt_value
261
- opts
175
+ MEMBERS.each_key do |attr_name|
176
+ instance_variable_set("@#{attr_name}", options[attr_name])
262
177
  end
263
178
  end
264
179
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws::SessionStore::DynamoDB::Errors
2
4
  # BaseErrorHandler provides an interface for error handlers
3
5
  # that can be passed in to {Aws::SessionStore::DynamoDB::RackMiddleware}.