global 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,6 +22,13 @@ module Global
22
22
  hash.select { |key, _| keys.include?(key) }
23
23
  end
24
24
 
25
+ def get_configuration_value(key)
26
+ return nil unless key?(key)
27
+
28
+ value = hash[key]
29
+ value.is_a?(Hash) ? Global::Configuration.new(value) : value
30
+ end
31
+
25
32
  private
26
33
 
27
34
  def filtered_keys_list(options)
@@ -30,6 +37,7 @@ module Global
30
37
 
31
38
  return hash.keys if options[:only] == :all
32
39
  return [] if options[:except] == :all
40
+
33
41
  []
34
42
  end
35
43
 
@@ -43,8 +51,7 @@ module Global
43
51
  def method_missing(method, *args, &block)
44
52
  method = normalize_key_by_method(method)
45
53
  if key?(method)
46
- value = hash[method]
47
- value.is_a?(Hash) ? Global::Configuration.new(value) : value
54
+ get_configuration_value(method)
48
55
  else
49
56
  super
50
57
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Global
4
4
 
5
- VERSION = '1.1.0'
5
+ VERSION = '2.0.0'
6
6
 
7
7
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'aws-sdk-ssm'
5
+ require 'global/backend/aws_parameter_store'
6
+
7
+ RSpec.describe Global::Backend::AwsParameterStore do
8
+ let(:client) do
9
+ Aws::SSM::Client.new(stub_responses: true)
10
+ end
11
+ subject do
12
+ described_class.new(prefix: '/testapp/', client: client)
13
+ end
14
+
15
+ it 'reads parameters from the parameter store' do
16
+ client.stub_responses(
17
+ :get_parameters_by_path,
18
+ [
19
+ lambda { |req_context|
20
+ expect(req_context.params[:next_token]).to be_nil
21
+ {
22
+ parameters: [
23
+ { name: '/testapp/foo', value: 'foo-value' },
24
+ { name: '/testapp/bar/baz', value: 'baz-value' }
25
+ ],
26
+ next_token: 'next-token'
27
+ }
28
+ },
29
+ lambda { |req_context|
30
+ expect(req_context.params[:next_token]).to eq('next-token')
31
+ {
32
+ parameters: [
33
+ { name: '/testapp/bar/qux', value: 'qux-value' }
34
+ ],
35
+ next_token: nil
36
+ }
37
+ }
38
+ ]
39
+ )
40
+ expect(subject.load).to eq(
41
+ foo: 'foo-value',
42
+ bar: {
43
+ baz: 'baz-value',
44
+ qux: 'qux-value'
45
+ }
46
+ )
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'google/cloud/secret_manager'
5
+ require 'global/backend/gcp_secret_manager'
6
+
7
+ RSpec.describe Global::Backend::GcpSecretManager do
8
+ let(:client) { double }
9
+
10
+ subject do
11
+ described_class.new(prefix: 'prod-myapp-', client: client, project_id: 'example')
12
+ end
13
+
14
+ before do
15
+ @match_item = double
16
+ allow(@match_item).to receive(:name).and_return('prod-myapp-example-test_key')
17
+
18
+ @secret_data = double
19
+ allow(@secret_data).to receive_message_chain(:payload, :data).and_return('secret value')
20
+
21
+ @not_match_item = double
22
+ allow(@not_match_item).to receive(:name).and_return('different_key')
23
+
24
+ @list = double
25
+ allow(@list).to receive(:next_page_token).and_return('')
26
+ allow(@list).to receive(:each).and_yield(@match_item).and_yield(@not_match_item)
27
+
28
+ allow(client).to receive(:project_path).and_return('projects/example')
29
+ allow(client).to receive(:secret_version_path)
30
+ .with(project: 'example', secret: 'prod-myapp-example-test_key', secret_version: 'latest')
31
+ .and_return('some_key_path')
32
+ allow(client).to receive(:access_secret_version).with(name: 'some_key_path').and_return(@secret_data)
33
+ allow(client).to receive(:list_secrets).and_return(@list)
34
+ end
35
+
36
+ it 'reads parameters from the secret manager' do
37
+ expect(subject.load).to eq({ example: { test_key: 'secret value' }})
38
+ end
39
+ end
@@ -4,34 +4,10 @@ require 'spec_helper'
4
4
 
5
5
  RSpec.describe Global do
6
6
 
7
+ let(:config_path) { File.join(Dir.pwd, 'spec/files') }
7
8
  before(:each) do
8
9
  described_class.configure do |config|
9
- config.environment = 'test'
10
- config.config_directory = File.join(Dir.pwd, 'spec/files')
11
- end
12
- end
13
-
14
- describe '.environment' do
15
- subject { described_class.environment }
16
-
17
- it { is_expected.to eq('test') }
18
-
19
- context 'when undefined' do
20
- before { described_class.environment = nil }
21
-
22
- it { expect { subject }.to raise_error('environment should be defined') }
23
- end
24
- end
25
-
26
- describe '.config_directory' do
27
- subject { described_class.config_directory }
28
-
29
- it { is_expected.to eq(File.join(Dir.pwd, 'spec/files')) }
30
-
31
- context 'when undefined' do
32
- before { described_class.config_directory = nil }
33
-
34
- it { expect { subject }.to raise_error('config_directory should be defined') }
10
+ config.backend :filesystem, path: config_path, environment: 'test'
35
11
  end
36
12
  end
37
13
 
@@ -51,10 +27,9 @@ RSpec.describe Global do
51
27
  end
52
28
 
53
29
  context 'when load from file' do
54
- before { described_class.config_directory = File.join(Dir.pwd, 'spec/files/rspec_config') }
30
+ let(:config_path) { File.join(Dir.pwd, 'spec/files/rspec_config') }
55
31
 
56
32
  describe '#rspec_config' do
57
- subject { super().rspec_config }
58
33
  describe '#to_hash' do
59
34
  subject { super().to_hash }
60
35
  it { is_expected.to eq('default_value' => 'default value', 'test_value' => 'test value') }
@@ -88,12 +63,8 @@ RSpec.describe Global do
88
63
 
89
64
  before do
90
65
  described_class.configuration
91
- described_class.environment = 'development'
92
- end
93
-
94
- after do
95
- described_class.environment = 'test'
96
- described_class.reload!
66
+ described_class.instance_variable_set('@backends', [])
67
+ described_class.backend :filesystem, path: config_path, environment: 'development'
97
68
  end
98
69
 
99
70
  it { is_expected.to be_instance_of(Global::Configuration) }
@@ -141,5 +112,4 @@ RSpec.describe Global do
141
112
  end
142
113
 
143
114
  end
144
-
145
115
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Global do
6
+ describe 'merging backends' do
7
+ it 'merges data from two backends together' do
8
+ backend_alpha = double('backend_alpha')
9
+ allow(backend_alpha).to receive(:load).and_return(foo: 'foo', bar: 'bar-alpha')
10
+ described_class.backend backend_alpha
11
+
12
+ backend_beta = double('backend_beta')
13
+ allow(backend_beta).to receive(:load).and_return('bar' => 'bar-beta', 'baz' => 'baz')
14
+ described_class.backend backend_beta
15
+
16
+ expect(described_class.configuration.to_hash).to eq(
17
+ 'foo' => 'foo',
18
+ 'bar' => 'bar-beta',
19
+ 'baz' => 'baz'
20
+ )
21
+ end
22
+ end
23
+ end
@@ -5,7 +5,6 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
  require 'rspec'
6
6
  require 'global'
7
7
  require 'simplecov'
8
- require 'support/javascript_helper'
9
8
 
10
9
  SimpleCov.start do
11
10
  add_filter '/spec/'
@@ -39,5 +38,9 @@ RSpec.configure do |config|
39
38
  config.filter_run :focus
40
39
 
41
40
  config.order = 'random'
42
- config.include JavascriptHelper
41
+
42
+ config.before do
43
+ Global.remove_instance_variable(:@backends) if Global.instance_variable_defined?(:@backends)
44
+ Global.remove_instance_variable(:@configuration) if Global.instance_variable_defined?(:@configuration)
45
+ end
43
46
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: global
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Railsware LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-09 00:00:00.000000000 Z
11
+ date: 2020-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-ssm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-secret_manager
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rake
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +72,14 @@ dependencies:
44
72
  requirements:
45
73
  - - "~>"
46
74
  - !ruby/object:Gem::Version
47
- version: '0.57'
75
+ version: 0.81.0
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
80
  - - "~>"
53
81
  - !ruby/object:Gem::Version
54
- version: '0.57'
82
+ version: 0.81.0
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: simplecov
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +94,6 @@ dependencies:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
96
  version: 0.16.1
69
- - !ruby/object:Gem::Dependency
70
- name: therubyracer
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: activesupport
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -94,9 +108,10 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '2.0'
97
- description: Simple way to load your configs from yaml
111
+ description: Simple way to load your configs from yaml/aws/gcp
98
112
  email: contact@railsware.com
99
- executables: []
113
+ executables:
114
+ - console
100
115
  extensions: []
101
116
  extra_rdoc_files: []
102
117
  files:
@@ -105,16 +120,19 @@ files:
105
120
  - ".rubocop.yml"
106
121
  - ".ruby-version"
107
122
  - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
108
124
  - Gemfile
109
125
  - LICENSE.txt
110
126
  - README.md
111
127
  - Rakefile
112
- - app/assets/javascripts/global-js.js.erb
128
+ - bin/console
113
129
  - global.gemspec
114
130
  - lib/global.rb
131
+ - lib/global/backend/aws_parameter_store.rb
132
+ - lib/global/backend/filesystem.rb
133
+ - lib/global/backend/gcp_secret_manager.rb
115
134
  - lib/global/base.rb
116
135
  - lib/global/configuration.rb
117
- - lib/global/engine.rb
118
136
  - lib/global/version.rb
119
137
  - spec/files/aws.test.yml
120
138
  - spec/files/aws.yml
@@ -122,11 +140,12 @@ files:
122
140
  - spec/files/nested_config.yml
123
141
  - spec/files/rspec/config.yml
124
142
  - spec/files/rspec_config.yml
143
+ - spec/global/backend/aws_parameter_store_spec.rb
144
+ - spec/global/backend/gcp_secret_manager_spec.rb
125
145
  - spec/global/configuration_spec.rb
126
- - spec/global/global_js_spec.rb
127
146
  - spec/global_spec.rb
147
+ - spec/merge_backends_spec.rb
128
148
  - spec/spec_helper.rb
129
- - spec/support/javascript_helper.rb
130
149
  homepage: https://github.com/railsware/global
131
150
  licenses:
132
151
  - MIT
@@ -146,11 +165,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
165
  - !ruby/object:Gem::Version
147
166
  version: '0'
148
167
  requirements: []
149
- rubyforge_project: global
150
- rubygems_version: 2.7.6
168
+ rubygems_version: 3.0.3
151
169
  signing_key:
152
170
  specification_version: 4
153
- summary: Simple way to load your configs from yaml
171
+ summary: Simple way to load your configs from yaml/aws/gcp
154
172
  test_files:
155
173
  - spec/files/aws.test.yml
156
174
  - spec/files/aws.yml
@@ -158,8 +176,9 @@ test_files:
158
176
  - spec/files/nested_config.yml
159
177
  - spec/files/rspec/config.yml
160
178
  - spec/files/rspec_config.yml
179
+ - spec/global/backend/aws_parameter_store_spec.rb
180
+ - spec/global/backend/gcp_secret_manager_spec.rb
161
181
  - spec/global/configuration_spec.rb
162
- - spec/global/global_js_spec.rb
163
182
  - spec/global_spec.rb
183
+ - spec/merge_backends_spec.rb
164
184
  - spec/spec_helper.rb
165
- - spec/support/javascript_helper.rb
@@ -1,2 +0,0 @@
1
- <%# encoding: UTF-8 %>
2
- <%= Global.generate_js %>
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Global
4
-
5
- class SprocketsExtension
6
-
7
- GLOBAL_JS_ASSET = 'global-js'
8
-
9
- def initialize(filename)
10
- @filename = filename
11
- @source = yield
12
- end
13
-
14
- def render(context, _empty_hash_wtf)
15
- self.class.run(@filename, @source, context)
16
- end
17
-
18
- def self.run(_filename, source, context)
19
- if GLOBAL_JS_ASSET == context.logical_path
20
- configs = Dir.glob("#{Global.config_directory}#{File::SEPARATOR}*.yml")
21
- configs.map { |config| context.depend_on(config) }
22
- end
23
- source
24
- end
25
-
26
- def self.call(input)
27
- filename = input[:filename]
28
- source = input[:data]
29
- context = input[:environment].context_class.new(input)
30
-
31
- result = run(filename, source, context)
32
- context.metadata.merge(data: result)
33
- end
34
-
35
- end
36
-
37
- class Engine < ::Rails::Engine
38
-
39
- require 'sprockets/version'
40
- v2 = Gem::Dependency.new('', ' ~> 2')
41
- vgte3 = Gem::Dependency.new('', ' >= 3')
42
- sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
43
- initializer_args = case sprockets_version
44
- when ->(v) { v2.match?('', v) }
45
- { after: 'sprockets.environment' }
46
- when ->(v) { vgte3.match?('', v) }
47
- { after: :engines_blank_point, before: :finisher_hook }
48
- else
49
- raise StandardError.new("Sprockets version #{sprockets_version} is not supported")
50
- end
51
-
52
- is_running_rails = defined?(Rails) && Rails.respond_to?(:version)
53
- is_running_rails32 = is_running_rails && Rails.version.match(/3\.2/)
54
-
55
- initializer 'global-js.dependent_on_configs', initializer_args do
56
- case sprockets_version
57
- when ->(v) { v2.match?('', v) },
58
- ->(v) { vgte3.match?('', v) }
59
-
60
- # It seems rails 3.2 is not working if
61
- # `Rails.application.config.assets.configure` is used for
62
- # registering preprocessor
63
- if is_running_rails32
64
- Rails.application.assets.register_preprocessor(
65
- 'application/javascript',
66
- SprocketsExtension
67
- )
68
- elsif Rails.application.config.respond_to?(:assets)
69
- # Other rails version, assumed newer
70
- Rails.application.config.assets.configure do |config|
71
- config.register_preprocessor(
72
- 'application/javascript',
73
- SprocketsExtension
74
- )
75
- end
76
- end
77
- else
78
- raise StandardError.new("Sprockets version #{sprockets_version} is not supported")
79
- end
80
- end
81
-
82
- end
83
-
84
- end