Skip to content

Commit

Permalink
feat(cli): update secrets --account flag as optional depending on ada…
Browse files Browse the repository at this point in the history
…pter
  • Loading branch information
mrbongiolo committed Nov 4, 2024
1 parent d0d9dfc commit 77cd29f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/kamal/cli/secrets.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Kamal::Cli::Secrets < Kamal::Cli::Base
desc "fetch [SECRETS...]", "Fetch secrets from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :account, type: :string, required: true, desc: "The account identifier or username"
option :account, type: :string, required: false, desc: "The account identifier or username"
option :from, type: :string, required: false, desc: "A vault or folder to fetch the secrets from"
option :inline, type: :boolean, required: false, hidden: true
def fetch(*secrets)
results = adapter(options[:adapter]).fetch(secrets, **options.slice(:account, :from).symbolize_keys)
adapter = initialize_adapter(options[:adapter])

if adapter.requires_account? && options[:account].blank?
return puts "No value provided for required options '--account'"
end

results = adapter.fetch(secrets, **options.slice(:account, :from).symbolize_keys)

return_or_puts JSON.dump(results).shellescape, inline: options[:inline]
end
Expand All @@ -29,7 +35,7 @@ def print
end

private
def adapter(adapter)
def initialize_adapter(adapter)
Kamal::Secrets::Adapters.lookup(adapter)
end

Expand Down
9 changes: 8 additions & 1 deletion lib/kamal/secrets/adapters/base.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
class Kamal::Secrets::Adapters::Base
delegate :optionize, to: Kamal::Utils

def fetch(secrets, account:, from: nil)
def fetch(secrets, account: nil, from: nil)
raise RuntimeError, "Missing required option '--account'" if requires_account? && account.blank?

check_dependencies!

session = login(account)
full_secrets = secrets.map { |secret| [ from, secret ].compact.join("/") }
fetch_secrets(full_secrets, account: account, session: session)
end

def requires_account?
true
end

private
def login(...)
raise NotImplementedError
Expand Down
18 changes: 18 additions & 0 deletions lib/kamal/secrets/adapters/test_optional_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Kamal::Secrets::Adapters::TestOptionalAccount < Kamal::Secrets::Adapters::Base
def requires_account?
false
end

private
def login(account)
true
end

def fetch_secrets(secrets, account:, session:)
secrets.to_h { |secret| [ secret, secret.reverse ] }
end

def check_dependencies!
# no op
end
end
12 changes: 12 additions & 0 deletions test/cli/secrets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ class CliSecretsTest < CliTestCase
run_command("fetch", "foo", "bar", "baz", "--account", "myaccount", "--adapter", "test")
end

test "fetch missing --acount" do
assert_equal \
"No value provided for required options '--account'",
run_command("fetch", "foo", "bar", "baz", "--adapter", "test")
end

test "fetch without required --account" do
assert_equal \
"\\{\\\"foo\\\":\\\"oof\\\",\\\"bar\\\":\\\"rab\\\",\\\"baz\\\":\\\"zab\\\"\\}",
run_command("fetch", "foo", "bar", "baz", "--adapter", "test_optional_account")
end

test "extract" do
assert_equal "oof", run_command("extract", "foo", "{\"foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
end
Expand Down

0 comments on commit 77cd29f

Please sign in to comment.