aws-ssm-env 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-ssm-env
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryohei Sonoda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-29 00:00:00.000000000 Z
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-ssm
@@ -108,16 +108,17 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: Loads environment variables from AWS EC2 System Parameters.
111
+ description: Set parameters acquired from AWS EC2 Parameter Store as environment variables.
112
112
  email:
113
113
  - ryohei-sonoda@m3.com
114
114
  executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - CHANGELOG.md
118
119
  - LICENSE
119
120
  - README.md
120
- - README_en.md
121
+ - README_ja.md
121
122
  - lib/aws-ssm-env.rb
122
123
  - lib/aws-ssm-env/fetcher.rb
123
124
  - lib/aws-ssm-env/fetchers/begins_with.rb
@@ -129,10 +130,24 @@ files:
129
130
  - lib/aws-ssm-env/naming_strategies/snakecase.rb
130
131
  - lib/aws-ssm-env/naming_strategy.rb
131
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
132
145
  homepage: https://github.com/sonodar/aws-ssm-env-ruby
133
146
  licenses:
134
- - Apache License 2.0
135
- metadata: {}
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/aws-ssm-env-ruby/CHANGELOG.md
136
151
  post_install_message:
137
152
  rdoc_options: []
138
153
  require_paths:
@@ -141,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
156
  requirements:
142
157
  - - ">="
143
158
  - !ruby/object:Gem::Version
144
- version: '0'
159
+ version: '2.2'
145
160
  required_rubygems_version: !ruby/object:Gem::Requirement
146
161
  requirements:
147
162
  - - ">="
@@ -149,8 +164,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
164
  version: '0'
150
165
  requirements: []
151
166
  rubyforge_project:
152
- rubygems_version: 2.6.13
167
+ rubygems_version: 2.7.4
153
168
  signing_key:
154
169
  specification_version: 4
155
- summary: Loads environment variables from AWS EC2 System Parameters.
156
- test_files: []
170
+ summary: Set parameters acquired from AWS EC2 Parameter Store as environment variables.
171
+ test_files:
172
+ - spec/aws-ssm-env/fetcher_spec.rb
173
+ - spec/aws-ssm-env/fetchers/begins_with_spec.rb
174
+ - spec/aws-ssm-env/fetchers/factory_spec.rb
175
+ - spec/aws-ssm-env/fetchers/path_spec.rb
176
+ - spec/aws-ssm-env/loader_spec.rb
177
+ - spec/aws-ssm-env/naming_strategies/basename_spec.rb
178
+ - spec/aws-ssm-env/naming_strategies/factory_spec.rb
179
+ - spec/aws-ssm-env/naming_strategies/snakecase_spec.rb
180
+ - spec/aws-ssm-env/naming_strategy_spec.rb
181
+ - spec/aws-ssm-env/parameter.rb
182
+ - spec/aws-ssm-env_spec.rb
183
+ - spec/spec_helper.rb
@@ -1,5 +0,0 @@
1
- # aws-ssm-env
2
-
3
- Set parameters acquired from `AWS EC2 Parameter Store` as environment variables.
4
-
5
- **TODO** translate