forked from gsterjov/go-libsecret
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem_funcs.go
422 lines (316 loc) · 8.58 KB
/
item_funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package gosecret
import (
"strconv"
"strings"
"time"
"github.com/godbus/dbus/v5"
)
// NewItem returns a pointer to an Item based on Collection and a Dbus path.
func NewItem(collection *Collection, path dbus.ObjectPath) (item *Item, err error) {
var splitPath []string
if collection == nil {
err = ErrNoDbusConn
}
if _, err = validConnPath(collection.Conn, path); err != nil {
return
}
item = &Item{
DbusObject: &DbusObject{
Conn: collection.Conn,
Dbus: collection.Conn.Object(DbusService, path),
},
}
splitPath = strings.Split(string(item.Dbus.Path()), "/")
item.idx, err = strconv.Atoi(splitPath[len(splitPath)-1])
item.collection = collection
// Populate the struct fields...
// TODO: use channel for errors; condense into a MultiError and switch to goroutines.
if _, err = item.GetSecret(collection.service.Session); err != nil {
return
}
if _, err = item.Locked(); err != nil {
return
}
if _, err = item.Attributes(); err != nil {
return
}
if _, err = item.Label(); err != nil {
return
}
if _, err = item.Type(); err != nil {
return
}
if _, err = item.Created(); err != nil {
return
}
if _, _, err = item.Modified(); err != nil {
return
}
return
}
// Attributes returns the Item's attributes from Dbus.
func (i *Item) Attributes() (attrs map[string]string, err error) {
var variant dbus.Variant
if variant, err = i.Dbus.GetProperty(DbusItemAttributes); err != nil {
return
}
attrs = variant.Value().(map[string]string)
i.Attrs = attrs
return
}
/*
ChangeItemType changes an Item.Type to newItemType.
Note that this is probably a bad idea unless you're also doing Item.SetSecret.
It must be a Dbus interface path (e.g. "foo.bar.Baz").
If newItemType is an empty string, DbusDefaultItemType will be used.
*/
func (i *Item) ChangeItemType(newItemType string) (err error) {
var variant dbus.Variant
// Legacy spec.
if i.collection.service.Legacy {
return
}
if strings.TrimSpace(newItemType) == "" {
newItemType = DbusDefaultItemType
}
variant = dbus.MakeVariant(newItemType)
if err = i.Dbus.SetProperty(DbusItemType, variant); err != nil {
return
}
i.SecretType = newItemType
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// 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 call = i.Dbus.Call(
DbusItemDelete, 0,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&promptPath); err != nil {
return
}
if isPrompt(promptPath) {
prompt = NewPrompt(i.Conn, promptPath)
if _, err = prompt.Prompt(); err != nil {
return
}
}
return
}
// 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
}
if _, err = connIsValid(session.Conn); err != nil {
return
}
if call = i.Dbus.Call(
DbusItemGetSecret, 0, session.Dbus.Path(),
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&secret); err != nil {
return
}
secret.session = session
secret.item = i
i.Secret = secret
return
}
// Label returns the label ("name") of an Item.
func (i *Item) Label() (label string, err error) {
var variant dbus.Variant
if variant, err = i.Dbus.GetProperty(DbusItemLabel); err != nil {
return
}
label = variant.Value().(string)
i.LabelName = label
return
}
/*
ModifyAttributes modifies the Item's attributes in Dbus.
This is similar to Item.ReplaceAttributes but will only modify the map's given keys so you do not need to provide
the entire attribute map.
If you wish to remove an attribute, use the value "" (empty string).
If you wish to explicitly provide a blank value/empty string, use the constant gosecret.ExplicitAttrEmptyValue.
This is more or less a convenience/wrapper function around Item.ReplaceAttributes.
*/
func (i *Item) ModifyAttributes(replaceAttrs map[string]string) (err error) {
var ok bool
var currentProps map[string]string = make(map[string]string, 0)
var currentVal string
if replaceAttrs == nil || len(replaceAttrs) == 0 {
err = ErrMissingAttrs
return
}
if currentProps, err = i.Attributes(); err != nil {
return
}
for k, v := range replaceAttrs {
if currentVal, ok = currentProps[k]; !ok { // If it isn't in the replacement map, do nothing (i.e. keep it).
continue
} else if v == currentVal { // If the value is the same, do nothing.
continue
} else if v == ExplicitAttrEmptyValue { // If it's the "magic empty value" constant, delete the key/value pair.
delete(currentProps, k)
continue
} else { // Otherwise, replace the value.
currentProps[k] = v
}
}
if err = i.ReplaceAttributes(currentProps); err != nil {
return
}
return
}
// Relabel modifies the Item's label in Dbus.
func (i *Item) Relabel(newLabel string) (err error) {
var variant dbus.Variant = dbus.MakeVariant(newLabel)
if err = i.Dbus.SetProperty(DbusItemLabel, variant); err != nil {
return
}
i.LabelName = newLabel
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// ReplaceAttributes replaces the Item's attributes in Dbus.
func (i *Item) ReplaceAttributes(newAttrs map[string]string) (err error) {
var props dbus.Variant
props = dbus.MakeVariant(newAttrs)
if err = i.Dbus.SetProperty(DbusItemAttributes, props); err != nil {
return
}
i.Attrs = newAttrs
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// SetSecret sets the Secret for an Item.
func (i *Item) SetSecret(secret *Secret) (err error) {
var call *dbus.Call
if call = i.Dbus.Call(
DbusItemSetSecret, 0,
); call.Err != nil {
err = call.Err
return
}
i.Secret = secret
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// Type updates the Item.ItemType from DBus (and returns it).
func (i *Item) Type() (itemType string, err error) {
var variant dbus.Variant
// Legacy spec.
if i.collection.service.Legacy {
return
}
if variant, err = i.Dbus.GetProperty(DbusItemType); err != nil {
return
}
itemType = variant.Value().(string)
i.SecretType = itemType
return
}
// Lock will lock an unlocked Item. It will no-op if the Item is currently locked.
func (i *Item) Lock() (err error) {
if _, err = i.Locked(); err != nil {
return
}
if i.IsLocked {
return
}
if err = i.collection.service.Lock(i); err != nil {
return
}
i.IsLocked = true
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// Locked indicates if an Item is locked (true) or unlocked (false).
func (i *Item) Locked() (isLocked bool, err error) {
var variant dbus.Variant
if variant, err = i.Dbus.GetProperty(DbusItemLocked); err != nil {
isLocked = true
return
}
isLocked = variant.Value().(bool)
i.IsLocked = isLocked
return
}
// Unlock will unlock a locked Item. It will no-op if the Item is currently unlocked.
func (i *Item) Unlock() (err error) {
if _, err = i.Locked(); err != nil {
return
}
if !i.IsLocked {
return
}
if err = i.collection.service.Unlock(i); err != nil {
return
}
i.IsLocked = false
if _, _, err = i.Modified(); err != nil {
return
}
return
}
// Created returns the time.Time of when an Item was created.
func (i *Item) Created() (created time.Time, err error) {
var variant dbus.Variant
var timeInt uint64
if variant, err = i.Dbus.GetProperty(DbusItemCreated); err != nil {
return
}
timeInt = variant.Value().(uint64)
created = time.Unix(int64(timeInt), 0)
i.CreatedAt = created
return
}
/*
Modified returns the time.Time of when an Item was last modified along with a boolean
that indicates if the collection has changed since the last call of Item.Modified.
Note that when calling NewItem, the internal library-tracked modification
time (Item.LastModified) will be set to the latest modification time of the Item
itself as reported by Dbus rather than the time that NewItem was called.
*/
func (i *Item) Modified() (modified time.Time, isChanged bool, err error) {
var variant dbus.Variant
var timeInt uint64
if variant, err = i.Dbus.GetProperty(DbusItemModified); err != nil {
return
}
timeInt = variant.Value().(uint64)
modified = time.Unix(int64(timeInt), 0)
if !i.lastModifiedSet {
// It's "nil", so set it to modified. We can't check for a zero-value in case Dbus has it as a zero-value.
i.LastModified = modified
i.lastModifiedSet = true
}
isChanged = modified.After(i.LastModified)
i.LastModified = modified
return
}
// path is a *very* thin wrapper around Item.Dbus.Path(). It is needed for LockableObject membership.
func (i *Item) path() (dbusPath dbus.ObjectPath) {
dbusPath = i.Dbus.Path()
return
}