shadow_db_credentials 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ shadow_db_credentials
@@ -0,0 +1 @@
1
+ 2.0.0
data/CHANGES CHANGED
@@ -2,4 +2,12 @@
2
2
 
3
3
  == Version 1.0.0
4
4
 
5
- * Initial release.
5
+ * Initial release.
6
+
7
+ == Version 1.0.3
8
+
9
+ * Bug fixes.
10
+
11
+ == Version 1.1.0
12
+
13
+ * Small API and build changes; writing documentation
data/Gemfile CHANGED
@@ -1,8 +1,4 @@
1
- source :rubygems
2
-
3
- group :default do
4
-
5
- end
1
+ source 'https://rubygems.org'
6
2
 
7
3
  group :development do
8
4
  gem "gemspec_deps_gen"
@@ -10,6 +6,11 @@ group :development do
10
6
  end
11
7
 
12
8
  group :test do
13
- gem "rspec", "2.9.0"
14
- gem "mocha", "0.10.3"
9
+ gem "rspec"
10
+ gem "mocha"
11
+ end
12
+
13
+ group :debug do
14
+ #gem "ruby-debug-base19x", "0.11.30.pre12"
15
+ gem "ruby-debug-ide", "0.4.17"
15
16
  end
data/README.md CHANGED
@@ -1,4 +1,126 @@
1
- shadow_db_credentials
2
- =====================
1
+ # Shadow DB Credentials
2
+
3
+ ## Helps to keep DB credentials for rails app in private place
4
+
5
+ ## Installation
6
+
7
+ Add this line to to your Gemfile:
8
+
9
+ gem "shadow_db_credentials"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Create "shadowed" credentials file (**your_prod_credentials**) inside some directory (e.g. **~/.credentials**):
18
+
19
+ ```yml
20
+ username: your_username
21
+ password: your_password
22
+ ```
23
+
24
+ The location of your **credentials directory** is controlled by **CREDENTIALS_DIR** environment
25
+ variable. Register it in **config/initializers/env_variables.rb** file:
26
+
27
+ ```ruby
28
+ ENV['CREDENTIALS_DIR'] ||= "#{ENV['HOME']}/.credentials"
29
+ ```
30
+
31
+ If you want to have different **credentials directory** per environment, define it in corresponding env file:
32
+
33
+ ```ruby
34
+ # config/environments/development.rb
35
+ ...
36
+ ENV['CREDENTIALS_DIR'] ||= "#{ENV['HOME']}/.credentials"
37
+ ```
38
+
39
+ Remove all the credentials (username/password) that you don't want to keep inside **config/database.yml**
40
+ and replace them with single **credentials** attriubute. It points to the name of credentials file
41
+ (**your_prod_credentials**):
42
+
43
+ ```yml
44
+ development:
45
+ adapter: postgresql
46
+ database: your_dev_db
47
+ credentials: your_dev_credentials
48
+
49
+ production:
50
+ adapter: postgresql
51
+ database: your_prod_db
52
+ credentials: your_prod_credentials
53
+ ```
54
+
55
+ If you want, you can also move other sensitive information, such as database name, url etc.
56
+
57
+ Next, you have to create code hook inside rails **config/application.rb** in order to call gem's API:
58
+
59
+ ```ruby
60
+ require 'shadow_db_credentials'
61
+
62
+ ...
63
+
64
+ module YourRailsApp
65
+ class Application < Rails::Application
66
+ ...
67
+
68
+ def config.database_configuration
69
+ orig_db_configurations = super
70
+
71
+ processor = ShadowDbCredentials.new(ENV['CREDENTIALS_DIR'])
72
+
73
+ processor.process_configurations(orig_db_configurations)
74
+ end
75
+ end
76
+ end
77
+ ```
78
+
79
+ The hook will access original DB configuration and try to expand all **credentials** attributes
80
+ with corresponding values dynamically, at run time.
81
+
82
+ You can check result of your work:
83
+
84
+ ```bash
85
+ $ rails console production
86
+
87
+ > ActiveRecord::Base.configurations["production"]
88
+ => {"adapter"=>"postgresql", "username"=>"your_username", "password"=>"your_password"}
89
+
90
+ > Rails.application.config.database_configuration['production']
91
+ => {"adapter"=>"postgresql", "username"=>"your_username", "password"=>"your_password"}
92
+ ```
93
+
94
+ Or you can get it with API call:
95
+
96
+ ```ruby
97
+ require 'shadow_db_credentials'
98
+
99
+ credentials_dir = ENV['CREDENTIALS_DIR']
100
+ processor = ShadowDbCredentials.new(credentials_dir)
101
+
102
+ # 1. get production hash, read configuration from default location
103
+
104
+ connection_hash1 = processor.retrieve_configuration "production"
105
+ connection_hash1.inspect
106
+
107
+ # 2. get development hash, read configuration from dynamic source
108
+
109
+ source = StringIO.new <<-TEXT
110
+ development:
111
+ adapter: postgresql
112
+ credentials: your_dev_db
113
+ TEXT
114
+
115
+ connection_hash2 = processor.retrieve_configuration 'development', source
116
+ connection_has2.inspect
117
+ ```
118
+
119
+ ## Contributing
120
+
121
+ 1. Fork it
122
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
123
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
124
+ 4. Push to the branch (`git push origin my-new-feature`)
125
+ 5. Create new Pull Request
3
126
 
