Skip to content

Don't use a client subset for config #27

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
merged 3 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased Changes
------------------

* Issue - Expose `:config` in `RackMiddleware` and `:config_file` in `Configuration`.

* Issue - V2 of this release was still loading SDK V1 credential keys. This removes support for client options specified in YAML configuration (behavior change). Instead, construct `Aws::DynamoDB::Client` and use the `dynamo_db_client` option.

2.0.0 (2020-11-11)
------------------

Expand Down
62 changes: 24 additions & 38 deletions lib/aws/session_store/dynamo_db/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Configuration
:secret_key => nil
}

### Feature options

# @return [String] Session table name.
attr_reader :table_name

Expand Down Expand Up @@ -84,14 +86,6 @@ class Configuration
# ErrorHandler is used.
attr_reader :raise_errors

# @return [DynamoDB Client] DynamoDB client.
attr_reader :dynamo_db_client

# @return [Error Handler] An error handling object that handles all exceptions
# thrown during execution of the AWS DynamoDB Session Store Rack Middleware.
# For more information see the Handling Errors Section.
attr_reader :error_handler

# @return [Integer] Maximum number of seconds earlier
# from the current time that a session was created.
attr_reader :max_age
Expand All @@ -100,9 +94,6 @@ class Configuration
# before the current time that the session was last accessed.
attr_reader :max_stale

# @return [String] The secret key for HMAC encryption.
attr_reader :secret_key

# @return [true] Pessimistic locking strategy will be implemented for
# all session accesses.
# @return [false] No locking strategy will be implemented for
Expand All @@ -120,6 +111,21 @@ class Configuration
# before giving up.
attr_reader :lock_max_wait_time

# @return [String] The secret key for HMAC encryption.
attr_reader :secret_key

# @return [String,Pathname]
attr_reader :config_file

### Client and Error Handling options

# @return [DynamoDB Client] DynamoDB client.
attr_reader :dynamo_db_client

# @return [Error Handler] An error handling object that handles all exceptions
# thrown during execution of the AWS DynamoDB Session Store Rack Middleware.
# For more information see the Handling Errors Section.
attr_reader :error_handler

# Provides configuration object that allows access to options defined
# during Runtime, in a YAML file, in the ENV and by default.
Expand Down Expand Up @@ -163,18 +169,16 @@ class Configuration
# @option options [Integer] :lock_retry_delay (500) Time in milleseconds to
# wait before retrying to obtain lock once an attempt to obtain lock
# has been made and has failed.
# @option options [Integer] :lock_max_wait_time (500) Maximum time in seconds
# to wait to acquire lock before giving up.
# @option options [Integer] :lock_max_wait_time (500) Maximum time
# in seconds to wait to acquire lock before giving up.
# @option options [String] :secret_key (SecureRandom.hex(64))
# Secret key for HMAC encription.
def initialize(options = {})
@options = default_options.merge(
env_options.merge(
file_options(options).merge(
symbolize_keys(options)
)
)
)
env_options.merge(
file_options(options).merge(symbolize_keys(options))
)
)
@options = client_error.merge(@options)
set_attributes(@options)
end
Expand All @@ -188,8 +192,7 @@ def to_hash

# @return [Hash] DDB client.
def gen_dynamo_db_client
client_opts = client_subset(@options)
client_opts[:user_agent_suffix] = _user_agent(@options.delete(:user_agent_suffix))
client_opts = { user_agent_suffix: " aws-sessionstore/#{VERSION}" }
client = Aws::DynamoDB::Client
dynamo_db_client = @options[:dynamo_db_client] || client.new(client_opts)
{:dynamo_db_client => dynamo_db_client}
Expand Down Expand Up @@ -258,22 +261,5 @@ def symbolize_keys(options)
opts
end
end

# @return [Hash] Client subset options hash.
def client_subset(options = {})
client_keys = [:secret_access_key, :region, :access_key_id, :session_token]
options.inject({}) do |opts, (opt_name, opt_value)|
opts[opt_name.to_sym] = opt_value if client_keys.include?(opt_name.to_sym)
opts
end
end

def _user_agent(custom)
if custom
custom
else
" aws-sessionstore/#{VERSION}"
end
end
end
end
3 changes: 3 additions & 0 deletions lib/aws/session_store/dynamo_db/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Aws::SessionStore::DynamoDB
# This class is an ID based Session Store Rack Middleware
# that uses a DynamoDB backend for session storage.
class RackMiddleware < Rack::Session::Abstract::Persisted
# @return [Configuration] An instance of Configuration that is used for
# this middleware.
attr_reader :config

# Initializes SessionStore middleware.
#
Expand Down
3 changes: 0 additions & 3 deletions spec/aws/session_store/dynamo_db/app_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@
table_name: NewTable
table_key: Somekey
consistent_read: true
AWS_ACCESS_KEY_ID: FakeKey
AWS_SECRET_ACCESS_KEY: Secret
AWS_REGION: New York
9 changes: 6 additions & 3 deletions spec/aws/session_store/dynamo_db/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
let(:expected_file_opts) do
{
consistent_read: true,
AWS_ACCESS_KEY_ID: 'FakeKey',
AWS_REGION: 'New York',
table_name: 'NewTable',
table_key: 'Somekey',
AWS_SECRET_ACCESS_KEY: 'Secret'
}
end

let(:client) { Aws::DynamoDB::Client.new(stub_responses: true) }

let(:runtime_options) do
{
table_name: 'SessionTable',
Expand All @@ -51,6 +50,10 @@ def expected_options(opts)
expect(cfg.to_hash).to include(expected_opts)
end

before do
allow(Aws::DynamoDB::Client).to receive(:new).and_return(client)
end

context 'Configuration Tests' do
it 'configures option with out runtime,YAML or ENV options' do
cfg = Aws::SessionStore::DynamoDB::Configuration.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def collect_garbage
}
end

let(:dynamo_db_client) {Aws::DynamoDB::Client.new}
let(:dynamo_db_client) {Aws::DynamoDB::Client.new(stub_responses: true)}

context 'Mock DynamoDB client with garbage collection' do
it 'processes scan result greater than 25 and deletes in batches of 25' do
Expand Down