Skip to content
Merged

v2 #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
ruby-version: 3.3
- name: Bundle install
run: bundle install
Comment thread
enjaku4 marked this conversation as resolved.
- name: Run RuboCop
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
ruby-version: 3.3
- name: Install grepfruit
run: gem install grepfruit
- name: Run grepfruit
Expand All @@ -48,12 +48,10 @@ jobs:
strategy:
matrix:
ruby:
- "3.2"
- "3.3"
- "3.4"
- "4.0"
rails:
- "~> 7.1.0"
- "~> 7.2.0"
- "~> 8.0.0"
- "~> 8.1.0"
Expand Down
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ plugins:
- rubocop-thread_safety

AllCops:
TargetRailsVersion: 7.1
TargetRubyVersion: 3.2
TargetRailsVersion: 7.2
TargetRubyVersion: 3.3
NewCops: enable

Layout/IndentationConsistency:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v2.0.0

- Dropped support for Ruby 3.2
- Dropped support for Rails 7.1
- Improved error messages and input validation
Comment thread
enjaku4 marked this conversation as resolved.
- Added `Kreds.env_show` method
- `Kreds.env_fetch!` now raises `InvalidArgumentError` when called with no keys

## v1.1.7

- Added support for Ruby 4
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gemspec

rails_version = ENV.fetch("RAILS_VERSION", "~> 7.1")
rails_version = ENV.fetch("RAILS_VERSION", "~> 7.2")

gem "byebug"
gem "rails", rails_version
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Rails.application.credentials.fetch(:recaptcha).fetch(:key)

# Kreds (clear, human-readable errors):
Kreds.fetch!(:recaptcha, :site_key)
# => Blank value in credentials: [:recaptcha][:site_key] (Kreds::BlankCredentialsError)
# => Blank value in credentials: :recaptcha => :site_key (Kreds::BlankCredentialsError)