4
- Class that helps to keep database credentials for rails application in private place
data/Rakefile CHANGED
@@ -6,20 +6,25 @@ require "rspec/core/rake_task"
6
6
  require "shadow_db_credentials/version"
7
7
  require "gemspec_deps_gen/gemspec_deps_gen"
8
8
 
9
- def version
10
- ShadowDbCredentials::VERSION
11
- end
9
+ version = ShadowDbCredentials::VERSION
10
+ project_name = File.basename(Dir.pwd)
11
+
12
+ task :gen do
13
+ generator = GemspecDepsGen.new project_name
12
14
 
13
- def project_name
14
- File.basename(Dir.pwd)
15
+ generator.generate_dependencies "spec", "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
15
16
  end
16
17
 
17
- task :build do
18
- generator = GemspecDepsGen.new
18
+ task :build => :gen do
19
+ system "gem build #{project_name}.gemspec"
20
+ end
19
21
 
20
- generator.generate_dependencies "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
22
+ task :install do
23
+ system "gem install #{project_name}-#{version}.gem"
24
+ end
21
25
 
22
- system "gem build #{project_name}.gemspec"
26
+ task :uninstall do
27
+ system "gem uninstall #{project_name}"
23
28
  end
24
29
 
25
30
  task :release => :build do
@@ -31,4 +36,3 @@ RSpec::Core::RakeTask.new do |task|
31
36
  task.verbose = false
32
37
  end
33
38
 
34
-
@@ -1 +1,2 @@
1
+ require "shadow_db_credentials/version"
1
2
  require "shadow_db_credentials/shadow_db_credentials"
@@ -1,3 +1,5 @@
1
+ require 'erb'
2
+
1
3
  class ShadowDbCredentials
2
4
 
3
5
  def initialize credentials_dir
@@ -9,12 +11,12 @@ class ShadowDbCredentials
9
11
 
10
12
  template = ERB.new file.read
11
13
 
12
- config = YAML.load(template.result(binding))
14
+ config = indifferent_access(YAML.load(template.result(binding)))
13
15
 
14
- process_credentials(config[rails_env])
16
+ process_credentials(config[rails_env.to_sym])
15
17
  end
16
18
 
17
- def process_all_configurations configurations
19
+ def process_configurations configurations
18
20
  new_configurations = {}
19
21
 
20
22
  configurations.each do |name, params|
@@ -24,24 +26,16 @@ class ShadowDbCredentials
24
26
  new_configurations
25
27
  end
26
28
 
27
- def process_configuration configurations, rails_env
28
- new_configurations = configurations.clone
29
-
30
- new_configurations[rails_env] = process_credentials(configurations[rails_env])
31
-
32
- new_configurations
33
- end
34
-
35
29
  private
36
30
 
37
31
  def process_credentials params
38
- if params['credentials']
39
- credentials = params.delete('credentials')
32
+ if params[:credentials]
33
+ credentials = params.delete(:credentials)
40
34
 
41
- file_name = "#@credentials_dir/#{credentials}"
35
+ file_name = "#{@credentials_dir}/#{credentials}"
42
36
 
43
37
  if File.exist? file_name
44
- params.merge!(YAML.load_file(file_name))
38
+ params.merge!(indifferent_access(YAML.load_file(file_name)))
45
39
  else
46
40
  puts "Missing credentials file: #{file_name}."
47
41
  end
@@ -49,5 +43,20 @@ class ShadowDbCredentials
49
43
 
50
44
  params
51
45
  end
46
+
47
+ def indifferent_access hash
48
+ case hash
49
+ when Hash
50
+ Hash[
51
+ hash.map do |k, v|
52
+ [ k.respond_to?(:to_sym) ? k.to_sym : k, indifferent_access(v) ]
53
+ end
54
+ ]
55
+ when Enumerable
56
+ hash.map { |v| indifferent_access(v) }
57
+ else
58
+ hash
59
+ end
60
+ end
52
61
  end
53
62
 
@@ -1,3 +1,3 @@
1
- module ShadowDbCredentials
2
- VERSION = "1.0.2"
1
+ class ShadowDbCredentials
2
+ VERSION = "1.1.0"
3
3
  end
@@ -17,8 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
  spec.version = ShadowDbCredentials::VERSION
19
19
 
