Skip to content

Commit

Permalink
Merge pull request #6 from autolist/f-before-init
Browse files Browse the repository at this point in the history
feat: Before Initialize
  • Loading branch information
mtchavez authored May 24, 2018
2 parents 962eba2 + cec0c8f commit d09946b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
## v0.2.0 (2018-05-24)

- Remove dependency on Rails [pr #5][pr5]
- Add Yard Documentation
- Use `before_initialize` for railtie

## v0.1.3 (2018-05-01)

- Capture exceptions and use fallback when getting secrets values

## v0.1.1 (2018-04-27)

- Initial release of gem

[pr5]: https://github.com/autolist/sekreto/pull/5
12 changes: 8 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sekreto (0.1.3)
sekreto (0.2.0)
aws-sdk-secretsmanager
multi_json

Expand All @@ -12,12 +12,14 @@ GEM
autocop (0.1.4)
rubocop (>= 0.52.1)
rubocop-rspec (>= 1.22.1)
aws-partitions (1.86.0)
aws-sdk-core (3.20.2)
aws-eventstream (1.0.0)
aws-partitions (1.87.0)
aws-sdk-core (3.21.2)
aws-eventstream (~> 1.0)
aws-partitions (~> 1.0)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-secretsmanager (1.4.0)
aws-sdk-secretsmanager (1.5.0)
aws-sdk-core (~> 3)
aws-sigv4 (~> 1.0)
aws-sigv4 (1.0.2)
Expand Down Expand Up @@ -61,6 +63,7 @@ GEM
stub_env (1.0.4)
rspec (>= 2.0, < 4.0)
unicode-display_width (1.3.2)
yard (0.9.12)

PLATFORMS
ruby
Expand All @@ -74,6 +77,7 @@ DEPENDENCIES
rspec-mocks (~> 3.0)
sekreto!
stub_env (~> 1.0)
yard (~> 0.9.11)

BUNDLED WITH
1.16.1
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require 'bundler'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'yard'

Bundler::GemHelper.install_tasks

Expand All @@ -19,3 +20,8 @@ desc 'Run specs'
RSpec::Core::RakeTask.new('spec') do |task|
task.pattern = 'spec/**/*_spec.rb'
end

YARD::Rake::YardocTask.new(:doc) do |task|
task.files = %w[lib/**/*.rb - README.md]
task.options = %w[no-private]
end
30 changes: 29 additions & 1 deletion lib/sekreto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@

require 'sekreto/railtie' if defined?(Rails)

# Sekreto holds configuration and interface for getting secrets
# Sekreto allows you to interact with the AWS Secrets Manager to
# retrieve your secrets from Ruby. This exposes a thin wrapper around
# the AWS SecretsManager SDK.
module Sekreto
class << self
##
# Set up the Sekreto gem configuration
#
# @yieldreturn [Sekreto::Config]
#
# @example Configuring Sekreto
# Sekreto.setup do |setup|
# setup.prefix = 'app-prefix'
# end
def setup
yield config if block_given?
end

##
#
# Get the value given a secret
#
# @param secret_id [String] - The secret ID to get the value for
# @return [String] - The value of the stored secret
def get_value(secret_id)
fail 'Not allowed env' unless config.is_allowed_env.call
response = secrets_manager.get_secret_value(secret_id: secret_name(secret_id))
Expand All @@ -22,11 +39,22 @@ def get_value(secret_id)
config.fallback_lookup.call(secret_id)
end

##
#
# Get the JSON value of a secret
#
# @param secret_id [String] - The secret ID to get the value for
# @return [Hash] - The parsed JSON value of the secret
def get_json_value(secret_id)
response = get_value(secret_id)
MultiJson.load(response)
end

##
#
# Get the configuration for Sekreto
#
# @return [Sekreto::Config] - The configuration
def config
@config ||= Config.new
end
Expand Down
7 changes: 7 additions & 0 deletions lib/sekreto/config.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
require 'logger'

module Sekreto
##
# Config class for setting up Sekreto for usage
class Config
attr_accessor :prefix
attr_accessor :is_allowed_env
attr_accessor :fallback_lookup
attr_accessor :secrets_manager
attr_accessor :logger

##
#
# Initialize a new Config
#
# @return [Sekreto::Config]
def initialize
@prefix = 'secrets'
@is_allowed_env = -> { true }
Expand Down
6 changes: 5 additions & 1 deletion lib/sekreto/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Sekreto
# Rails Railtie to set up the Sekreto configuration
#
# Defaults the secrets Manager to Aws::SecretsManager::Client.new
# Defaults the prefix to Rails app name and Rails.env e.g. foo-staging
# Defaults allowed envs to check Rails.env is production or staging
class Railtie < ::Rails::Railtie
config.after_initialize do
config.before_initialize do
app_name = ::Rails.application.class.to_s.split('::').first.downcase

Sekreto.setup do |setup|
Expand Down
2 changes: 1 addition & 1 deletion lib/sekreto/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sekreto
VERSION = '0.1.3'.freeze
VERSION = '0.2.0'.freeze
end
1 change: 1 addition & 0 deletions sekreto.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rspec-mocks', '~> 3.0'
spec.add_development_dependency 'stub_env', '~> 1.0'
spec.add_development_dependency 'yard', '~> 0.9.11'
end

0 comments on commit d09946b

Please sign in to comment.