secret_config 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6ac6f9526e20dd9f6b39762ff6363ec3090fa51db2dfa0726351742d7abe5f3e
4
+ data.tar.gz: 78e69dd6f2bed35f5dde021a0ef247d392fee388ae5d3b3c3f41ca551928ec0c
5
+ SHA512:
6
+ metadata.gz: 3f8fd6c22492f28bf71b1938792fe1cc392bfd073a0e541645dfd7869fe41f12f54e53575eabb9252440f34a137cce1b2bad89667dba84f80fec3183ef17de8f
7
+ data.tar.gz: f2ea35ffbb3acecacff2eca9ca16c73f3e6b1de15a78b3c19e8f2b59b6425fccc01c8c06779167ae3470d7fb5688bd7cbb5ec4e652f31841518bb824662417c9
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # Secret Config
2
+ [![Gem Version](https://img.shields.io/gem/v/secret_config.svg)](https://rubygems.org/gems/secret_config) [![Build Status](https://travis-ci.org/rocketjob/secret_config.svg?branch=master)](https://travis-ci.org/rocketjob/secret_config) [![License](https://img.shields.io/badge/license-Apache%202.0-brightgreen.svg)](http://opensource.org/licenses/Apache-2.0) ![](https://img.shields.io/badge/status-Beta-yellow.svg) [![Gitter chat](https://img.shields.io/badge/IRC%20(gitter)-Support-brightgreen.svg)](https://gitter.im/rocketjob/support)
3
+
4
+ Centralized Configuration and Secrets Management for Ruby and Rails applications.
5
+
6
+ Securely store configuration information centrally.
7
+
8
+ ## Project Status
9
+
10
+ Early development.
11
+
12
+ ## Features
13
+
14
+ Supports storing configuration information in:
15
+ * File
16
+ * Development and testing use.
17
+ * AWS System Manager Parameter Store
18
+ * Encrypt and store secrets such as passwords centrally.
19
+
20
+ ## Benefits
21
+
22
+ Benefits of moving sensitive configuration information into AWS System Manager Parameter Store:
23
+
24
+ * Hierarchical structure is maintained.
25
+ * Environment variables force all config into a single level.
26
+ * Reduces the number of environment variables.
27
+ * In a large application the number of secrets can grow dramatically.
28
+ * Removes the need to encrypt sensitive data config files.
29
+ * Including securing and managing encryption keys.
30
+ * When encryption keys change, such as during a key rotation, config files don;t have to be changed.
31
+ * Removes security concerns with placing passwords in the clear into environment variables.
32
+ * AWS System Manager Parameter Store does not charge for parameters.
33
+ * Still recommend using a custom KMS key that charges only $1 per month.
34
+ * Amounts as of 4/2019. Confirm what AWS charges you for these services.
35
+ * AWS Secrets Manager charges for every secret being managed, which can accumulate quickly with large projects.
36
+
37
+ ## Development and Test use
38
+
39
+ In the development environment create the file `config/application.yml` within which to store local development credentials.
40
+ Depending on your team setup you may want to use the same file for all developers so can check it into you change control system.
41
+
42
+ For example: `config/application.yml`
43
+
44
+ ~~~yaml
45
+ development:
46
+ mysql:
47
+ database: secret_config_development
48
+ username: secret_config
49
+ password: secret_configrules
50
+ host: 127.0.0.1
51
+
52
+ mongo:
53
+ database: secret_config_development
54
+ primary: 127.0.0.1:27017
55
+ secondary: 127.0.0.1:27018
56
+
57
+ secrets:
58
+ secret_key_base: somereallylongstring
59
+
60
+ test:
61
+ mysql:
62
+ database: secret_config_test
63
+ username: secret_config
64
+ password: secret_configrules
65
+ host: 127.0.0.1
66
+
67
+ mongo:
68
+ database: secret_config_test
69
+ primary: 127.0.0.1:27017
70
+ secondary: 127.0.0.1:27018
71
+
72
+ secrets:
73
+ secret_key_base: somereallylongteststring
74
+ ~~~
75
+
76
+ Note how the hierarchical nature of configuration values is maintained. Typical environment variable approaches have
77
+ to flatten everything into a single level.
78
+
79
+ Note: Do not put any production credentials into this file.
80
+
81
+ ### Usage
82
+
83
+ Go through all the configuration files and look for sensitive data such as passwords:
84
+
85
+ Example `database.yml`:
86
+
87
+ ~~~yaml
88
+ defaults: &defaults
89
+ encoding: utf8
90
+ adapter: mysql2
91
+
92
+ development:
93
+ <<: *defaults
94
+ database: secure_config_development
95
+ username: jack
96
+ password: jackrules
97
+ host: localhost
98
+
99
+ test:
100
+ <<: *defaults
101
+ database: secure_config_test
102
+ username: tester
103
+ password: khjsdjhdsjhdsr32
104
+ host: test.server
105
+
106
+ production:
107
+ <<: *defaults
108
+ database: secure_config_production
109
+ username: product
110
+ password: donotexpose45
111
+ host: production.server
112
+ ~~~
113
+
114
+ Replace the sensitive data with a `SecureConfig.fetch`:
115
+
116
+ Updated `database.yml`:
117
+
118
+ ~~~yaml
119
+ configuration: &configuration
120
+ database: <%= SecretConfig.fetch("mysql/database") %>
121
+ username: <%= SecretConfig.fetch("mysql/username") %>
122
+ password: <%= SecretConfig.fetch("mysql/password") %>
123
+ host: <%= SecretConfig.fetch("mysql/host") %>
124
+ encoding: utf8
125
+ adapter: mysql2
126
+
127
+ development:
128
+ <<: *configuration
129
+
130
+ test:
131
+ <<: *configuration
132
+
133
+ production:
134
+ <<: *configuration
135
+ ~~~
136
+
137
+ Since the secrets are externalized the configuration between environments is simpler.
138
+
139
+ ## Versioning
140
+
141
+ This project adheres to [Semantic Versioning](http://semver.org/).
142
+
143
+ ## Author
144
+
145
+ [Reid Morrison](https://github.com/reidmorrison)
146
+
147
+ ## License
148
+
149
+ Copyright 2019 Reid Morrison
150
+
151
+ Licensed under the Apache License, Version 2.0 (the "License");
152
+ you may not use this file except in compliance with the License.
153
+ You may obtain a copy of the License at
154
+
155
+ http://www.apache.org/licenses/LICENSE-2.0
156
+
157
+ Unless required by applicable law or agreed to in writing, software
158
+ distributed under the License is distributed on an "AS IS" BASIS,
159
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
160
+ See the License for the specific language governing permissions and
161
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake/testtask'
2
+ require_relative 'lib/secret_config/version'
3
+
4
+ task :gem do
5
+ system 'gem build secret_config.gemspec'
6
+ end
7
+
8
+ task :publish => :gem do
9
+ system "git tag -a v#{SecretConfig::VERSION} -m 'Tagging #{SecretConfig::VERSION}'"
10
+ system 'git push --tags'
11
+ system "gem push secret_config-#{SecretConfig::VERSION}.gem"
12
+ system "rm secret_config-#{SecretConfig::VERSION}.gem"
13
+ end
14
+
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ t.warning = true
19
+ end
20
+
21
+ task :default => :test
@@ -0,0 +1,48 @@
1
+ require 'sync_attr'
2
+ require 'secret_config/version'
3
+ require 'secret_config/errors'
4
+ require 'secret_config/registry'
5
+
6
+ # Centralized Configuration and Secrets Management for Ruby and Rails applications.
7
+ module SecretConfig
8
+ module Providers
9
+ autoload :File, 'secret_config/providers/file'
10
+ autoload :Ssm, 'secret_config/providers/ssm'
11
+ end
12
+
13
+ class << self
14
+ extend Forwardable
15
+
16
+ def_delegator :registry, :fetch
17
+ def_delegator :registry, :configuration
18
+ def_delegator :registry, :[]
19
+ def_delegator :registry, :[]=
20
+ def_delegator :registry, :key?
21
+ def_delegator :registry, :fetch
22
+ def_delegator :registry, :set
23
+ def_delegator :registry, :refresh!
24
+ end
25
+
26
+ def self.root
27
+ @root ||= ENV["SECRETCONFIG_ROOT"] ||
28
+ raise(UndefinedRootError, "Either set env var 'SECRETCONFIG_ROOT' or call SecretConfig.root=")
29
+ end
30
+
31
+ def self.root=(root)
32
+ @root = root
33
+ @registry = nil if @registry
34
+ end
35
+
36
+ def self.provider #(provider, **args)
37
+ @provider ||= (ENV["SECRETCONFIG_PROVIDER"] || :file).to_sym
38
+ end
39
+
40
+ def self.provider=(provider)
41
+ @provider = provider
42
+ @registry = nil if @registry
43
+ end
44
+
45
+ def self.registry
46
+ @registry ||= SecretConfig::Registry.new(root: root, provider: provider)
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ module SecretConfig
2
+ class Error < StandardError
3
+ end
4
+
5
+ class MissingMandatoryKey < Error
6
+ end
7
+
8
+ class UndefinedRootError < Error
9
+ end
10
+
11
+ class ConfigurationError < Error
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module SecretConfig
5
+ module Providers
6
+ # Read configuration from a local file
7
+ class File
8
+ attr_reader :file_name
9
+
10
+ def initialize(file_name: ENV['SECRETCONFIG_FILE_NAME'] || "config/application.yml")
11
+ @file_name = file_name
12
+ raise(ConfigurationError, "Cannot find config file: #{file_name}") unless ::File.exist?(file_name)
13
+ end
14
+
15
+ def each(path, &block)
16
+ config = YAML.load(ERB.new(::File.new(file_name).read).result)
17
+
18
+ paths = path.sub(/\A\/*/, '').sub(/\/*\Z/, '').split("/")
19
+ settings = config.dig(*paths)
20
+
21
+ raise(ConfigError, "Path #{paths.join(".")} not found in file: #{file_name}") unless settings
22
+
23
+ flatten_each(path, settings, &block)
24
+ nil
25
+ end
26
+
27
+ def set(key, value)
28
+ raise NotImplementedError
29
+ end
30
+
31
+ private
32
+
33
+ def flatten_each(path, hash, &block)
34
+ hash.each_pair do |key, value|
35
+ if value.is_a?(Hash)
36
+ flatten_each("#{path}/#{key}", value, &block)
37
+ else
38
+ key = "#{path}/#{key}" unless key.start_with?('/')
39
+ yield(key, value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,41 @@
1
+ require 'aws-sdk-ssm'
2
+
3
+ module SecretConfig
4
+ module Providers
5
+ # Use the AWS System Manager Parameter Store for Centralized Configuration / Secrets Management
6
+ class Ssm
7
+ attr_reader :client, :key_id
8
+
9
+ def initialize(key_id: ENV["SECRETCONFIG_KEY_ID"])
10
+ @key_id = key_id
11
+ logger = SemanticLogger['Aws::SSM'] if defined?(SemanticLogger)
12
+ @client = Aws::SSM::Client.new(logger: logger)
13
+ end
14
+
15
+ def each(path)
16
+ token = nil
17
+ loop do
18
+ resp = client.get_parameters_by_path(
19
+ path: path,
20
+ recursive: true,
21
+ with_decryption: true,
22
+ next_token: token
23
+ )
24
+ resp.parameters.each { |param| yield(param.name, param.value) }
25
+ token = resp.next_token
26
+ break if token.nil?
27
+ end
28
+ end
29
+
30
+ def set(key, value, encrypt: true)
31
+ client.put_parameter(
32
+ name: key,
33
+ value: value.to_s,
34
+ type: encrypt ? "SecureString" : "String",
35
+ key_id: key_id,
36
+ overwrite: true
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,112 @@
1
+ require 'base64'
2
+
3
+ module SecretConfig
4
+ # Centralized configuration with values stored in AWS System Manager Parameter Store
5
+ #
6
+ # Values are fetched from the central store on startup. Only those values starting with the specified
7
+ # root are loaded, supply multiple paths using the env var SECRETCONFIG_PATHS.
8
+ #
9
+ # Existing event mechanisms can be used to force a reload of the cached copy.
10
+ class Registry
11
+ attr_reader :provider
12
+ attr_accessor :root
13
+
14
+ def initialize(root:, provider: :ssm)
15
+ # TODO: Validate root starts with /, etc
16
+ @root = root
17
+ @provider = provider
18
+ refresh!
19
+ end
20
+
21
+ # Returns [Hash] a copy of the in memory configuration data.
22
+ def configuration
23
+ h = {}
24
+ registry.each_pair { |key, value| h[key] = value }
25
+ h
26
+ end
27
+
28
+ # Returns [String] configuration value for the supplied key, or nil when missing.
29
+ def [](key)
30
+ registry[expand_key(key)]
31
+ end
32
+
33
+ # Returns [String] configuration value for the supplied key, or nil when missing.
34
+ def key?(key)
35
+ registry.key?(expand_key(key))
36
+ end
37
+
38
+ # Returns [String] configuration value for the supplied key
39
+ def fetch(key, default: nil, type: :string, encoding: nil)
40
+ value = self[key]
41
+ if value.nil?
42
+ raise(MissingMandatoryKey, "Missing configuration value for #{root}/#{key}") unless default
43
+
44
+ value = default.respond_to?(:call) ? default.call : default
45
+ end
46
+
47
+ value = convert_encoding(encoding, value) if encoding
48
+ type == :string ? value : convert_type(type, value)
49
+ end
50
+
51
+ def set(key:, value:, encrypt: true)
52
+ SSM.new(key_id: key_id).set(expand_key(key), value, encrypt: encrypt)
53
+ end
54
+
55
+ def refresh!
56
+ h = {}
57
+ implementation.each(root) { |k, v| h[k] = v }
58
+ @registry = h
59
+ end
60
+
61
+ private
62
+
63
+ attr_reader :registry
64
+
65
+ def expand_key(key)
66
+ key.start_with?('/') ? key : "#{root}/#{key}"
67
+ end
68
+
69
+ def implementation
70
+ @implementation ||= constantize_symbol(provider).new
71
+ end
72
+
73
+ def convert_encoding(encoding, value)
74
+ case encoding
75
+ when :base64
76
+ Base64.decode64(value)
77
+ else
78
+ value
79
+ end
80
+ end
81
+
82
+ def convert_type(type, value)
83
+ case type
84
+ when :integer
85
+ value.to_i
86
+ when :float
87
+ value.to_f
88
+ when :string
89
+ value
90
+ end
91
+ end
92
+
93
+ def constantize_symbol(symbol, namespace = 'SecretConfig::Providers')
94
+ klass = "#{namespace}::#{camelize(symbol.to_s)}"
95
+ begin
96
+ Object.const_get(klass)
97
+ rescue NameError
98
+ raise(ArgumentError, "Could not convert symbol: #{symbol.inspect} to a class in: #{namespace}. Looking for: #{klass}")
99
+ end
100
+ end
101
+
102
+ # Borrow from Rails, when not running Rails
103
+ def camelize(term)
104
+ string = term.to_s
105
+ string = string.sub(/^[a-z\d]*/, &:capitalize)
106
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
107
+ string.gsub!('/'.freeze, '::'.freeze)
108
+ string
109
+ end
110
+
111
+ end
112
+ end
@@ -0,0 +1,3 @@
1
+ module SecretConfig
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,47 @@
1
+ # Local application config goes here. Do not check in production secrets.
2
+ # These are for development and test only.
3
+
4
+ #
5
+ # Development - Local - Root: '/development/connect'
6
+ #
7
+ development:
8
+ connect:
9
+ symmetric_encryption:
10
+ key: QUJDREVGMTIzNDU2Nzg5MEFCQ0RFRjEyMzQ1Njc4OTA=
11
+ iv: QUJDREVGMTIzNDU2Nzg5MA==
12
+ version: 2
13
+
14
+ mysql:
15
+ database: secret_config_development
16
+ username: secret_config
17
+ password: secret_configrules
18
+ host: 127.0.0.1
19
+
20
+ mongo:
21
+ database: secret_config_development
22
+ primary: 127.0.0.1:27017
23
+ secondary: 127.0.0.1:27018
24
+
25
+ secrets:
26
+ secret_key_base: somereallylongstring
27
+
28
+ test:
29
+ connect:
30
+ symmetric_encryption:
31
+ key: QUJDREVGMTIzNDU2Nzg5MEFCQ0RFRjEyMzQ1Njc4OTA=
32
+ iv: QUJDREVGMTIzNDU2Nzg5MA==
33
+ version: 2
34
+
35
+ mysql:
36
+ database: secret_config_test
37
+ username: secret_config
38
+ password: secret_configrules
39
+ host: 127.0.0.1
40
+
41
+ mongo:
42
+ database: secret_config_test
43
+ primary: 127.0.0.1:27017
44
+ secondary: 127.0.0.1:27018
45
+
46
+ secrets:
47
+ secret_key_base: somereallylongteststring
@@ -0,0 +1,43 @@
1
+ require_relative '../test_helper'
2
+
3
+ module Providers
4
+ class FileTest < Minitest::Test
5
+ describe SecretConfig::Providers::File do
6
+ let :file_name do
7
+ File.join(File.dirname(__FILE__), '..', 'config', 'application.yml')
8
+ end
9
+
10
+ let :root do
11
+ "/development/connect"
12
+ end
13
+
14
+ let :expected do
15
+ {
16
+ "/development/connect/mongo/database" => "secret_config_development",
17
+ "/development/connect/mongo/primary" => "127.0.0.1:27017",
18
+ "/development/connect/mongo/secondary" => "127.0.0.1:27018",
19
+ "/development/connect/mysql/database" => "secret_config_development",
20
+ "/development/connect/mysql/password" => "secret_configrules",
21
+ "/development/connect/mysql/username" => "secret_config",
22
+ "/development/connect/mysql/host" => "127.0.0.1",
23
+ "/development/connect/secrets/secret_key_base" => "somereallylongstring",
24
+ "/development/connect/symmetric_encryption/key" => "QUJDREVGMTIzNDU2Nzg5MEFCQ0RFRjEyMzQ1Njc4OTA=",
25
+ "/development/connect/symmetric_encryption/version" => 2,
26
+ "/development/connect/symmetric_encryption/iv" => "QUJDREVGMTIzNDU2Nzg5MA=="
27
+ }
28
+ end
29
+
30
+ describe '#each' do
31
+ it 'file' do
32
+ file_provider = SecretConfig::Providers::File.new(file_name: file_name)
33
+ paths = {}
34
+ file_provider.each(root) { |key, value| paths[key] = value }
35
+
36
+ expected.each_pair do |key, value|
37
+ assert_equal value, paths[key], "Path: #{key}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../test_helper'
2
+
3
+ module Providers
4
+ class SsmTest < Minitest::Test
5
+ describe SecretConfig::Providers::Ssm do
6
+ let :file_name do
7
+ File.join(File.dirname(__FILE__), '..', 'config', 'application.yml')
8
+ end
9
+
10
+ let :root do
11
+ "/development/connect"
12
+ end
13
+
14
+ let :expected do
15
+ {
16
+ "/development/connect/mongo/database" => "secret_config_development",
17
+ "/development/connect/mongo/primary" => "127.0.0.1:27017",
18
+ "/development/connect/mongo/secondary" => "127.0.0.1:27018",
19
+ "/development/connect/mysql/database" => "secret_config_development",
20
+ "/development/connect/mysql/password" => "secret_configrules",
21
+ "/development/connect/mysql/username" => "secret_config",
22
+ "/development/connect/mysql/host" => "127.0.0.1",
23
+ "/development/connect/secrets/secret_key_base" => "somereallylongstring",
24
+ "/development/connect/symmetric_encryption/key" => "QUJDREVGMTIzNDU2Nzg5MEFCQ0RFRjEyMzQ1Njc4OTA=",
25
+ "/development/connect/symmetric_encryption/version" => "2",
26
+ "/development/connect/symmetric_encryption/iv" => "QUJDREVGMTIzNDU2Nzg5MA=="
27
+ }
28
+ end
29
+
30
+ describe '#each' do
31
+ it 'fetches all keys in path' do
32
+ upload_settings if ENV['SECRETCONFIG_TEST_UPLOAD_SSM']
33
+
34
+ ssm = SecretConfig::Providers::Ssm.new
35
+ paths = {}
36
+ ssm.each(root) { |key, value| paths[key] = value }
37
+
38
+ expected.each_pair do |key, value|
39
+ assert_equal paths[key], value, "Path: #{key}"
40
+ end
41
+ end
42
+ end
43
+
44
+ def upload_settings
45
+ file_provider = SecretConfig::Providers::File.new(file_name: file_name)
46
+ ssm = SecretConfig::Providers::Ssm.new
47
+ file_provider.each(root) { |key, value| ap key; ssm.set(key, value) }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,112 @@
1
+ require_relative 'test_helper'
2
+
3
+ class RegistryTest < Minitest::Test
4
+ describe SecretConfig::Providers::File do
5
+ let :file_name do
6
+ File.join(File.dirname(__FILE__), 'config', 'application.yml')
7
+ end
8
+
9
+ let :root do
10
+ "/development/connect"
11
+ end
12
+
13
+ let :registry do
14
+ ENV['SECRETCONFIG_FILE_NAME'] = file_name
15
+
16
+ SecretConfig::Registry.new(root: root, provider: :file)
17
+ end
18
+
19
+ let :expected do
20
+ {
21
+ "/development/connect/mongo/database" => "secret_config_development",
22
+ "/development/connect/mongo/primary" => "127.0.0.1:27017",
23
+ "/development/connect/mongo/secondary" => "127.0.0.1:27018",
24
+ "/development/connect/mysql/database" => "secret_config_development",
25
+ "/development/connect/mysql/password" => "secret_configrules",
26
+ "/development/connect/mysql/username" => "secret_config",
27
+ "/development/connect/mysql/host" => "127.0.0.1",
28
+ "/development/connect/secrets/secret_key_base" => "somereallylongstring",
29
+ "/development/connect/symmetric_encryption/key" => "QUJDREVGMTIzNDU2Nzg5MEFCQ0RFRjEyMzQ1Njc4OTA=",
30
+ "/development/connect/symmetric_encryption/version" => 2,
31
+ "/development/connect/symmetric_encryption/iv" => "QUJDREVGMTIzNDU2Nzg5MA=="
32
+ }
33
+ end
34
+
35
+ describe '#configuration' do
36
+ it 'returns a copy of the config' do
37
+ paths = registry.configuration
38
+
39
+ expected.each_pair do |key, value|
40
+ assert_equal value, paths[key], "Path: #{key}"
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#key?' do
46
+ it 'has key' do
47
+ expected.each_pair do |key, value|
48
+ key = key.sub("#{root}/", "")
49
+ assert registry.key?(key), "Path: #{key}"
50
+ end
51
+ end
52
+
53
+ it 'returns false with missing relative key' do
54
+ refute registry.key?("invalid/path")
55
+ end
56
+
57
+ it 'returns nil with missing full key' do
58
+ refute registry.key?("/development/invalid/path")
59
+ end
60
+ end
61
+
62
+ describe '#[]' do
63
+ it 'returns values' do
64
+ expected.each_pair do |key, value|
65
+ key = key.sub("#{root}/", "")
66
+ assert_equal value, registry[key], "Path: #{key}"
67
+ end
68
+ end
69
+
70
+ it 'returns nil with missing relative key' do
71
+ assert_nil registry["invalid/path"]
72
+ end
73
+
74
+ it 'returns nil with missing full key' do
75
+ assert_nil registry["/development/invalid/path"]
76
+ end
77
+ end
78
+
79
+ describe '#fetch' do
80
+ it 'returns values' do
81
+ expected.each_pair do |key, value|
82
+ key = key.sub("#{root}/", "")
83
+ assert_equal value, registry.fetch(key), "Path: #{key}"
84
+ end
85
+ end
86
+
87
+ it 'exception missing relative key' do
88
+ assert_raises SecretConfig::MissingMandatoryKey do
89
+ registry.fetch("invalid/path")
90
+ end
91
+ end
92
+
93
+ it 'returns nil with missing full key' do
94
+ assert_raises SecretConfig::MissingMandatoryKey do
95
+ registry.fetch("/development/invalid/path")
96
+ end
97
+ end
98
+
99
+ it 'returns default with missing key' do
100
+ assert_equal "default_value", registry.fetch("/development/invalid/path", default: "default_value")
101
+ end
102
+
103
+ it 'converts to integer' do
104
+ assert_equal 2, registry.fetch("symmetric_encryption/version", type: :integer)
105
+ end
106
+
107
+ it 'decodes Base 64' do
108
+ assert_equal "ABCDEF1234567890ABCDEF1234567890", registry.fetch("symmetric_encryption/key", encoding: :base64)
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SecretConfigTest < Minitest::Test
4
+ describe SecretConfig::Providers::File do
5
+ let :file_name do
6
+ File.join(File.dirname(__FILE__), 'config', 'application.yml')
7
+ end
8
+
9
+ let :root do
10
+ "/development/connect"
11
+ end
12
+
13
+ before do
14
+ ENV['SECRETCONFIG_FILE_NAME'] = file_name
15
+ SecretConfig.root = root
16
+ SecretConfig.provider = :file
17
+ end
18
+
19
+ describe '#configuration' do
20
+ it 'returns a copy of the config' do
21
+ assert_equal "127.0.0.1", SecretConfig.configuration["/development/connect/mysql/host"]
22
+ end
23
+ end
24
+
25
+ describe '#key?' do
26
+ it 'has key' do
27
+ assert SecretConfig.key?("mysql/database")
28
+ end
29
+ end
30
+
31
+ describe '#[]' do
32
+ it 'returns values' do
33
+ assert_equal "secret_config_development", SecretConfig["mysql/database"]
34
+ end
35
+ end
36
+
37
+ describe '#fetch' do
38
+ it 'fetches values' do
39
+ assert_equal "secret_config_development", SecretConfig.fetch("mysql/database")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'yaml'
4
+ require 'minitest/autorun'
5
+ require 'minitest/reporters'
6
+ require 'secret_config'
7
+ require 'awesome_print'
8
+
9
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secret_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Reid Morrison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sync_attr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: aws-sdk-ssm
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - reidmo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/secret_config.rb
66
+ - lib/secret_config/errors.rb
67
+ - lib/secret_config/providers/file.rb
68
+ - lib/secret_config/providers/ssm.rb
69
+ - lib/secret_config/registry.rb
70
+ - lib/secret_config/version.rb
71
+ - test/config/application.yml
72
+ - test/providers/file_test.rb
73
+ - test/providers/ssm_test.rb
74
+ - test/registry_test.rb
75
+ - test/secret_config_test.rb
76
+ - test/test_helper.rb
77
+ homepage: https://github.com/rocketjob/secret_config
78
+ licenses:
79
+ - Apache-2.0
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Centralized Configuration and Secrets Management for Ruby and Rails applications.
100
+ test_files:
101
+ - test/config/application.yml
102
+ - test/providers/ssm_test.rb
103
+ - test/providers/file_test.rb
104
+ - test/registry_test.rb
105
+ - test/test_helper.rb
106
+ - test/secret_config_test.rb