Skip to content

Use AWS_ prefixed environment keys #40

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 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ documentation.
All Configuration options can be loaded from the environment except for
`:dynamo_db_client` and `:error_handler`, which must be set in Ruby code
directly if needed. The environment options must be prefixed with
`DYNAMO_DB_SESSION_` and then the name of the option:
`AWS_DYNAMO_DB_SESSION_` and then the name of the option:

DYNAMO_DB_SESSION_<name-of-option>
AWS_DYNAMO_DB_SESSION_<name-of-option>

The example below would be a valid way to set the session table name:

export DYNAMO_DB_SESSION_TABLE_NAME='your-table-name'
export AWS_DYNAMO_DB_SESSION_TABLE_NAME='your-table-name'

### YAML Configuration

You can create a YAML configuration file to set the options. The file must be
passed into Configuration as the `:config_file` option or with the
`DYNAMO_DB_SESSION_CONFIG_FILE` environment variable.
`AWS_DYNAMO_DB_SESSION_CONFIG_FILE` environment variable.

## Creating the session table

Expand Down
20 changes: 16 additions & 4 deletions lib/aws/session_store/dynamo_db/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module Aws::SessionStore::DynamoDB
# == Environment Variables
# The Configuration object can load default values from your environment. All configuration
# keys are supported except for `:dynamo_db_client` and `:error_handler`. The keys take the form
# of DYNAMO_DB_SESSION_<KEY_NAME>. Example:
# of AWS_DYNAMO_DB_SESSION_<KEY_NAME>. Example:
#
# export DYNAMO_DB_SESSION_TABLE_NAME='Sessions'
# export DYNAMO_DB_SESSION_TABLE_KEY='id'
# export AWS_DYNAMO_DB_SESSION_TABLE_NAME='Sessions'
# export AWS_DYNAMO_DB_SESSION_TABLE_KEY='id'
#
# == Locking Strategy
# By default, locking is disabled for session store access. To enable locking, set the
Expand Down Expand Up @@ -138,13 +138,25 @@ def default_error_handler(options)
def env_options
unsupported_keys = %i[dynamo_db_client error_handler]
(MEMBERS.keys - unsupported_keys).each_with_object({}) do |opt_name, opts|
key = "DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
key = env_key(opt_name)
next unless ENV.key?(key)

opts[opt_name] = parse_env_value(key)
end
end

def env_key(opt_name)
# legacy - remove this in aws-sessionstore-dynamodb ~> 4
key = "DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
if ENV.key?(key)
Kernel.warn("The environment variable `#{key}` is deprecated.
Please use `AWS_DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}` instead.")
else
key = "AWS_DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
end
key
end

def parse_env_value(key)
val = ENV.fetch(key, nil)
Integer(val)
Expand Down
26 changes: 13 additions & 13 deletions spec/aws/session_store/dynamo_db/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

def setup_env(options)
options.each do |k, v|
ENV["DYNAMO_DB_SESSION_#{k.to_s.upcase}"] = v.to_s
ENV["AWS_DYNAMO_DB_SESSION_#{k.to_s.upcase}"] = v.to_s
end
end

def teardown_env(options)
options.each_key { |k| ENV.delete("DYNAMO_DB_SESSION_#{k.to_s.upcase}") }
options.each_key { |k| ENV.delete("AWS_DYNAMO_DB_SESSION_#{k.to_s.upcase}") }
end

let(:client) { Aws::DynamoDB::Client.new(stub_responses: true) }
Expand All @@ -47,7 +47,7 @@ def teardown_env(options)
end

it 'configures with YAML with precedence over defaults' do
Tempfile.create('dynamo_db_session_store.yml') do |f|
Tempfile.create('AWS_DYNAMO_DB_SESSION_store.yml') do |f|
f << options.transform_keys(&:to_s).to_yaml
f.rewind
cfg = Aws::SessionStore::DynamoDB::Configuration.new(config_file: f.path)
Expand All @@ -57,7 +57,7 @@ def teardown_env(options)

it 'configures with ENV with precedence over YAML' do
setup_env(options)
Tempfile.create('dynamo_db_session_store.yml') do |f|
Tempfile.create('AWS_DYNAMO_DB_SESSION_store.yml') do |f|
f << { table_name: 'OldTable', table_key: 'OldKey' }.transform_keys(&:to_s).to_yaml
f.rewind
cfg = Aws::SessionStore::DynamoDB::Configuration.new(config_file: f.path)
Expand All @@ -70,7 +70,7 @@ def teardown_env(options)
it 'configures in code with full precedence' do
old = { table_name: 'OldTable', table_key: 'OldKey' }
setup_env(options.merge(old))
Tempfile.create('dynamo_db_session_store.yml') do |f|
Tempfile.create('aws_dynamo_db_session_store.yml') do |f|
f << old.transform_keys(&:to_s).to_yaml
f.rewind
cfg = Aws::SessionStore::DynamoDB::Configuration.new(options.merge(config_file: f.path))
Expand All @@ -81,29 +81,29 @@ def teardown_env(options)
end

it 'allows for config file to be configured with ENV' do
Tempfile.create('dynamo_db_session_store.yml') do |f|
Tempfile.create('aws_dynamo_db_session_store.yml') do |f|
f << options.transform_keys(&:to_s).to_yaml
f.rewind
ENV['DYNAMO_DB_SESSION_CONFIG_FILE'] = f.path
ENV['AWS_DYNAMO_DB_SESSION_CONFIG_FILE'] = f.path
cfg = Aws::SessionStore::DynamoDB::Configuration.new
expect(cfg.to_hash).to include(options)
ensure
ENV.delete('DYNAMO_DB_SESSION_CONFIG_FILE')
ENV.delete('AWS_DYNAMO_DB_SESSION_CONFIG_FILE')
end
end

it 'ignores unsupported keys in ENV' do
ENV['DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT'] = 'Client'
ENV['DYNAMO_DB_SESSION_ERROR_HANDLER'] = 'Handler'
ENV['AWS_DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT'] = 'Client'
ENV['AWS_DYNAMO_DB_SESSION_ERROR_HANDLER'] = 'Handler'
cfg = Aws::SessionStore::DynamoDB::Configuration.new
expect(cfg.to_hash).to include(defaults)
ensure
ENV.delete('DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT')
ENV.delete('DYNAMO_DB_SESSION_ERROR_HANDLER')
ENV.delete('AWS_DYNAMO_DB_SESSION_DYNAMO_DB_CLIENT')
ENV.delete('AWS_DYNAMO_DB_SESSION_ERROR_HANDLER')
end

it 'ignores unsupported keys in YAML' do
Tempfile.create('dynamo_db_session_store.yml') do |f|
Tempfile.create('aws_dynamo_db_session_store.yml') do |f|
options = { dynamo_db_client: 'Client', error_handler: 'Handler', config_file: 'File' }
f << options.transform_keys(&:to_s).to_yaml
f.rewind
Expand Down