|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class BitwardenSecretsManagerAdapterTest < SecretAdapterTestCase |
| 4 | + test "fetch with no parameters" do |
| 5 | + stub_ticks.with("bws --version 2> /dev/null") |
| 6 | + stub_login |
| 7 | + |
| 8 | + error = assert_raises RuntimeError do |
| 9 | + (shellunescape(run_command("fetch"))) |
| 10 | + end |
| 11 | + assert_equal("You must specify what to retrieve from Bitwarden Secrets Manager", error.message) |
| 12 | + end |
| 13 | + |
| 14 | + test "fetch all" do |
| 15 | + stub_ticks.with("bws --version 2> /dev/null") |
| 16 | + stub_login |
| 17 | + stub_ticks |
| 18 | + .with("bws secret list -o env") |
| 19 | + .returns("KAMAL_REGISTRY_PASSWORD=\"some_password\"\nMY_OTHER_SECRET=\"my=weird\"secret\"") |
| 20 | + |
| 21 | + expected = '{"KAMAL_REGISTRY_PASSWORD":"some_password","MY_OTHER_SECRET":"my\=weird\"secret"}' |
| 22 | + actual = shellunescape(run_command("fetch", "all")) |
| 23 | + assert_equal expected, actual |
| 24 | + end |
| 25 | + |
| 26 | + test "fetch all with from" do |
| 27 | + stub_ticks.with("bws --version 2> /dev/null") |
| 28 | + stub_login |
| 29 | + stub_ticks |
| 30 | + .with("bws secret list -o env 82aeb5bd-6958-4a89-8197-eacab758acce") |
| 31 | + .returns("KAMAL_REGISTRY_PASSWORD=\"some_password\"\nMY_OTHER_SECRET=\"my=weird\"secret\"") |
| 32 | + |
| 33 | + expected = '{"KAMAL_REGISTRY_PASSWORD":"some_password","MY_OTHER_SECRET":"my\=weird\"secret"}' |
| 34 | + actual = shellunescape(run_command("fetch", "all", "--from", "82aeb5bd-6958-4a89-8197-eacab758acce")) |
| 35 | + assert_equal expected, actual |
| 36 | + end |
| 37 | + |
| 38 | + test "fetch item" do |
| 39 | + stub_ticks.with("bws --version 2> /dev/null") |
| 40 | + stub_login |
| 41 | + stub_ticks |
| 42 | + .with("bws secret get -o env 82aeb5bd-6958-4a89-8197-eacab758acce") |
| 43 | + .returns("KAMAL_REGISTRY_PASSWORD=\"some_password\"") |
| 44 | + |
| 45 | + expected = '{"KAMAL_REGISTRY_PASSWORD":"some_password"}' |
| 46 | + actual = shellunescape(run_command("fetch", "82aeb5bd-6958-4a89-8197-eacab758acce")) |
| 47 | + assert_equal expected, actual |
| 48 | + end |
| 49 | + |
| 50 | + test "fetch with multiple items" do |
| 51 | + stub_ticks.with("bws --version 2> /dev/null") |
| 52 | + stub_login |
| 53 | + stub_ticks |
| 54 | + .with("bws secret get -o env 82aeb5bd-6958-4a89-8197-eacab758acce") |
| 55 | + .returns("KAMAL_REGISTRY_PASSWORD=\"some_password\"") |
| 56 | + stub_ticks |
| 57 | + .with("bws secret get -o env 6f8cdf27-de2b-4c77-a35d-07df8050e332") |
| 58 | + .returns("MY_OTHER_SECRET=\"my=weird\"secret\"") |
| 59 | + |
| 60 | + expected = '{"KAMAL_REGISTRY_PASSWORD":"some_password","MY_OTHER_SECRET":"my\=weird\"secret"}' |
| 61 | + actual = shellunescape(run_command("fetch", "82aeb5bd-6958-4a89-8197-eacab758acce", "6f8cdf27-de2b-4c77-a35d-07df8050e332")) |
| 62 | + assert_equal expected, actual |
| 63 | + end |
| 64 | + |
| 65 | + test "fetch all empty" do |
| 66 | + stub_ticks.with("bws --version 2> /dev/null") |
| 67 | + stub_login |
| 68 | + stub_ticks_with("bws secret list -o env", succeed: false).returns("Error:\n0: Received error message from server") |
| 69 | + |
| 70 | + error = assert_raises RuntimeError do |
| 71 | + (shellunescape(run_command("fetch", "all"))) |
| 72 | + end |
| 73 | + assert_equal("Could not read secrets from Bitwarden Secrets Manager", error.message) |
| 74 | + end |
| 75 | + |
| 76 | + test "fetch nonexistent item" do |
| 77 | + stub_ticks.with("bws --version 2> /dev/null") |
| 78 | + stub_login |
| 79 | + stub_ticks_with("bws secret get -o env 82aeb5bd-6958-4a89-8197-eacab758acce", succeed: false) |
| 80 | + .returns("ERROR (RuntimeError): Could not read 82aeb5bd-6958-4a89-8197-eacab758acce from Bitwarden Secrets Manager") |
| 81 | + |
| 82 | + error = assert_raises RuntimeError do |
| 83 | + (shellunescape(run_command("fetch", "82aeb5bd-6958-4a89-8197-eacab758acce"))) |
| 84 | + end |
| 85 | + assert_equal("Could not read 82aeb5bd-6958-4a89-8197-eacab758acce from Bitwarden Secrets Manager", error.message) |
| 86 | + end |
| 87 | + |
| 88 | + test "fetch with no access token" do |
| 89 | + stub_ticks.with("bws --version 2> /dev/null") |
| 90 | + stub_ticks_with("bws run 'echo OK'", succeed: false) |
| 91 | + |
| 92 | + error = assert_raises RuntimeError do |
| 93 | + (shellunescape(run_command("fetch", "all"))) |
| 94 | + end |
| 95 | + assert_equal("Could not authenticate to Bitwarden Secrets Manager. Did you set a valid access token?", error.message) |
| 96 | + end |
| 97 | + |
| 98 | + test "fetch without CLI installed" do |
| 99 | + stub_ticks_with("bws --version 2> /dev/null", succeed: false) |
| 100 | + |
| 101 | + error = assert_raises RuntimeError do |
| 102 | + shellunescape(run_command("fetch")) |
| 103 | + end |
| 104 | + assert_equal "Bitwarden Secrets Manager CLI is not installed", error.message |
| 105 | + end |
| 106 | + |
| 107 | + private |
| 108 | + def stub_login |
| 109 | + stub_ticks.with("bws run 'echo OK'").returns("OK") |
| 110 | + end |
| 111 | + |
| 112 | + def run_command(*command) |
| 113 | + stdouted do |
| 114 | + Kamal::Cli::Secrets.start \ |
| 115 | + [ *command, |
| 116 | + "--adapter", "bitwarden-sm" ] |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments