|
1 |
| -## My Project |
| 1 | +# ActionDispatch Session Storage with Amazon DynamoDB |
2 | 2 |
|
3 |
| -TODO: Fill this README out! |
| 3 | +[](https://badge.fury.io/rb/aws-actiondispatch-dynamodb-ruby) |
| 4 | +[](https://github.com/aws/aws-actiondispatch-dynamodb-ruby/actions) |
| 5 | +[](https://github.com/aws/aws-actiondispatch-dynamodb-ruby/network) |
| 6 | +[](https://github.com/aws/aws-actiondispatch-dynamodb-ruby/stargazers) |
4 | 7 |
|
5 |
| -Be sure to: |
| 8 | +This gem contains an |
| 9 | +[ActionDispatch AbstractStore](https://api.rubyonrails.org/classes/ActionDispatch/Session/AbstractStore.html) |
| 10 | +implementation that uses Amazon DynamoDB as a session store, allowing access |
| 11 | +to sessions from other applications and devices. |
6 | 12 |
|
7 |
| -* Change the title in this README |
8 |
| -* Edit your repository description on GitHub |
| 13 | +## Installation |
9 | 14 |
|
10 |
| -## Security |
| 15 | +Add this gem to your Rails project's Gemfile: |
11 | 16 |
|
12 |
| -See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
| 17 | +```ruby |
| 18 | +gem 'aws-sdk-rails', '~> 4' |
| 19 | +gem 'aws-actiondispatch-dynamodb', '~> 0' |
| 20 | +``` |
13 | 21 |
|
14 |
| -## License |
| 22 | +Then run `bundle install`. |
15 | 23 |
|
16 |
| -This project is licensed under the Apache-2.0 License. |
| 24 | +This gem also brings in the following AWS gems: |
17 | 25 |
|
| 26 | +* `aws-sessionstore-dynamodb` -> `aws-sdk-dynamodb` |
| 27 | + |
| 28 | +You will have to ensure that you provide credentials for the SDK to use. See the |
| 29 | +latest [AWS SDK for Ruby Docs](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/index.html#Configuration) |
| 30 | +for details. |
| 31 | + |
| 32 | +If you're running your Rails application on Amazon EC2, the AWS SDK will |
| 33 | +check Amazon EC2 instance metadata for credentials to load. Learn more: |
| 34 | +[IAM Roles for Amazon EC2](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html) |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +To use the session store, add or edit your |
| 39 | +`config/initializers/session_store.rb` file: |
| 40 | + |
| 41 | +```ruby |
| 42 | +options = { table_name: '_your_app_session' } |
| 43 | +Rails.application.config.session_store :dynamo_db_store, **options |
| 44 | +``` |
| 45 | + |
| 46 | +You can now start your Rails application with DynamoDB session support. |
| 47 | + |
| 48 | +**Note**: You will need a DynamoDB table to store your sessions. You can create |
| 49 | +the table using the provided Rake tasks or ActiveRecord migration generator |
| 50 | +after configuring. |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +You can configure the session store with code, ENV, or a YAML file, in this |
| 55 | +order of precedence. To configure in code, you can directly pass options to your |
| 56 | +`config/initializers/session_store.rb` file like so: |
| 57 | + |
| 58 | +```ruby |
| 59 | +options = { |
| 60 | + table_name: 'your-table-name', |
| 61 | + table_key: 'your-session-key', |
| 62 | + dynamo_db_client: Aws::DynamoDB::Client.new(region: 'us-west-2') |
| 63 | +} |
| 64 | +Rails.application.config.session_store :dynamo_db_store, **options |
| 65 | +``` |
| 66 | + |
| 67 | +The session store inherits from the |
| 68 | +[`Rack::Session::Abstract::Persisted`](https://rubydoc.info/github/rack/rack-session/main/Rack/Session/Abstract/Persisted) |
| 69 | +class, which also supports additional options (such as `:key`). |
| 70 | + |
| 71 | +For more information about this feature and configuration options, see the |
| 72 | +[Configuration](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html) |
| 73 | +class and the |
| 74 | +[GitHub repository](https://github.com/aws/aws-sessionstore-dynamodb-ruby). |
| 75 | + |
| 76 | +### Configuration file generator |
| 77 | + |
| 78 | +You can generate a configuration file for the session store using the following |
| 79 | +command (--environment=<environment> is optional): |
| 80 | + |
| 81 | +```bash |
| 82 | +rails generate dynamo_db:session_store_config --environment=<Rails.env> |
| 83 | +``` |
| 84 | + |
| 85 | +The session store configuration generator command will generate a YAML file |
| 86 | +`config/aws_dynamo_db_session_store.yml` with default options. If provided an |
| 87 | +environment, the file will be named |
| 88 | +`config/aws_dynamo_db_session_store/<Rails.env>.yml`, which takes precedence |
| 89 | +over the default file. |
| 90 | + |
| 91 | +## Migration |
| 92 | + |
| 93 | +### ActiveRecord Migration generator |
| 94 | + |
| 95 | +You can generate a migration file for the session table using the following |
| 96 | +command (<MigrationName> is optional): |
| 97 | + |
| 98 | +```bash |
| 99 | +rails generate dynamo_db:session_store_migration <MigrationName> |
| 100 | +``` |
| 101 | + |
| 102 | +The session store migration generator command will generate a |
| 103 | +migration file `db/migration/#{VERSION}_#{MIGRATION_NAME}.rb`. |
| 104 | + |
| 105 | +The migration file will create and delete a table with default options. These |
| 106 | +options can be changed prior to running the migration either by changing the |
| 107 | +configuration in the initializer, editing the migration file, in ENV, or in the |
| 108 | +config YAML file. These options are documented in the |
| 109 | +[Table](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Table.html) |
| 110 | +class. |
| 111 | + |
| 112 | +To create the table, run migrations as normal with: |
| 113 | + |
| 114 | +```bash |
| 115 | +rails db:migrate |
| 116 | +``` |
| 117 | + |
| 118 | +To delete the table and rollback, run the following command: |
| 119 | + |
| 120 | +```bash |
| 121 | +rails db:migrate:down VERSION=<VERSION> |
| 122 | +``` |
| 123 | + |
| 124 | +### Migration Rake tasks |
| 125 | + |
| 126 | +If you are not using ActiveRecord, you can manage the table using the provided |
| 127 | +Rake tasks: |
| 128 | + |
| 129 | +```bash |
| 130 | +rake dynamo_db:session_store:create_table |
| 131 | +rake dynamo_db:session_store:delete_table |
| 132 | +``` |
| 133 | + |
| 134 | +The rake tasks will create and delete a table with default options. These |
| 135 | +options can be changed prior to running the task either by changing the |
| 136 | +configuration in the initializer, in ENV, or in the config YAML file. These |
| 137 | +options are documented in the |
| 138 | +[Table](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Table.html) |
| 139 | +class. |
| 140 | + |
| 141 | +## Cleaning old sessions |
| 142 | + |
| 143 | +By default sessions do not expire. You can use `:max_age` and `:max_stale` to |
| 144 | +configure the max age or stale period of a session. |
| 145 | + |
| 146 | +You can use the DynamoDB |
| 147 | +[Time to Live (TTL) feature](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) |
| 148 | +on the `expire_at` attribute to automatically delete expired items, saving you |
| 149 | +the trouble of manually deleting them and reducing costs. |
| 150 | + |
| 151 | +If you wish to delete old sessions based on creation age (invalidating valid |
| 152 | +sessions) or if you want control over the garbage collection process, you can |
| 153 | +use the provided Rake task: |
| 154 | + |
| 155 | +```bash |
| 156 | +rake dynamo_db:session_store:clean |
| 157 | +``` |
| 158 | + |
| 159 | +The rake task will clean the table with default options. These options can be |
| 160 | +changed prior to running the task either by changing the configuration in the |
| 161 | +initializer, in ENV, or in the config YAML file. These options are documented in |
| 162 | +the |
| 163 | +[GarbageCollector](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/GarbageCollector.html) |
| 164 | +class. |
0 commit comments