Initial commit

This commit is contained in:
Andrew Tomaka 2018-03-22 18:01:53 -04:00
commit 84383e8911
17 changed files with 394 additions and 0 deletions

11
.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
# rspec failure tracking
.rspec_status

3
.rspec Normal file
View file

@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper

5
.travis.yml Normal file
View file

@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.4.2
before_install: gem install bundler -v 1.16.1

8
Gemfile Normal file
View file

@ -0,0 +1,8 @@
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in parameter_store_loader.gemspec
gemspec name: "psenv"
gemspec name: "psenv-rails"

100
Gemfile.lock Normal file
View file

@ -0,0 +1,100 @@
PATH
remote: .
specs:
psenv (0.0.1)
aws-sdk-ssm (~> 1)
psenv-rails (0.0.1)
psenv (= 0.0.1)
railties (>= 3.2, < 5.2)
GEM
remote: https://rubygems.org/
specs:
actionpack (5.1.5)
actionview (= 5.1.5)
activesupport (= 5.1.5)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.1.5)
activesupport (= 5.1.5)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activesupport (5.1.5)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
aws-partitions (1.71.0)
aws-sdk-core (3.17.1)
aws-partitions (~> 1.0)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-ssm (1.8.0)
aws-sdk-core (~> 3)
aws-sigv4 (~> 1.0)
aws-sigv4 (1.0.2)
builder (3.2.3)
concurrent-ruby (1.0.5)
crass (1.0.3)
diff-lcs (1.3)
erubi (1.7.1)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
jmespath (1.3.1)
loofah (2.2.2)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (0.9.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
rack (2.0.4)
rack-test (0.8.3)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.1.5)
actionpack (= 5.1.5)
activesupport (= 5.1.5)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.5.0)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
thor (0.20.0)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.16)
psenv!
psenv-rails!
rake (~> 10.0)
rspec (~> 3.0)
BUNDLED WITH
1.16.1

21
LICENSE.txt Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 Andrew Tomaka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

78
README.md Normal file
View file

@ -0,0 +1,78 @@
# Psenv
**Work in progress**
Shim to load environment variables from [AWS Systems Manager Parameter Store](https://aws.amazon.com/systems-manager/features/#Parameter_Store) into ENV.
Psenv currently heavily borrows from [Dotenv](https://github.com/bkeepers/dotenv), mainly because I use it in roughly every project so it made since for the APIs to match.
## Installation
### Rails
Add this line to your application's Gemfile:
```ruby
gem 'psenv-rails'
```
And then execute:
$ bundle
Set the `PARAMETER_STORE_PATH` environment variable with the AWS Parameter
Store path that you wish to load.
### Plain Ruby
Add this line to your application's Gemfile:
```ruby
gem 'psenv'
```
And then execute:
$ bundle
Set the `PARAMETER_STORE_PATH` environment variable with the AWS Parameter
Store path that you wish to load.
Finally, trigger the loading:
```ruby
require 'psenv'
Psenv.load
```
## Usage
* Create a variable in parameter store using the AWS console or the CLI
```
aws ssm put-parameter \
--name /psenv/test/API_KEY \
--value "api_key_value" \
--type String
```
* Ensure your application has at least the following IAM permissions
* `ssm:GetParametersByPath` on resource `arn:aws:ssm:::parameter/psenv/test/*`
* Set the `PARAMETER_STORE_PATH` environment variable to `/psenv/test/`
This example will set the `API_KEY` to `api_key_value` and make it available to
your application.
## Development
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.
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).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/atomaka/psenv.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

24
Rakefile Normal file
View file

@ -0,0 +1,24 @@
require "bundler/gem_helper"
require "rspec/core/rake_task"
namespace "psenv" do
Bundler::GemHelper.install_tasks name: "psenv"
end
namespace "psenv-rails" do
class ParameterStoreEnvRailsGemHelper < Bundler::GemHelper
def guard_already_tagged; end # noop
def tag_version; end # noop
end
ParameterStoreEnvRailsGemHelper.install_tasks name: "psenv-rails"
end
task build: ["psenv:build", "psenv-rails:build"]
task install: ["psenv:install", "psenv-rails:install"]
task release: ["psenv:release", "psenv-rails:release"]
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

14
bin/console Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require "bundler/setup"
require "parameter_store_loader"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)

8
bin/setup Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

20
lib/psenv-rails.rb Normal file
View file

@ -0,0 +1,20 @@
require "psenv"
if defined?(Rake.application)
is_running_specs = Rake.application.top_level_tasks.grep(/^spec(:|$)/).any?
Rails.env = ENV["RAILS_ENV"] ||= "test" if is_running_specs
end
module Psenv
class Railtie < Rails::Railtie
def load
Psenv.load
end
def self.load
instance.load
end
config.before_configuration { load }
end
end

23
lib/psenv.rb Normal file
View file

@ -0,0 +1,23 @@
require "psenv/version"
require "aws-sdk-ssm"
module Psenv
module_function
def load
if ENV["PARAMETER_STORE_PATH"] != nil
ssm = Aws::SSM::Client.new
ssm.
get_parameters_by_path(
path: ENV["PARAMETER_STORE_PATH"],
with_decryption: true,
).
parameters.
each do |param|
ENV.store(param.name.split("/").last, param.value)
end
end
end
end

3
lib/psenv/version.rb Normal file
View file

@ -0,0 +1,3 @@
module Psenv
VERSION = "0.0.1"
end

27
psenv-rails.gemspec Normal file
View file

@ -0,0 +1,27 @@
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "psenv/version"
Gem::Specification.new do |spec|
spec.name = "psenv-rails"
spec.version = Psenv::VERSION
spec.authors = ["Andrew Tomaka"]
spec.email = ["atomaka@gmail.com"]
spec.summary = %q{Load AWS SSM Parameter Store values into your Rails environment.}
spec.homepage = "https://github.com/atomaka/psenv"
spec.license = "MIT"
spec.files = `git ls-files lib | grep rails`.
split($OUTPUT_RECORD_SEPARATOR).
reject do |f|
f.match(%r{^(test|spec|features)/})
end + ["README.md", "LICENSE.txt"]
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "psenv", Psenv::VERSION
spec.add_dependency "railties", ">= 3.2", "< 5.2"
end

28
psenv.gemspec Normal file
View file

@ -0,0 +1,28 @@
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "psenv/version"
Gem::Specification.new do |spec|
spec.name = "psenv"
spec.version = Psenv::VERSION
spec.authors = ["Andrew Tomaka"]
spec.email = ["atomaka@gmail.com"]
spec.summary = %q{Load AWS SSM Parameter Store values into your environment.}
spec.homepage = "https://github.com/atomaka/psenv"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_dependency "aws-sdk-ssm", "~> 1"
end

7
spec/psenv_spec.rb Normal file
View file

@ -0,0 +1,7 @@
RSpec.describe Psenv do
it "has a version number" do
expect(Psenv::VERSION).not_to be nil
end
it "tests something useful"
end

14
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,14 @@
require "bundler/setup"
require "psenv"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
end