-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyinfo.go
105 lines (90 loc) · 2.53 KB
/
keyinfo.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
package hsm
import (
"fmt"
"time"
"github.com/go-phorce/dolly/cmd/dollypki/cli"
"github.com/go-phorce/dolly/ctl"
"github.com/go-phorce/dolly/xpki/cryptoprov"
"github.com/juju/errors"
)
// KeyInfoFlags specifies flags for the key info action
type KeyInfoFlags struct {
// Token specifies slot token
Token *string
// Serial specifies slot serial
Serial *string
// ID specifies key Id
ID *string
// Prefix specifies if public key should be listed
Public *bool
}
func ensureKeyInfoFlags(f *KeyInfoFlags) *KeyInfoFlags {
var (
emptyString = ""
falseVal = false
)
if f.Token == nil {
f.Token = &emptyString
}
if f.Serial == nil {
f.Serial = &emptyString
}
if f.ID == nil {
f.ID = &emptyString
}
if f.Public == nil {
f.Public = &falseVal
}
return f
}
// KeyInfo retrieves info about a key
func KeyInfo(c ctl.Control, p interface{}) error {
flags := ensureKeyInfoFlags(p.(*KeyInfoFlags))
keyProv, ok := c.(*cli.Cli).CryptoProv().Default().(cryptoprov.KeyManager)
if !ok {
return errors.Errorf("unsupported command for this crypto provider")
}
filterSerial := *flags.Serial
isDefaultSlot := filterSerial == ""
if isDefaultSlot {
filterSerial = "--@--"
}
out := c.Writer()
slotCount := 0
printSlot := func(slotID uint, description, label, manufacturer, model, serial string) error {
if isDefaultSlot || serial == filterSerial {
slotCount++
fmt.Fprintf(out, "Slot: %d\n", slotID)
fmt.Fprintf(out, " Description: %s\n", description)
fmt.Fprintf(out, " Token serial: %s\n", serial)
count := 0
err := keyProv.KeyInfo(slotID, *flags.ID, *flags.Public, func(id, label, typ, class, currentVersionID, pubKey string, creationTime *time.Time) error {
count++
fmt.Fprintf(out, "[%d]\n", count)
fmt.Fprintf(out, " Id: %s\n", id)
fmt.Fprintf(out, " Label: %s\n", label)
fmt.Fprintf(out, " Type: %s\n", typ)
fmt.Fprintf(out, " Class: %s\n", class)
fmt.Fprintf(out, " Version: %s\n", currentVersionID)
fmt.Fprintf(out, " Public key: \n%s\n", pubKey)
if creationTime != nil {
fmt.Fprintf(out, " Created: %s\n", creationTime.Format(time.RFC3339))
}
return nil
})
if err != nil {
fmt.Fprintf(out, "failed to get key info on slot %d, keyID %s: %v\n", slotID, *flags.ID, err)
return nil
}
if count == 0 {
fmt.Fprintf(out, "no keys found with ID: %s\n", *flags.ID)
}
}
return nil
}
keyProv.EnumTokens(isDefaultSlot, printSlot)
if slotCount == 0 {
fmt.Fprintf(out, "no slots found with serial: %s\n", filterSerial)
}
return nil
}