-
Notifications
You must be signed in to change notification settings - Fork 650
Add KeePassXC Secret Adapter with CI Passthrough Support #1715
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
Open
jonyoi
wants to merge
5
commits into
basecamp:main
Choose a base branch
from
jonyoi:add-keepassxc-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−0
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1bbe3b
Add KeePassXC secrets adapter with CI passthrough
jonyoi e07f0be
remove explicit CI checks and stub system calls in tests
jonyoi cfc845c
--amend
jonyoi 2f5aa80
Remove ENV fallback from KeepassXC secrets adapter
jonyoi 210a0b8
consistent raise
jonyoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| require "open3" | ||
|
|
||
| class Kamal::Secrets::Adapters::Keepassxc < Kamal::Secrets::Adapters::Base | ||
| # Usage Example: | ||
| # kamal secrets fetch --adapter keepassxc --account ~/path/to/secrets.kdbx --from entry-title KAMAL_REGISTRY_PASSWORD RAILS_MASTER_KEY ANY_OTHER_ATTRIBUTE_SAVED_IN_ADVANCE_TAB_OF_AN_ENTRY | ||
|
|
||
| private | ||
|
|
||
| # 1. Login / Authentication | ||
| def login(account) | ||
| # In CI, we don't authenticate. Return a dummy session. | ||
| return "ci-session" if ci_mode? | ||
|
|
||
| if ENV["KEEPASS_PWD"] && !ENV["KEEPASS_PWD"].empty? | ||
jonyoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ENV["KEEPASS_PWD"] | ||
| else | ||
| ask_for_password(account) | ||
| end | ||
| end | ||
|
|
||
| # 2. Dispatcher | ||
| def fetch_secrets(secrets, from:, account:, session:) | ||
| if ci_mode? | ||
| # CI Mode: Passthrough (Strict check) | ||
| secrets.each_with_object({}) do |secret, results| | ||
| value = ENV[secret] | ||
| raise "Missing ENV secret '#{secret}' in CI mode." if value.nil? || value.empty? | ||
| results[secret] = value | ||
| end | ||
| else | ||
jonyoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Local Mode: Fetch secrets. | ||
| if secrets.empty? | ||
| raise ArgumentError, "No secrets specified. Please list the attribute names you want to fetch." | ||
| end | ||
| fetch_specified_secrets(secrets, from: from, account: account, session: session) | ||
| end | ||
| end | ||
|
|
||
| # 3. Fetch secrets | ||
| def fetch_specified_secrets(secrets, from:, account:, session:) | ||
| secrets.each_with_object({}) do |secret, results| | ||
| # If asking for "password", use standard field, otherwise use Attribute lookup | ||
| attr_flag = (secret == "password") ? [] : ["-a", secret] | ||
|
|
||
| results[secret] = run_command("show", account, from, *attr_flag, "-q", "--show-protected", session: session) | ||
| end | ||
| end | ||
|
|
||
| # 4. Check Dependencies | ||
| def check_dependencies! | ||
| # BYPASS: Don't check for CLI in CI | ||
| return if ci_mode? | ||
| raise "KeePassXC CLI is not installed" unless cli_installed? | ||
| end | ||
|
|
||
| def cli_installed? | ||
jonyoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `keepassxc-cli --version 2> /dev/null` | ||
| $?.success? | ||
| end | ||
|
|
||
| # --- Helpers --- | ||
jonyoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def ci_mode? | ||
| ENV["CI"] == "true" || ENV["GITHUB_ACTIONS"] == "true" | ||
| end | ||
|
|
||
| def ask_for_password(account) | ||
| require "io/console" | ||
jonyoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| File.open("/dev/tty", "r+") do |tty| | ||
| tty.getpass("Enter KeePassXC Master Password for #{File.basename(account)}: ") | ||
| end | ||
| end | ||
|
|
||
| def run_command(*args, session:) | ||
| cmd = ["keepassxc-cli", *args] | ||
| stdout, stderr, status = Open3.capture3(*cmd, stdin_data: session) | ||
| raise "KeePassXC Error: #{stderr.strip}" unless status.success? | ||
| stdout.strip | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| require "test_helper" | ||
|
|
||
| class KeepassxcAdapterTest < SecretAdapterTestCase | ||
| setup do | ||
| @keepassxc = Kamal::Secrets::Adapters::Keepassxc.new | ||
| end | ||
|
|
||
| test "fetch specific secrets in local mode calling CLI" do | ||
| with_ci_mode(false) do | ||
| @keepassxc.stub :ask_for_password, "dummy_master_pass" do | ||
jonyoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @keepassxc.stub :run_command, "secret_value" do | ||
| secrets = @keepassxc.fetch([ "MY_SECRET" ], account: "/tmp/secrets.kdbx", from: "test-env") | ||
| assert_equal({ "MY_SECRET" => "secret_value" }, secrets) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test "fetch password field uses no -a flag" do | ||
| with_ci_mode(false) do | ||
| @keepassxc.stub :ask_for_password, "pass" do | ||
| verifier = ->(cmd, account, from, *args, session:) { | ||
| if args.include?("-a") | ||
| raise "Error: 'password' field should not use -a flag" | ||
| end | ||
| "pw_value" | ||
| } | ||
|
|
||
| @keepassxc.stub :run_command, verifier do | ||
| secrets = @keepassxc.fetch([ "password" ], account: "/tmp/db.kdbx", from: "entry") | ||
| assert_equal({ "password" => "pw_value" }, secrets) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test "fetch secrets in CI mode reads from ENV" do | ||
| with_ci_mode(true) do | ||
| with_env("MY_SECRET" => "env_value") do | ||
| secrets = @keepassxc.fetch([ "MY_SECRET" ], account: "ignore", from: "ignore") | ||
| assert_equal({ "MY_SECRET" => "env_value" }, secrets) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test "fetch secrets in CI mode fails fast if ENV missing" do | ||
| with_ci_mode(true) do | ||
| error = assert_raises(RuntimeError) do | ||
| @keepassxc.fetch([ "MISSING_SECRET" ], account: "ignore", from: "ignore") | ||
| end | ||
| assert_match /Missing ENV secret 'MISSING_SECRET' in CI mode/, error.message | ||
| end | ||
| end | ||
|
|
||
| test "login returns dummy session in CI mode" do | ||
| with_ci_mode(true) do | ||
| assert_equal "ci-session", @keepassxc.send(:login, "account") | ||
| end | ||
| end | ||
|
|
||
| test "dependency check is skipped in CI mode" do | ||
| with_ci_mode(true) do | ||
| # Should NOT raise even if we force cli_installed? to false | ||
| @keepassxc.stub :cli_installed?, false do | ||
| assert_nothing_raised { @keepassxc.send(:check_dependencies!) } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test "dependency check raises in local mode if CLI missing" do | ||
| with_ci_mode(false) do | ||
| @keepassxc.stub :cli_installed?, false do | ||
| assert_raises(RuntimeError) { @keepassxc.send(:check_dependencies!) } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def with_ci_mode(enabled) | ||
| key = "CI" | ||
| original = ENV[key] | ||
| ENV[key] = enabled ? "true" : nil | ||
| yield | ||
| ensure | ||
| ENV[key] = original | ||
| end | ||
|
|
||
| def with_env(values) | ||
| original = ENV.to_h | ||
| values.each { |k, v| ENV[k] = v } | ||
| yield | ||
| ensure | ||
| values.keys.each { |k| ENV[k] = original[k] } | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.