env_configuration 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e16758211998964af2449e93f0d2752ca8a151e7
4
+ data.tar.gz: 33f121edd8f7bcf3a49cbdad4e7e81c98a0ad00f
5
+ SHA512:
6
+ metadata.gz: e8bce5b57819d92dae03853a32772999c4bf80104dee81367e7d8074532d5b3f61dd3db1d059e77d392c8d668abd02b91a6fbc2bcf880283070a0884f52bda4c
7
+ data.tar.gz: 59348900392be1747750635cd405e93d7ed1300edf59329a4f90f08a8b5ac0c2a3d317cdfa287ae9035c2f8d943ca6f6140217c4b85ea36d1013e60557d3b503
data/.env ADDED
@@ -0,0 +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'
6
+ HOST='http://DotRoot.local'
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at channa.info@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in env_configuration.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +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
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Channa Ly
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,289 @@
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).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "env_configuration"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "env_configuration/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "env_configuration"
8
+ spec.version = EnvConfiguration::VERSION
9
+ spec.authors = ["Channa Ly"]
10
+ spec.email = ["channa.info@gmail.com"]
11
+
12
+ spec.summary = %q{
13
+ A gem to config env variable in development in dotenv, yaml, aws ssm parameter store, heroku in ruby and rails project.
14
+ }
15
+ spec.description = %q{
16
+ This gem aims to load config to ENV varaible easily. Currently configuration can be done by a .env, a yaml file or by loading from
17
+ AWS System Manager Parameter Store which is recommended for production deployment.
18
+
19
+ }
20
+ spec.homepage = "https://github.com/channainfo/env_configuration"
21
+ spec.license = "MIT"
22
+
23
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
24
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
25
+ # if spec.respond_to?(:metadata)
26
+ # spec.metadata["allowed_push_host"] = "http://mygemserver.com"
27
+ # else
28
+ # raise "RubyGems 2.0 or newer is required to protect against " \
29
+ # "public gem pushes."
30
+ # end
31
+
32
+ # Specify which files should be added to the gem when it is released.
33
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
34
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
35
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
36
+ end
37
+ spec.bindir = "exe"
38
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
+ spec.require_paths = ["lib"]
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.16"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+
45
+ spec.add_dependency "aws-sdk-ssm"
46
+ spec.add_dependency "dotenv-rails", "2.7.4"
47
+ end
@@ -0,0 +1,27 @@
1
+ require "env_configuration/version"
2
+ require "env_configuration/configurator"
3
+ require "env_configuration/configuration"
4
+ require "env_configuration/aws_ssm_parameter_store_writer"
5
+
6
+ module EnvConfiguration
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield(self.configuration)
20
+ self.configuration
21
+ end
22
+
23
+ def self.reset_configuration
24
+ @configuration = Configuration.new
25
+ end
26
+
27
+ end
@@ -0,0 +1,91 @@
1
+ module EnvConfiguration
2
+ module Adapter
3
+ class AwsSsmParameterStoreAdapter < Base
4
+
5
+ # if you set ENV['AWS_ACCESS_KEY_ID'],ENV['AWS_SECRET_ACCESS_KEY'], ENV['AWS_REGION']
6
+ # you don't need to pass the options
7
+ # { access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION']
8
+ def initialize(options={})
9
+ super(options)
10
+
11
+ end
12
+
13
+ def client
14
+ aws_options = {
15
+ access_key_id: access_key_id,
16
+ secret_access_key: secret_access_key,
17
+ region: region,
18
+ }
19
+ @client ||= Aws::SSM::Client.new(@options)
20
+ end
21
+
22
+ def access_key_id
23
+ @options[:access_key_id] || EnvConfiguration.configuration.aws_access_key_id
24
+ end
25
+
26
+ def secret_access_key
27
+ @options[:secret_access_key] || EnvConfiguration.configuration.aws_secret_access_key
28
+ end
29
+
30
+ def region
31
+ @options[:region] || EnvConfiguration.configuration.aws_region
32
+ end
33
+
34
+ def path
35
+ path_value = @options[:path] || EnvConfiguration.configuration.aws_path
36
+ raise ":path options is required for example /staging" if path_value.nil?
37
+ path_value
38
+ end
39
+
40
+ def load
41
+ # fetch_configs do |item|
42
+ # ENV["#{item[:name]}"] = item[:value]
43
+ # end
44
+
45
+ configs = fetch_configs
46
+ ENV.update(configs)
47
+ configs
48
+ end
49
+
50
+ def fetch_configs(&block)
51
+ next_token = nil
52
+ result = {}
53
+
54
+ while(true)
55
+
56
+ fetch_options = {
57
+ path: path,
58
+ with_decryption: false,
59
+ recursive: true,
60
+ max_results: 10
61
+ }
62
+
63
+ fetch_options[:next_token] = next_token if next_token
64
+
65
+ response = client.get_parameters_by_path(fetch_options)
66
+
67
+ items = sanitize_configs(response.parameters, &block)
68
+ result.merge!(items)
69
+
70
+ next_token = response.next_token
71
+ break if next_token.nil?
72
+ end
73
+
74
+ result
75
+ end
76
+
77
+ def sanitize_configs(parameters, &block)
78
+ items = {}
79
+ parameters.each do |param|
80
+ name = param.name.gsub("#{path}/", '')
81
+ value = param.value
82
+ item = {name: name, value: value}
83
+ items[name] = value
84
+ yield(item) if block_given?
85
+ end
86
+ items
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,15 @@
1
+ module EnvConfiguration
2
+ module Adapter
3
+ attr_accessor :options
4
+
5
+ class Base
6
+ def initialize(options={})
7
+ @options = options.clone
8
+ end
9
+
10
+ def update_env_variable_with(configs)
11
+ ENV.update(configs)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require 'dotenv'
2
+
3
+ module EnvConfiguration
4
+ module Adapter
5
+ class DotEnvAdapter < Base
6
+
7
+ def initialize(options={})
8
+ super(options)
9
+ end
10
+
11
+ def file
12
+ @options[:dot_env_file] || EnvConfiguration.configuration.dot_env_file
13
+ end
14
+
15
+ def load
16
+ p file
17
+ if file.nil?
18
+ Dotenv.load
19
+ else
20
+ Dotenv.load(file)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ require 'yaml'
2
+
3
+ module EnvConfiguration
4
+ module Adapter
5
+ class YamlAdapter < Base
6
+
7
+ def initialize(options = {})
8
+ super(options)
9
+ end
10
+
11
+ def section
12
+ section_value = @options[:section] || EnvConfiguration.configuration.yaml_section
13
+ raise ":section in the options{} is required, for example :staging, :test, :production" if section_value.nil?
14
+ section_value
15
+ end
16
+
17
+ def yaml_file
18
+ yaml_file_value = @options[:yaml_file] || EnvConfiguration.configuration.yaml_file
19
+ raise ":yaml_file in the options{} is required, for example config/application.yml" if yaml_file_value.nil?
20
+ yaml_file_value
21
+ end
22
+
23
+ def load
24
+ configs = fetch_configs
25
+ update_env_variable_with(configs)
26
+ configs
27
+ end
28
+
29
+ def fetch_configs
30
+ ::YAML.load_file(yaml_file)[section]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,48 @@
1
+ require 'aws-sdk-ssm'
2
+
3
+ module EnvConfiguration
4
+ class AwsSsmParameterStoreWriter
5
+ attr_accessor :env_name #allow to reset env but not region
6
+
7
+ # if you set ENV['AWS_ACCESS_KEY_ID'],ENV['AWS_SECRET_ACCESS_KEY'], ENV['AWS_REGION']
8
+ # you don't need to pass the options
9
+ # { access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION']
10
+ def initialize(env_name, options = {})
11
+ @env_name = env_name
12
+ @options = options
13
+ end
14
+
15
+ def client
16
+ @client ||= Aws::SSM::Client.new(@options)
17
+ end
18
+
19
+ def put_configs_from_yaml_file(config_yml)
20
+ configs = YAML.load_file(config_yml)[@env_name]
21
+ put_configs(configs)
22
+ end
23
+
24
+ def put_configs(configs)
25
+ configs.each do |key, value|
26
+ put_config(key, value) if !value.empty? #aws don't allow value to have empty value
27
+ end
28
+ end
29
+
30
+ def put_config(name, value, type='String')
31
+ Rails.logger.info { "preparing: #{name}=#{value}" }
32
+
33
+ param_name = "/#{@env_name}/#{name}"
34
+
35
+ options = {
36
+ name: param_name, # required
37
+ value: value, # required
38
+ type: type, # required, accepts String, StringList, SecureString
39
+ overwrite: true,
40
+ tier: "Standard", # accepts Standard, Advanced
41
+ }
42
+ response = client.put_parameter(options)
43
+
44
+ Rails.logger.info { "setting: #{options}" }
45
+ response
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ module EnvConfiguration
2
+
3
+ class Configuration
4
+ attr_accessor :dot_env, :aws_ssm_parameter_store, :yaml
5
+
6
+ # ------ :dot_env ---------
7
+ # :dot_env_file
8
+
9
+ def dot_env_file
10
+ dot_env && dot_env[:dot_env_file]
11
+ end
12
+
13
+ # ------ :yaml ---------
14
+ # :yaml_file, :section
15
+ def yaml_file
16
+ yaml && yaml[:yaml_file]
17
+ end
18
+
19
+ def yaml_section
20
+ yaml && yaml[:section]
21
+ end
22
+
23
+ # ------ :aws_ssm_paramer_store ---------
24
+ # :access_key_id, :secret_access_key, :region, :path
25
+ def aws_access_key_id
26
+ aws_ssm_parameter_store && aws_ssm_parameter_store[:access_key_id]
27
+ end
28
+
29
+ def aws_secret_access_key
30
+ aws_ssm_parameter_store && aws_ssm_parameter_store[:secret_access_key]
31
+ end
32
+
33
+ def aws_region
34
+ aws_ssm_parameter_store && aws_ssm_parameter_store[:region]
35
+ end
36
+
37
+ def aws_path
38
+ aws_ssm_parameter_store && aws_ssm_parameter_store[:path]
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,36 @@
1
+ module EnvConfiguration
2
+
3
+ class Configurator
4
+ def self.load(adapter_name, options={})
5
+ if adapter_name.class == Symbol
6
+ loader = self.adapter(adapter_name, options)
7
+ loader.load
8
+ else
9
+ adapter_name.load
10
+ end
11
+ end
12
+
13
+ def self.adapter(adapter_name, options={})
14
+ case adapter_name
15
+ when :dot_env
16
+ require "env_configuration/adapter/base"
17
+ require "env_configuration/adapter/dot_env_adapter"
18
+ Adapter::DotEnvAdapter.new(options)
19
+ when :aws_ssm_parameter_store
20
+ require 'aws-sdk-ssm'
21
+ require "env_configuration/adapter/base"
22
+ require "env_configuration/adapter/aws_ssm_parameter_store_adapter"
23
+ require "env_configuration/aws_ssm_parameter_store_writer"
24
+ Adapter::AwsSsmParameterStoreAdapter.new(options)
25
+ when :yaml
26
+ require "env_configuration/adapter/base"
27
+ require "env_configuration/adapter/yaml_adapter"
28
+ Adapter::YamlAdapter.new(options)
29
+ else
30
+ existings = [:dot_env, :yaml, :aws_ssm_parameter_store]
31
+ raise "adapter :#{adapter_name} must be one of the following #{existings}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,3 @@
1
+ module EnvConfiguration
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: env_configuration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Channa Ly
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk-ssm
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.7.4
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.4
83
+ description: "\n This gem aims to load config to ENV varaible easily. Currently
84
+ configuration can be done by a .env, a yaml file or by loading from \n AWS System
85
+ Manager Parameter Store which is recommended for production deployment.\n\n "
86
+ email:
87
+ - channa.info@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".env"
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".travis.yml"
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - env_configuration.gemspec
105
+ - lib/env_configuration.rb
106
+ - lib/env_configuration/adapter/aws_ssm_parameter_store_adapter.rb
107
+ - lib/env_configuration/adapter/base.rb
108
+ - lib/env_configuration/adapter/dot_env_adapter.rb
109
+ - lib/env_configuration/adapter/yaml_adapter.rb
110
+ - lib/env_configuration/aws_ssm_parameter_store_writer.rb
111
+ - lib/env_configuration/configuration.rb
112
+ - lib/env_configuration/configurator.rb
113
+ - lib/env_configuration/version.rb
114
+ homepage: https://github.com/channainfo/env_configuration
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A gem to config env variable in development in dotenv, yaml, aws ssm parameter
138
+ store, heroku in ruby and rails project.
139
+ test_files: []