zalando / go-keyring

Cross-platform keyring interface for Go
MIT License
815 stars 82 forks source link

Adding IsAvailable to the interface #27

Open snorberhuis opened 6 years ago

snorberhuis commented 6 years ago

I am integrating this library in a project of mine and I am running into the issue that I want to bypass caching if it is not available locally.

A method added to the interface would be helpful. The method IsAvailable would check if the service is available. I looked at the source and saw that it might have been already tried to add a function like IsAvailable. I see a commented out function:

// func (*MacOSXKeychain) IsAvailable() bool {
//  return exec.Command(execPathKeychain).Run() != exec.ErrNotFound
// }

Before I go into developing this more deeply, I was wondering if you tried to adding this method and have come into fundamental problems that prevent implementation.

We can also discuss if this is a good feature to be merged upstream into this library.

mikkeloscar commented 6 years ago

Hi, I completely lost track of this issue, sorry about that!

There is already this special error: https://github.com/zalando/go-keyring/blob/master/keyring_darwin.go#L43 returned from Get() so you can just check if this error is returned and handle the is-not-available case.

pjcdawkins commented 3 years ago

https://github.com/zalando/go-keyring/blob/9725ced9beac3f859c3657eec29871c53215ad8c/keyring_darwin.go#L47

ErrNotFound merely indicates that the secret was not found. You'd get that error when you just hadn't yet stored a secret.

I am (and I think the OP is) looking for a method that checks for security/dbus/libsecret/etc. being available, so external code can find a fallback to store secrets via another library or other behavior.

Alternatively a different special error should be returned in that case. Perhaps it could reuse or wrap the ErrUnsupportedPlatform error, because it's pretty much the same problem. I think that would work just as well as the method.

erdaltsksn commented 2 years ago

I am (and I think the OP is) looking for a method that checks for security/dbus/libsecret/etc. being available, so external code can find a fallback to store secrets via another library or other behavior.

A fallback solution can be beneficial. A hashed file can be a simple solution for this. There is already a fallback provider. We just need to implement a file solution for it. Happy to work on it if it is okay for maintainers.