Skip to content

Commit

Permalink
Use AbstractStore for DynamoDbSession (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Oct 29, 2024
1 parent 10930e5 commit 1b6559c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
matrix:
ruby: [2.7, '3.0', 3.1, 3.2, 3.3, jruby-9.4]
rails: ['7.0', 7.1, 7.2, main]
rails: [7.1, 7.2, main]

exclude:
# Rails 7.2 dropped support for older rubygems
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Gemfile.lock
gemfiles/*.gemfile.lock
sample-app/Gemfile.lock
sample-app/log
sample-app/tmp

test/dummy/db/migrate
test/dummy/log/
Expand Down
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ AllCops:
SuggestExtensions: false
Exclude:
- 'tasks/release/**/*'
- 'sample_app/**/*'
- 'sample-app/**/*'
- 'sample_app_old/**/*'
- 'spec/dummy/**/*'
- 'spec/fixtures/**/*'
- 'spec/fixtures/**/*'
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
Unreleased Changes
------------------

* Feature - Add session store config generation.
* Feature - Add session store config generation with `rails generate dynamo_db:session_store_config`. Config generation is no longer tied to the DynamoDB SessionStore ActiveRecord migration generator.

* Feature - Prepare modularization of `aws-sessionstore-dynamodb`.

* Feature - Depend on `aws-sessionstore-dynamodb ~> 3` which depends on `rack ~> 3` and is not supported by Rails `7.0`.

* Issue - `ActionDispatch::Session::DynamoDbStore` now inherits `ActionDispatch::Session::AbstractStore` by wrapping `Aws::SessionStore::DynamoDB::RackMiddleware`.

4.1.0 (2024-09-27)
------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ to have or create an existing Amazon DynamoDB session table to use this feature.
To enable this feature, add the following to your Gemfile:

```ruby
gem 'aws-sessionstore-dynamodb', '~> 2'
gem 'aws-sessionstore-dynamodb', '~> 3'
```

For more information about this feature and configuration options, see the
Expand Down
2 changes: 1 addition & 1 deletion aws-sdk-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = ['aws_sqs_active_job']

spec.add_dependency('aws-record', '~> 2') # for Aws::Record integration
spec.add_dependency('aws-sessionstore-dynamodb', '~> 2') # includes DynamoDB
spec.add_dependency('aws-sessionstore-dynamodb', '~> 3') # includes DynamoDB

# Require these versions for user_agent_framework configs
spec.add_dependency('aws-sdk-s3', '~> 1', '>= 1.123.0')
Expand Down
12 changes: 0 additions & 12 deletions gemfiles/rails-7.0.gemfile

This file was deleted.

35 changes: 23 additions & 12 deletions lib/action_dispatch/session/dynamo_db_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,39 @@ module Session
# Configuration files that are environment-specific will take precedence.
#
# @see https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html
class DynamoDbStore < Aws::SessionStore::DynamoDB::RackMiddleware
# Because of how Ruby searches for methods in the inheritance chain,
# generate_sid in Compatibility takes precedence over our generate_sid.
# Compatibility is needed so that the request is an ActionDispatch::Request.
CompatibilityWithoutSid = Compatibility.dup
CompatibilityWithoutSid.remove_method(:generate_sid)
include CompatibilityWithoutSid

include StaleSessionCheck
include SessionObject

class DynamoDbStore < ActionDispatch::Session::AbstractStore
def initialize(app, options = {})
Rails.logger.warn('** aws-sessionstore-dynamodb will no longer be a direct dependency of aws-sdk-rails ~> 5. ' \
'To avoid disruption, please add aws-sessionstore-dynamodb ~> 2 to your Gemfile to enable ' \
'To avoid disruption, please add aws-sessionstore-dynamodb ~> 3 to your Gemfile to enable ' \
'this feature when upgrading to aws-sdk-rails ~> 5. **')
options[:config_file] ||= config_file
options[:secret_key] ||= Rails.application.secret_key_base
@middleware = Aws::SessionStore::DynamoDB::RackMiddleware.new(app, options)
super
end

# @return [Aws::SessionStore::DynamoDB::Configuration]
def config
@middleware.config
end

private

# Required by `ActionDispatch::Session::AbstractStore`
def find_session(req, sid)
@middleware.find_session(req, sid)
end

# Required by `ActionDispatch::Session::AbstractStore`
def write_session(req, sid, session, options)
@middleware.write_session(req, sid, session, options)
end

# Required by `ActionDispatch::Session::AbstractStore`
def delete_session(req, sid, options)
@middleware.delete_session(req, sid, options)
end

def config_file
file = Rails.root.join("config/dynamo_db_session_store/#{Rails.env}.yml")
file = Rails.root.join('config/dynamo_db_session_store.yml') unless File.exist?(file)
Expand Down

0 comments on commit 1b6559c

Please sign in to comment.