Kreds.fetch!(:recaptcha, :key)
# => Credentials key not found: [:recaptcha][:key] (Kreds::UnknownCredentialsError)
# => Credentials key not found: :recaptcha => :key (Kreds::UnknownCredentialsError)
```

## Table of Contents
Expand Down Expand Up @@ -139,6 +139,18 @@ Useful for debugging and exploring available credentials in the Rails console.

```ruby
Kreds.show
# => { production: { aws: { access_key_id: "...", secret_access_key: "..." } }, ... }
```

**`Kreds.env_show`**

Like `show`, but scoped to the current Rails environment.

**Returns:** Hash containing credentials for `Rails.env`

```ruby
# In production, returns credentials[:production]
Comment thread
enjaku4 marked this conversation as resolved.
Kreds.env_show
# => { aws: { access_key_id: "...", secret_access_key: "..." }, ... }
```

Expand Down
4 changes: 2 additions & 2 deletions kreds.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Gem::Specification.new do |spec|
spec.summary = "The missing shorthand for Rails credentials"
spec.description = "Simpler and safer Rails credentials access with blank value detection and clear error messages"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.2", "< 4.1"
spec.required_ruby_version = ">= 3.3", "< 4.1"

spec.files = [
"kreds.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
] + Dir.glob("lib/**/*")

spec.require_paths = ["lib"]

spec.add_dependency "rails", ">= 7.1", "< 8.2"
spec.add_dependency "rails", ">= 7.2", "< 8.2"
end
8 changes: 4 additions & 4 deletions lib/kreds/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def fetch!(*keys, var: nil, &)
end

def env_fetch!(*keys, var: nil, &)
fetch!(Rails.env, *keys, var:, &)
fetch!(Rails.env, *Kreds::Inputs.process(keys, as: :symbol_array), var:, &)
end

def var!(var, &)
Expand All @@ -33,15 +33,15 @@ def fetch_key(hash, key, path, keys)
value = hash.fetch(key)

raise Kreds::BlankCredentialsError, "Blank value in credentials: #{path_to_s(path)}" if value.blank?
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path)}[:#{keys[path.size]}]" unless value.is_a?(Hash) || keys == path
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path.append(keys[path.size]))}" unless value.is_a?(Hash) || keys == path

value
rescue KeyError
raise Kreds::UnknownCredentialsError, "Credentials key not found: #{path_to_s(path)}"
end

def fallback_to_var(error, var, &)
return raise_or_yield(error, &) if var.blank?
return raise_or_yield(error, &) if var.nil?

var!(var, &)
rescue Kreds::BlankEnvironmentVariableError, Kreds::UnknownEnvironmentVariableError => e
Expand All @@ -53,7 +53,7 @@ def raise_or_yield(error, &)
end

def path_to_s(path)
"[:#{path.join("][:")}]"
path.map(&:inspect).join(" => ")
end
end
end
6 changes: 5 additions & 1 deletion lib/kreds/show.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Kreds
module Show
def show
Rails.application.credentials.as_json.deep_symbolize_keys
Rails.application.credentials.config.to_h
Comment thread
enjaku4 marked this conversation as resolved.
end

def env_show
show[Rails.env.to_sym] || {}
end
end
end
2 changes: 1 addition & 1 deletion lib/kreds/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Kreds
VERSION = "1.1.7".freeze
VERSION = "2.0.0".freeze
end
33 changes: 25 additions & 8 deletions spec/kreds_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
end
end

describe ".env_show" do
it "returns credentials for the current environment" do
expect(described_class.env_show).to eq({ foo: [1, 2, 3] })
end

it "returns empty hash for an environment not in credentials" do
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("staging"))
expect(described_class.env_show).to eq({})
end

it "returns the raw value when environment entry is not a hash" do
allow(described_class).to receive(:show).and_return({ test: "not_a_hash" })
expect(described_class.env_show).to eq("not_a_hash")
end
end

describe ".fetch!" do
describe "input validation" do
it "raises error for empty keys" do
Expand All @@ -38,17 +54,17 @@

it "raises error for missing end key" do
expect { described_class.fetch!(:foo, :bar, :bad) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bar][:bad]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bar => :bad")
end

it "raises error for missing middle key" do
expect { described_class.fetch!(:foo, :bad, :baz) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bad]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bad")
end

it "raises error for blank value" do
expect { described_class.fetch!(:bad) }
.to raise_error(Kreds::BlankCredentialsError, "Blank value in credentials: [:bad]")
.to raise_error(Kreds::BlankCredentialsError, "Blank value in credentials: :bad")
end
end

Expand Down Expand Up @@ -116,7 +132,7 @@
context "when handling edge cases" do
it "raises error when trying to access non-hash as hash" do
expect { described_class.fetch!(:foo, :bar, :baz, :extra) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:foo][:bar][:baz][:extra]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :foo => :bar => :baz => :extra")
end

it "works with single key" do
Expand All @@ -128,7 +144,7 @@
describe ".env_fetch!" do
it "raises error for invalid key types" do
expect { described_class.env_fetch!(:foo, 42) }
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[\"test\", :foo, 42]`")
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[:foo, 42]`")
end

it "raises error for invalid var type when fallback is needed" do
Expand All @@ -137,8 +153,9 @@
end

context "without var" do
it "returns entire test environment hash when no keys provided" do
expect(described_class.env_fetch!).to eq({ foo: [1, 2, 3] })
it "raises error for empty keys" do
expect { described_class.env_fetch! }
.to raise_error(Kreds::InvalidArgumentError, "Expected an array of symbols or strings, got `[]`")
Comment thread
enjaku4 marked this conversation as resolved.
end

it "returns specific value from test environment" do
Expand All @@ -147,7 +164,7 @@

it "raises error for missing key in test environment" do
expect { described_class.env_fetch!(:foo, :bar, :bad) }
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: [:test][:foo][:bar]")
.to raise_error(Kreds::UnknownCredentialsError, "Credentials key not found: :test => :foo => :bar")
end
end

Expand Down
Loading