Skip to content

Commit

Permalink
Add KV#update for patch support
Browse files Browse the repository at this point in the history
  • Loading branch information
phallstrom committed Dec 23, 2021
1 parent 714a305 commit a14170e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/vault/api/kv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ def write_metadata(path, metadata = {})
true
end

# Update the secret at the given path with the given data. Note that the
# data must be a {Hash}! Data will be merged with existing values.
#
# Note: This will raise an error if used on KV Secrets Engine Version 1.
#
# @example
# Vault.kv.write("secret/multiple", password: "secret") #=> #<Vault::Secret lease_id="">
#
# @param [String] path
# the path to update
# @param [Hash] data
# the data to merge
#
# @return [Secret]
def update(path, data = {}, options = {})
headers = extract_headers!(options)
headers["Content-Type"] = "application/merge-patch+json"
json = client.patch("/v1/#{mount}/data/#{encode_path(path)}", JSON.fast_generate(:data => data), headers)
if json.nil?
return true
else
return Secret.decode(json)
end
end


# Delete the secret at the given path. If the secret does not exist, vault
# will still return true.
#
Expand Down
32 changes: 32 additions & 0 deletions spec/integration/api/kv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,38 @@ module Vault
end
end

describe "#update" do
it "merges data and returns the secret" do
subject.write("test-update", zip: "zap")
subject.update("test-update", zig: "zag")
result = subject.read("test-update")
expect(result).to be
expect(result.data).to eq(zip: "zap", zig: "zag")
end

it "raises an error if the path does not exist" do
expect {
subject.update("test-update-non-existent", zig: "zag")
}.to raise_error(Vault::HTTPClientError, /404/)
end

it "raises an error if the path has been deleted" do
expect {
subject.write("test-update-deleted", zip: "zap")
subject.delete("test-update-deleted")
subject.update("test-update-deleted", zig: "zag")
}.to raise_error(Vault::HTTPClientError, /404/)
end

it "raises an error if the path has been destroyed" do
expect {
subject.write("test-update-destroyed", zip: "zap")
subject.delete("test-update-destroyed")
subject.update("test-update-destroyed", zig: "zag")
}.to raise_error(Vault::HTTPClientError, /404/)
end
end

describe "#delete" do
it "deletes the secret" do
subject.write("delete", foo: "bar")
Expand Down

0 comments on commit a14170e

Please sign in to comment.