Skip to content
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

feat: Implement builtin mode for keepassxc #4149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bakito
Copy link

@bakito bakito commented Dec 20, 2024

Implements: #4148
Depends on: #4150

Copy link
Owner

@twpayne twpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Instead of a new set of template functions, I think this different "mode" for opening KeepassXC databases. Currently chemzoi supports "cache-password" and "open" modes. I think this should be a new "builtin" mode.

What do you think?

.gitignore Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
@bakito
Copy link
Author

bakito commented Dec 20, 2024

Thanks for this!

Instead of a new set of template functions, I think this different "mode" for opening KeepassXC databases. Currently chemzoi supports "cache-password" and "open" modes. I think this should be a new "builtin" mode.

What do you think?

Sounds reasonable.
I modified the implementation and added the functions for the new mode.

@@ -100,6 +113,18 @@ func (c *Config) keepassxcTemplateFunc(entry string) map[string]string {
}

func (c *Config) keepassxcAttributeTemplateFunc(entry, attribute string) string {
if c.Keepassxc.Mode == keepassxcModeBuiltin {
// builtin stores attributes in cache
if data, ok := c.Keepassxc.cache[entry]; ok {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the library handles default values the same as attributes, both functions use the same cache in builtin mode.
I don't think that i makes sense to keep separate caches in this case.

@@ -329,3 +354,82 @@ func (c *Config) keepassxcClose() error {
}
return nil
}

// keepassxcBuiltinExtractValues extract builtin values
func (c *Config) keepassxcBuiltinExtractValues(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation reads all values from the keepass database to reduce access to the file itself.
If this might blow the memory I can change the implementation to only cache the requested values.

@bakito bakito force-pushed the keepass branch 3 times, most recently from 5396dc8 to f42961b Compare December 20, 2024 22:20
Copy link
Owner

@twpayne twpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is looking excellent, and I will do a full review after the weekend.

Please also check the contributing guide for low-hanging fruit.

But, thank you, this PR is excellent.

@bakito bakito changed the title [WIP] implement keepasslib as alternative to keepassxc feat: Implement builtin mode for keepassxc Dec 21, 2024
@@ -117,6 +117,7 @@ func TestKeepassxcTemplateFuncs(t *testing.T) {
for _, mode := range []keepassxcMode{
keepassxcModeCachePassword,
keepassxcModeOpen,
keepassxcModeBuiltin,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having some issues on running this test.
When only keepassxcModeBuiltin mode is enabled, the test is running.
But when the other 2 are also enabled (or only the 2 other) the test fails.

I can't figure out the issue here. Might be some precondition in my environment.

Enter password to encrypt database (optional): 
Repeat password: 
Successfully created new database.
Enter password to unlock /tmp/TestKeepassxcTemplateFuncs1798468072/001/KeePassXC Passwords.kdbx: 
Successfully added group test group.
Enter password to unlock /tmp/TestKeepassxcTemplateFuncs1798468072/001/KeePassXC Passwords.kdbx: 
Enter password for new entry: 
Successfully added entry test entry.
Enter password to unlock /tmp/TestKeepassxcTemplateFuncs1798468072/001/KeePassXC Passwords.kdbx: 
Successfully imported attachment /tmp/TestKeepassxcTemplateFuncs1798468072/001/import file as test / attachment name to entry test group/test entry.
error: Permission denied
--- FAIL: TestKeepassxcTemplateFuncs (0.37s)
    --- FAIL: TestKeepassxcTemplateFuncs/cache-password (0.01s)
        --- FAIL: TestKeepassxcTemplateFuncs/cache-password/correct_password (0.01s)
panic: /home/bakito/bin/keepassxc-cli show '/tmp/TestKeepassxcTemplateFuncs1798468072/001/KeePassXC Passwords.kdbx' --quiet --show-protected 'test group/test entry': exit status 1 [recovered]
	panic: /home/bakito/bin/keepassxc-cli show '/tmp/TestKeepassxcTemplateFuncs1798468072/001/KeePassXC Passwords.kdbx' --quiet --show-protected 'test group/test entry': exit status 1

goroutine 415 [running]:
testing.tRunner.func1.2({0x17a2560, 0xc006f6e370})
	/home/bakito/.gimme/versions/go1.23.4.linux.amd64/src/testing/testing.go:1632 +0x230
testing.tRunner.func1()
	/home/bakito/.gimme/versions/go1.23.4.linux.amd64/src/testing/testing.go:1635 +0x35e
panic({0x17a2560?, 0xc006f6e370?})
	/home/bakito/.gimme/versions/go1.23.4.linux.amd64/src/runtime/panic.go:785 +0x132
github.com/twpayne/chezmoi/v2/internal/cmd.mustValue[...](...)
	/home/bakito/go/src/github.com/bakito/chezmoi/internal/cmd/cmd.go:254
github.com/twpayne/chezmoi/v2/internal/cmd.(*Config).keepassxcTemplateFunc(0xc0073aa008, {0xc008243e18, 0x15})
	/home/bakito/go/src/github.com/bakito/chezmoi/internal/cmd/keepassxctemplatefuncs.go:103 +0x24a
github.com/twpayne/chezmoi/v2/internal/cmd.TestKeepassxcTemplateFuncs.func1.1(0xc0072c3380)
	/home/bakito/go/src/github.com/bakito/chezmoi/internal/cmd/keepassxctemplatefuncs_test.go:130 +0x1fa
testing.tRunner(0xc0072c3380, 0xc00737b170)
	/home/bakito/.gimme/versions/go1.23.4.linux.amd64/src/testing/testing.go:1690 +0xf4
created by testing.(*T).Run in goroutine 414
	/home/bakito/.gimme/versions/go1.23.4.linux.amd64/src/testing/testing.go:1743 +0x390

@bakito bakito marked this pull request as ready for review December 21, 2024 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants