config-rails 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/lib/config/rails_credentials/railtie.rb +13 -0
- data/lib/config/rails_credentials/version.rb +5 -0
- data/lib/config/rails_credentials.rb +29 -0
- data/lib/config/sources/rails_credentials_source.rb +24 -0
- data/lib/config-rails.rb +1 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3f400091fc360f391ce5812d4a7e37df84c878059d05a8758c73789a03326777
|
|
4
|
+
data.tar.gz: 9be7545904371f43c70f51ee52afdd2cf9632bf05a87b2acf2b29d970a383e37
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4327e4b039a12b664533990afe7807f95392c4db7f457ce9708f88ca606ec8440bd8343fce4c40f04ac74e2d50f75efc1d01fb21e9915acb13599827f21e0953
|
|
7
|
+
data.tar.gz: 0fd07b41244b144c5a7a82b3edd196542213828a7a177d4e893aa27bc77d5cc83ec9d4a084f9d50bbe510836bbda07f7e0fe44df45a3dc136dd6d81db52f8c93
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Deepak Mahakale
|
|
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,91 @@
|
|
|
1
|
+
# config-rails
|
|
2
|
+
|
|
3
|
+
Adds Rails encrypted credentials to the [config](https://github.com/rubyconfig/config) gem's `Settings`
|
|
4
|
+
([rubyconfig/config#68](https://github.com/rubyconfig/config/issues/68)).
|
|
5
|
+
|
|
6
|
+
Keep using `Settings.x.y`. Values come from your settings YAML files as before, plus `Rails.application.credentials`.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem "config"
|
|
12
|
+
gem "config-rails"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
There is nothing else to set up.
|
|
16
|
+
|
|
17
|
+
## Precedence
|
|
18
|
+
|
|
19
|
+
Later sources win, deep-merged:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
config/settings.yml
|
|
23
|
+
config/settings/<env>.yml
|
|
24
|
+
config/environments/<env>.yml
|
|
25
|
+
*.local.yml
|
|
26
|
+
Config.extra_sources
|
|
27
|
+
Rails credentials <- added by this gem
|
|
28
|
+
ENV (when Config.use_env)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
So you can keep defaults in YAML and put only the secrets in credentials:
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
# config/settings.yml
|
|
35
|
+
stripe:
|
|
36
|
+
api_base: https://api.stripe.com
|
|
37
|
+
secret_key: ~
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```yaml
|
|
41
|
+
# bin/rails credentials:edit
|
|
42
|
+
stripe:
|
|
43
|
+
secret_key: sk_live_...
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
Settings.stripe.api_base # => "https://api.stripe.com"
|
|
48
|
+
Settings.stripe.secret_key # => "sk_live_..."
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Credentials are read the same way Rails reads them. This covers per-environment files
|
|
52
|
+
(`config/credentials/<env>.yml.enc`), `RAILS_MASTER_KEY`, `config.credentials.content_path` / `key_path`,
|
|
53
|
+
and `config.require_master_key`. If no key is available, `Settings` holds only the YAML values, unless
|
|
54
|
+
`require_master_key` is set, in which case it raises.
|
|
55
|
+
|
|
56
|
+
## Disabling
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
# config/initializers/config.rb
|
|
60
|
+
Config.setup do |config|
|
|
61
|
+
config.use_rails_credentials = false
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## How it works
|
|
66
|
+
|
|
67
|
+
This gem is a small patch on top of `config`:
|
|
68
|
+
|
|
69
|
+
* `Config.load_and_set_settings` (which builds the `Settings` constant) gets an extra source,
|
|
70
|
+
`Config::Sources::RailsCredentialsSource`. `Config.load_files` is left alone.
|
|
71
|
+
* The source decrypts the credentials again on every load, so `Settings.reload!`, and the per-request
|
|
72
|
+
reload in development, pick up edits.
|
|
73
|
+
* It does **not** call `Rails.application.credentials`, because that method caches its result. `config` builds
|
|
74
|
+
`Settings` before `config/application.rb` runs. Reading that method at that point would cache the credentials
|
|
75
|
+
before your app could change their path, which is the "eager loads credentials" problem reported in #68.
|
|
76
|
+
* `Settings` is rebuilt once in `before_initialize`, after `config/application.rb` and
|
|
77
|
+
`config/environments/*.rb` have run, so settings made there (custom paths, `require_master_key`) are picked up.
|
|
78
|
+
|
|
79
|
+
Caveat: if `config/application.rb` itself reads `Settings`, it sees the credentials from the default path,
|
|
80
|
+
because your overrides have not run yet at that point.
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
bundle install
|
|
86
|
+
bundle exec rake test
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module Config
|
|
2
|
+
module RailsCredentials
|
|
3
|
+
class Railtie < ::Rails::Railtie
|
|
4
|
+
# Config builds Settings in before_configuration, i.e. before the body of
|
|
5
|
+
# config/application.rb and config/environments/*.rb have run. Rebuild it
|
|
6
|
+
# once they have, so custom config.credentials paths and
|
|
7
|
+
# config.require_master_key are honoured.
|
|
8
|
+
config.before_initialize do
|
|
9
|
+
::Config.reload! if ::Config.use_rails_credentials
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "config"
|
|
2
|
+
require "config/rails_credentials/version"
|
|
3
|
+
require "config/sources/rails_credentials_source"
|
|
4
|
+
|
|
5
|
+
module Config
|
|
6
|
+
extend Config::Configuration.new(use_rails_credentials: true)
|
|
7
|
+
|
|
8
|
+
module RailsCredentials
|
|
9
|
+
# Adds Rails credentials as a source of the Settings constant, after the
|
|
10
|
+
# YAML files and before ENV (when Config.use_env is on), so:
|
|
11
|
+
#
|
|
12
|
+
# settings.yml < settings/<env>.yml < *.local.yml < credentials < ENV
|
|
13
|
+
module LoadAndSetSettings
|
|
14
|
+
def load_and_set_settings(*sources)
|
|
15
|
+
return super unless Config.use_rails_credentials && RailsCredentials.rails_app?
|
|
16
|
+
|
|
17
|
+
super(*sources, Config::Sources::RailsCredentialsSource.new)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.rails_app?
|
|
22
|
+
defined?(::Rails) && ::Rails.respond_to?(:application) && !::Rails.application.nil?
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
singleton_class.prepend RailsCredentials::LoadAndSetSettings
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
require "config/rails_credentials/railtie" if defined?(::Rails::Railtie)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Config
|
|
2
|
+
module Sources
|
|
3
|
+
class RailsCredentialsSource
|
|
4
|
+
# Credentials are decrypted on every load, so Settings.reload! (and the
|
|
5
|
+
# per-request reload in development) picks up edited credentials.
|
|
6
|
+
#
|
|
7
|
+
# Keys are stringified to match the YAML and ENV sources; otherwise
|
|
8
|
+
# DeepMerge treats :foo and "foo" as different keys.
|
|
9
|
+
def load
|
|
10
|
+
credentials.config.deep_stringify_keys
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# Deliberately not Rails.application.credentials: that is memoized, and
|
|
16
|
+
# this runs before config/application.rb, so memoizing here would pin the
|
|
17
|
+
# credentials before the app gets a chance to configure them.
|
|
18
|
+
def credentials
|
|
19
|
+
app = ::Rails.application
|
|
20
|
+
app.encrypted(app.config.credentials.content_path, key_path: app.config.credentials.key_path)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
data/lib/config-rails.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "config/rails_credentials"
|
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: config-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Deepak Mahakale
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: config
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '5.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '6'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '5.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '6'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: railties
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '7.1'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '7.1'
|
|
46
|
+
description: A small patch for the config gem (rubyconfig/config) that adds Rails.application.credentials
|
|
47
|
+
as a Settings source, so Settings.x.y reads from settings YAML files and Rails credentials
|
|
48
|
+
alike.
|
|
49
|
+
email:
|
|
50
|
+
- deepakmahakale@gmail.com
|
|
51
|
+
executables: []
|
|
52
|
+
extensions: []
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- CHANGELOG.md
|
|
56
|
+
- LICENSE.txt
|
|
57
|
+
- README.md
|
|
58
|
+
- lib/config-rails.rb
|
|
59
|
+
- lib/config/rails_credentials.rb
|
|
60
|
+
- lib/config/rails_credentials/railtie.rb
|
|
61
|
+
- lib/config/rails_credentials/version.rb
|
|
62
|
+
- lib/config/sources/rails_credentials_source.rb
|
|
63
|
+
homepage: https://github.com/deepakmahakale/config-rails
|
|
64
|
+
licenses:
|
|
65
|
+
- MIT
|
|
66
|
+
metadata:
|
|
67
|
+
source_code_uri: https://github.com/deepakmahakale/config-rails
|
|
68
|
+
changelog_uri: https://github.com/deepakmahakale/config-rails/blob/master/CHANGELOG.md
|
|
69
|
+
bug_tracker_uri: https://github.com/deepakmahakale/config-rails/issues
|
|
70
|
+
rubygems_mfa_required: 'true'
|
|
71
|
+
rdoc_options: []
|
|
72
|
+
require_paths:
|
|
73
|
+
- lib
|
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '3.1'
|
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
requirements: []
|
|
85
|
+
rubygems_version: 4.0.20
|
|
86
|
+
specification_version: 4
|
|
87
|
+
summary: Merge Rails encrypted credentials into the config gem's Settings
|
|
88
|
+
test_files: []
|