smart_credentials 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3185b9dfdee2fb9e4d543184ba63f55f13f5d22c3119a5b8f441174e0270bce
4
+ data.tar.gz: 76c41546d8df16846954300e6f6bb12c05086ce9cad00664868e08fa53ee8382
5
+ SHA512:
6
+ metadata.gz: 4d4c21211508dd45eab5358ced09c61260182385be09fd73182dfe772e09c2bef3723b1b5e64640af69ab3d24c64a40b9c8229417def7211f6a9fda2d8eb97d5
7
+ data.tar.gz: 5cac75d30d501e0b254c319cd42e7608b0505018a572530a56bd5f16ae4ada8ac81f9b04f5fe47584b69e03d367935de641917573aecd87135e38f8e018b20bb
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.0.0] - 2025-10-21
6
+
7
+ ### Added
8
+ - Initial stable release
9
+ - ENV variable override with Rails credentials fallback
10
+ - Support for nested credential access (e.g., `SmartCredentials.aws.access_key_id`)
11
+ - Support for deeply nested credentials (e.g., `SmartCredentials.aws.s3.bucket_name`)
12
+ - Hash-style access support (`SmartCredentials[:api_key]`)
13
+ - `dig` method for nested access
14
+ - Configuration via `SmartCredentials::Config.setup`
15
+ - Custom ENV prefix and ENV separator configuration
16
+
17
+ [1.0.0]: https://github.com/yarbro/smart_credentials/releases/tag/v1.0.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 David Yarbro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # SmartCredentials
2
+
3
+ SmartCredentials intelligently manages your Rails application credentials by allowing environment variables to override Rails encrypted credentials with automatic fallback. Perfect for development, staging, and production environments where you want the flexibility of ENV variables without losing the security of encrypted credentials.
4
+
5
+ ## Features
6
+
7
+ - **Automatic Fallback** - ENV variables take priority, falling back to Rails credentials
8
+ - **Nested Credentials** - Full support for deeply nested credential structures
9
+ - **Familiar Interface** - Works just like `Rails.application.credentials`
10
+ - **Multiple Access Patterns** - Method chaining, hash-style, and dig support
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+ ```ruby
16
+ gem "smart_credentials"
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Basic Usage
22
+
23
+ Instead of using `Rails.application.credentials.api_key`, use:
24
+ ```ruby
25
+ SmartCredentials.api_key
26
+ ```
27
+
28
+ This will:
29
+ 1. First check for `ENV["API_KEY"]`
30
+ 2. If not found, fall back to `Rails.application.credentials.api_key`
31
+
32
+ ### Nested Credentials
33
+
34
+ For nested credentials like `Rails.application.credentials.aws.access_key_id`:
35
+ ```ruby
36
+ SmartCredentials.aws.access_key_id
37
+ ```
38
+
39
+ This will:
40
+ 1. First check for `ENV["AWS_ACCESS_KEY_ID"]`
41
+ 2. If not found, fall back to `Rails.application.credentials.aws.access_key_id`
42
+
43
+ ### Hash-Style Access
44
+
45
+ You can also use hash-style access:
46
+ ```ruby
47
+ SmartCredentials[:api_key]
48
+ SmartCredentials[:aws][:access_key_id]
49
+
50
+ # or mix and match
51
+ SmartCredentials.aws[:access_key_id]
52
+ SmartCredentials[:aws].access_key_id
53
+ ```
54
+
55
+ ### Dig Method
56
+
57
+ For deeply nested values:
58
+ ```ruby
59
+ SmartCredentials.dig(:aws, :s3, :bucket_name)
60
+ # Checks ENV["AWS_S3_BUCKET_NAME"]
61
+ # Falls back to Rails.application.credentials.aws.s3.bucket_name
62
+ ```
63
+
64
+ ## Configuration
65
+
66
+ SmartCredentials works without any configuration, but you can customize its behavior.
67
+
68
+ ### Using an Initializer
69
+
70
+ Create `config/initializers/smart_credentials.rb`:
71
+ ```ruby
72
+ # Block style
73
+ SmartCredentials::Config.setup do |config|
74
+ config.env_prefix = "MYAPP"
75
+ config.env_separator = "__"
76
+ end
77
+
78
+ # or direct assignment style
79
+ SmartCredentials::Config.env_prefix = "MYAPP"
80
+ SmartCredentials::Config.env_separator = "__"
81
+ ```
82
+
83
+ ### Configuration Options
84
+
85
+ #### `env_prefix`
86
+
87
+ Add a prefix to all ENV variable lookups:
88
+ ```ruby
89
+ SmartCredentials::Config.env_prefix = "MYAPP"
90
+
91
+ # Now looks for ENV["MYAPP_API_KEY"] instead of ENV["API_KEY"]
92
+ SmartCredentials.api_key
93
+ ```
94
+
95
+ **Default:** `nil` (no prefix)
96
+
97
+ #### `env_separator`
98
+
99
+ Change the separator used for nested keys:
100
+ ```ruby
101
+ SmartCredentials::Config.env_separator = "__"
102
+
103
+ # Now looks for ENV["AWS__ACCESS_KEY_ID"] instead of ENV["AWS_ACCESS_KEY_ID"]
104
+ SmartCredentials.aws.access_key_id
105
+
106
+ # Also affects prefix separator:
107
+ SmartCredentials::Config.env_prefix = "MYAPP"
108
+ # Now looks for ENV["MYAPP__AWS__ACCESS_KEY_ID"] instead of ENV["MYAPP_AWS_ACCESS_KEY_ID"]
109
+ SmartCredentials.aws.access_key_id
110
+ ```
111
+
112
+ **Default:** `"_"` (underscore)
113
+
114
+ ## Real-World Examples
115
+
116
+ ### Example credentials.yml.enc
117
+ ```yaml
118
+ production:
119
+ api_key: "production_key_from_credentials"
120
+ secret_key_base: "long_secret_string"
121
+ aws:
122
+ access_key_id: "AKIAIOSFODNN7EXAMPLE"
123
+ secret_access_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
124
+ region: "us-east-1"
125
+ s3:
126
+ bucket_name: "my-app-production"
127
+ stripe:
128
+ publishable_key: "pk_live_123"
129
+ secret_key: "sk_live_456"
130
+ database:
131
+ host: "db.example.com"
132
+ username: "app_user"
133
+ password: "secure_password"
134
+ ```
135
+
136
+ ### AWS Configuration
137
+ ```ruby
138
+ # config/initializers/aws.rb
139
+ Aws.config.update(
140
+ credentials: Aws::Credentials.new(
141
+ SmartCredentials.aws.access_key_id,
142
+ SmartCredentials.aws.secret_access_key
143
+ ),
144
+ region: SmartCredentials.aws.region
145
+ )
146
+ ```
147
+
148
+ ### Stripe Configuration
149
+ ```ruby
150
+ # config/initializers/stripe.rb
151
+ Stripe.api_key = SmartCredentials.stripe.secret_key
152
+ ```
153
+
154
+ ### Database Configuration
155
+ ```ruby
156
+ # config/database.yml
157
+ production:
158
+ adapter: postgresql
159
+ host: <%= SmartCredentials.database.host %>
160
+ username: <%= SmartCredentials.database.username %>
161
+ password: <%= SmartCredentials.database.password %>
162
+ database: myapp_production
163
+ ```
164
+
165
+ ### In Your Application Code
166
+ ```ruby
167
+ # app/services/api_client.rb
168
+ class ApiClient
169
+ def initialize
170
+ @api_key = SmartCredentials.api_key
171
+ @base_url = SmartCredentials.api.base_url
172
+ end
173
+
174
+ def call
175
+ HTTParty.get(@base_url, headers: { "Authorization" => "Bearer #{@api_key}" })
176
+ end
177
+ end
178
+ ```
179
+
180
+ ## Use Cases
181
+
182
+ ### Development
183
+
184
+ Keep encrypted credentials for your team while allowing individual developers to override specific values:
185
+ ```bash
186
+ # Developer can use their own AWS credentials without touching credentials file
187
+ export AWS_ACCESS_KEY_ID="my_dev_key"
188
+ export AWS_SECRET_ACCESS_KEY="my_dev_secret"
189
+ ```
190
+
191
+ ### Staging/Production
192
+
193
+ Use ENV variables for secrets in production while keeping sensible defaults in credentials for development:
194
+ ```ruby
195
+ # Always works in any environment
196
+ SmartCredentials.stripe.secret_key
197
+ ```
198
+
199
+ ## Behavior
200
+
201
+ SmartCredentials behaves like Rails credentials:
202
+ ```ruby
203
+ # Returns the value if it exists
204
+ SmartCredentials.api_key
205
+ # => "your_api_key"
206
+
207
+ # Returns nil if it doesn't exist
208
+ SmartCredentials.nonexistent_key
209
+ # => nil
210
+
211
+ # Raises NoMethodError when chaining after nil (like Rails)
212
+ SmartCredentials.nonexistent.nested.key
213
+ # => NoMethodError: undefined method `nested' for nil
214
+ ```
215
+
216
+ ## Contributing
217
+
218
+ Bug reports and pull requests are welcome.
219
+
220
+ 1. Fork it
221
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
222
+ 3. Commit your changes (`git commit -am "Added some new feature"`)
223
+ 4. Push to the branch (`git push origin my-new-feature`)
224
+ 5. Create a new Pull Request
225
+
226
+ ## License
227
+
228
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCredentials
4
+ class Accessor
5
+ def initialize(path = [])
6
+ @path = path
7
+ end
8
+
9
+ def method_missing(method_name, *args, &block)
10
+ if args.empty? && !block_given?
11
+ fetch(method_name)
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ def respond_to_missing?(_method_name, _include_private = false)
18
+ true
19
+ end
20
+
21
+ def [](key)
22
+ fetch(key)
23
+ end
24
+
25
+ def dig(*keys)
26
+ keys.reduce(self) do |acc, key|
27
+ break nil if acc.nil?
28
+
29
+ acc[key]
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def fetch(key)
36
+ new_path = @path + [key]
37
+ env_value = fetch_from_env(new_path)
38
+ return env_value if env_value
39
+
40
+ credentials_value = fetch_from_credentials(new_path)
41
+
42
+ if hash_like?(credentials_value)
43
+ Accessor.new(new_path)
44
+ else
45
+ credentials_value
46
+ end
47
+ end
48
+
49
+ def fetch_from_env(path)
50
+ env_key = build_env_key(path)
51
+ ENV[env_key]
52
+ end
53
+
54
+ def build_env_key(path)
55
+ prefix = SmartCredentials::Config.env_prefix
56
+ separator = SmartCredentials::Config.env_separator
57
+
58
+ key_parts = path.map { |part| part.to_s.upcase }
59
+ key_parts.unshift(prefix.upcase) if prefix
60
+
61
+ key_parts.join(separator)
62
+ end
63
+
64
+ def fetch_from_credentials(path)
65
+ return nil unless rails_credentials_available?
66
+
67
+ credentials = Rails.application.credentials
68
+ path.reduce(credentials) do |creds, key|
69
+ return nil if creds.nil?
70
+
71
+ access_credential(creds, key)
72
+ end
73
+ end
74
+
75
+ def rails_credentials_available?
76
+ defined?(Rails) && Rails.application.respond_to?(:credentials)
77
+ end
78
+
79
+ def access_credential(creds, key)
80
+ if creds.respond_to?(:[])
81
+ creds[key.to_sym] || creds[key.to_s]
82
+ elsif creds.respond_to?(key)
83
+ creds.public_send(key)
84
+ end
85
+ end
86
+
87
+ def hash_like?(value)
88
+ value.is_a?(Hash) ||
89
+ (defined?(ActiveSupport::HashWithIndifferentAccess) &&
90
+ value.is_a?(ActiveSupport::HashWithIndifferentAccess))
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCredentials
4
+ module Config
5
+ DEFAULTS = {
6
+ env_prefix: nil,
7
+ env_separator: "_"
8
+ }.freeze
9
+
10
+ class << self
11
+ attr_accessor :env_prefix
12
+ attr_writer :env_separator
13
+
14
+ def setup
15
+ yield self
16
+ end
17
+
18
+ def env_separator
19
+ @env_separator ||= DEFAULTS[:env_separator]
20
+ end
21
+ end
22
+
23
+ @env_prefix = DEFAULTS[:env_prefix]
24
+ @env_separator = DEFAULTS[:env_separator]
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCredentials
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "smart_credentials/version"
4
+ require_relative "smart_credentials/config"
5
+ require_relative "smart_credentials/accessor"
6
+
7
+ module SmartCredentials
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ def method_missing(method_name, *args, &block)
12
+ if args.empty? && !block_given?
13
+ Accessor.new.public_send(method_name)
14
+ else
15
+ super
16
+ end
17
+ end
18
+
19
+ def respond_to_missing?(_method_name, _include_private = false)
20
+ true
21
+ end
22
+
23
+ def [](key)
24
+ Accessor.new[key]
25
+ end
26
+
27
+ def dig(*keys)
28
+ Accessor.new.dig(*keys)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module SmartCredentials
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_credentials
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Yarbro
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ description: Seamlessly use environment variables with automatic fallback to Rails
27
+ credentials, supporting nested values and flexible configuration
28
+ email:
29
+ - david@yarb.ro
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/smart_credentials.rb
39
+ - lib/smart_credentials/accessor.rb
40
+ - lib/smart_credentials/config.rb
41
+ - lib/smart_credentials/version.rb
42
+ - sig/smart_credentials.rbs
43
+ homepage: https://github.com/yarbro/smart_credentials
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/yarbro/smart_credentials
48
+ source_code_uri: https://github.com/yarbro/smart_credentials
49
+ changelog_uri: https://github.com/yarbro/smart_credentials/releases
50
+ rubygems_mfa_required: 'true'
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.7.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.6.9
66
+ specification_version: 4
67
+ summary: Intelligently override Rails credentials with environment variables
68
+ test_files: []