2020-07-27 01:14:01 +03:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-12-05 05:08:54 +03:00
|
|
|
"path/filepath"
|
2020-07-27 01:14:01 +03:00
|
|
|
|
|
|
|
"github.com/99designs/keyring"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Item = keyring.Item
|
|
|
|
|
|
|
|
var ErrKeyringKeyNotFound = keyring.ErrKeyNotFound
|
|
|
|
|
|
|
|
// Keyring provides the uniform interface over the underlying backends
|
|
|
|
type Keyring interface {
|
2022-02-15 23:35:49 +03:00
|
|
|
// Get returns an Item matching the key or ErrKeyringKeyNotFound
|
2020-07-27 01:14:01 +03:00
|
|
|
Get(key string) (Item, error)
|
2022-02-15 23:35:49 +03:00
|
|
|
// Set stores an Item on the keyring. Set is idempotent.
|
2020-07-27 01:14:01 +03:00
|
|
|
Set(item Item) error
|
2022-02-15 23:35:49 +03:00
|
|
|
// Remove removes the item with matching key
|
2020-07-27 01:14:01 +03:00
|
|
|
Remove(key string) error
|
2022-02-15 23:35:49 +03:00
|
|
|
// Keys provides a slice of all keys stored on the keyring
|
2020-07-27 01:14:01 +03:00
|
|
|
Keys() ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultKeyring() (Keyring, error) {
|
|
|
|
ucd, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyring.Open(keyring.Config{
|
2020-09-27 00:01:37 +03:00
|
|
|
// only use the file backend until https://github.com/99designs/keyring/issues/74 is resolved
|
|
|
|
AllowedBackends: []keyring.BackendType{
|
|
|
|
keyring.FileBackend,
|
|
|
|
},
|
2020-07-27 01:14:01 +03:00
|
|
|
|
|
|
|
ServiceName: "git-bug",
|
|
|
|
|
|
|
|
// Fallback encrypted file
|
2020-12-05 05:08:54 +03:00
|
|
|
FileDir: filepath.Join(ucd, "git-bug", "keyring"),
|
2020-07-27 01:14:01 +03:00
|
|
|
// As we write the file in the user's config directory, this file should already be protected by the OS against
|
|
|
|
// other user's access. We actually don't terribly need to protect it further and a password prompt across all
|
|
|
|
// UI's would be a pain. Therefore we use here a constant password so the file will be unreadable by generic file
|
|
|
|
// scanners if the user's machine get compromised.
|
|
|
|
FilePasswordFunc: func(string) (string, error) {
|
|
|
|
return "git-bug", nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-01-04 01:59:25 +03:00
|
|
|
|
|
|
|
// replaceKeyring allow to replace the Keyring of the underlying repo
|
|
|
|
type replaceKeyring struct {
|
|
|
|
TestedRepo
|
|
|
|
keyring Keyring
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rk replaceKeyring) Keyring() Keyring {
|
|
|
|
return rk.keyring
|
|
|
|
}
|