Skip to content

Commit

Permalink
add dependencies check and improve methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davilajose23 committed Oct 31, 2024
1 parent 580e1a1 commit d08bd04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 15 additions & 2 deletions lib/kamal/secrets/adapters/dashlane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ class Kamal::Secrets::Adapters::Dashlane < Kamal::Secrets::Adapters::Base
private
def login(account)
unless loggedin?(account)
`dcli sync`
if account.nil? || account.empty?
`dcli sync`
else
`(echo #{account.shellescape}; cat) | dcli sync`
end
raise RuntimeError, "Failed to login to Dashlane" unless $?.success?
end
end

def loggedin?(account)
`dcli accounts whoami`.strip == account
`dcli accounts whoami < /dev/null`.strip == account && $?.success?
end

def fetch_secrets(secrets, account:, session:)
Expand All @@ -27,4 +31,13 @@ def fetch_secrets(secrets, account:, session:)
end
end
end

def check_dependencies!
raise "Dashlane CLI is not installed" unless cli_installed?
end

def cli_installed?
`dcli --version 2> /dev/null`
$?.success?
end
end
20 changes: 16 additions & 4 deletions test/secrets/dashlane_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class DashlaneAdapterTest < SecretAdapterTestCase
end

test "fetch" do
stub_ticks.with("dcli accounts whoami").returns("[email protected]")
stub_ticks.with("dcli --version 2> /dev/null")
stub_ticks.with("dcli accounts whoami < /dev/null").returns("[email protected]")

stub_ticks
.with("dcli secret SECRET1 FOLDER1/FSECRET1 FOLDER1/FSECRET2 -o json")
Expand Down Expand Up @@ -63,7 +64,8 @@ class DashlaneAdapterTest < SecretAdapterTestCase
end

test "fetch with from" do
stub_ticks.with("dcli accounts whoami").returns("[email protected]")
stub_ticks.with("dcli --version 2> /dev/null")
stub_ticks.with("dcli accounts whoami < /dev/null").returns("[email protected]")

stub_ticks
.with("dcli secret FOLDER1/FSECRET1 FOLDER1/FSECRET2 -o json")
Expand Down Expand Up @@ -107,8 +109,9 @@ class DashlaneAdapterTest < SecretAdapterTestCase
end

test "fetch with signin" do
stub_ticks_with("dcli accounts whoami", succeed: false).returns("")
stub_ticks_with("dcli sync", succeed: true).returns("")
stub_ticks.with("dcli --version 2> /dev/null")
stub_ticks_with("dcli accounts whoami < /dev/null", succeed: false).returns("")
stub_ticks_with("(echo [email protected]; cat) | dcli sync").returns("")
stub_ticks.with("dcli secret SECRET1 -o json").returns(single_item_json)

json = JSON.parse(shellunescape(run_command("fetch", "SECRET1")))
Expand All @@ -120,6 +123,15 @@ class DashlaneAdapterTest < SecretAdapterTestCase
assert_equal expected_json, json
end

test "fetch without CLI installed" do
stub_ticks_with("dcli --version 2> /dev/null", succeed: false)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "SECRET1")))
end
assert_equal "Dashlane CLI is not installed", error.message
end

private
def run_command(*command)
stdouted do
Expand Down

0 comments on commit d08bd04

Please sign in to comment.