Skip to content

Commit

Permalink
Merge pull request #995 from honzasterba/bw_nicer_error_message_on_no…
Browse files Browse the repository at this point in the history
…n_field_fetch

[bitwarden] default fetch raises NoMethodError
  • Loading branch information
djmb authored Sep 30, 2024
2 parents 28a0226 + 29b02f5 commit 32caf4b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/kamal/secrets/adapters/bitwarden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def fetch_secrets(secrets, account:, session:)
value = item_field["value"]
results["#{item}/#{field}"] = value
end
elsif item_json.dig("login", "password")
results[item] = item_json.dig("login", "password")
else
results[item] = item_json["login"]["password"]
raise RuntimeError, "Item #{item} is not a login type item and no fields were specified"
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/secrets/bitwarden_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ class BitwardenAdapterTest < SecretAdapterTestCase
assert_equal expected_json, json
end

test "fetch with no login" do
stub_unlocked
stub_ticks.with("bw sync").returns("")
stub_noteitem

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "mynote")))
end
assert_match(/not a login type item/, error.message)
end

test "fetch with from" do
stub_unlocked
stub_ticks.with("bw sync").returns("")
Expand Down Expand Up @@ -181,6 +192,30 @@ def stub_mypassword(session: nil)
JSON
end

def stub_noteitem(session: nil)
stub_ticks
.with("#{"BW_SESSION=#{session} " if session}bw get item mynote")
.returns(<<~JSON)
{
"passwordHistory":null,
"revisionDate":"2024-09-28T09:07:27.461Z",
"creationDate":"2024-09-28T09:07:00.740Z",
"deletedDate":null,
"object":"item",
"id":"aaaaaaaa-cccc-eeee-0000-222222222222",
"organizationId":null,
"folderId":null,
"type":2,
"reprompt":0,
"name":"noteitem",
"notes":"NOTES",
"favorite":false,
"secureNote":{"type":0},
"collectionIds":[]
}
JSON
end

def stub_myitem
stub_ticks
.with("bw get item myitem")
Expand Down

0 comments on commit 32caf4b

Please sign in to comment.