-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Bitwarden Secrets Manager adapter
- Loading branch information
Showing
5 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Kamal::Secrets::Adapters::BitwardenSecretsManager < Kamal::Secrets::Adapters::Base | ||
private | ||
def login(account) | ||
nil | ||
end | ||
|
||
def fetch_secrets(secrets, account:, session:) | ||
{}.tap do |results| | ||
secrets = run_command("secret list -o env") | ||
raise RuntimeError, "Could not read secrets from Bitwarden Secrets Manager" unless $?.success? | ||
secrets.split("\n").each do |secret| | ||
key, value = secret.split("=", 2) | ||
value = value.gsub(/^"|"$/, "") | ||
results[key] = value | ||
end | ||
end | ||
end | ||
|
||
def run_command(command, session: nil) | ||
full_command = [ "bws", command ].join(" ") | ||
`#{full_command}`.strip unless full_command.nil? | ||
end | ||
|
||
def check_dependencies! | ||
raise RuntimeError, "Bitwarden Secrets Manager CLI is not installed" unless cli_installed? | ||
end | ||
|
||
def cli_installed? | ||
`bws --version 2> /dev/null` | ||
$?.success? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "test_helper" | ||
|
||
class BitwardenSecretsManagerAdapterTest < SecretAdapterTestCase | ||
test "fetch" do | ||
stub_ticks.with("bws --version 2> /dev/null") | ||
|
||
stub_ticks | ||
.with("bws secret list -o env") | ||
.returns("KAMAL_REGISTRY_PASSWORD=\"some_password\"\nMY_OTHER_SECRET=\"my=weird\"secret\"") | ||
|
||
actual = shellunescape(run_command("fetch")) | ||
|
||
expected = | ||
'{"KAMAL_REGISTRY_PASSWORD":"some_password","MY_OTHER_SECRET":"my\=weird\"secret"}' | ||
|
||
assert_equal expected, actual | ||
end | ||
|
||
test "fetch empty" do | ||
stub_ticks.with("bws --version 2> /dev/null") | ||
|
||
stub_ticks_with("bws secret list -o env", succeed: false).returns("Error:\n0: Received error message from server") | ||
|
||
error = assert_raises RuntimeError do | ||
(shellunescape(run_command("fetch"))) | ||
end | ||
assert_equal("Could not read secrets from Bitwarden Secrets Manager", error.message) | ||
end | ||
|
||
test "fetch with no session token" do | ||
stub_ticks.with("bws --version 2> /dev/null") | ||
|
||
stub_ticks_with("bws secret list -o env", succeed: false).returns("Error:\n0: Missing access token") | ||
|
||
error = assert_raises RuntimeError do | ||
(shellunescape(run_command("fetch"))) | ||
end | ||
assert_equal("Could not read secrets from Bitwarden Secrets Manager", error.message) | ||
end | ||
|
||
test "fetch without CLI installed" do | ||
stub_ticks_with("bws --version 2> /dev/null", succeed: false) | ||
|
||
error = assert_raises RuntimeError do | ||
shellunescape(run_command("fetch")) | ||
end | ||
assert_equal "Bitwarden Secrets Manager CLI is not installed", error.message | ||
end | ||
|
||
private | ||
def run_command(*command) | ||
stdouted do | ||
Kamal::Cli::Secrets.start \ | ||
[ *command, | ||
"--adapter", "bitwarden-sm" ] | ||
end | ||
end | ||
end |