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
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 51 additions & 7 deletions keyring_mock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package keyring

type mockProviderItem struct {
Value string
Locked bool
}

type mockProvider struct {
mockStore map[string]map[string]string
mockStore map[string]map[string]*mockProviderItem
mockError error
}

Expand All @@ -12,12 +17,12 @@ func (m *mockProvider) Set(service, user, pass string) error {
return m.mockError
}
if m.mockStore == nil {
m.mockStore = make(map[string]map[string]string)
m.mockStore = make(map[string]map[string]*mockProviderItem)
}
if m.mockStore[service] == nil {
m.mockStore[service] = make(map[string]string)
m.mockStore[service] = make(map[string]*mockProviderItem)
}
m.mockStore[service][user] = pass
m.mockStore[service][user] = &mockProviderItem{Value: pass, Locked: true}
return nil
}

Expand All @@ -27,8 +32,12 @@ func (m *mockProvider) Get(service, user string) (string, error) {
return "", m.mockError
}
if b, ok := m.mockStore[service]; ok {
if v, ok := b[user]; ok {
return v, nil
if item, ok := b[user]; ok {
if item.Locked {
return "", ErrNotFound
}
_ = m.Lock(service, user)
return item.Value, nil
}
}
return "", ErrNotFound
Expand All @@ -41,7 +50,10 @@ func (m *mockProvider) Delete(service, user string) error {
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if _, ok := m.mockStore[service][user]; ok {
if item, ok := m.mockStore[service][user]; ok {
if item.Locked {
return ErrNotFound
}
delete(m.mockStore[service], user)
return nil
}
Expand All @@ -50,6 +62,38 @@ func (m *mockProvider) Delete(service, user string) error {
return ErrNotFound
}

// Unlock unlocks item from the keyring given a service name and a user
func (m *mockProvider) Unlock(service, user string) error {
AlanD20 marked this conversation as resolved.
Show resolved Hide resolved
if m.mockError != nil {
return m.mockError
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if item, ok := m.mockStore[service][user]; ok {
item.Locked = false
return nil
}
}
}
return ErrNotFound
}

// Lock locks item from the keyring given a service name and a user
func (m *mockProvider) Lock(service, user string) error {
if m.mockError != nil {
return m.mockError
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if item, ok := m.mockStore[service][user]; ok {
item.Locked = true
return nil
}
}
}
return ErrNotFound
}

// MockInit sets the provider to a mocked memory store
func MockInit() {
provider = &mockProvider{}
Expand Down
18 changes: 18 additions & 0 deletions keyring_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestMockGet(t *testing.T) {
t.Errorf("Should not fail, got: %s", err)
}

_ = mp.Unlock(service, user)
pw, err := mp.Get(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
Expand All @@ -32,6 +33,22 @@ func TestMockGet(t *testing.T) {
}
}

// TestGetLocked tests getting a locked password from the keyring.
func TestMockGetLocked(t *testing.T) {
mp := mockProvider{}
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}

pwd, err := mp.Get(service, user)
assertError(t, err, ErrNotFound)

if pwd != "" {
t.Errorf("Should not return item value, got: %s", pwd)
}
}

// TestGetNonExisting tests getting a secret not in the keyring.
func TestMockGetNonExisting(t *testing.T) {
mp := mockProvider{}
Expand All @@ -49,6 +66,7 @@ func TestMockDelete(t *testing.T) {
t.Errorf("Should not fail, got: %s", err)
}

_ = mp.Unlock(service, user)
err = mp.Delete(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
Expand Down
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