-
Notifications
You must be signed in to change notification settings - Fork 652
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 3 commits
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,69 @@ | ||
| require "open3" | ||
| require "io/console" | ||
|
|
||
| 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. Dependency Check | ||
| def check_dependencies! | ||
| @cli_installed = cli_installed? | ||
| end | ||
|
|
||
| # 2. Login | ||
| def login(account) | ||
| # If CLI is missing, we skip login (Fallback Mode). | ||
| return unless @cli_installed | ||
|
|
||
| ask_for_password(account) | ||
| end | ||
|
|
||
| # 3. Fetch | ||
| def fetch_secrets(secrets, from:, account:, session:) | ||
| if @cli_installed | ||
| # Local / CLI Mode | ||
| fetch_from_cli(secrets, from: from, account: account, session: session) | ||
| else | ||
| # Fallback Mode (CI/Server) | ||
| fetch_from_env(secrets) | ||
| end | ||
| end | ||
|
|
||
| def fetch_from_cli(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 | ||
|
|
||
| def fetch_from_env(secrets) | ||
| secrets.each_with_object({}) do |secret, results| | ||
| if (value = ENV[secret]).present? | ||
| results[secret] = value | ||
| else | ||
| raise "KeePassXC CLI is not Installed & Secret '#{secret}' is missing in ENV." | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def cli_installed? | ||
jonyoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `keepassxc-cli --version 2> /dev/null` | ||
| $?.success? | ||
| end | ||
|
|
||
| def ask_for_password(account) | ||
| 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,56 @@ | ||
| require "test_helper" | ||
|
|
||
| class KeepassxcAdapterTest < SecretAdapterTestCase | ||
| setup do | ||
| @keepassxc = Kamal::Secrets::Adapters::Keepassxc.new | ||
| end | ||
|
|
||
| test "fetch via CLI (Local Mode)" do | ||
| # Simulate when CLI is installed | ||
| stub_ticks_with("keepassxc-cli --version 2> /dev/null", succeed: true) | ||
|
|
||
| @keepassxc.stub :ask_for_password, "dummy_pass" do | ||
| Open3.stubs(:capture3).with("keepassxc-cli", "show", "/tmp/secrets.kdbx", "test-env", "-a", "MY_SECRET", "-q", "--show-protected", stdin_data: "dummy_pass") | ||
| .returns(["cli_value", "", mock(success?: true)]) | ||
|
|
||
| secrets = @keepassxc.fetch(["MY_SECRET"], account: "/tmp/secrets.kdbx", from: "test-env") | ||
| assert_equal({"MY_SECRET" => "cli_value"}, secrets) | ||
| end | ||
| end | ||
|
|
||
| test "fetch via ENV (Fallback/CI Mode)" do | ||
| # Simulate when CLI is missing | ||
| stub_ticks_with("keepassxc-cli --version 2> /dev/null", succeed: false) | ||
|
|
||
| with_env("MY_SECRET" => "env_value") do | ||
| @keepassxc.expects(:ask_for_password).never | ||
| Open3.expects(:capture3).never | ||
|
|
||
| secrets = @keepassxc.fetch(["MY_SECRET"], account: "ignore", from: "ignore") | ||
| assert_equal({"MY_SECRET" => "env_value"}, secrets) | ||
| end | ||
| end | ||
|
|
||
| test "fetch raises if CLI missing AND Env missing" do | ||
| stub_ticks_with("keepassxc-cli --version 2> /dev/null", succeed: false) | ||
|
|
||
| error = assert_raises(RuntimeError) do | ||
| @keepassxc.fetch(["MISSING_SECRET"], account: "ignore", from: "ignore") | ||
| end | ||
| assert_match(/KeePassXC CLI is not Installed & secret 'MISSING_SECRET' is missing in ENV./, error.message) | ||
| end | ||
|
|
||
| test "check_dependencies! is no-op (supports fallback)" do | ||
| assert_nothing_raised { @keepassxc.send(:check_dependencies!) } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| 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.