Skip to content

Commit 79bc758

Browse files
committed
make --account optional and pass Enpass vault in --from
1 parent c9dec8c commit 79bc758

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

lib/kamal/cli/secrets.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Kamal::Cli::Secrets < Kamal::Cli::Base
22
desc "fetch [SECRETS...]", "Fetch secrets from a vault"
33
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
4-
option :account, type: :string, required: true, desc: "The account identifier or username"
4+
option :account, type: :string, required: false, desc: "The account identifier or username"
55
option :from, type: :string, required: false, desc: "A vault or folder to fetch the secrets from"
66
option :inline, type: :boolean, required: false, hidden: true
77
def fetch(*secrets)

lib/kamal/secrets/adapters/enpass.rb

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
##
2-
# Enpass is different from most password managers, in a way that it's offline. A path to a vault is treated as account.
2+
# Enpass is different from most password managers, in a way that it's offline and doesn't need an account.
33
#
4-
# Pass it like so: `kamal secrets fetch --adapter enpass --account /Users/YOUR_USERNAME/Library/Containers/in.sinew.Enpass-Desktop/Data/Documents/Vaults/primary --from MY_PROD_SERVER`
4+
# Usage
5+
#
6+
# Fetch all password from FooBar item
7+
# `kamal secrets fetch --adapter enpass --from /Users/YOUR_USERNAME/Library/Containers/in.sinew.Enpass-Desktop/Data/Documents/Vaults/primary FooBar`
8+
#
9+
# Fetch only DB_PASSWORD from FooBar item
10+
# `kamal secrets fetch --adapter enpass --from /Users/YOUR_USERNAME/Library/Containers/in.sinew.Enpass-Desktop/Data/Documents/Vaults/primary FooBar/DB_PASSWORD`
511
class Kamal::Secrets::Adapters::Enpass < Kamal::Secrets::Adapters::Base
6-
private
7-
def login(account)
8-
# There is no concept of session in enpass-cli
9-
true
10-
end
12+
def fetch(secrets, account: nil, from:)
13+
check_dependencies!
14+
fetch_secrets(secrets, from)
15+
end
1116

12-
def fetch_secrets(secrets, account:, session:)
17+
private
18+
def fetch_secrets(secrets, vault)
1319
secrets_titles = fetch_secret_titles(secrets)
1420

15-
# Enpass outputs result as stderr, I did not find a way to stub backticks and output to stderr. Open3 did the job.
16-
result = `enpass-cli -json -vault #{account.shellescape} show #{secrets.map(&:shellescape).join(" ")}`.strip
21+
result = `enpass-cli -json -vault #{vault.shellescape} show #{secrets_titles.map(&:shellescape).join(" ")}`.strip
1722

1823
parse_result_and_take_secrets(result, secrets)
1924
end

test/secrets/enpass_adapter_test.rb

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require "test_helper"
22

33
class EnpassAdapterTest < SecretAdapterTestCase
4-
setup do
5-
`true` # Ensure $? is 0
6-
end
7-
84
test "fetch without CLI installed" do
95
stub_ticks_with("enpass-cli version 2> /dev/null", succeed: false)
106

@@ -19,7 +15,7 @@ class EnpassAdapterTest < SecretAdapterTestCase
1915
stub_ticks_with("enpass-cli version 2> /dev/null")
2016

2117
stub_ticks
22-
.with("enpass-cli -json -vault vault-path show FooBar/SECRET_1")
18+
.with("enpass-cli -json -vault vault-path show FooBar")
2319
.returns(<<~JSON)
2420
[{"category":"computer","label":"SECRET_1","login":"","password":"my-password-1","title":"FooBar","type":"password"}]
2521
JSON
@@ -35,7 +31,7 @@ class EnpassAdapterTest < SecretAdapterTestCase
3531
stub_ticks_with("enpass-cli version 2> /dev/null")
3632

3733
stub_ticks
38-
.with("enpass-cli -json -vault vault-path show FooBar/SECRET_1 FooBar/SECRET_2")
34+
.with("enpass-cli -json -vault vault-path show FooBar")
3935
.returns(<<~JSON)
4036
[
4137
{"category":"computer","label":"SECRET_1","login":"","password":"my-password-1","title":"FooBar","type":"password"},
@@ -51,26 +47,6 @@ class EnpassAdapterTest < SecretAdapterTestCase
5147
assert_equal expected_json, json
5248
end
5349

54-
test "fetch multiple items with from" do
55-
stub_ticks_with("enpass-cli version 2> /dev/null")
56-
57-
stub_ticks
58-
.with("enpass-cli -json -vault vault-path show FooBar/SECRET_1 FooBar/SECRET_2")
59-
.returns(<<~JSON)
60-
[
61-
{"category":"computer","label":"SECRET_1","login":"","password":"my-password-1","title":"FooBar","type":"password"},
62-
{"category":"computer","label":"SECRET_2","login":"","password":"my-password-2","title":"FooBar","type":"password"},
63-
{"category":"computer","label":"SECRET_3","login":"","password":"my-password-1","title":"Hello","type":"password"}
64-
]
65-
JSON
66-
67-
json = JSON.parse(shellunescape(run_command("fetch", "--from", "FooBar", "SECRET_1", "SECRET_2")))
68-
69-
expected_json = { "FooBar/SECRET_1" => "my-password-1", "FooBar/SECRET_2" => "my-password-2" }
70-
71-
assert_equal expected_json, json
72-
end
73-
7450
test "fetch all with from" do
7551
stub_ticks_with("enpass-cli version 2> /dev/null")
7652

@@ -99,7 +75,7 @@ def run_command(*command)
9975
[ *command,
10076
"-c", "test/fixtures/deploy_with_accessories.yml",
10177
"--adapter", "enpass",
102-
"--account", "vault-path" ]
78+
"--from", "vault-path" ]
10379
end
10480
end
10581
end

0 commit comments

Comments
 (0)