Single fetch command

This commit is contained in:
Donal McBreen
2024-08-26 15:20:13 +01:00
parent 0ae8046905
commit 79731da619
2 changed files with 30 additions and 63 deletions

View File

@@ -1,36 +1,19 @@
class Kamal::Cli::Secrets < Kamal::Cli::Base
desc "login", "Login to a secrets vault"
desc "fetch [ITEM] [FIELDS...]", "Fetch secrets from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def login
puts adapter(options).login(**adapter_options(options))
option :account, type: :string, aliases: "-a", required: true, desc: "The account identifier or username"
def fetch(item, *fields)
ENV["KAMAL_SECRETS_KILL_PARENT"] = "1"
puts JSON.dump(adapter(options[:adapter]).fetch(item, fields, account: options[:account])).shellescape
end
desc "fetch", "Fetch a secret from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def fetch(name)
puts adapter(options).fetch(name, **adapter_options(options))
end
desc "fetch_all", "Fetch multiple secrets from a vault"
option :adapter, type: :string, aliases: "-a", required: true, desc: "Which vault adapter to use"
option :adapter_options, type: :hash, aliases: "-O", required: false, desc: "Options to pass to the vault adapter"
def fetch_all(*names)
puts JSON.dump(adapter(options).fetch_all(*names, **adapter_options(options))).shellescape
end
desc "extract", "Extract a single secret from the results of a fetch_all call"
desc "extract", "Extract a single secret from the results of a fetch call"
def extract(name, secrets)
puts JSON.parse(secrets).fetch(name)
end
private
def adapter(options)
Kamal::Secrets::Adapters.lookup(options[:adapter])
end
def adapter_options(options)
options.fetch(:adapter_options, {}).transform_keys(&:to_sym)
def adapter(adapter)
Kamal::Secrets::Adapters.lookup(adapter)
end
end