20
+
20
21
  spec.add_development_dependency "gemspec_deps_gen", [">= 0"]
21
22
  spec.add_development_dependency "gemcutter", [">= 0"]
22
-
23
+
23
24
  end
24
25
 
@@ -17,6 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
  spec.version = ShadowDbCredentials::VERSION
19
19
 
20
- <%= include_dependencies %>
20
+ <%= project_dependencies %>
21
21
  end
22
22
 
@@ -4,17 +4,38 @@ require 'shadow_db_credentials'
4
4
  require 'yaml'
5
5
 
6
6
  describe ShadowDbCredentials do
7
- it "should read credentials from the file" do
8
- YAML.expects(:load_file).with("#{ENV['HOME']}/.credentials/rails_app_tmpl").returns(
9
- {"url" => "some_url", "username" => "username", "password" => "password"}
7
+ before do
8
+ @credentials_dir = 'some_credentials_dir'
9
+
10
+ credentials_file_name = "#{@credentials_dir}/your_dev_db"
11
+
12
+ YAML.expects(:load_file).with(credentials_file_name).returns(
13
+ {url: "some_url", username: "username", password: "password"}
10
14
  )
15
+ File.expects(:exist? => true).with(credentials_file_name)
16
+ end
17
+
18
+ subject do
19
+ ShadowDbCredentials.new(@credentials_dir)
20
+ end
21
+
22
+ it "should process credentials attrubute" do
23
+ configurations = {production: { adapter: 'oracle_enhanced', credentials: 'your_dev_db'}}
11
24
 
12
- subject = ShadowDbCredentials.new("#{ENV['HOME']}/.credentials")
25
+ result = subject.process_configurations configurations
26
+
27
+ result[:production][:url].should == 'some_url'
28
+ end
13
29
 
14
- configurations = {'production' => { 'adapter' => 'oracle_enhanced', 'credentials' => 'rails_app_tmpl'}}
30
+ it "should retrieve given configuration" do
31
+ source = StringIO.new <<-TEXT
32
+ development:
33
+ adapter: postgresql
34
+ credentials: your_dev_db
35
+ TEXT
15
36
 
16
- result = subject.process_all_configurations configurations
37
+ result = subject.retrieve_configuration "development", source
17
38
 
18
- result["production"]["url"].should == 'some_url'
39
+ result[:development][:url].should == 'some_url'
19
40
  end
20
- end
41
+ end
@@ -1,14 +1,5 @@
1
1
  $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
2
 
3
3
  RSpec.configure do |config|
4
- # ## Mock Framework
5
- #
6
- # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
7
-
8
4
  config.mock_with :mocha
9
- #config.mock_with :rspec
10
-
11
- # config.mock_with :flexmock
12
- # config.mock_with :rr
13
-
14
5
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shadow_db_credentials
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alexander Shvets
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-06-16 00:00:00.000000000 Z
11
+ date: 2013-08-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: gemspec_deps_gen
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: gemcutter
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Class that helps to keep database credentials for rails application in
@@ -55,13 +50,15 @@ files:
55
50
  - .idea/.rakeTasks
56
51
  - .idea/cssxfire.xml
57
52
  - .idea/encodings.xml
53
+ - .idea/jenkinsSettings.xml
58
54
  - .idea/misc.xml
59
55
  - .idea/modules.xml
60
56
  - .idea/scopes/scope_settings.xml
61
57
  - .idea/shadow_db_credentials.iml
62
58
  - .idea/vcs.xml
63
59
  - .idea/workspace.xml
64
- - .rvmrc
60
+ - .ruby-gemset
61
+ - .ruby-version
65
62
  - CHANGES
66
63
  - Gemfile
67
64
  - LICENSE
@@ -76,27 +73,26 @@ files:
76
73
  - spec/spec_helper.rb
77
74
  homepage: http://github.com/shvets/shadow_db_credentials
78
75
  licenses: []
76
+ metadata: {}
79
77
  post_install_message:
80
78
  rdoc_options: []
81
79
  require_paths:
82
80
  - lib
83
81
  required_ruby_version: !ruby/object:Gem::Requirement
84
- none: false
85
82
  requirements:
86
- - - ! '>='
83
+ - - '>='
87
84
  - !ruby/object:Gem::Version
88
85
  version: '0'
89
86
  required_rubygems_version: !ruby/object:Gem::Requirement
90
- none: false
91
87
  requirements:
92
- - - ! '>='
88
+ - - '>='
93
89
  - !ruby/object:Gem::Version
94
90
  version: '0'
95
91
  requirements: []
96
92
  rubyforge_project:
97
- rubygems_version: 1.8.23
93
+ rubygems_version: 2.0.5
98
94
  signing_key:
99
- specification_version: 3
95
+ specification_version: 4
100
96
  summary: Class that helps to keep database credentials for rails application in private
101
97
  place
102
98
  test_files: