is2-aws-ssm-env 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe AwsSsmEnv::Loader do
4
+ let(:args) { { path: '/foo' } }
5
+
6
+ describe '#initialize' do
7
+ let(:loader) { described_class.new(args) }
8
+
9
+ it 'has fetcher and naming_strategy' do
10
+ expect(loader.instance_variable_get(:@fetcher)).to be_a(AwsSsmEnv::PathFetcher)
11
+ expect(loader.instance_variable_get(:@naming_strategy)).to be_a(AwsSsmEnv::BasenameNamingStrategy)
12
+ end
13
+
14
+ describe 'overwrite option' do
15
+ let(:applier) { loader.instance_variable_get(:@applier) }
16
+
17
+ context 'when overwrite was not set' do
18
+ it '@applier is not overwrite method' do
19
+ expect(applier).to eq(:apply)
20
+ end
21
+ end
22
+
23
+ context 'when overwrite is nil' do
24
+ let(:args) { { path: '/foo', overwrite: nil } }
25
+
26
+ it '@applier is not overwrite method' do
27
+ expect(applier).to eq(:apply)
28
+ end
29
+ end
30
+
31
+ context 'when overwrite is truthy string' do
32
+ let(:args) { { path: '/foo', overwrite: 'truE' } }
33
+
34
+ it '@applier is overwrite method' do
35
+ expect(applier).to eq(:apply!)
36
+ end
37
+ end
38
+
39
+ context 'when overwrite is not truthy string' do
40
+ let(:args) { { path: '/foo', overwrite: 'foo' } }
41
+
42
+ it '@applier is not overwrite method' do
43
+ expect(applier).to eq(:apply)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#load' do
50
+ context 'when overwrite is false' do
51
+ it 'environment variables were not overwritten' do
52
+ ENV['foo'] = nil
53
+ ENV['fizz'] = 'fizz'
54
+
55
+ loader = described_class.new(args)
56
+ loader.instance_variable_set(:@fetcher,
57
+ [ Parameter.new('foo', 'bar'), Parameter.new('fizz', 'buzz') ])
58
+
59
+ loader.load
60
+
61
+ expect(ENV['foo']).to eq('bar')
62
+ expect(ENV['fizz']).to eq('fizz')
63
+ end
64
+ end
65
+
66
+ context 'when overwrite is true' do
67
+ let(:args) { { path: '/foo', overwrite: true } }
68
+
69
+ it 'environment variables were overwritten' do
70
+ ENV['foo'] = nil
71
+ ENV['fizz'] = 'fizz'
72
+
73
+ loader = described_class.new(args)
74
+ loader.instance_variable_set(:@fetcher,
75
+ [ Parameter.new('foo', 'bar'), Parameter.new('fizz', 'buzz') ])
76
+
77
+ loader.load
78
+
79
+ expect(ENV['foo']).to eq('bar')
80
+ expect(ENV['fizz']).to eq('buzz')
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require 'aws-ssm-env/naming_strategies/basename'
3
+
4
+ describe AwsSsmEnv::BasenameNamingStrategy do
5
+ describe '#parse_name' do
6
+ let(:naming_strategy) { described_class.new }
7
+
8
+ it 'return the last element name of the path hierarchy' do
9
+ expect(naming_strategy.parse_name(Parameter.new('/path/to/ENV_NAME'))).to eq('ENV_NAME')
10
+ expect(naming_strategy.parse_name(Parameter.new('env_name'))).to eq('env_name')
11
+ expect(naming_strategy.parse_name(Parameter.new('env/name'))).to eq('name')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe AwsSsmEnv::NamingStrategyFactory do
4
+ describe '#create_naming_strategy' do
5
+ let(:naming_strategy) { described_class.create_naming_strategy(args) }
6
+
7
+ context 'when naming was not set' do
8
+ let(:args) { { naming: nil } }
9
+
10
+ it 'return AwsSsmEnv::BasenameNamingStrategy' do
11
+ expect(naming_strategy).to be_a(AwsSsmEnv::BasenameNamingStrategy)
12
+ end
13
+ end
14
+
15
+ context 'when naming is :basename' do
16
+ let(:args) { { naming: :basename } }
17
+
18
+ it 'return AwsSsmEnv::BasenameNamingStrategy' do
19
+ expect(naming_strategy).to be_a(AwsSsmEnv::BasenameNamingStrategy)
20
+ end
21
+ end
22
+
23
+ context 'when naming is :snakecase' do
24
+ let(:args) { { naming: :snakecase } }
25
+
26
+ it 'return AwsSsmEnv::SnakeCaseNamingStrategy' do
27
+ expect(naming_strategy).to be_a(AwsSsmEnv::SnakeCaseNamingStrategy)
28
+ end
29
+ end
30
+
31
+ context 'when naming is AwsSsmEnv::NamingStrategy implementation instance' do
32
+ let(:naming_class) { Class.new(AwsSsmEnv::NamingStrategy) { def parse_name(_); end } }
33
+ let(:naming_instance) { naming_class.new }
34
+ let(:args) { { naming: naming_instance } }
35
+
36
+ it 'return it as is' do
37
+ expect(naming_strategy).to eq(naming_instance)
38
+ end
39
+ end
40
+
41
+ context 'when naming has parse_name method' do
42
+ let(:naming_class) { Class.new { def parse_name(_); end } }
43
+ let(:naming_instance) { naming_class.new }
44
+ let(:args) { { naming: naming_instance } }
45
+
46
+ it 'return it as is' do
47
+ expect(naming_strategy).to eq(naming_instance)
48
+ end
49
+ end
50
+
51
+ context 'in other cases' do
52
+ it 'raise error' do
53
+ expect { described_class.create_naming_strategy(naming: 'foo') }.to raise_error(ArgumentError)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'aws-ssm-env/naming_strategies/snakecase'
3
+
4
+ describe AwsSsmEnv::SnakeCaseNamingStrategy do
5
+ let(:strategy) { described_class.new(args) }
6
+ let(:name) { 'path.to.db/password' }
7
+ let(:env_name) { strategy.parse_name(Parameter.new(name)) }
8
+
9
+ describe '#parse_name' do
10
+ context 'when :removed_prefix was not set' do
11
+ context 'when :begins_with was not set' do
12
+ let(:args) { {} }
13
+
14
+ it 'return converted path hierarchy into snake case' do
15
+ expect(env_name).to eq('PATH.TO.DB_PASSWORD')
16
+ end
17
+ end
18
+
19
+ context 'when :begins_with was set' do
20
+ let(:args) { { begins_with: 'path.to.' } }
21
+
22
+ it 'return converted path hierarchy without begins_with into snake case' do
23
+ expect(env_name).to eq('DB_PASSWORD')
24
+ end
25
+ end
26
+
27
+ context 'when :path was set' do
28
+ let(:args) { { path: 'path.to.' } }
29
+
30
+ it 'return converted path hierarchy without begins_with into snake case' do
31
+ expect(env_name).to eq('DB_PASSWORD')
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'when :removed_prefix was set' do
37
+ context 'when :begins_with was not set' do
38
+ let(:args) { { removed_prefix: 'path.' } }
39
+
40
+ it 'return converted path hierarchy without removed_prefix into snake case' do
41
+ expect(env_name).to eq('TO.DB_PASSWORD')
42
+ end
43
+ end
44
+
45
+ context 'when :begins_with was set' do
46
+ let(:args) { { removed_prefix: 'path.', begins_with: 'path.to.' } }
47
+
48
+ it 'return converted path hierarchy without removed_prefix into snake case' do
49
+ expect(env_name).to eq('TO.DB_PASSWORD')
50
+ end
51
+ end
52
+
53
+ context 'when :path was set' do
54
+ let(:args) { { removed_prefix: 'path.', path: 'path.to.' } }
55
+
56
+ it 'return converted path hierarchy without removed_prefix into snake case' do
57
+ expect(env_name).to eq('TO.DB_PASSWORD')
58
+ end
59
+ end
60
+ end
61
+
62
+ context 'when :delimiter was not set' do
63
+ let(:args) { { removed_prefix: 'path.' } }
64
+
65
+ it 'return converted path hierarchy with replace "/" to "_"' do
66
+ expect(env_name).to eq('TO.DB_PASSWORD')
67
+ end
68
+ end
69
+
70
+ context 'when :delimiter was set' do
71
+ let(:args) { { removed_prefix: 'path.', delimiter: %r{[./]} } }
72
+
73
+ it 'return converted path hierarchy with replace "/" or "." to "_"' do
74
+ expect(env_name).to eq('TO_DB_PASSWORD')
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe AwsSsmEnv::NamingStrategy do
4
+ describe '#parse_name' do
5
+ let!(:strategy) { described_class.new }
6
+
7
+ it 'raise error' do
8
+ expect { strategy.parse_name(Parameter.new('foo')) }.to raise_error(NotImplementedError)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ class Parameter
2
+ attr_reader :name, :value
3
+ def initialize(*args)
4
+ @name = args[0]
5
+ @value = args[1]
6
+ end
7
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe AwsSsmEnv do
5
+ let(:ssm_client) {
6
+ stub_responses = {
7
+ get_parameters_by_path: {
8
+ parameters: [
9
+ { name: 'foo', value: 'bar', type: 'String', version: 1 },
10
+ { name: 'fizz', value: 'buzz', type: 'String', version: 1 }
11
+ ],
12
+ next_token: nil
13
+ }
14
+ }
15
+ Aws::SSM::Client.new(stub_responses: stub_responses)
16
+ }
17
+ let(:logger) { Logger.new('/dev/null') }
18
+
19
+ describe '#load' do
20
+ context 'when overwrite is false' do
21
+ it 'environment variables were not overwritten' do
22
+ ENV['foo'] = nil
23
+ ENV['fizz'] = 'fizz'
24
+
25
+ described_class.load(client: ssm_client, path: '/path')
26
+
27
+ expect(ENV['foo']).to eq('bar')
28
+ expect(ENV['fizz']).to eq('fizz')
29
+ end
30
+ end
31
+
32
+ context 'when overwrite is true' do
33
+ it 'environment variables were overwritten' do
34
+ ENV['foo'] = nil
35
+ ENV['fizz'] = 'fizz'
36
+
37
+ described_class.load!(client: ssm_client, path: '/path', logger: logger)
38
+
39
+ expect(ENV['foo']).to eq('bar')
40
+ expect(ENV['fizz']).to eq('buzz')
41
+ end
42
+ end
43
+ end
44
+
45
+ # @example Request syntax with placeholder values
46
+ #
47
+ # resp = client.put_parameter({
48
+ # name: "PSParameterName", # required
49
+ # description: "ParameterDescription",
50
+ # value: "PSParameterValue", # required
51
+ # type: "String", # required, accepts String, StringList, SecureString
52
+ # key_id: "ParameterKeyId",
53
+ # overwrite: false,
54
+ # allowed_pattern: "AllowedPattern",
55
+ # })
56
+ #
57
+ describe 'Integration test', integration: true do
58
+ PARAMETERS = [
59
+ { name: "/test/#{RUBY_VERSION}/aws-ssm-env/db_password", value: 'db_password', type: :SecureString },
60
+ { name: "/test/#{RUBY_VERSION}/aws-ssm-env/db/username", value: 'db_username', type: :String },
61
+ { name: "/test/#{RUBY_VERSION}/aws-ssm-env/roles", value: 'admin,guest', type: :StringList },
62
+ { name: "test.#{RUBY_VERSION}.aws-ssm-env.db_password", value: 'db_password', type: :SecureString },
63
+ { name: "test.#{RUBY_VERSION}.aws-ssm-env.username", value: 'db_username', type: :String },
64
+ { name: "test.#{RUBY_VERSION}.aws-ssm-env.roles", value: 'admin,guest', type: :StringList },
65
+ ].freeze
66
+ ENV_NAMES = %w[db_password username roles].freeze
67
+
68
+ def remove_env_all
69
+ ENV_NAMES.each do |name|
70
+ ENV[name] = nil
71
+ ENV[name.upcase] = nil
72
+ end
73
+ end
74
+
75
+ before :all do
76
+ @client = Aws::SSM::Client.new
77
+ PARAMETERS.each do |parameter|
78
+ @client.put_parameter(
79
+ name: parameter[:name],
80
+ value: parameter[:value],
81
+ type: parameter[:type].to_s,
82
+ overwrite: true
83
+ )
84
+ end
85
+ end
86
+
87
+ before do
88
+ remove_env_all
89
+ end
90
+
91
+ after :all do
92
+ @client.delete_parameters(names: PARAMETERS.map { |p| p[:name] })
93
+ end
94
+
95
+ after do
96
+ remove_env_all
97
+ end
98
+
99
+ describe 'path fetcher' do
100
+ context 'when recursive is true' do
101
+ it 'set environment variables from EC2 Parameter Store parameters' do
102
+ described_class.load(path: "/test/#{RUBY_VERSION}/aws-ssm-env", recursive: true, logger: logger)
103
+ expect(ENV['db_password']).to eq('db_password')
104
+ expect(ENV['username']).to eq('db_username')
105
+ expect(ENV['roles']).to eq('admin,guest')
106
+ end
107
+ end
108
+
109
+ context 'when recursive is false' do
110
+ it 'set environment variables from EC2 Parameter Store parameters' do
111
+ described_class.load(path: "/test/#{RUBY_VERSION}/aws-ssm-env", recursive: false, logger: logger)
112
+ expect(ENV['db_password']).to eq('db_password')
113
+ expect(ENV['username']).to be_nil
114
+ expect(ENV['roles']).to eq('admin,guest')
115
+ end
116
+ end
117
+ end
118
+
119
+ describe 'begins_with fetcher' do
120
+ it 'set environment variables from EC2 Parameter Store parameters' do
121
+ described_class.load(begins_with: "test.#{RUBY_VERSION}.aws-ssm-env.", naming: :snakecase, delimiter: '.', logger: logger)
122
+ expect(ENV['DB_PASSWORD']).to eq('db_password')
123
+ expect(ENV['USERNAME']).to eq('db_username')
124
+ expect(ENV['ROLES']).to eq('admin,guest')
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,22 @@
1
+ if ENV['CI']
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ else
5
+ require 'simplecov'
6
+ require 'simplecov-console'
7
+ SimpleCov.start do
8
+ add_filter '/vendor/'
9
+ add_filter '/spec/'
10
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
11
+ SimpleCov::Formatter::HTMLFormatter,
12
+ SimpleCov::Formatter::Console,
13
+ ]
14
+ end
15
+ end
16
+
17
+ require 'aws-ssm-env'
18
+ require 'aws-ssm-env/parameter'
19
+
20
+ RSpec.configure do |config|
21
+ config.filter_run_excluding(integration: true) unless ENV['CI']
22
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is2-aws-ssm-env
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryohei Sonoda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-16 00:00:00.000000000 Z
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: :runtime
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: rake
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.48.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.48.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
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
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-console
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Set parameters acquired from AWS EC2 Parameter Store as environment variables.
112
+ email:
113
+ - ryohei-sonoda@m3.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - CHANGELOG.md
119
+ - LICENSE
120
+ - README.md
121
+ - README_ja.md
122
+ - lib/aws-ssm-env.rb
123
+ - lib/aws-ssm-env/fetcher.rb
124
+ - lib/aws-ssm-env/fetchers/begins_with.rb
125
+ - lib/aws-ssm-env/fetchers/factory.rb
126
+ - lib/aws-ssm-env/fetchers/path.rb
127
+ - lib/aws-ssm-env/loader.rb
128
+ - lib/aws-ssm-env/naming_strategies/basename.rb
129
+ - lib/aws-ssm-env/naming_strategies/factory.rb
130
+ - lib/aws-ssm-env/naming_strategies/snakecase.rb
131
+ - lib/aws-ssm-env/naming_strategy.rb
132
+ - lib/aws-ssm-env/version.rb
133
+ - spec/aws-ssm-env/fetcher_spec.rb
134
+ - spec/aws-ssm-env/fetchers/begins_with_spec.rb
135
+ - spec/aws-ssm-env/fetchers/factory_spec.rb
136
+ - spec/aws-ssm-env/fetchers/path_spec.rb
137
+ - spec/aws-ssm-env/loader_spec.rb
138
+ - spec/aws-ssm-env/naming_strategies/basename_spec.rb
139
+ - spec/aws-ssm-env/naming_strategies/factory_spec.rb
140
+ - spec/aws-ssm-env/naming_strategies/snakecase_spec.rb
141
+ - spec/aws-ssm-env/naming_strategy_spec.rb
142
+ - spec/aws-ssm-env/parameter.rb
143
+ - spec/aws-ssm-env_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: https://github.com/sonodar/aws-ssm-env-ruby
146
+ licenses:
147
+ - Apache-2.0
148
+ metadata:
149
+ source_code_uri: https://github.com/sonodar/aws-ssm-env-ruby
150
+ changelog_uri: https://github.com/sonodar/aws-ssm-env-ruby/tree/master/CHANGELOG.md
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '2.2'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubygems_version: 3.0.3
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Set parameters acquired from AWS EC2 Parameter Store as environment variables.
170
+ test_files:
171
+ - spec/aws-ssm-env/fetcher_spec.rb
172
+ - spec/aws-ssm-env/fetchers/begins_with_spec.rb
173
+ - spec/aws-ssm-env/fetchers/factory_spec.rb
174
+ - spec/aws-ssm-env/fetchers/path_spec.rb
175
+ - spec/aws-ssm-env/loader_spec.rb
176
+ - spec/aws-ssm-env/naming_strategies/basename_spec.rb
177
+ - spec/aws-ssm-env/naming_strategies/factory_spec.rb
178
+ - spec/aws-ssm-env/naming_strategies/snakecase_spec.rb
179
+ - spec/aws-ssm-env/naming_strategy_spec.rb
180
+ - spec/aws-ssm-env/parameter.rb
181
+ - spec/aws-ssm-env_spec.rb
182
+ - spec/spec_helper.rb