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

Prompt to unlock individual item when getting secret from keyring in unix system #108

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions keyring_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func (s secretServiceProvider) Get(service, user string) (string, error) {
}
defer svc.Close(session)

// unlock if invdividual item is locked
err = svc.Unlock(item)
if err != nil {
return "", err
}
Copy link
Member

Choose a reason for hiding this comment

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

Would be awesome if it's possible to create a test case showing this working as expected. You can see how we do the tests currently: https://github.com/zalando/go-keyring/blob/master/.github/workflows/go.yml

Copy link
Contributor Author

@AlanD20 AlanD20 May 31, 2024

Choose a reason for hiding this comment

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

Ok, I figured out how to reproduce using the test cases but I dont know how to write the test cases for it because it's a bit tricky.

basically, a test case has to get an existing secret from the keyring. The issue with the existing TestGet case is that it initially sets the secret which it already has an open/unlocked session, and then it gets the secret, which is totally fine.

But in the case of KeepassXC, where it has the option to lock items, when the session gets closed and another session opens to only retrieve the secret, it requires a prompt from the user to confirm.

I enabled gnome-keyring-daemon locally and ran the test case but it seems to work because it looks like the gnome-keyring-daemon does not have an option to prompt when a secret is retrieved.

If you have KeepassXC enabled as a secret service. Make sure to enable the prompt option, then try this test case:

func TestGetLockedItem(t *testing.T) {
   pw, err := Get(service, "locked-user")
   if err != nil {
      if err == ErrNotFound {
         err := Set(service, "locked-user", password)
         if err != nil {
            t.Errorf("Should not fail, got: %s", err)
         }
         t.Skip("Skipping: Secret stored in keyring, rerun tests to confirm test case.")
      }

      t.Errorf("Should not fail, got: %s", err)
   }

   if password != pw {
      t.Errorf("Expected password %s, got %s", password, pw)
   }
}

This test case has to be run twice. First to set the secret if it's not already set, and then if it's already set, get the secret. I dont like this since it requires to run go test twice.

Any idea how to test this? 😅

here is also a video

go-keyring-test-case.mp4


secret, err := svc.GetSecret(item, session.Path())
if err != nil {
return "", err
Expand Down