Skip to content

Commit 2ec792d

Browse files
authored
Use ENV with precedence over YAML configuration (#39)
1 parent abb02ae commit 2ec792d

File tree

5 files changed

+76
-46
lines changed

5 files changed

+76
-46
lines changed

CHANGELOG.md

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

4+
* Issue - `Configuration` now takes environment variables with precedence over YAML configuration.
5+
46
3.0.0 (2024-10-29)
57
------------------
68

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,13 @@ gem 'aws-sdk-rails', '~> 4'
3737

3838
A number of options are available to be set in
3939
`Aws::SessionStore::DynamoDB::Configuration`, which is used throughout the
40-
application. These options can be set directly by Ruby code, through
41-
a YAML configuration file, or Environment variables, in order of precedence.
40+
application. These options can be set directly in Ruby code, in ENV variables,
41+
or in a YAML configuration file, in order of precedence.
4242

4343
The full set of options along with defaults can be found in the
4444
[Configuration](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html)
4545
documentation.
4646

47-
### YAML Configuration
48-
49-
You can create a YAML configuration file to set the options. The file must be
50-
passed into Configuration as the `:config_file` option.
51-
5247
### Environment Options
5348

5449
All Configuration options can be loaded from the environment except for
@@ -62,6 +57,12 @@ The example below would be a valid way to set the session table name:
6257

6358
export DYNAMO_DB_SESSION_TABLE_NAME='your-table-name'
6459

60+
### YAML Configuration
61+
62+
You can create a YAML configuration file to set the options. The file must be
63+
passed into Configuration as the `:config_file` option or with the
64+
`DYNAMO_DB_SESSION_CONFIG_FILE` environment variable.
65+
6566
## Creating the session table
6667

6768
After installation and configuration, you must create the session table using

lib/aws/session_store/dynamo_db/configuration.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Aws::SessionStore::DynamoDB
66
# This class provides a Configuration object for all DynamoDB session store operations
7-
# by pulling configuration options from Runtime, a YAML file, the ENV, and default
7+
# by pulling configuration options from Runtime, the ENV, a YAML file, and default
88
# settings, in that order.
99
#
1010
# == Environment Variables
@@ -63,7 +63,7 @@ class Configuration
6363
}.freeze
6464

6565
# Provides configuration object that allows access to options defined
66-
# during Runtime, in a YAML file, in the ENV, and by default.
66+
# during Runtime, in the ENV, in a YAML file, and by default.
6767
#
6868
# @option options [String] :table_name ("sessions") Name of the session table.
6969
# @option options [String] :table_key ("session_id") The hash key of the session table.
@@ -99,8 +99,9 @@ class Configuration
9999
# @option options [Aws::DynamoDB::Client] :dynamo_db_client (Aws::DynamoDB::Client.new)
100100
# DynamoDB client used to perform database operations inside of the rack application.
101101
def initialize(options = {})
102-
opts = file_options(options).merge(options)
102+
opts = options
103103
opts = env_options.merge(opts)
104+
opts = file_options(opts).merge(opts)
104105
MEMBERS.each_pair do |opt_name, default_value|
105106
opts[opt_name] = default_value unless opts.key?(opt_name)
106107
end
@@ -145,13 +146,10 @@ def env_options
145146
end
146147

147148
def parse_env_value(key)
148-
Integer(ENV.fetch(key, nil))
149+
val = ENV.fetch(key, nil)
150+
Integer(val)
149151
rescue ArgumentError
150-
if ENV[key] == 'true' || ENV[key] == 'false'
151-
ENV[key] == 'true'
152-
else
153-
ENV.fetch(key, nil)
154-
end
152+
%w[true false].include?(val) ? val == 'true' : val
155153
end
156154

157155
# @return [Hash] File options.
@@ -168,7 +166,8 @@ def load_from_file(file_path)
168166
require 'erb'
169167
require 'yaml'
170168
opts = YAML.safe_load(ERB.new(File.read(file_path)).result) || {}
171-
opts.transform_keys(&:to_sym)
169+
unsupported_keys = %i[dynamo_db_client error_handler config_file]
170+
opts.transform_keys(&:to_sym).reject { |k, _| unsupported_keys.include?(k) }
172171
end
173172

174173
def set_attributes(options)

spec/aws/session_store/dynamo_db/configuration_spec.rb

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
}
2626
end
2727

28-
def setup_env
28+
def setup_env(options)
2929
options.each do |k, v|
3030
ENV["DYNAMO_DB_SESSION_#{k.to_s.upcase}"] = v.to_s
3131
end
3232
end
3333

34-
def teardown_env
34+
def teardown_env(options)
3535
options.each_key { |k| ENV.delete("DYNAMO_DB_SESSION_#{k.to_s.upcase}") }
3636
end
3737

@@ -41,44 +41,75 @@ def teardown_env
4141
allow(Aws::DynamoDB::Client).to receive(:new).and_return(client)
4242
end
4343

