feat: lazysecrets list's JSON says when an entry was last used, and how often
This commit is contained in:
3 files changed
+23
-11
No files matched your search
@@ -40,9 +40,11 @@ type Entry struct {
|
||||
// AccountLabel names the az account, when there are several.
|
||||
AccountLabel string `json:"accountLabel,omitempty"`
|
||||
// Starred is starred for this account; Recent is used lately (Recent,
|
||||
// R, in the TUI).
|
||||
Starred bool `json:"starred"`
|
||||
Recent bool `json:"recent"`
|
||||
// R, in the TUI): LastUsed is when, Uses how often.
|
||||
Starred bool `json:"starred"`
|
||||
Recent bool `json:"recent"`
|
||||
LastUsed time.Time `json:"lastUsed,omitzero"`
|
||||
Uses int `json:"uses,omitempty"`
|
||||
// Error says why a source's vaults or a vault's secrets couldn't be
|
||||
// listed. The entry is then that source or vault, again, or with an
|
||||
// account's sources unreadable, the account without a link.
|
||||
@@ -61,9 +63,10 @@ type Options struct {
|
||||
// Lists only needed for other kinds aren't loaded: sources and vaults
|
||||
// alone don't load any secrets. Lists that fail are always listed.
|
||||
Kinds []string
|
||||
// Mark tells whether the entry with link is starred for account, and
|
||||
// used lately; nil marks nothing.
|
||||
Mark func(link, account string) (starred, recent bool)
|
||||
// Mark tells whether the entry with link is starred for account,
|
||||
// and when it was last used and how often (zero: not lately); nil
|
||||
// marks nothing.
|
||||
Mark func(link, account string) (starred bool, lastUsed time.Time, uses int)
|
||||
Timeout func(provider.Kind) time.Duration
|
||||
Now func() time.Time
|
||||
}
|
||||
@@ -225,7 +228,8 @@ func (l *lister) put(p provider.Provider, e Entry) {
|
||||
}
|
||||
e.Account = p.ID()
|
||||
if l.o.Mark != nil && e.Link != "" {
|
||||
e.Starred, e.Recent = l.o.Mark(e.Link, e.Account)
|
||||
e.Starred, e.LastUsed, e.Uses = l.o.Mark(e.Link, e.Account)
|
||||
e.Recent = !e.LastUsed.IsZero()
|
||||
}
|
||||
id := p.ID() + "\x00" + e.Link + "\x00" + e.Error
|
||||
if l.stopped != nil || l.seen[id] {
|
||||
|
||||
@@ -229,11 +229,14 @@ func TestListMarks(t *testing.T) {
|
||||
az := fake.Azure(0)
|
||||
var starredLink string
|
||||
got, _, _ := run(t, Options{Providers: []provider.Provider{az}, MaxAge: time.Minute, Kinds: []string{"secret"},
|
||||
Mark: func(link, account string) (bool, bool) {
|
||||
Mark: func(link, account string) (bool, time.Time, int) {
|
||||
if starredLink == "" && strings.HasSuffix(link, "/db-password") {
|
||||
starredLink = link
|
||||
}
|
||||
return link == starredLink && account == az.ID(), strings.HasSuffix(link, "/api-key")
|
||||
if strings.HasSuffix(link, "/api-key") {
|
||||
return false, time.Now(), 3
|
||||
}
|
||||
return link == starredLink && account == az.ID(), time.Time{}, 0
|
||||
}})
|
||||
var starred, recent int
|
||||
for _, e := range got {
|
||||
@@ -245,6 +248,11 @@ func TestListMarks(t *testing.T) {
|
||||
}
|
||||
if e.Recent {
|
||||
recent++
|
||||
if e.Uses != 3 || e.LastUsed.IsZero() {
|
||||
t.Errorf("%s: %d uses, last %v", e.Link, e.Uses, e.LastUsed)
|
||||
}
|
||||
} else if e.Uses != 0 || !e.LastUsed.IsZero() {
|
||||
t.Errorf("%s not recent, but %d uses, last %v", e.Link, e.Uses, e.LastUsed)
|
||||
}
|
||||
}
|
||||
if starred != 1 || recent == 0 {
|
||||
|
||||
Reference in new issue
Block a user