From 81cea7bb2056399f9da87cdf7c05550ffe608817 Mon Sep 17 00:00:00 2001 From: Jan Sterba Date: Sat, 28 Sep 2024 11:46:54 +0200 Subject: [PATCH] [bitwarden] ability to fetch all fields from an item Sometimes a projects has a lot of secrets (more than 10). And its cumbersome to write $(kama secrets fetch ...) with a lot of field names. I want to be able to just fetch all the fields from a given item and then just use these with $(kamal extract NAME) --- lib/kamal/secrets/adapters/bitwarden.rb | 1 + test/secrets/bitwarden_adapter_test.rb | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/kamal/secrets/adapters/bitwarden.rb b/lib/kamal/secrets/adapters/bitwarden.rb index e84a0d93e..f4eacbae4 100644 --- a/lib/kamal/secrets/adapters/bitwarden.rb +++ b/lib/kamal/secrets/adapters/bitwarden.rb @@ -29,6 +29,7 @@ def fetch_secrets(secrets, account:, session:) item_json = JSON.parse(item_json) if fields.any? + fields = item_json["fields"].pluck("name") if fields == [ "__ALL__" ] fields.each do |field| item_field = item_json["fields"].find { |f| f["name"] == field } raise RuntimeError, "Could not find field #{field} in item #{item} in Bitwarden" unless item_field diff --git a/test/secrets/bitwarden_adapter_test.rb b/test/secrets/bitwarden_adapter_test.rb index e2a3ac375..08d40209e 100644 --- a/test/secrets/bitwarden_adapter_test.rb +++ b/test/secrets/bitwarden_adapter_test.rb @@ -38,6 +38,20 @@ class BitwardenAdapterTest < SecretAdapterTestCase assert_equal expected_json, json end + test "fetch all with from" do + stub_unlocked + stub_ticks.with("bw sync").returns("") + stub_myitem + + json = JSON.parse(shellunescape(run_command("fetch", "--from", "myitem", "__ALL__"))) + + expected_json = { + "myitem/field1"=>"secret1", "myitem/field2"=>"blam", "myitem/field3"=>"fewgrwjgk", "myitem/field4"=>"auto" + } + + assert_equal expected_json, json + end + test "fetch with multiple items" do stub_unlocked @@ -237,7 +251,8 @@ def stub_myitem "fields":[ {"name":"field1","value":"secret1","type":1,"linkedId":null}, {"name":"field2","value":"blam","type":1,"linkedId":null}, - {"name":"field3","value":"fewgrwjgk","type":1,"linkedId":null} + {"name":"field3","value":"fewgrwjgk","type":1,"linkedId":null}, + {"name":"field4","value":"auto","type":1,"linkedId":null} ], "login":{"fido2Credentials":[],"uris":[],"username":null,"password":null,"totp":null,"passwordRevisionDate":null},"collectionIds":[] }