Skip to content

Commit 4d03368

Browse files
committed
Initial commit of aws-actiondispatch-dynamodb
1 parent 7548519 commit 4d03368

34 files changed

+851
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*Issue #, if available:*
2+
3+
*Description of changes:*
4+
5+
By submitting this pull request, I confirm that my contribution is made under
6+
the terms of the Apache 2.0 license.

.github/workflows/ci.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
12+
env:
13+
ruby_version: 3.3
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-latest
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
ruby: [2.7, '3.0', 3.1, 3.2, 3.3, jruby-9.4]
22+
rails: [7.1, 7.2, '8.0', main]
23+
24+
exclude:
25+
# Rails 7.2 is Ruby >= 3.1
26+
- rails: 7.2
27+
ruby: 2.7
28+
- rails: 7.2
29+
ruby: 3.0
30+
# Rails 8.0 is Ruby >= 3.2
31+
- rails: '8.0'
32+
ruby: 2.7
33+
- rails: '8.0'
34+
ruby: 3.0
35+
- rails: '8.0'
36+
ruby: 3.1
37+
- rails: '8.0'
38+
ruby: jruby-9.4
39+
# Rails main is Ruby >= 3.2
40+
- rails: main
41+
ruby: 2.7
42+
- rails: main
43+
ruby: 3.0
44+
- rails: main
45+
ruby: 3.1
46+
- rails: main
47+
ruby: jruby-9.4
48+
49+
steps:
50+
- uses: actions/checkout@v4
51+
52+
- name: Setup Ruby
53+
uses: ruby/setup-ruby@v1
54+
with:
55+
ruby-version: ${{ matrix.ruby }}
56+
bundler-cache: true
57+
58+
- name: Install gems
59+
run: bundle install
60+
env:
61+
BUNDLE_GEMFILE: gemfiles/rails-${{ matrix.rails }}.gemfile
62+
63+
- name: Test
64+
run: bundle exec rake spec
65+
env:
66+
BUNDLE_GEMFILE: gemfiles/rails-${{ matrix.rails }}.gemfile
67+
68+
rubocop:
69+
runs-on: ubuntu-latest
70+
71+
steps:
72+
- uses: actions/checkout@v4
73+
74+
- name: Setup Ruby
75+
uses: ruby/setup-ruby@v1
76+
with:
77+
ruby-version: ${{ env.ruby_version }}
78+
79+
- name: Install gems
80+
run: |
81+
bundle config set --local with 'development'
82+
bundle install
83+
84+
- name: Rubocop
85+
run: bundle exec rake rubocop

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
Gemfile.lock
3+
gemfiles/*.gemfile.lock
4+
5+
test/dummy/db/migrate
6+
test/dummy/log/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tasks/release"]
2+
path = tasks/release
3+
url = https://github.com/aws/aws-sdk-ruby-release-tools

.rubocop.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
AllCops:
2+
NewCops: enable
3+
TargetRubyVersion: 2.7
4+
SuggestExtensions: false
5+
6+
Gemspec/RequireMFA:
7+
Enabled: false
8+
9+
Metrics/BlockLength:
10+
Exclude:
11+
- 'spec/**/*.rb'
12+
13+
Naming/FileName:
14+
Exclude:
15+
- 'gemfiles/*.gemfile'
16+
- 'lib/aws-actiondispatch-dynamodb.rb'
17+
18+
Style/BlockComments:
19+
Exclude:
20+
- 'spec/spec_helper.rb'

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Unreleased Changes
2+
------------------
3+
4+
* Feature - Initial version of this gem.

Gemfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gemspec
6+
7+
gem 'rake', require: false
8+
9+
group :development do
10+
gem 'byebug', platforms: :ruby
11+
gem 'rubocop'
12+
end
13+
14+
group :test do
15+
gem 'minitest-spec-rails'
16+
end
17+
18+
group :docs do
19+
gem 'yard'
20+
gem 'yard-sitemap', '~> 1.0'
21+
end
22+
23+
group :release do
24+
gem 'octokit'
25+
end

Rakefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'rake/testtask'
4+
require 'rubocop/rake_task'
5+
6+
Dir.glob('tasks/**/*.rake').each do |task_file|
7+
load task_file
8+
end
9+
10+
RuboCop::RakeTask.new
11+
12+
Rake::TestTask.new do |t|
13+
t.libs << 'test'
14+
t.pattern = 'test/**/*_test.rb'
15+
t.warning = false
16+
end
17+
18+
task 'release:test' => :test

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

aws-actiondispatch-dynamodb.gemspec

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
version = File.read(File.expand_path('VERSION', __dir__)).strip
4+
5+
Gem::Specification.new do |spec|
6+
spec.name = 'aws-actiondispatch-dynamodb'
7+
spec.version = version
8+
spec.author = 'Amazon Web Services'
9+
spec.email = ['[email protected]']
10+
spec.summary = 'ActionDispatch integration with DynamoDB'
11+
spec.description = 'Amazon Dynamo DB as an ActionDispatch session store'
12+
spec.homepage = 'https://github.com/aws/aws-actiondispatch-dynamodb-ruby'
13+
spec.license = 'Apache-2.0'
14+
spec.files = Dir['LICENSE', 'CHANGELOG.md', 'VERSION', 'lib/**/*']
15+
16+
spec.add_dependency('actionpack', '>= 7.1.0')
17+
spec.add_dependency('aws-sessionstore-dynamodb', '~> 3')
18+
19+
spec.required_ruby_version = '>= 2.7'
20+
end

gemfiles/rails-7.1.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile '../Gemfile'
4+
5+
gem 'rails', '~> 7.1.0'

