Skip to content

Commit 032d6ff

Browse files
authored
Expose config and config_file readers, and don't use client option keys in config (#27)
1 parent 83c412e commit 032d6ff

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Expose `:config` in `RackMiddleware` and `:config_file` in `Configuration`.
5+
6+
* 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.
7+
48
2.0.0 (2020-11-11)
59
------------------
610

lib/aws/session_store/dynamo_db/configuration.rb

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class Configuration
5656
:secret_key => nil
5757
}
5858

59+
### Feature options
60+
5961
# @return [String] Session table name.
6062
attr_reader :table_name
6163

@@ -84,14 +86,6 @@ class Configuration
8486
# ErrorHandler is used.
8587
attr_reader :raise_errors
8688

87-
# @return [DynamoDB Client] DynamoDB client.
88-
attr_reader :dynamo_db_client
89-
90-
# @return [Error Handler] An error handling object that handles all exceptions
91-
# thrown during execution of the AWS DynamoDB Session Store Rack Middleware.
92-
# For more information see the Handling Errors Section.
93-
attr_reader :error_handler
94-
9589
# @return [Integer] Maximum number of seconds earlier
9690
# from the current time that a session was created.
9791
attr_reader :max_age
@@ -100,9 +94,6 @@ class Configuration
10094
# before the current time that the session was last accessed.
10195
attr_reader :max_stale
10296

103-
# @return [String] The secret key for HMAC encryption.
104-
attr_reader :secret_key
105-
10697
# @return [true] Pessimistic locking strategy will be implemented for
10798
# all session accesses.
10899
# @return [false] No locking strategy will be implemented for
@@ -120,6 +111,21 @@ class Configuration
120111
# before giving up.
121112
attr_reader :lock_max_wait_time
122113

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
123129

124130
# Provides configuration object that allows access to options defined
125131
# during Runtime, in a YAML file, in the ENV and by default.
@@ -163,18 +169,16 @@ class Configuration
163169
# @option options [Integer] :lock_retry_delay (500) Time in milleseconds to
164170
# wait before retrying to obtain lock once an attempt to obtain lock
165171
# has been made and has failed.
166-
# @option options [Integer] :lock_max_wait_time (500) Maximum time in seconds
167-
# to wait to acquire lock before giving up.
172+
# @option options [Integer] :lock_max_wait_time (500) Maximum time
173+
# in seconds to wait to acquire lock before giving up.
168174
# @option options [String] :secret_key (SecureRandom.hex(64))
169175
# Secret key for HMAC encription.
170176
def initialize(options = {})
171177
@options = default_options.merge(
172-
env_options.merge(
173-
file_options(options).merge(
174-
symbolize_keys(options)
175-
)
176-
)
177-
)
178+
env_options.merge(
179+
file_options(options).merge(symbolize_keys(options))
180+
)
181+
)
178182
@options = client_error.merge(@options)
179183
set_attributes(@options)
180184
end
@@ -188,8 +192,7 @@ def to_hash
188192

189193
# @return [Hash] DDB client.
190194
def gen_dynamo_db_client
191-
client_opts = client_subset(@options)
192-
client_opts[:user_agent_suffix] = _user_agent(@options.delete(:user_agent_suffix))
195+
client_opts = { user_agent_suffix: " aws-sessionstore/#{VERSION}" }
193196
client = Aws::DynamoDB::Client
194197
dynamo_db_client = @options[:dynamo_db_client] || client.new(client_opts)
195198
{:dynamo_db_client => dynamo_db_client}
@@ -258,22 +261,5 @@ def symbolize_keys(options)
258261
opts
259262
end
260263
end
261-
262-
# @return [Hash] Client subset options hash.
263-
def client_subset(options = {})
264-
client_keys = [:secret_access_key, :region, :access_key_id, :session_token]
265-
options.inject({}) do |opts, (opt_name, opt_value)|
266-
opts[opt_name.to_sym] = opt_value if client_keys.include?(opt_name.to_sym)
267-
opts
268-
end
269-
end
270-
271-
def _user_agent(custom)
272-
if custom
273-
custom
274-
else
275-
" aws-sessionstore/#{VERSION}"
276-
end
277-
end
278264
end
279265
end

lib/aws/session_store/dynamo_db/rack_middleware.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module Aws::SessionStore::DynamoDB
66
# This class is an ID based Session Store Rack Middleware
77
# that uses a DynamoDB backend for session storage.
88
class RackMiddleware < Rack::Session::Abstract::Persisted
9+
# @return [Configuration] An instance of Configuration that is used for
10+
# this middleware.
11+
attr_reader :config
912

1013
# Initializes SessionStore middleware.
1114
#

spec/aws/session_store/dynamo_db/app_config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,3 @@
1414
table_name: NewTable
1515
table_key: Somekey
1616
consistent_read: true
17-
AWS_ACCESS_KEY_ID: FakeKey
18-
AWS_SECRET_ACCESS_KEY: Secret
19-
AWS_REGION: New York

spec/aws/session_store/dynamo_db/configuration_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
let(:expected_file_opts) do
3131
{
3232
consistent_read: true,
33-
AWS_ACCESS_KEY_ID: 'FakeKey',
34-
AWS_REGION: 'New York',
3533
table_name: 'NewTable',
3634
table_key: 'Somekey',
37-
AWS_SECRET_ACCESS_KEY: 'Secret'
3835
}
3936
end
4037

38+
let(:client) { Aws::DynamoDB::Client.new(stub_responses: true) }
39+
4140
let(:runtime_options) do
4241
{
4342
table_name: 'SessionTable',
@@ -51,6 +50,10 @@ def expected_options(opts)
5150
expect(cfg.to_hash).to include(expected_opts)
5251
end
5352

53+
before do
54+
allow(Aws::DynamoDB::Client).to receive(:new).and_return(client)
55+
end
56+
5457
context 'Configuration Tests' do
5558
it 'configures option with out runtime,YAML or ENV options' do
5659
cfg = Aws::SessionStore::DynamoDB::Configuration.new

spec/aws/session_store/dynamo_db/garbage_collection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def collect_garbage
9898
}
9999
end
100100

101-
let(:dynamo_db_client) {Aws::DynamoDB::Client.new}
101+
let(:dynamo_db_client) {Aws::DynamoDB::Client.new(stub_responses: true)}
102102

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

0 commit comments

Comments
 (0)