commit 84383e8911759d8a38252200ae05550d6c691bc9 Author: Andrew Tomaka Date: Thu Mar 22 18:01:53 2018 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..06bf19c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +sudo: false +language: ruby +rvm: + - 2.4.2 +before_install: gem install bundler -v 1.16.1 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7525581 --- /dev/null +++ b/Gemfile @@ -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" + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..07ae230 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..27ef974 --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7db9e6b --- /dev/null +++ b/README.md @@ -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). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c1e9fa1 --- /dev/null +++ b/Rakefile @@ -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 diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..b91b269 --- /dev/null +++ b/bin/console @@ -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__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -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 diff --git a/lib/psenv-rails.rb b/lib/psenv-rails.rb new file mode 100644 index 0000000..1841158 --- /dev/null +++ b/lib/psenv-rails.rb @@ -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 diff --git a/lib/psenv.rb b/lib/psenv.rb new file mode 100644 index 0000000..6355792 --- /dev/null +++ b/lib/psenv.rb @@ -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 diff --git a/lib/psenv/version.rb b/lib/psenv/version.rb new file mode 100644 index 0000000..5073514 --- /dev/null +++ b/lib/psenv/version.rb @@ -0,0 +1,3 @@ +module Psenv + VERSION = "0.0.1" +end diff --git a/psenv-rails.gemspec b/psenv-rails.gemspec new file mode 100644 index 0000000..88bd0c0 --- /dev/null +++ b/psenv-rails.gemspec @@ -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 diff --git a/psenv.gemspec b/psenv.gemspec new file mode 100644 index 0000000..e4f442b --- /dev/null +++ b/psenv.gemspec @@ -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 diff --git a/spec/psenv_spec.rb b/spec/psenv_spec.rb new file mode 100644 index 0000000..b9a5d3f --- /dev/null +++ b/spec/psenv_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..9f2da4e --- /dev/null +++ b/spec/spec_helper.rb @@ -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