env_configuration 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e16758211998964af2449e93f0d2752ca8a151e7
4
- data.tar.gz: 33f121edd8f7bcf3a49cbdad4e7e81c98a0ad00f
3
+ metadata.gz: 51e9ea543b8b6c7daff5ec4013e15e5fb101e2f8
4
+ data.tar.gz: f03dd58f8f7af34d43fe941f3670bf8c6714f084
5
5
  SHA512:
6
- metadata.gz: e8bce5b57819d92dae03853a32772999c4bf80104dee81367e7d8074532d5b3f61dd3db1d059e77d392c8d668abd02b91a6fbc2bcf880283070a0884f52bda4c
7
- data.tar.gz: 59348900392be1747750635cd405e93d7ed1300edf59329a4f90f08a8b5ac0c2a3d317cdfa287ae9035c2f8d943ca6f6140217c4b85ea36d1013e60557d3b503
6
+ metadata.gz: 7b2fccc7ad1cf9e5d3b27642fe9225f895a3581ed5c3fe2093697e41899aab7093a07e37d2031097fce6db1eb47d04a6c042979f562d3009b6f79d48644df4cf
7
+ data.tar.gz: 6b5347369b28d1d9d068f0493c5fec77cd5c9be405c3f50deb4a3cfbfb9a7d78fb2f60d208567e8b66d762e7504ec017fe5fc0c840698c1ccc86db54da992d5f
data/.env CHANGED
@@ -1,6 +1,6 @@
1
- APP_NAME='BookMeBus DotRoot'
2
- COMPANY_NAME='Camtasia Technology DotRoot'
3
- APP_VERSION='1.0.0-DotRoot'
4
- ENABLE_HTTPS='no-DotRoot'
5
- ASSET_HOST_URL='http://DotRoot.local'
1
+ APP_NAME='BookMeBus DotRoot'
2
+ COMPANY_NAME='Camtasia Technology DotRoot'
3
+ APP_VERSION='1.0.0-DotRoot'
4
+ ENABLE_HTTPS='no-DotRoot'
5
+ ASSET_HOST_URL='http://DotRoot.local'
6
6
  HOST='http://DotRoot.local'
data/.gitignore CHANGED
@@ -6,6 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- *.gem
9
+
10
10
  # rspec failure tracking
11
11
  .rspec_status