gemfiles/rails-7.2.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile '../Gemfile'
4+
5+
gem 'rails', '~> 7.2.0'

gemfiles/rails-8.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile '../Gemfile'
4+
5+
gem 'rails', '~> 8.0.0'

gemfiles/rails-main.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
eval_gemfile '../Gemfile'
4+
5+
gem 'rails', github: 'rails/rails'
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'action_dispatch/middleware/session/abstract_store'
4+
require 'aws-sessionstore-dynamodb'
5+
6+
module ActionDispatch
7+
module Session
8+
# Uses the Dynamo DB Session Store implementation to create a class that
9+
# extends `ActionDispatch::Session`. Rails will create a `:dynamo_db_store`
10+
# configuration for `:session_store` from this class name.
11+
#
12+
# This class will use `Rails.application.secret_key_base` as the secret key
13+
# unless otherwise provided.
14+
#
15+
# Configuration can also be provided in YAML files from Rails config, either
16+
# in "config/dynamo_db_session_store.yml" or "config/dynamo_db_session_store/#\\{Rails.env}.yml".
17+
# Configuration files that are environment-specific will take precedence.
18+
#
19+
# @see https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html
20+
class DynamoDbStore < ActionDispatch::Session::AbstractStore
21+
def initialize(app, options = {})
22+
Rails.logger.warn('** Dynamo DB Session Storage no longer lives in aws-sdk-rails. ' \
23+
'To avoid disruption, please add aws-actiondispatch-dynamodb ~> 0 to your Gemfile to ' \
24+
'enable this feature when upgrading to aws-sdk-rails ~> 5. **')
25+
options[:config_file] ||= config_file
26+
options[:secret_key] ||= Rails.application.secret_key_base
27+
@middleware = Aws::SessionStore::DynamoDB::RackMiddleware.new(app, options)
28+
config.dynamo_db_client.config.user_agent_frameworks << 'aws-actiondispatch-dynamodb'
29+
super
30+
end
31+
32+
# @return [Aws::SessionStore::DynamoDB::Configuration]
33+
def config
34+
@middleware.config
35+
end
36+
37+
private
38+
39+
# Required by `ActionDispatch::Session::AbstractStore`
40+
def find_session(req, sid)
41+
@middleware.find_session(req, sid)
42+
end
43+
44+
# Required by `ActionDispatch::Session::AbstractStore`
45+
def write_session(req, sid, session, options)
46+
@middleware.write_session(req, sid, session, options)
47+
end
48+
49+
# Required by `ActionDispatch::Session::AbstractStore`
50+
def delete_session(req, sid, options)
51+
@middleware.delete_session(req, sid, options)
52+
end
53+
54+
def config_file
55+
file = ENV.fetch('AWS_DYNAMO_DB_SESSION_CONFIG_FILE', nil)
56+
file ||= Rails.root.join("config/dynamo_db_session_store/#{Rails.env}.yml")
57+
file = Rails.root.join('config/dynamo_db_session_store.yml') unless File.exist?(file)
58+
if File.exist?(file)
59+
Rails.logger.warn('The dynamo_db_session_store configuration file has been renamed.' \
60+
'Please use aws_dynamo_db_session_store/<Rails.env>.yml or ' \
61+
'aws_dynamo_db_session_store.yml. This will be removed in ' \
62+
'aws-actiondispatch-dynamodb ~> 1')
63+
else
64+
file = Rails.root.join("config/aws_dynamo_db_session_store/#{Rails.env}.yml")
65+
file = Rails.root.join('config/aws_dynamo_db_session_store.yml') unless File.exist?(file)
66+
end
67+
68+
file if File.exist?(file)
69+
end
70+
end
71+
72+
# @api private
73+
class DynamodbStore < DynamoDbStore
74+
def initialize(app, options = {})
75+
Rails.logger.warn('** Session Store :dynamodb_store configuration key has been renamed to :dynamo_db_store, ' \
76+
'please use the new key instead. The :dynamodb_store key name will be removed in ' \
77+
'aws-actiondispatch-dynamodb ~> 1 **')
78+
super
79+
end
80+
end
81+
end
82+
end

lib/aws-actiondispatch-dynamodb.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'action_dispatch/session/dynamo_db_store'
4+
5+
module Aws
6+
module ActionDispatch
7+
module DynamoDb
8+
VERSION = File.read(File.expand_path('../VERSION', __dir__)).strip
9+
end
10+
end
11+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Description:
2+
Generates a sample configuration file DynamoDB session store.
3+
4+
Examples:
5+
`rails generate dynamo_db:session_store_config`
6+
7+
This will create:
8+
config/dynamo_db_session_store.yml
9+
10+
Optionally, you can specify the Rails environment to generate
11+
the configuration file for:
12+
13+
`rails generate dynamo_db:session_store_config --environment=development`
14+
15+
This will create:
16+
config/dynamo_db_session_store/development.yml
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators'
4+
5+
module DynamoDb
6+
module Generators
7+
# Generates a config file for DynamoDB session storage.
8+
class SessionStoreConfigGenerator < Rails::Generators::Base
9+
source_root File.expand_path('templates', __dir__)
10+
11+
# Environment to generate the config file for
12+
class_option :environment,
13+
desc: 'Optional rails environment to generate the config file for',
14+
type: :string,
15+
default: nil
16+
17+
def copy_sample_config_file
18+
path = 'config/aws_dynamo_db_session_store'
19+
path += "/#{options['environment']}" if options['environment']
20+
template('aws_dynamo_db_session_store.yml', "#{path}.yml")
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)