4 Commits

8 changed files with 162 additions and 69 deletions

2
TODO
View File

@@ -1 +1,3 @@
- Benchmarking?
- call .Close() on dbus.Conns

View File

@@ -65,6 +65,7 @@ func NewCollection(service *Service, path dbus.ObjectPath) (coll *Collection, er
*/
func (c *Collection) CreateItem(label string, attrs map[string]string, secret *Secret, replace bool, itemType ...string) (item *Item, err error) {
var call *dbus.Call
var prompt *Prompt
var path dbus.ObjectPath
var promptPath dbus.ObjectPath
@@ -84,9 +85,13 @@ func (c *Collection) CreateItem(label string, attrs map[string]string, secret *S
props[DbusItemCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
// props[DbusItemModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
if err = c.Dbus.Call(
if call = c.Dbus.Call(
DbusCollectionCreateItem, 0, props, secret, replace,
).Store(&path, &promptPath); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&path, &promptPath); err != nil {
return
}
@@ -114,10 +119,17 @@ func (c *Collection) CreateItem(label string, attrs map[string]string, secret *S
*/
func (c *Collection) Delete() (err error) {
var call *dbus.Call
var promptPath dbus.ObjectPath
var prompt *Prompt
if err = c.Dbus.Call(DbusCollectionDelete, 0).Store(&promptPath); err != nil {
if call = c.Dbus.Call(
DbusCollectionDelete, 0,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&promptPath); err != nil {
return
}
@@ -146,15 +158,16 @@ func (c *Collection) Items() (items []*Item, err error) {
paths = variant.Value().([]dbus.ObjectPath)
items = make([]*Item, len(paths))
items = make([]*Item, 0)
for idx, path := range paths {
for _, path := range paths {
item = nil
if item, err = NewItem(c, path); err != nil {
errs = append(errs, err)
err = nil
continue
}
items[idx] = item
items = append(items, item)
}
err = NewErrors(err)
@@ -242,26 +255,34 @@ func (c *Collection) Relabel(newLabel string) (err error) {
*/
func (c *Collection) SearchItems(profile string) (items []*Item, err error) {
var call *dbus.Call
var paths []dbus.ObjectPath
var errs []error = make([]error, 0)
var attrs map[string]string = make(map[string]string, 0)
var item *Item
attrs["profile"] = profile
if err = c.Dbus.Call(
if call = c.Dbus.Call(
DbusCollectionSearchItems, 0, attrs,
).Store(&paths); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&paths); err != nil {
return
}
items = make([]*Item, len(paths))
items = make([]*Item, 0)
for idx, path := range paths {
if items[idx], err = NewItem(c, path); err != nil {
for _, path := range paths {
item = nil
if item, err = NewItem(c, path); err != nil {
errs = append(errs, err)
err = nil
continue
}
items = append(items, item)
}
err = NewErrors(err)
@@ -273,11 +294,10 @@ func (c *Collection) SetAlias(alias string) (err error) {
var call *dbus.Call
call = c.service.Dbus.Call(
if call = c.service.Dbus.Call(
DbusServiceSetAlias, 0, alias, c.Dbus.Path(),
)
if err = call.Err; err != nil {
); call.Err != nil {
err = call.Err
return
}

View File

@@ -1,9 +1,9 @@
package gosecret
import (
`testing`
"testing"
`github.com/godbus/dbus/v5`
"github.com/godbus/dbus/v5"
)
// Some functions are covered in the Service tests.
@@ -57,7 +57,8 @@ func TestCollection_Items(t *testing.T) {
var collection *Collection
var items []*Item
var item *Item
var searchItemResults []*Item
var searchResultsUnlocked []*Item
var searchResultsLocked []*Item
var secret *Secret
var err error
@@ -109,12 +110,12 @@ func TestCollection_Items(t *testing.T) {
)
} else {
if searchItemResults, err = collection.SearchItems(testItemLabel); err != nil {
if searchResultsUnlocked, searchResultsLocked, err = collection.service.SearchItems(itemAttrs); err != nil {
t.Errorf("failed to find item '%v' via Collection.SearchItems: %v", string(item.Dbus.Path()), err.Error())
} else if len(searchItemResults) == 0 {
} else if (len(searchResultsLocked) + len(searchResultsUnlocked)) == 0 {
t.Errorf("failed to find item '%v' via Collection.SearchItems, returned 0 results (should be at least 1)", testItemLabel)
} else {
t.Logf("found %v results for Collection.SearchItems", len(searchItemResults))
t.Logf("found %v results for Collection.SearchItems", len(searchResultsUnlocked)+len(searchResultsLocked))
}
if err = item.Delete(); err != nil {
@@ -148,6 +149,7 @@ func TestCollection_Label(t *testing.T) {
t.Fatalf("NewService failed: %v", err.Error())
}
t.Logf("Attempting to get label of collection: %v", defaultCollectionLabel)
if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil {
t.Errorf(
"failed when fetching collection '%v': %v",

View File

@@ -1,7 +1,7 @@
package gosecret
import (
`github.com/godbus/dbus/v5`
"github.com/godbus/dbus/v5"
)
// Constants for use with gosecret.
@@ -30,6 +30,7 @@ const (
// Libsecret/SecretService special values.
var (
// DbusRemoveAliasPath is used to remove an alias from a Collection and/or Item.
DbusRemoveAliasPath dbus.ObjectPath = dbus.ObjectPath("/")
)

View File

@@ -106,10 +106,17 @@ func (i *Item) ChangeItemType(newItemType string) (err error) {
// Delete removes an Item from a Collection.
func (i *Item) Delete() (err error) {
var call *dbus.Call
var promptPath dbus.ObjectPath
var prompt *Prompt
if err = i.Dbus.Call(DbusItemDelete, 0).Store(&promptPath); err != nil {
if call = i.Dbus.Call(
DbusItemDelete, 0,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&promptPath); err != nil {
return
}
@@ -127,6 +134,8 @@ func (i *Item) Delete() (err error) {
// GetSecret returns the Secret in an Item using a Session.
func (i *Item) GetSecret(session *Session) (secret *Secret, err error) {
var call *dbus.Call
if session == nil {
err = ErrNoDbusConn
}
@@ -135,9 +144,13 @@ func (i *Item) GetSecret(session *Session) (secret *Secret, err error) {
return
}
if err = i.Dbus.Call(
if call = i.Dbus.Call(
DbusItemGetSecret, 0, session.Dbus.Path(),
).Store(&secret); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&secret); err != nil {
return
}
@@ -246,15 +259,15 @@ func (i *Item) ReplaceAttributes(newAttrs map[string]string) (err error) {
// SetSecret sets the Secret for an Item.
func (i *Item) SetSecret(secret *Secret) (err error) {
var c *dbus.Call
var call *dbus.Call
c = i.Dbus.Call(
if call = i.Dbus.Call(
DbusItemSetSecret, 0,
)
if c.Err != nil {
err = c.Err
); call.Err != nil {
err = call.Err
return
}
i.Secret = secret
if _, _, err = i.Modified(); err != nil {

View File

@@ -1,7 +1,7 @@
package gosecret
import (
`fmt`
"fmt"
)
/*
@@ -35,6 +35,7 @@ func NewErrors(errs ...error) (err error) {
return
}
// Error makes a MultiError conform to the error interface.
func (e *MultiError) Error() (errStr string) {
var numErrs int
@@ -47,7 +48,7 @@ func (e *MultiError) Error() (errStr string) {
for idx, err := range e.Errors {
if (idx + 1) < numErrs {
errStr += fmt.Sprintf(err.Error(), e.ErrorSep)
errStr += fmt.Sprintf("%v%v", err.Error(), e.ErrorSep)
} else {
errStr += err.Error()
}

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"path/filepath"
"strings"
`time`
"time"
"github.com/godbus/dbus/v5"
)
@@ -38,7 +38,13 @@ func NewService() (service *Service, err error) {
// Close cleanly closes a Service and all its underlying connections (e.g. Service.Session).
func (s *Service) Close() (err error) {
err = s.Session.Close()
if err = s.Session.Close(); err != nil {
return
}
if err = s.Conn.Close(); err != nil {
return
}
return
}
@@ -57,16 +63,17 @@ func (s *Service) Collections() (collections []*Collection, err error) {
paths = variant.Value().([]dbus.ObjectPath)
collections = make([]*Collection, len(paths))
collections = make([]*Collection, 0)
for idx, path := range paths {
for _, path := range paths {
coll = nil
if coll, err = NewCollection(s, path); err != nil {
// return
errs = append(errs, err)
err = nil
continue
}
collections[idx] = coll
collections = append(collections, coll)
}
err = NewErrors(err)
@@ -79,6 +86,7 @@ func (s *Service) Collections() (collections []*Collection, err error) {
*/
func (s *Service) CreateAliasedCollection(label, alias string) (collection *Collection, err error) {
var call *dbus.Call
var variant *dbus.Variant
var path dbus.ObjectPath
var promptPath dbus.ObjectPath
@@ -89,9 +97,13 @@ func (s *Service) CreateAliasedCollection(label, alias string) (collection *Coll
props[DbusCollectionCreated] = dbus.MakeVariant(uint64(time.Now().Unix()))
props[DbusCollectionModified] = dbus.MakeVariant(uint64(time.Now().Unix()))
if err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceCreateCollection, 0, props, alias,
).Store(&path, &promptPath); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&path, &promptPath); err != nil {
return
}
@@ -196,6 +208,7 @@ func (s *Service) GetSecrets(itemPaths ...dbus.ObjectPath) (secrets map[dbus.Obj
}
*/
var results map[dbus.ObjectPath][]interface{}
var call *dbus.Call
if itemPaths == nil || len(itemPaths) == 0 {
err = ErrMissingPaths
@@ -206,9 +219,13 @@ func (s *Service) GetSecrets(itemPaths ...dbus.ObjectPath) (secrets map[dbus.Obj
results = make(map[dbus.ObjectPath][]interface{}, len(itemPaths))
// TODO: trigger a Service.Unlock for any locked items?
if err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceGetSecrets, 0, itemPaths, s.Session.Dbus.Path(),
).Store(&results); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&results); err != nil {
return
}
@@ -235,6 +252,7 @@ func (s *Service) GetSession() (ssn *Session, err error) {
// Lock locks an Unlocked Collection or Item (LockableObject).
func (s *Service) Lock(objects ...LockableObject) (err error) {
var call *dbus.Call
var toLock []dbus.ObjectPath
// We only use these as destinations.
var locked []dbus.ObjectPath
@@ -252,9 +270,13 @@ func (s *Service) Lock(objects ...LockableObject) (err error) {
toLock[idx] = o.path()
}
if err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceLock, 0, toLock,
).Store(&locked, &promptPath); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&locked, &promptPath); err != nil {
return
}
@@ -284,6 +306,7 @@ func (s *Service) Lock(objects ...LockableObject) (err error) {
*/
func (s *Service) OpenSession(algo, input string) (session *Session, output dbus.Variant, err error) {
var call *dbus.Call
var path dbus.ObjectPath
var inputVariant dbus.Variant
@@ -297,9 +320,13 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus
// TODO: confirm this.
// Possible flags are dbus.Flags consts: https://pkg.go.dev/github.com/godbus/dbus#Flags
// Oddly, there is no "None" flag. So it's explicitly specified as a null byte.
if err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceOpenSession, 0, algo, inputVariant,
).Store(&output, &path); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&output, &path); err != nil {
return
}
@@ -315,17 +342,20 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus
*/
func (s *Service) ReadAlias(alias string) (collection *Collection, err error) {
var call *dbus.Call
var objectPath dbus.ObjectPath
err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceReadAlias, 0, alias,
).Store(&objectPath)
); call.Err != nil {
err = call.Err
return
}
/*
TODO: Confirm that a nonexistent alias will NOT cause an error to return.
If it does, alter the below logic.
*/
if err != nil {
if err = call.Store(&objectPath); err != nil {
return
}
@@ -357,6 +387,7 @@ func (s *Service) RemoveAlias(alias string) (err error) {
*/
func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*Item, lockedItems []*Item, err error) {
var call *dbus.Call
var locked []dbus.ObjectPath
var unlocked []dbus.ObjectPath
var collectionObjs []*Collection
@@ -364,6 +395,7 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
var ok bool
var c *Collection
var cPath dbus.ObjectPath
var item *Item
var errs []error = make([]error, 0)
if attributes == nil || len(attributes) == 0 {
@@ -371,12 +403,16 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
return
}
err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceSearchItems, 0, attributes,
).Store(&unlocked, &locked)
); call.Err != nil {
}
if err = call.Store(&unlocked, &locked); err != nil {
return
}
lockedItems = make([]*Item, len(locked))
unlockedItems = make([]*Item, len(unlocked))
lockedItems = make([]*Item, 0)
unlockedItems = make([]*Item, 0)
if collectionObjs, err = s.Collections(); err != nil {
return
@@ -389,8 +425,9 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
}
// Locked items
for idx, i := range locked {
for _, i := range locked {
item = nil
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
if c, ok = collections[cPath]; !ok {
@@ -400,18 +437,20 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
continue
}
if lockedItems[idx], err = NewItem(c, i); err != nil {
if item, err = NewItem(c, i); err != nil {
errs = append(errs, errors.New(fmt.Sprintf(
"could not create Item for locked item %v", string(i),
)))
err = nil
continue
}
lockedItems = append(lockedItems, item)
}
// Unlocked items
for idx, i := range unlocked {
for _, i := range unlocked {
item = nil
cPath = dbus.ObjectPath(filepath.Dir(string(i)))
if c, ok = collections[cPath]; !ok {
@@ -421,13 +460,14 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
continue
}
if unlockedItems[idx], err = NewItem(c, i); err != nil {
if item, err = NewItem(c, i); err != nil {
errs = append(errs, errors.New(fmt.Sprintf(
"could not create Item for unlocked item %v", string(i),
)))
err = nil
continue
}
unlockedItems = append(unlockedItems, item)
}
if errs != nil && len(errs) > 0 {
@@ -444,18 +484,17 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It
*/
func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error) {
var c *dbus.Call
var call *dbus.Call
var collection *Collection
if collection, err = s.GetCollection(alias); err != nil {
return
}
c = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceSetAlias, 0, alias, objectPath,
)
if err = c.Err; err != nil {
); call.Err != nil {
err = call.Err
return
}
@@ -471,6 +510,7 @@ func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error)
// Unlock unlocks a locked Collection or Item (LockableObject).
func (s *Service) Unlock(objects ...LockableObject) (err error) {
var call *dbus.Call
var toUnlock []dbus.ObjectPath
// We only use these as destinations.
var unlocked []dbus.ObjectPath
@@ -488,9 +528,13 @@ func (s *Service) Unlock(objects ...LockableObject) (err error) {
toUnlock[idx] = o.path()
}
if err = s.Dbus.Call(
if call = s.Dbus.Call(
DbusServiceUnlock, 0, toUnlock,
).Store(&unlocked, &resultPath); err != nil {
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&unlocked, &resultPath); err != nil {
return
}

View File

@@ -1,6 +1,8 @@
package gosecret
import (
"fmt"
"github.com/godbus/dbus/v5"
)
@@ -32,13 +34,21 @@ func NewSession(service *Service, path dbus.ObjectPath) (session *Session, err e
// Close cleanly closes a Session.
func (s *Session) Close() (err error) {
var c *dbus.Call
var call *dbus.Call
c = s.Dbus.Call(
if call = s.Dbus.Call(
DbusSessionClose, 0,
)
_ = c
); call.Err != nil {
/*
I... still haven't 100% figured out why this happens, but the session DOES seem to close...?
PRs or input welcome.
TODO: figure out why this error gets triggered.
*/
if call.Err.Error() != fmt.Sprintf("The name %v was not provided by any .service files", DbusInterfaceSession) {
err = call.Err
return
}
}
return
}