Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add dashlane adapter #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/kamal/secrets/adapters/dashlane.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Kamal::Secrets::Adapters::Dashlane < Kamal::Secrets::Adapters::Base
private
def login(account)
unless loggedin?(account)
`(echo #{account.shellescape}; cat) | dcli sync`
raise RuntimeError, "Failed to login to Dashlane" unless $?.success?
end
end

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

def fetch_secrets(secrets, account:, session:)
items = `dcli secret #{secrets.map(&:shellescape).join(" ")} -o json`
raise RuntimeError, "Could not read #{secrets} from Dashlane" unless $?.success?

items = JSON.parse(items)

{}.tap do |results|
items.each do |item|
results[item["title"]] = item["content"]
end

if (missing_items = secrets - results.keys).any?
raise RuntimeError, "Could not find #{missing_items.join(", ")} in Dashlane"
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
164 changes: 164 additions & 0 deletions test/secrets/dashlane_adapter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
require "test_helper"

class DashlaneAdapterTest < SecretAdapterTestCase
setup do
`true` # Ensure $? is 0
end

test "fetch" do
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")
.returns(<<~JSON)
[
{
"id": "1234567891234567891",
"title": "SECRET1",
"content": "secret1",
"creationDatetime": "1724926635",
"lastBackupTime": "1724926635",
"lastUse": "1724926635",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "1724926635",
"secured": "false"
},
{
"id": "1234567891234567891",
"title": "FOLDER1/FSECRET1",
"content": "fsecret1",
"creationDatetime": "1724926084",
"lastBackupTime": "1724926635",
"lastUse": "123456789",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "123456789",
"secured": "false"
},
{
"id": "1234567891234567891",
"title": "FOLDER1/FSECRET2",
"content": "fsecret2",
"creationDatetime": "1724926084",
"lastBackupTime": "1724926635",
"lastUse": "123456789",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "123456789",
"secured": "false"
}
]
JSON

json = JSON.parse(shellunescape(run_command("fetch", "SECRET1", "FOLDER1/FSECRET1", "FOLDER1/FSECRET2")))

expected_json = {
"SECRET1"=>"secret1",
"FOLDER1/FSECRET1"=>"fsecret1",
"FOLDER1/FSECRET2"=>"fsecret2"
}

assert_equal expected_json, json
end

test "fetch with from" do
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")
.returns(<<~JSON)
[
{
"id": "1234567891234567891",
"title": "FOLDER1/FSECRET1",
"content": "fsecret1",
"creationDatetime": "1724926084",
"lastBackupTime": "1724926635",
"lastUse": "123456789",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "123456789",
"secured": "false"
},
{
"id": "1234567891234567891",
"title": "FOLDER1/FSECRET2",
"content": "fsecret2",
"creationDatetime": "1724926084",
"lastBackupTime": "1724926635",
"lastUse": "123456789",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "123456789",
"secured": "false"
}
]
JSON

json = JSON.parse(shellunescape(run_command("fetch", "--from", "FOLDER1", "FSECRET1", "FSECRET2")))

expected_json = {
"FOLDER1/FSECRET1"=>"fsecret1",
"FOLDER1/FSECRET2"=>"fsecret2"
}

assert_equal expected_json, json
end

test "fetch with signin" do
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")))

expected_json = {
"SECRET1"=>"secret1"
}

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
Kamal::Cli::Secrets.start \
[ *command,
"-c", "test/fixtures/deploy_with_accessories.yml",
"--adapter", "dashlane",
"--account", "[email protected]" ]
end
end

def single_item_json
<<~JSON
[
{
"id": "1234567891234567891",
"title": "SECRET1",
"content": "secret1",
"creationDatetime": "1724926635",
"lastBackupTime": "1724926635",
"lastUse": "1724926635",
"localeFormat": "UNIVERSAL",
"spaceId": "123456",
"userModificationDatetime": "1724926635",
"secured": "false"
}
]
JSON
end
end