-
Notifications
You must be signed in to change notification settings - Fork 63
Add DynamoDB session store (v2 session store gem) #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ | |
coverage | ||
doc | ||
Gemfile.lock | ||
test/dummy/log/ | ||
test/dummy/tmp/ | ||
spec/dummy/log/ | ||
spec/dummy/tmp/ | ||
vendor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,19 @@ Gem::Specification.new do |spec| | |
spec.name = 'aws-sdk-rails' | ||
spec.version = version | ||
spec.authors = ['Amazon Web Services'] | ||
spec.email = ['[email protected]', '[email protected]'] | ||
spec.email = ['[email protected]', '[email protected]'] | ||
|
||
spec.summary = 'AWS SDK for Ruby on Rails Plugin' | ||
spec.description = 'Integrates the AWS Ruby SDK with Ruby on Rails' | ||
spec.homepage = 'https://github.com/aws/aws-sdk-rails' | ||
spec.license = 'Apache-2.0' | ||
|
||
spec.require_paths = ['lib'] | ||
spec.files += Dir['lib/**/*.rb', 'lib/aws-sdk-rails.rb'] | ||
spec.files += Dir['lib/**/*.rb'] | ||
|
||
spec.add_dependency('aws-sdk-ses', '~> 1') | ||
spec.add_dependency('railties', '>= 5.2.0') | ||
spec.add_dependency('aws-sdk-ses', '~> 1') # for ActionMailer | ||
spec.add_dependency('aws-sessionstore-dynamodb', '~> 2') # includes dynamo db | ||
spec.add_dependency('railties', '>= 5.2.0') # encrypted credentials | ||
|
||
spec.add_development_dependency('rails') | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'aws-sessionstore-dynamodb' | ||
|
||
module ActionDispatch | ||
module Session | ||
# Uses the Dynamo DB Session Store implementation to create a class that | ||
# extends ActionDispatch::Session. Rails will create a :dynamodb_store | ||
# configuration for session_store from this class name. | ||
# | ||
# This class will use the Rails secret_key_base unless otherwise provided. | ||
# | ||
# Configuration can also be provided in YAML files from Rails config, either | ||
# in "config/session_store.yml" or "config/session_store/#{Rails.env}.yml". | ||
# 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 | ||
def initialize(app, options = {}) | ||
options[:config_file] ||= config_file if config_file.exist? | ||
options[:secret_key] ||= Rails.application.secret_key_base | ||
super | ||
end | ||
|
||
private | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'aws/rails/mailer' | ||
require_relative 'aws/rails/notifications_instrument_plugin' | ||
require_relative 'aws/rails/railtie' | ||
require_relative 'aws/rails/notifications_instrument' | ||
|
||
module Aws | ||
|
||
# Use the Rails namespace. | ||
module Rails | ||
|
||
# @api private | ||
class Railtie < ::Rails::Railtie | ||
initializer 'aws-sdk-rails.initialize', | ||
before: :load_config_initializers do | ||
# Initialization Actions | ||
Aws::Rails.use_rails_encrypted_credentials | ||
Aws::Rails.add_action_mailer_delivery_method | ||
Aws::Rails.log_to_rails_logger | ||
end | ||
end | ||
|
||
# This is called automatically from the SDK's Railtie, but can be manually | ||
# called if you want to specify options for building the Aws::SES::Client. | ||
# | ||
# @param [Symbol] name The name of the ActionMailer delivery method to | ||
# register. | ||
# @param [Hash] options The options you wish to pass on to the | ||
# Aws::SES::Client initialization method. | ||
def self.add_action_mailer_delivery_method(name = :ses, options = {}) | ||
ActiveSupport.on_load(:action_mailer) do | ||
add_delivery_method(name, Aws::Rails::Mailer, options) | ||
end | ||
end | ||
|
||
# Configures the AWS SDK for Ruby's logger to use the Rails logger. | ||
def self.log_to_rails_logger | ||
Aws.config[:logger] = ::Rails.logger | ||
nil | ||
end | ||
|
||
# Configures the AWS SDK with credentials from Rails encrypted credentials. | ||
def self.use_rails_encrypted_credentials | ||
# limit the config keys we merge to credentials only | ||
aws_credential_keys = %i[access_key_id secret_access_key session_token] | ||
|
||
Aws.config.merge!( | ||
::Rails.application | ||
.try(:credentials) | ||
.try(:aws) | ||
.to_h.slice(*aws_credential_keys) | ||
) | ||
end | ||
|
||
# Adds ActiveSupport Notifications instrumentation to AWS SDK | ||
# client operations. Each operation will produce an event with a name: | ||
# <operation>.<service>.aws. For example, S3's put_object has an event | ||
# name of: put_object.S3.aws | ||
def self.instrument_sdk_operations | ||
Aws.constants.each do|c| | ||
m = Aws.const_get(c) | ||
if m.is_a?(Module) && | ||
m.const_defined?(:Client) && | ||
m.const_get(:Client).superclass == Seahorse::Client::Base | ||
m.const_get(:Client).add_plugin(Aws::Rails::Notifications) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
require_relative 'action_dispatch/session/dynamodb_store' |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module Aws | ||
# Use the Rails namespace. | ||
module Rails | ||
# @api private | ||
class Railtie < ::Rails::Railtie | ||
initializer 'aws-sdk-rails.initialize', | ||
mullermp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
before: :load_config_initializers do | ||
# Initialization Actions | ||
Aws::Rails.use_rails_encrypted_credentials | ||
Aws::Rails.add_action_mailer_delivery_method | ||
Aws::Rails.log_to_rails_logger | ||
end | ||
|
||
rake_tasks do | ||
load 'tasks/dynamo_db/session_store/clean.rake' | ||
end | ||
end | ||
|
||
# This is called automatically from the SDK's Railtie, but can be manually | ||
# called if you want to specify options for building the Aws::SES::Client. | ||
# | ||
# @param [Symbol] name The name of the ActionMailer delivery method to | ||
# register. | ||
# @param [Hash] options The options you wish to pass on to the | ||
# Aws::SES::Client initialization method. | ||
def self.add_action_mailer_delivery_method(name = :ses, options = {}) | ||
ActiveSupport.on_load(:action_mailer) do | ||
add_delivery_method(name, Aws::Rails::Mailer, options) | ||
end | ||
end | ||
|
||
# Configures the AWS SDK for Ruby's logger to use the Rails logger. | ||
def self.log_to_rails_logger | ||
Aws.config[:logger] = ::Rails.logger | ||
nil | ||
end | ||
|
||
# Configures the AWS SDK with credentials from Rails encrypted credentials. | ||
def self.use_rails_encrypted_credentials | ||
# limit the config keys we merge to credentials only | ||
aws_credential_keys = %i[access_key_id secret_access_key session_token] | ||
|
||
Aws.config.merge!( | ||
::Rails.application | ||
.try(:credentials) | ||
.try(:aws) | ||
.to_h.slice(*aws_credential_keys) | ||
) | ||
end | ||
|
||
# Adds ActiveSupport Notifications instrumentation to AWS SDK | ||
# client operations. Each operation will produce an event with a name: | ||
# <operation>.<service>.aws. For example, S3's put_object has an event | ||
# name of: put_object.S3.aws | ||
def self.instrument_sdk_operations | ||
Aws.constants.each do|c| | ||
m = Aws.const_get(c) | ||
if m.is_a?(Module) && | ||
m.const_defined?(:Client) && | ||
m.const_get(:Client).superclass == Seahorse::Client::Base | ||
m.const_get(:Client).add_plugin(Aws::Rails::Notifications) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Description: | ||
Generates a migration file for deleting and a creating a DynamoDB | ||
sessions table, and a configuration file for the session store. | ||
|
||
Example: | ||
rails generate dynamo_db:session_store_migration <MIGRATION_NAME> | ||
|
||
This will create: | ||
db/migrate/#{VERSION}_#{MIGRATION_NAME}.rb | ||
config/dynamo_db_session_store.yml | ||
|
||
The migration will be run when the command rake db:migrate is run | ||
in the command line. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.