kreds 0.1.0 → 1.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 +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +66 -6
- data/kreds.gemspec +2 -2
- data/lib/kreds/fetch.rb +68 -6
- data/lib/kreds/show.rb +7 -0
- data/lib/kreds/version.rb +1 -1
- data/lib/kreds.rb +7 -3
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de9b5e5f467bfe86da6487a402620213b7108dbcd90d64a5c088e8f563dc466e
|
4
|
+
data.tar.gz: c147c37a7ca2ea09f166022b0ffb294c5f452a33392a8d15edfe07ed30167a40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 164bdf54d2f0ec9037cc568c6749bd22fdca2fed40388a74f9a35a8954cf34cc74697efdb2516fa57c34966c194f9d65c466c0a447f6ca4489504c1d3c913bb3
|
7
|
+
data.tar.gz: 90850b11cbe6f9890dd7e60752251af0f3e323cf7c87a460677fef764a80421690d9de2979fd0e0761ffd29295fc686501385fd8e911bd6c35af8aaad51b8383
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## v1.1.0
|
2
|
+
|
3
|
+
- Added block support for fallback behavior in all fetching methods
|
4
|
+
|
5
|
+
## v1.0.0
|
6
|
+
|
7
|
+
- Dropped support for Rails 7.0
|
8
|
+
- Dropped support for Ruby 3.1
|
9
|
+
- Replaced `Kreds::BlankValueError` and `Kreds::UnknownKeyError` with `Kreds::BlankCredentialsError` and `Kreds::UnknownCredentialsError`, respectively
|
10
|
+
- Added optional fallback to environment variables for `Kreds.fetch!` method
|
11
|
+
- Added shortcut method `Kreds.var!` for fetching values directly from environment variables
|
12
|
+
- Added `Kreds.env_fetch!` method for fetching values from credentials per Rails environment
|
13
|
+
- Added `Kreds.show` method for displaying all credentials as a hash
|
14
|
+
|
1
15
|
## v0.1.0
|
2
16
|
|
3
17
|
- Initial release
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](http://badge.fury.io/rb/kreds)
|
4
4
|
[](https://github.com/enjaku4/kreds/actions/workflows/ci.yml)
|
5
5
|
|
6
|
-
Kreds is a simpler and
|
6
|
+
Kreds is a simpler, shorter, and safer way to access Rails credentials, with a few additional features built in. Rails credentials are a convenient way to store secrets, but retrieving them could be more intuitive — that’s where Kreds comes in.
|
7
7
|
|
8
8
|
Instead of writing:
|
9
9
|
|
@@ -17,7 +17,7 @@ You can simply use:
|
|
17
17
|
Kreds.fetch!(:recaptcha, :site_key)
|
18
18
|
```
|
19
19
|
|
20
|
-
This not only shortens your code but also ensures an exception is raised if a key is missing or a value is blank
|
20
|
+
This not only shortens your code but also ensures an exception is raised if a key is missing or a value is blank — with a clear, human-readable error message.
|
21
21
|
|
22
22
|
## Installation
|
23
23
|
|
@@ -35,17 +35,77 @@ bundle install
|
|
35
35
|
|
36
36
|
## Usage
|
37
37
|
|
38
|
-
|
38
|
+
### Fetch from credentials
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
Kreds.fetch!(:aws, :s3, :credentials, :access_key_id)
|
42
42
|
```
|
43
43
|
|
44
|
-
If you make a typo, such as writing `access_key` instead of `access_key_id`, Kreds will raise `Kreds::
|
44
|
+
If you make a typo, such as writing `access_key` instead of `access_key_id`, Kreds will raise `Kreds::UnknownCredentialsError` with the message: `Credentials key not found: [:aws][:s3][:credentials][:access_key]`.
|
45
45
|
|
46
|
-
Similarly, if you add an extra key that doesn’t exist
|
46
|
+
Similarly, if you add an extra key that doesn’t exist: `Kreds.fetch!(:aws, :s3, :credentials, :access_key_id, :id)`, Kreds will raise `Kreds::UnknownCredentialsError` with the message: `Credentials key not found: [:aws][:s3][:credentials][:access_key_id][:id]`.
|
47
47
|
|
48
|
-
|
48
|
+
If all keys are correct but the value is blank, Kreds will raise `Kreds::BlankCredentialsError` with the message: `Blank value in credentials: [:aws][:s3][:credentials][:access_key_id]`.
|
49
|
+
|
50
|
+
### Fallback to environment variables
|
51
|
+
|
52
|
+
You can optionally provide a fallback environment variable:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Kreds.fetch!(:aws, :s3, :credentials, :access_key_id, var: "AWS_ACCESS_KEY_ID")
|
56
|
+
```
|
57
|
+
|
58
|
+
If the key is missing or blank in credentials, Kreds will attempt to fetch the value from the specified environment variable.
|
59
|
+
|
60
|
+
If both sources are missing or blank, a combined error is raised.
|
61
|
+
|
62
|
+
### Directly fetch from environment variable
|
63
|
+
|
64
|
+
You can also fetch directly from an environment variable:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
Kreds.var!("AWS_ACCESS_KEY_ID")
|
68
|
+
```
|
69
|
+
|
70
|
+
This raises `Kreds::UnknownEnvironmentVariableError` if the variable is missing, and `Kreds::BlankEnvironmentVariableError` if it is present but the value is blank.
|
71
|
+
|
72
|
+
### Fetch per Rails environment
|
73
|
+
|
74
|
+
If your credentials are scoped by Rails environment (e.g., `:production`, `:staging`, `:development`), you can fetch keys under the current environment using:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
Kreds.env_fetch!(:recaptcha, :site_key)
|
78
|
+
```
|
79
|
+
|
80
|
+
This will look for the key in `Rails.application.credentials[Rails.env]`. For example, in the `production` environment, it will look for `Rails.application.credentials[:production][:recaptcha][:site_key]`, and raise the same errors if the key is missing or the value is blank.
|
81
|
+
|
82
|
+
You can also provide an optional fallback environment variable:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Kreds.env_fetch!(:recaptcha, :site_key, var: "RECAPTCHA_SITE_KEY")
|
86
|
+
```
|
87
|
+
|
88
|
+
### Pass a block on failure
|
89
|
+
|
90
|
+
You can pass a block to `fetch!`, `env_fetch!`, and `var!`, which will be executed if the method fails to retrieve the value.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
Kreds.fetch!(:aws, :s3, :credentials, :access_key_id) do
|
94
|
+
raise MyCustomError, "Custom error message"
|
95
|
+
end
|
96
|
+
|
97
|
+
Kreds.var!("THREADS") { 1 }
|
98
|
+
```
|
99
|
+
|
100
|
+
### Show credentials
|
101
|
+
|
102
|
+
To inspect all credentials as a hash:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
Kreds.show
|
106
|
+
```
|
107
|
+
|
108
|
+
Useful for debugging or working in the Rails console.
|
49
109
|
|
50
110
|
## Problems?
|
51
111
|
|
data/kreds.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.metadata["rubygems_mfa_required"] = "true"
|
12
12
|
spec.summary = "The missing shorthand for Rails credentials"
|
13
13
|
spec.license = "MIT"
|
14
|
-
spec.required_ruby_version = ">= 3.
|
14
|
+
spec.required_ruby_version = ">= 3.2", "< 3.5"
|
15
15
|
|
16
16
|
spec.files = [
|
17
17
|
"kreds.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
|
@@ -19,5 +19,5 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "rails", ">= 7.
|
22
|
+
spec.add_dependency "rails", ">= 7.1", "< 8.1"
|
23
23
|
end
|
data/lib/kreds/fetch.rb
CHANGED
@@ -1,20 +1,82 @@
|
|
1
1
|
module Kreds
|
2
2
|
module Fetch
|
3
|
-
def fetch!(*keys)
|
3
|
+
def fetch!(*keys, var: nil, &)
|
4
|
+
validate_keys!(keys)
|
5
|
+
validate_var!(var)
|
6
|
+
|
4
7
|
path = []
|
5
8
|
|
6
9
|
keys.reduce(Rails.application.credentials) do |hash, key|
|
7
10
|
path << key
|
11
|
+
fetch_key(hash, key, path, keys)
|
12
|
+
end
|
13
|
+
rescue Kreds::BlankCredentialsError, Kreds::UnknownCredentialsError => e
|
14
|
+
fallback_to_var(e, var, &)
|
15
|
+
end
|
16
|
+
|
17
|
+
def env_fetch!(*keys, var: nil, &)
|
18
|
+
fetch!(Rails.env, *keys, var: var, &)
|
19
|
+
end
|
20
|
+
|
21
|
+
def var!(var, &)
|
22
|
+
validate_var!(var)
|
8
23
|
|
9
|
-
|
24
|
+
result, success = check_var(var)
|
10
25
|
|
11
|
-
|
12
|
-
|
26
|
+
return result if success
|
27
|
+
|
28
|
+
raise_or_yield(result, &)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def validate_keys!(keys)
|
34
|
+
raise Kreds::InvalidArgumentError, "No keys provided" if keys.empty?
|
35
|
+
|
36
|
+
return if keys.all? { _1.is_a?(Symbol) || _1.is_a?(String) }
|
37
|
+
|
38
|
+
raise Kreds::InvalidArgumentError, "Credentials Key must be a Symbol or a String"
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate_var!(var)
|
42
|
+
raise Kreds::InvalidArgumentError, "Environment variable must be a String" if var.present? && !var.is_a?(String)
|
43
|
+
end
|
13
44
|
|
14
|
-
|
45
|
+
def fetch_key(hash, key, path, keys)
|
46
|
+
value = hash.fetch(key.to_sym)
|
47
|
+
|
48
|
+
raise Kreds::BlankCredentialsError, "Blank value in credentials: [:#{path.join("][:")}]" if value.blank?
|
49
|
+
raise Kreds::UnknownCredentialsError, "Credentials key not found: [:#{path.join("][:")}][:#{keys[path.size]}]" if !value.is_a?(Hash) && keys != path
|
50
|
+
|
51
|
+
value
|
52
|
+
rescue KeyError
|
53
|
+
raise Kreds::UnknownCredentialsError, "Credentials key not found: [:#{path.join("][:")}]"
|
54
|
+
end
|
55
|
+
|
56
|
+
def fallback_to_var(error, var, &)
|
57
|
+
if var.present?
|
58
|
+
result, success = check_var(var)
|
59
|
+
|
60
|
+
return result if success
|
61
|
+
|
62
|
+
raise_or_yield(Kreds::Error.new([error.message, result.message].join(", ")), &)
|
15
63
|
end
|
64
|
+
|
65
|
+
raise_or_yield(error, &)
|
66
|
+
end
|
67
|
+
|
68
|
+
def check_var(var)
|
69
|
+
value = ENV.fetch(var)
|
70
|
+
|
71
|
+
return [Kreds::BlankEnvironmentVariableError.new("Blank value in environment variable: #{var.inspect}"), false] if value.blank?
|
72
|
+
|
73
|
+
[value, true]
|
16
74
|
rescue KeyError
|
17
|
-
|
75
|
+
[Kreds::UnknownEnvironmentVariableError.new("Environment variable not found: #{var.inspect}"), false]
|
76
|
+
end
|
77
|
+
|
78
|
+
def raise_or_yield(error, &)
|
79
|
+
block_given? ? yield : raise(error)
|
18
80
|
end
|
19
81
|
end
|
20
82
|
end
|
data/lib/kreds/show.rb
ADDED
data/lib/kreds/version.rb
CHANGED
data/lib/kreds.rb
CHANGED
@@ -3,12 +3,16 @@ require_relative "kreds/version"
|
|
3
3
|
require "rails"
|
4
4
|
|
5
5
|
require_relative "kreds/fetch"
|
6
|
+
require_relative "kreds/show"
|
6
7
|
|
7
8
|
module Kreds
|
8
9
|
class Error < StandardError; end
|
9
|
-
|
10
|
-
class
|
11
|
-
class
|
10
|
+
class InvalidArgumentError < Error; end
|
11
|
+
class BlankCredentialsError < Error; end
|
12
|
+
class BlankEnvironmentVariableError < Error; end
|
13
|
+
class UnknownCredentialsError < Error; end
|
14
|
+
class UnknownEnvironmentVariableError < Error; end
|
12
15
|
|
13
16
|
extend ::Kreds::Fetch
|
17
|
+
extend ::Kreds::Show
|
14
18
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kreds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- enjaku4
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rails
|
@@ -15,7 +15,7 @@ dependencies:
|
|
15
15
|
requirements:
|
16
16
|
- - ">="
|
17
17
|
- !ruby/object:Gem::Version
|
18
|
-
version: '7.
|
18
|
+
version: '7.1'
|
19
19
|
- - "<"
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '8.1'
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '7.
|
28
|
+
version: '7.1'
|
29
29
|
- - "<"
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: '8.1'
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- kreds.gemspec
|
40
40
|
- lib/kreds.rb
|
41
41
|
- lib/kreds/fetch.rb
|
42
|
+
- lib/kreds/show.rb
|
42
43
|
- lib/kreds/version.rb
|
43
44
|
homepage: https://github.com/enjaku4/kreds
|
44
45
|
licenses:
|
@@ -55,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
56
|
requirements:
|
56
57
|
- - ">="
|
57
58
|
- !ruby/object:Gem::Version
|
58
|
-
version: '3.
|
59
|
+
version: '3.2'
|
59
60
|
- - "<"
|
60
61
|
- !ruby/object:Gem::Version
|
61
62
|
version: '3.5'
|
@@ -65,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
66
|
- !ruby/object:Gem::Version
|
66
67
|
version: '0'
|
67
68
|
requirements: []
|
68
|
-
rubygems_version: 3.6.
|
69
|
+
rubygems_version: 3.6.7
|
69
70
|
specification_version: 4
|
70
71
|
summary: The missing shorthand for Rails credentials
|
71
72
|
test_files: []
|