-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
64 lines (51 loc) · 1.24 KB
/
agent.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
/*
agent lister
*/
package main
import (
"fmt"
"net"
"strings"
"time"
"golang.org/x/crypto/ssh/agent"
)
// agentCerts returns the certificates in the agent connected at socket,
func agentCerts(socket string) ([]*agent.Key, error) {
conn, err := net.Dial("unix", socket)
if err != nil {
return []*agent.Key{}, fmt.Errorf("socket err: %v", err)
}
a := agent.NewClient(conn)
l, err := a.List()
if err != nil {
return l, fmt.Errorf("agent listing error: %v", err)
}
return l, nil
}
// keyFilter filters a key by string, an expiration duration window
// filtering the name or comment by the filter and either expired or
// expiring within a duration of expDur
func keyFilter(pk *agent.Key, filter string, expDur time.Duration) (*pubKey, error) {
c, err := newPubKey(pk)
if err != nil {
return c, fmt.Errorf("key parsing error: %w", err)
}
// skip non-certificate keys
if !c.isCert {
c.isCert = false
}
// a naive filter
c.filterMatch = true
if filter != "" && c.isCert {
c.filterMatch = false
f := strings.ToLower(filter)
if strings.Contains(strings.ToLower(c.key.Comment+c.key.Format), f) {
c.filterMatch = true
}
}
// expiration
c.expiring(expDur)
// mark filtered and expiring certificates
c.mark()
return c, nil
}