44-
it 'configures defaults without runtime, YAML or ENV options' do
44+
it 'configures defaults without runtime, ENV, or YAML options' do
4545
cfg = Aws::SessionStore::DynamoDB::Configuration.new
4646
expect(cfg.to_hash).to include(defaults)
4747
end
4848

49-
it 'configures with ENV with precedence over defaults' do
50-
setup_env
51-
cfg = Aws::SessionStore::DynamoDB::Configuration.new
52-
expect(cfg.to_hash).to include(options)
53-
teardown_env
49+
it 'configures with YAML with precedence over defaults' do
50+
Tempfile.create('dynamo_db_session_store.yml') do |f|
51+
f << options.transform_keys(&:to_s).to_yaml
52+
f.rewind
53+
cfg = Aws::SessionStore::DynamoDB::Configuration.new(config_file: f.path)
54+
expect(cfg.to_hash).to include(options)
55+
end
56+
end
57+
58+
it 'configures with ENV with precedence over YAML' do
59+
setup_env(options)
60+
Tempfile.create('dynamo_db_session_store.yml') do |f|
61+
f << { table_name: 'OldTable', table_key: 'OldKey' }.transform_keys(&:to_s).to_yaml
62+
f.rewind
63+
cfg = Aws::SessionStore::DynamoDB::Configuration.new(config_file: f.path)
64+
expect(cfg.to_hash).to include(options)
65+
ensure
66+
teardown_env(options)
67+
end
68+
end
69+
70+
it 'configures in code with full precedence' do
71+
old = { table_name: 'OldTable', table_key: 'OldKey' }
72+
setup_env(options.merge(old))
73+
Tempfile.create('dynamo_db_session_store.yml') do |f|
74+
f << old.transform_keys(&:to_s).to_yaml
75+
f.rewind
76+
cfg = Aws::SessionStore::DynamoDB::Configuration.new(options.merge(config_file: f.path))
77+
expect(cfg.to_hash).to include(options)
78+
ensure
79+
teardown_env(options.merge(old))
80+
end
5481
end
5582

56-
it 'configs with YAML with precedence over ENV' do
57-
setup_env
83+
it 'allows for config file to be configured with ENV' do
5884
Tempfile.create('dynamo_db_session_store.yml') do |f|
5985
f << options.transform_keys(&:to_s).to_yaml
6086
f.rewind
6187
ENV['DYNAMO_DB_SESSION_CONFIG_FILE'] = f.path
6288
cfg = Aws::SessionStore::DynamoDB::Configuration.new
63-
ENV.delete('DYNAMO_DB_SESSION_CONFIG_FILE')
6489
expect(cfg.to_hash).to include(options)
90+
ensure
91+
ENV.delete('DYNAMO_DB_SESSION_CONFIG_FILE')
6592
end
66-
teardown_env
6793
end
6894

69-
it 'configures with runtime options with full precedence' do
70-
setup_env
95+
it 'ignores unsupported keys in ENV' do
96+
ENV['DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT'] = 'Client'
97+
ENV['DYNAMO_DB_SESSION_ERROR_HANDLER'] = 'Handler'
98+
cfg = Aws::SessionStore::DynamoDB::Configuration.new
99+
expect(cfg.to_hash).to include(defaults)
100+
ensure
101+
ENV.delete('DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT')
102+
ENV.delete('DYNAMO_DB_SESSION_ERROR_HANDLER')
103+
end
104+
105+
it 'ignores unsupported keys in YAML' do
71106
Tempfile.create('dynamo_db_session_store.yml') do |f|
72-
f << { table_name: 'OldTable', table_key: 'OldKey' }.transform_keys(&:to_s).to_yaml
107+
options = { dynamo_db_client: 'Client', error_handler: 'Handler', config_file: 'File' }
108+
f << options.transform_keys(&:to_s).to_yaml
73109
f.rewind
74-
cfg = Aws::SessionStore::DynamoDB::Configuration.new(
75-
options.merge(
76-
config_file: f.path
77-
)
78-
)
79-
expect(cfg.to_hash).to include(options)
110+
cfg = Aws::SessionStore::DynamoDB::Configuration.new(config_file: f.path)
111+
expect(cfg.to_hash).to include(defaults.merge(config_file: f.path))
80112
end
81-
teardown_env
82113
end
83114

84115
it 'raises an exception when wrong path for file' do

spec/aws/session_store/dynamo_db/garbage_collection_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
describe Aws::SessionStore::DynamoDB::GarbageCollection do
66
def items(min, max)
7-
items = []
8-
(min..max).each do |i|
9-
items << { 'session_id' => { s: i.to_s } }
7+
(min..max).map do |i|
8+
{ 'session_id' => { s: i.to_s } }
109
end
11-
items
1210
end
1311

1412
def format_scan_result
15-
items = []
16-
(31..49).each do |i|
17-
items << { 'session_id' => { s: i.to_s } }
13+
items = (31..49).map do |i|
14+
{ 'session_id' => { s: i.to_s } }
1815
end
1916

2017
items.each_with_object([]) do |item, rqst_array|

0 commit comments

Comments
 (0)