Skip to content

Commit

Permalink
Don't write encrypted file unless credentials have been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
svoop committed Oct 30, 2023
1 parent f704e85 commit 31449ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@

* Fall back to `APP_ENV` instead of `RACK_ENV`

#### Fixes

* Don't re-encrypt if credentials haven't been modified

## 0.1.0

#### Initial Implementation
Expand Down
10 changes: 6 additions & 4 deletions lib/dry/credentials/extension.rb
Expand Up @@ -36,13 +36,15 @@ def reload!
def edit!(env=nil)
helpers = Dry::Credentials::Helpers.new(self, env)
create = helpers.create?
yaml = helpers.read_yaml
yaml = read_yaml = helpers.read_yaml
begin
yaml = helpers.edit_yaml yaml
end until helpers.yaml_valid? yaml
helpers.write_yaml yaml
puts [helpers.key_ev, ENV[helpers.key_ev]].join('=') if create
reload!
unless yaml == read_yaml
helpers.write_yaml yaml
puts [helpers.key_ev, ENV[helpers.key_ev]].join('=') if create
reload!
end
end

# Query settings
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/dry/credentials/extension_spec.rb
Expand Up @@ -85,16 +85,35 @@
it "updates the encrypted file and reloads the credentials" do
Dir.mktmpdir do |tmp_dir|
FileUtils.cp(fixtures_path.join('encrypted', 'test.yml.enc'), tmp_dir)
original_mtime = File.mtime("#{tmp_dir}/test.yml.enc")
sleep 0.1
subject.credentials do
env 'test'
dir tmp_dir
end
ENV['EDITOR'] = 'echo "added_root: ADDED ROOT" >>'
_{ subject.credentials.edit! }.must_be_silent
_(File.exist?("#{tmp_dir}/test.yml.enc")).must_equal true
_(File.mtime("#{tmp_dir}/test.yml.enc")).wont_equal original_mtime
_(subject.credentials.added_root).must_equal 'ADDED ROOT'
_(subject.credentials.one_root).must_equal 'ONE ROOT'
end
end

it "doesn't update the encrypted file if no changes were made" do
Dir.mktmpdir do |tmp_dir|
FileUtils.cp(fixtures_path.join('encrypted', 'test.yml.enc'), tmp_dir)
original_mtime = File.mtime("#{tmp_dir}/test.yml.enc")
sleep 0.1
subject.credentials do
env 'test'
dir tmp_dir
end
ENV['EDITOR'] = 'true'
_{ subject.credentials.edit! }.must_be_silent
_(File.exist?("#{tmp_dir}/test.yml.enc")).must_equal true
_(File.mtime("#{tmp_dir}/test.yml.enc")).must_equal original_mtime
end
end
end
end

0 comments on commit 31449ac

Please sign in to comment.