@@ -1,104 +1,104 @@
1
- PATH
2
- remote: .
3
- specs:
4
- env_configuration (0.1.1)
5
- aws-sdk-ssm
6
- dotenv-rails (= 2.7.4)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- actionpack (5.2.3)
12
- actionview (= 5.2.3)
13
- activesupport (= 5.2.3)
14
- rack (~> 2.0)
15
- rack-test (>= 0.6.3)
16
- rails-dom-testing (~> 2.0)
17
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
- actionview (5.2.3)
19
- activesupport (= 5.2.3)
20
- builder (~> 3.1)
21
- erubi (~> 1.4)
22
- rails-dom-testing (~> 2.0)
23
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
- activesupport (5.2.3)
25
- concurrent-ruby (~> 1.0, >= 1.0.2)
26
- i18n (>= 0.7, < 2)
27
- minitest (~> 5.1)
28
- tzinfo (~> 1.1)
29
- aws-eventstream (1.0.3)
30
- aws-partitions (1.215.0)
31
- aws-sdk-core (3.68.0)
32
- aws-eventstream (~> 1.0, >= 1.0.2)
33
- aws-partitions (~> 1.0)
34
- aws-sigv4 (~> 1.1)
35
- jmespath (~> 1.0)
36
- aws-sdk-ssm (1.55.0)
37
- aws-sdk-core (~> 3, >= 3.61.1)
38
- aws-sigv4 (~> 1.1)
39
- aws-sigv4 (1.1.0)
40
- aws-eventstream (~> 1.0, >= 1.0.2)
41
- builder (3.2.3)
42
- concurrent-ruby (1.1.5)
43
- crass (1.0.4)
44
- diff-lcs (1.3)
45
- dotenv (2.7.4)
46
- dotenv-rails (2.7.4)
47
- dotenv (= 2.7.4)
48
- railties (>= 3.2, < 6.1)
49
- erubi (1.8.0)
50
- i18n (1.6.0)
51
- concurrent-ruby (~> 1.0)
52
- jmespath (1.4.0)
53
- loofah (2.2.3)
54
- crass (~> 1.0.2)
55
- nokogiri (>= 1.5.9)
56
- method_source (0.9.2)
57
- mini_portile2 (2.4.0)
58
- minitest (5.11.3)
59
- nokogiri (1.10.4)
60
- mini_portile2 (~> 2.4.0)
61
- rack (2.0.7)
62
- rack-test (1.1.0)
63
- rack (>= 1.0, < 3)
64
- rails-dom-testing (2.0.3)
65
- activesupport (>= 4.2.0)
66
- nokogiri (>= 1.6)
67
- rails-html-sanitizer (1.0.4)
68
- loofah (~> 2.2, >= 2.2.2)
69
- railties (5.2.3)
70
- actionpack (= 5.2.3)
71
- activesupport (= 5.2.3)
72
- method_source
73
- rake (>= 0.8.7)
74
- thor (>= 0.19.0, < 2.0)
75
- rake (10.5.0)
76
- rspec (3.8.0)
77
- rspec-core (~> 3.8.0)
78
- rspec-expectations (~> 3.8.0)
79
- rspec-mocks (~> 3.8.0)
80
- rspec-core (3.8.2)
81
- rspec-support (~> 3.8.0)
82
- rspec-expectations (3.8.4)
83
- diff-lcs (>= 1.2.0, < 2.0)
84
- rspec-support (~> 3.8.0)
85
- rspec-mocks (3.8.1)
86
- diff-lcs (>= 1.2.0, < 2.0)
87
- rspec-support (~> 3.8.0)
88
- rspec-support (3.8.2)
89
- thor (0.20.3)
90
- thread_safe (0.3.6)
91
- tzinfo (1.2.5)
92
- thread_safe (~> 0.1)
93
-
94
- PLATFORMS
95
- ruby
96
-
97
- DEPENDENCIES
98
- bundler (~> 1.16)
99
- env_configuration!
100
- rake (~> 10.0)
101
- rspec (~> 3.0)
102
-
103
- BUNDLED WITH
104
- 1.16.2
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ env_configuration (0.1.0)
5
+ aws-sdk-ssm
6
+ dotenv-rails (= 2.7.4)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (5.2.3)
12
+ actionview (= 5.2.3)
13
+ activesupport (= 5.2.3)
14
+ rack (~> 2.0)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
+ actionview (5.2.3)
19
+ activesupport (= 5.2.3)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
+ activesupport (5.2.3)
25
+ concurrent-ruby (~> 1.0, >= 1.0.2)
26
+ i18n (>= 0.7, < 2)
27
+ minitest (~> 5.1)
28
+ tzinfo (~> 1.1)
29
+ aws-eventstream (1.0.3)
30
+ aws-partitions (1.189.0)
31
+ aws-sdk-core (3.59.0)
32
+ aws-eventstream (~> 1.0, >= 1.0.2)
33
+ aws-partitions (~> 1.0)
34
+ aws-sigv4 (~> 1.1)
35
+ jmespath (~> 1.0)
36
+ aws-sdk-ssm (1.52.0)
37
+ aws-sdk-core (~> 3, >= 3.58.0)
38
+ aws-sigv4 (~> 1.1)
39
+ aws-sigv4 (1.1.0)
40
+ aws-eventstream (~> 1.0, >= 1.0.2)
41
+ builder (3.2.3)
42
+ concurrent-ruby (1.1.5)
43
+ crass (1.0.4)
44
+ diff-lcs (1.3)
45
+ dotenv (2.7.4)
46
+ dotenv-rails (2.7.4)
47
+ dotenv (= 2.7.4)
48
+ railties (>= 3.2, < 6.1)
49
+ erubi (1.8.0)
50
+ i18n (1.6.0)
51
+ concurrent-ruby (~> 1.0)
52
+ jmespath (1.4.0)
53
+ loofah (2.2.3)
54
+ crass (~> 1.0.2)
55
+ nokogiri (>= 1.5.9)
56
+ method_source (0.9.2)
57
+ mini_portile2 (2.4.0)
58
+ minitest (5.11.3)
59
+ nokogiri (1.10.3)
60
+ mini_portile2 (~> 2.4.0)
61
+ rack (2.0.7)
62
+ rack-test (1.1.0)
63
+ rack (>= 1.0, < 3)
64
+ rails-dom-testing (2.0.3)
65
+ activesupport (>= 4.2.0)
66
+ nokogiri (>= 1.6)
67
+ rails-html-sanitizer (1.0.4)
68
+ loofah (~> 2.2, >= 2.2.2)
69
+ railties (5.2.3)
70
+ actionpack (= 5.2.3)
71
+ activesupport (= 5.2.3)
72
+ method_source
73
+ rake (>= 0.8.7)
74
+ thor (>= 0.19.0, < 2.0)
75
+ rake (10.5.0)
76
+ rspec (3.8.0)
77
+ rspec-core (~> 3.8.0)
78
+ rspec-expectations (~> 3.8.0)
79
+ rspec-mocks (~> 3.8.0)
80
+ rspec-core (3.8.2)
81
+ rspec-support (~> 3.8.0)
82
+ rspec-expectations (3.8.4)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.8.0)
85
+ rspec-mocks (3.8.1)
86
+ diff-lcs (>= 1.2.0, < 2.0)
87
+ rspec-support (~> 3.8.0)
88
+ rspec-support (3.8.2)
89
+ thor (0.20.3)
90
+ thread_safe (0.3.6)
91
+ tzinfo (1.2.5)
92
+ thread_safe (~> 0.1)
93
+
94
+ PLATFORMS
95
+ ruby
96
+
97
+ DEPENDENCIES
98
+ bundler (~> 1.16)
99
+ env_configuration!
100
+ rake (~> 10.0)
101
+ rspec (~> 3.0)
102
+
103
+ BUNDLED WITH
104
+ 1.16.2
data/README.md CHANGED
@@ -1,289 +1,268 @@
1
- # EnvConfiguration
2
-
3
- [![Build Status](https://travis-ci.org/channainfo/env_configuration.svg?branch=develop)](https://travis-ci.org/channainfo/env_configuration)
4
-
5
- This gem aims to load your config into ENV. Currently configuration can be done via 3 different adapters
6
-
7
- 1. Using .env powered by [dotenv](https://github.com/bkeepers/dotenv) gem. This is very popular in development environment.
8
- 2. Using Yaml file config. As Yaml can be nested it is a bit more convenient than dotenv to separate setting between environment.
9
- 3. Using [AWS System Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) - recommended for production.
10
-
11
- How about container service link [Heroku](https://heroku.com/) and [ElasticBeanstalk](https://aws.amazon.com/elasticbeanstalk/)?
12
-
13
- Heroku has an application settings for ENV which allows the config var to have up to 32kb max in size. ElasticBeanstalk has a similar approach by allowing you to set config var in the ElasticBeanstalk Settings, however[it allows only 4096bytes max for key, value combined](https://stackoverflow.com/questions/54344236/environmentvariables-default-value-length-is-greater-than-4096). This might cause issues for some application that integrate with many third parties.
14
-
15
-
16
-
17
- ## Why Store config in the environment?
18
-
19
- An app’s _config_ is everything that is likely to vary between [deploys](https://12factor.net/codebase) (staging, production, developer environments, etc). This includes:
20
-
21
- - Resource handles to the database, Memcached, and other [backing services](https://12factor.net/backing-services)
22
- - Credentials to external services such as Amazon S3 or Twitter
23
- - Per-deploy values such as the canonical hostname for the deploy
24
-
25
- Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires **strict separation of config from code**. Config varies substantially across deploys, code does not.
26
-
27
- A litmus test for whether an app has all config correctly factored out of the code is whether the codebase could be made open source at any moment, without compromising any credentials.
28
-
29
- ## Installation
30
- ```ruby
31
- gem 'env_configuration'
32
-
33
- ```
34
-
35
- And then execute:
36
-
37
- ```bash
38
- $ bundle
39
- ```
40
-
41
-
42
- Or install it yourself as:
43
-
44
-
45
- ```bash
46
- $ gem install env_configuration
47
- ```
48
-
49
-
50
- ## Usage
51
- There are 3 types of adapter :dot_env, :yaml, :aws_ssm_parameter_store.
52
- ```ruby
53
- EnvConfiguration::Configurator.load(:adapter_name, options={})
54
- ```
55
- Options value varies from adapter to adapter.
56
-
57
- ### Configure gem
58
- You can configure the gem with the following:
59
- ```ruby
60
- EnvConfiguration.configure do |config|
61
- config.dot_env = { dot_env_file: 'config/app.env' }
62
- config.yaml = { yaml_file: 'config/app.yaml'}
63
- config.aws_ssm_parameter_store = { access_key_id: 'aws-key', secret_access_key: 'aws-secret', region: 'ap-southeast-1', path: '/staging'}
64
- end
65
- ```
66
-
67
- ### DotEnv Adapter
68
- Internally EnvConfiguration use [https://github.com/bkeepers/dotenv](https://github.com/bkeepers/dotenv) to handle this:
69
-
70
- ```ruby
71
- # Configuration (optional)
72
- # EnvConfiguration.configure do |config|
73
- # config.dot_env = { dot_env_file: 'config/app.env' }
74
- # end
75
-
76
- options = { dot_env_file: 'your-dotenv-app.env' }
77
- EnvConfiguration::Configurator.load(:dot_env, options)
78
- ```
79
- If options is not provided then the :dot_env adapter will try to get the from the gem configuration. It still does not exist then it will load the .env file located in the root of the project.
80
-
81
- ### Yaml adapter
82
- Internally EnvConfiguration use 'yaml' library from ruby to handle this.
83
- ```ruby
84
- # Configuration (optional)
85
- #EnvConfiguration.configure do |config|
86
- # config.yaml = { section: 'default-dev', yaml_file: 'config/default-app.yml'}
87
- #end
88
-
89
- # optional if you configure the gem above.
90
- options = { yaml_file: 'config/app.yaml', __FILE__), section: 'development' }
91
- result = EnvConfiguration::Configurator.load(:yaml, options)
92
- ```
93
- Both :yaml_file and :section must exist . If you options is being specified EnvConfiguration will take value from options. otherwise gem configuration must provide the values.
94
-
95
- if you miss to provide :yaml_file and :section the gem will raise the following errors:
96
-
97
- - :yaml_file in the options{} is required, for example config/application.yml
98
- - :section in the options{} is required, for example :staging, :test, :production
99
-
100
- As an example value of yaml_file: [config/app.yml.](https://github.com/channainfo/env_configuration/blob/develop/spec/config/app.yaml) Sections here are (test, development, staging, production)
101
- ```yml
102
- default: &default
103
- APP_NAME: "BookMeBus"
104
- COMPANY_NAME: "Camtasia Technology"
105
- APP_VERSION: "Development"
106
- ENABLE_HTTPS: 'no'
107
- ASSET_HOST_URL: 'http://localhost:3000'
108
- HOST: 'http://localhost:3000'
109
-
110
- test:
111
- <<: *default
112
-
113
- development:
114
- <<: *default
115
-
116
- staging:
117
- <<: *default
118
-
119
- production:
120
- <<: *default
121
- ```
122
- ### AWS SSM Parameter Store
123
-
124
- EnvConfiguration will fetch from the aws ssm parameter store service 10 key, value per request. If you have hundred it will ended up fetching as many requests until it finishes.
125
-
126
- AWS Systems Manager Parameter Store provides secure, hierarchical storage for configuration data management and secrets management. You can store data such as passwords, database strings, and license codes as parameter values. You can store values as plain text or encrypted data. You can then reference values by using the unique name that you specified when you created the parameter. Highly scalable, available, and durable, Parameter Store is backed by the AWS Cloud.
127
-
128
- Internally EnvConfiguration use aws-sdk-ssm gem to handle this.
129
-
130
- ```ruby
131
- # Configuration (optional)
132
- #EnvConfiguration.configure do |config|
133
- # options = { access_key_id: 'default-key-id', secret_access_key: 'default-secret-key', region: 'default-region', path: 'default-path' }
134
- # config.aws_ssm_parameter_store = options
135
- #end
136
-
137
- options = {access_key_id: 'your-aws-key', secret_access_key: 'your-aws-secret', region: 'your-region' , path: '/staging'}
138
- EnvConfiguration::Configurator.load(:aws_ssm_parameter_store, options)
139
- ```
140
- EnvConfiguration will use the options if options exists, otherwise it will use the options from the gem configuration above. The options will then be passed to aws-sdk-ssm.
141
- EnvConfiguration gem will hand over the options to aws-sdk-ssm. Interesting aws-sdk-ssk will follows the rules as below:
142
-
143
- ### Access key id and secret_access_key
144
- :access_key_id, :secret_access_key are search for in the following locations:
145
- - :access_key_id, :secret_access_key
146
- - ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']
147
- - ~/.aws/credentials
148
- - ~/.aws/config
149
-
150
- ### The Region
151
- The region is search for in the following locations:
152
- - :region
153
- - ENV['AWS_REGION']
154
- - ENV['AMAZON_REGION']
155
- - ENV['AWS_DEFAULT_REGION']
156
- - ~/.aws/credentials
157
- - ~/.aws/config
158
- For more details on the configuration [https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SSM/Client.html](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SSM/Client.html)
159
-
160
-
161
- ### Aws policy for SSM Parameter Store
162
- In order to be able to fetch the parameter store from aws ssm you need at least **ssm:GetParametersByPath** policy to attach to your IAM account
163
- ```json
164
- {
165
- "Version": "2012-10-17",
166
- "Statement": [
167
- {
168
- "Sid": "VisualEditor0",
169
- "Effect": "Allow",
170
- "Action": [
171
- "ssm:PutParameter",
172
- "ssm:GetParametersByPath",
173
- "ssm:GetParameters",
174
- "ssm:GetParameter"
175
- ],
176
- "Resource": "*"
177
- }
178
- ]
179
- }
180
- ```
181
-
182
- ## Integrate with rails
183
-
184
- ### Add Gem to application.rb
185
- Add the following line
186
- ```ruby
187
- EnvConfiguration::Configurator.load(:dot_env)
188
- ```
189
- right below the
190
- ```ruby
191
- Bundler.require(*Rails.groups)
192
- ```
193
- in the config/application.rb as below:
194
-
195
- ```ruby
196
- require File.expand_path('boot', __dir__)
197
- require 'rails/all'
198
- # Require the gems listed in Gemfile, including any gems
199
- # you've limited to :test, :development, or :production.
200
- Bundler.require(*Rails.groups)
201
- # EnvConfiguration
202
- EnvConfiguration::Configurator.load(:dot_env)
203
- ```
204
- As this gem does not handle the Rails env, you might need to do this yourself in case you use different adapters for each Rails env:
205
-
206
- ```ruby
207
- require File.expand_path('boot', __dir__)
208
-
209
- require 'rails/all'
210
-
211
- # Require the gems listed in Gemfile, including any gems
212
- # you've limited to :test, :development, or :production.
213
- Bundler.require(*Rails.groups)
214
-
215
- # EnvConfiguration
216
- if(Rails.env.development?)
217
- EnvConfiguration::Configurator.load(:dot_env)
218
- elsif Rails.env.test?
219
- EnvConfiguration::Configurator.load(:yaml, yaml_file: "#{Rails.root}/config/app-test.yml")
220
- else
221
- #separete aws ssm parameter store from staging with production /Rails.env
222
- EnvConfiguration::Configurator.load(:aws_ssm_parameter_store, path: "/#{Rails.env}")
223
- end
224
- ```
225
-
226
-
227
- ### Gem configuration in config/initializers/env_configuration.rb
228
- ```ruby
229
- EnvConfiguration.configure do |config|
230
- # Dot Env
231
- config.dot_env = { dot_env_file: 'config/app.env' }
232
- # Yaml Config
233
- config.yaml = { yaml_file: File.join(Rails.root, "config/application_#{Rails.env}.yaml" )}
234
-
235
- # Aws ssm parameter store
236
- config.aws_ssm_parameter_store = { access_key_id: ENV['AWS_ACCESS_KEY_ID'],
237
- secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
238
- region: ENV['AWS_REGION'],
239
- path: "/#{Rails.env}"}
240
- end
241
-
242
- ```
243
-
244
-
245
- ### Putting Parameters to AWS SSM Parameter store
246
- The main purpose of this gem is to read the config however, it offers an easy way to import the existing config in yml to parameter store as followed:
247
-
248
- ```ruby
249
- options = { access_key_id: ENV['AWS_ACCESS_KEY_ID'],
250
- secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
251
- region: ENV['AWS_REGION'] }
252
- path = 'staging'
253
- writer = EnvConfiguration::AwsSsmParameterStoreWriter.new('staging', options)
254
- config_file = "project/application.yml"
255
- writer.put_configs_from_yaml_file(config_file)
256
- # output {APP_NAME: "BookMeBus" COMPANY_NAME: "BookMeBus Pte," APP_VERSION: "Staging" ,ENABLE_HTTPS: 'yes'}
257
- ```
258
-
259
- application.yml
260
- ```yaml
261
- default: &default
262
- APP_NAME: "BookMeBus"
263
- COMPANY_NAME: "BookMeBus Pte,"
264
- APP_VERSION: "Staging"
265
- ENABLE_HTTPS: 'yes'
266
- staging:
267
- <<: *default
268
- ```
269
-
270
- > AWS parameter store put does not allow any var with value of length < 1. If you have any variable with empty value the variable will be ignored from pushing to aws ssm parameter store eventually you will lose it.
271
-
272
- ## Development
273
-
274
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
275
-
276
-
277
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
278
-
279
-
280
-
281
- ## Contributing
282
-
283
- Bug reports and pull requests are welcome on GitHub at https://github.com/channainfo/env_configuration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
284
-
285
-
286
-
287
- ## License
288
-
289
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
1
+ # EnvConfiguration
2
+
3
+ [![Build Status](https://travis-ci.org/channainfo/env_configuration.svg?branch=develop)](https://travis-ci.org/channainfo/env_configuration)
4
+
5
+ This gem aims to load your config into ENV. Currently configuration can be done via 3 different adapters
6
+
7
+ 1. Using .env powered by [dotenv](https://github.com/bkeepers/dotenv) gem. This is very popular in development environment.
8
+ 2. Using Yaml file config. As Yaml can be nested it is a bit more convenient than dotenv to separate setting between environment.
9
+ 3. Using [AWS System Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) - recommended for production.
10
+
11
+ How about container service link [Heroku](https://heroku.com/) and [ElasticBeanstalk](https://aws.amazon.com/elasticbeanstalk/)?
12
+
13
+ Heroku has an application settings for ENV which allows the config var to have up to 32kb max in size. ElasticBeanstalk has a similar approach by allowing you to set config var in the ElasticBeanstalk Settings, however[it allows only 4096bytes max for key, value combined](https://stackoverflow.com/questions/54344236/environmentvariables-default-value-length-is-greater-than-4096). This might cause issues for some application that integrate with many third parties.
14
+
15
+
16
+
17
+ ## Why Store config in the environment?
18
+
19
+ An app’s _config_ is everything that is likely to vary between [deploys](https://12factor.net/codebase) (staging, production, developer environments, etc). This includes:
20
+
21
+ - Resource handles to the database, Memcached, and other [backing services](https://12factor.net/backing-services)
22
+ - Credentials to external services such as Amazon S3 or Twitter
23
+ - Per-deploy values such as the canonical hostname for the deploy
24
+
25
+ Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires **strict separation of config from code**. Config varies substantially across deploys, code does not.
26
+
27
+ A litmus test for whether an app has all config correctly factored out of the code is whether the codebase could be made open source at any moment, without compromising any credentials.
28
+
29
+ ## Installation
30
+ ```ruby
31
+ gem 'env_configuration'
32
+
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ ```bash
38
+ $ bundle
39
+ ```
40
+
41
+
42
+ Or install it yourself as:
43
+
44
+
45
+ ```bash
46
+ $ gem install env_configuration
47
+ ```
48
+
49
+
50
+ ## Usage
51
+ There are 3 types of adapter :dot_env, :yaml, :aws_ssm_parameter_store.
52
+ ```ruby
53
+ EnvConfiguration::Configurator.load(:adapter_name, options={})
54
+ ```
55
+ Options value varies from adapter to adapter.
56
+
57
+ ### Configure gem
58
+ You can configure the gem with the following:
59
+ ```ruby
60
+ EnvConfiguration.configure do |config|
61
+ config.dot_env = { dot_env_file: 'config/app.env' }
62
+ config.yaml = { yaml_file: 'config/app.yaml'}
63
+ config.aws_ssm_parameter_store = { access_key_id: 'aws-key', secret_access_key: 'aws-secret', region: 'ap-southeast-1', path: '/staging'}
64
+ end
65
+ ```
66
+
67
+ ### DotEnv Adapter
68
+ Internally EnvConfiguration use [https://github.com/bkeepers/dotenv](https://github.com/bkeepers/dotenv) to handle this:
69
+
70
+ ```ruby
71
+ # Configuration (optional)
72
+ # EnvConfiguration.configure do |config|
73
+ # config.dot_env = { dot_env_file: 'config/app.env' }
74
+ # end
75
+
76
+ options = { dot_env_file: 'your-dotenv-app.env' }
77
+ EnvConfiguration::Configurator.load(:dot_env, options)
78
+ ```
79
+ If options is not provided then the :dot_env adapter will try to get the from the gem configuration. It still does not exist then it will load the .env file located in the root of the project.
80
+
81
+ ### Yaml adapter
82
+ Internally EnvConfiguration use 'yaml' library from ruby to handle this.
83
+ ```ruby
84
+ # Configuration (optional)
85
+ #EnvConfiguration.configure do |config|
86
+ # config.yaml = { section: 'default-dev', yaml_file: 'config/default-app.yml'}
87
+ #end
88
+
89
+ # optional if you configure the gem above.
90
+ options = { yaml_file: 'config/app.yaml', __FILE__), section: 'development' }
91
+ result = EnvConfiguration::Configurator.load(:yaml, options)
92
+ ```
93
+ Both :yaml_file and :section must exist . If you options is being specified EnvConfiguration will take value from options. otherwise gem configuration must provide the values.
94
+
95
+ if you miss to provide :yaml_file and :section the gem will raise the following errors:
96
+
97
+ - :yaml_file in the options{} is required, for example config/application.yml
98
+ - :section in the options{} is required, for example :staging, :test, :production
99
+
100
+ As an example value of yaml_file: [config/app.yml.](https://github.com/channainfo/env_configuration/blob/develop/spec/config/app.yaml) Sections here are (test, development, staging, production)
101
+ ```yml
102
+ default: &default
103
+ APP_NAME: "BookMeBus"
104
+ COMPANY_NAME: "Camtasia Technology"
105
+ APP_VERSION: "Development"
106
+ ENABLE_HTTPS: 'no'
107
+ ASSET_HOST_URL: 'http://localhost:3000'
108
+ HOST: 'http://localhost:3000'
109
+
110
+ test:
111
+ <<: *default
112
+
113
+ development:
114
+ <<: *default
115
+
116
+ staging:
117
+ <<: *default
118
+
119
+ production:
120
+ <<: *default
121
+ ```
122
+ ### AWS SSM Parameter Store
123
+
124
+ EnvConfiguration will fetch from the aws ssm parameter store service 10 key, value per request. If you have hundred it will ended up fetching as many requests until it finishes.
125
+
126
+ AWS Systems Manager Parameter Store provides secure, hierarchical storage for configuration data management and secrets management. You can store data such as passwords, database strings, and license codes as parameter values. You can store values as plain text or encrypted data. You can then reference values by using the unique name that you specified when you created the parameter. Highly scalable, available, and durable, Parameter Store is backed by the AWS Cloud.
127
+
128
+ Internally EnvConfiguration use aws-sdk-ssm gem to handle this.
129
+
130
+ ```ruby
131
+ # Configuration (optional)
132
+ #EnvConfiguration.configure do |config|
133
+ # options = { access_key_id: 'default-key-id', secret_access_key: 'default-secret-key', region: 'default-region', path: 'default-path' }
134
+ # config.aws_ssm_parameter_store = options
135
+ #end
136
+
137
+ options = {access_key_id: 'your-aws-key', secret_access_key: 'your-aws-secret', region: 'your-region' , path: '/staging'}
138
+ EnvConfiguration::Configurator.load(:aws_ssm_parameter_store, options)
139
+ ```
140
+ EnvConfiguration will use the options if options exists, otherwise it will use the options from the gem configuration above. The options will then be passed to aws-sdk-ssm.
141
+ EnvConfiguration gem will hand over the options to aws-sdk-ssm. Interesting aws-sdk-ssk will follows the rules as below:
142
+
143
+ ### Access key id and secret_access_key
144
+ :access_key_id, :secret_access_key are search for in the following locations:
145
+ - :access_key_id, :secret_access_key
146
+ - ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']
147
+ - ~/.aws/credentials
148
+ - ~/.aws/config
149
+
150
+ ### The Region
151
+ The region is search for in the following locations:
152
+ - :region
153
+ - ENV['AWS_REGION']
154
+ - ENV['AMAZON_REGION']
155
+ - ENV['AWS_DEFAULT_REGION']
156
+ - ~/.aws/credentials
157
+ - ~/.aws/config
158
+ For more details on the configuration [https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SSM/Client.html](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SSM/Client.html)
159
+
160
+
161
+ ### Aws policy for SSM Parameter Store
162
+ In order to be able to fetch the parameter store from aws ssm you need at least **ssm:GetParametersByPath** policy to attach to your IAM account
163
+ ```json
164
+ {
165
+ "Version": "2012-10-17",
166
+ "Statement": [
167
+ {
168
+ "Sid": "VisualEditor0",
169
+ "Effect": "Allow",
170
+ "Action": [
171
+ "ssm:PutParameter",
172
+ "ssm:GetParametersByPath",
173
+ "ssm:GetParameters",
174
+ "ssm:GetParameter"
175
+ ],
176
+ "Resource": "*"
177
+ }
178
+ ]
179
+ }
180
+ ```
181
+
182
+ ## Integrate with rails
183
+
184
+ ### Add Gem to application.rb
185
+ Add the following line
186
+ ```ruby
187
+ EnvConfiguration::Configurator.load(:dot_env)
188
+ ```
189
+ right below the
190
+ ```ruby
191
+ Bundler.require(*Rails.groups)
192
+ ```
193
+ in the config/application.rb as below:
194
+
195
+ ```ruby
196
+ require File.expand_path('boot', __dir__)
197
+ require 'rails/all'
198
+ # Require the gems listed in Gemfile, including any gems
199
+ # you've limited to :test, :development, or :production.
200
+ Bundler.require(*Rails.groups)
201
+ # EnvConfiguration
202
+ EnvConfiguration::Configurator.load(:dot_env)
203
+ ```
204
+ As this gem does not handle the Rails env, you might need to do this yourself in case you use different adapters for each Rails env:
205
+
206
+ ```ruby
207
+ require File.expand_path('boot', __dir__)
208
+
209
+ require 'rails/all'
210
+
211
+ # Require the gems listed in Gemfile, including any gems
212
+ # you've limited to :test, :development, or :production.
213
+ Bundler.require(*Rails.groups)
214
+
215
+ # EnvConfiguration
216
+ if(Rails.env.development?)
217
+ EnvConfiguration::Configurator.load(:dot_env)
218
+ elsif Rails.env.test?
219
+ EnvConfiguration::Configurator.load(:yaml, yaml_file: "#{Rails.root}/config/app-test.yml")
220
+ else
221
+ #separete aws ssm parameter store from staging with production /Rails.env
222
+ EnvConfiguration::Configurator.load(:aws_ssm_parameter_store, path: "/#{Rails.env}")
223
+ end
224
+ ```
225
+
226
+
227
+ ### Gem configuration in config/initializers/env_configuration.rb
228
+ ```ruby
229
+ EnvConfiguration.configure do |config|
230
+ # Dot Env
231
+ config.dot_env = { dot_env_file: 'config/app.env' }
232
+ # Yaml Config
233
+ config.yaml = { yaml_file: File.join(Rails.root, "config/application_#{Rails.env}.yaml" )}
234
+
235
+ # Aws ssm parameter store
236
+ config.aws_ssm_parameter_store = { access_key_id: ENV['AWS_ACCESS_KEY_ID'],
237
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
238
+ region: ENV['AWS_REGION'],
239
+ path: "/#{Rails.env}"}
240
+ end
241
+
242
+ ```
243
+
244
+
245
+
246
+
247
+
248
+
249
+ ## Development
250
+
251
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
252
+
253
+
254
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
255
+
256
+
257
+
258
+ ## Contributing
259
+
260
+ Bug reports and pull requests are welcome on GitHub at https://github.com/channainfo/env_configuration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
261
+
262
+
263
+
264
+ ## License
265
+
266
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
267
+
268
+