-
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.
Open
Changes from all 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,43 @@ | ||
| 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 | ||
|
|
||
| def check_dependencies! | ||
| raise "KeePassXC CLI is not installed." unless cli_installed? | ||
| end | ||
|
|
||
| def login(account) | ||
| ask_for_password(account) | ||
| end | ||
|
|
||
| def fetch_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 | ||
|
|
||
| def cli_installed? | ||
| `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,29 @@ | ||
| require "test_helper" | ||
|
|
||
| class KeepassxcAdapterTest < SecretAdapterTestCase | ||
| setup do | ||
| @keepassxc = Kamal::Secrets::Adapters::Keepassxc.new | ||
| end | ||
|
|
||
| test "fetch via CLI" 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 "check_dependencies! raises if CLI is missing" do | ||
| stub_ticks_with("keepassxc-cli --version 2> /dev/null", succeed: false) | ||
|
|
||
| error = assert_raises(RuntimeError) do | ||
| @keepassxc.send(:check_dependencies!) | ||
| end | ||
| assert_match(/KeePassXC CLI is not installed/, error.message) | ||
| 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.