/
cache.go
109 lines (90 loc) · 2.88 KB
/
cache.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
package granted
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/AlecAivazis/survey/v2"
"github.com/common-fate/clio"
"github.com/common-fate/granted/pkg/securestorage"
"github.com/common-fate/granted/pkg/testable"
"github.com/urfave/cli/v2"
)
var CacheCommand = cli.Command{
Name: "cache",
Usage: "Manage your cached credentials that are stored in secure storage",
Subcommands: []*cli.Command{&ClearCommand, &ListCommand},
}
var ListCommand = cli.Command{
Name: "list",
Usage: "List currently cached credentials and secure storage type",
Action: func(c *cli.Context) error {
storageToNameMap := map[string]securestorage.SecureStorage{
"aws-iam-credentials": securestorage.NewSecureIAMCredentialStorage().SecureStorage,
"sso-token": securestorage.NewSecureSSOTokenStorage().SecureStorage,
"session-credentials": securestorage.NewSecureSessionCredentialStorage().SecureStorage,
}
tw := tabwriter.NewWriter(os.Stderr, 10, 1, 5, ' ', 0)
headers := strings.Join([]string{"STORAGE TYPE", "KEY"}, "\t")
fmt.Fprintln(tw, headers)
for storageName, v := range storageToNameMap {
keys, err := v.ListKeys()
if err != nil {
return err
}
for _, key := range keys {
tabbed := strings.Join([]string{storageName, key}, "\t")
fmt.Fprintln(tw, tabbed)
}
}
tw.Flush()
return nil
},
}
var ClearCommand = cli.Command{
Name: "clear",
Usage: "Clear cached credential from the secure storage",
Action: func(c *cli.Context) error {
withStdio := survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
in := survey.Select{
Message: "Select which secure storage would you like to clear cache from",
Options: []string{"aws-iam-credentials", "sso-token", "session-credentials"},
}
var selection string
clio.NewLine()
err := testable.AskOne(&in, &selection, withStdio)
if err != nil {
return err
}
storageToNameMap := map[string]securestorage.SecureStorage{
"aws-iam-credentials": securestorage.NewSecureIAMCredentialStorage().SecureStorage,
"sso-token": securestorage.NewSecureSSOTokenStorage().SecureStorage,
"session-credentials": securestorage.NewSecureSessionCredentialStorage().SecureStorage,
}
// store the credentials in secure storage
selectedStorage := storageToNameMap[selection]
keys, err := selectedStorage.ListKeys()
if err != nil {
return err
}
if len(keys) == 0 {
clio.Warnf("You do not have any cached credentials for %s storage", selection)
return nil
}
prompt := &survey.Select{
Message: "Select the profile name you want to clear cache for",
Options: keys,
}
var selectedProfile string
err = survey.AskOne(prompt, &selectedProfile)
if err != nil {
return err
}
err = selectedStorage.Clear(selectedProfile)
if err != nil {
return err
}
clio.Successf("successfully cleared the cached credentials for '%s'", selectedProfile)
return nil
},
}