-
Notifications
You must be signed in to change notification settings - Fork 9
/
kms.go
101 lines (90 loc) · 2.21 KB
/
kms.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
package main
import (
"encoding/base64"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/sam701/awstools/colors"
"github.com/sam701/awstools/config"
"github.com/sam701/awstools/sess"
"github.com/sam701/tcolor"
"github.com/urfave/cli"
)
func kmsAction(c *cli.Context) error {
txt := c.Args().First()
if c.Bool("list-keys") {
listKmsKeys()
return nil
}
if txt == "" {
cli.ShowCommandHelp(c, "kms")
return nil
}
quiet := c.Bool("quiet")
cl := kms.New(sess.FromEnvVar())
if c.Bool("decrypt") {
bb, err := base64.StdEncoding.DecodeString(txt)
if err != nil {
log.Fatalln("ERROR", err)
}
out, err := cl.Decrypt(&kms.DecryptInput{
CiphertextBlob: bb,
})
if err != nil {
log.Fatalln("ERROR", err)
}
if !quiet {
fmt.Print("Decrypted: ")
}
fmt.Println(string(out.Plaintext))
} else if c.Bool("encrypt") {
keyId := c.String("key-id")
if keyId == "" {
keyId = config.Current.DefaultKmsKey
}
if keyId == "" {
log.Fatalln("No key-id provided")
}
out, err := cl.Encrypt(&kms.EncryptInput{
KeyId: aws.String(keyId),
Plaintext: []byte(txt),
})
if err != nil {
log.Fatalln("ERROR", err)
}
if !quiet {
fmt.Print("Encrypted: ")
}
fmt.Println(base64.StdEncoding.EncodeToString(out.CiphertextBlob))
} else {
cli.ShowCommandHelp(c, "kms")
}
return nil
}
func listKmsKeys() {
cl := kms.New(sess.FromEnvVar())
out, err := cl.ListAliases(&kms.ListAliasesInput{})
if err != nil {
log.Fatalln("ERROR", err)
}
for _, v := range out.Aliases {
res, err := cl.DescribeKey(&kms.DescribeKeyInput{
KeyId: v.AliasArn,
})
if err != nil {
log.Fatalln("ERROR", err)
}
md := res.KeyMetadata
fmt.Println(tcolor.Colorize(*md.Arn, tcolor.New().Foreground(tcolor.BrightGreen)))
fmt.Println(formatProp("Alias"), tcolor.Colorize(*v.AliasName, tcolor.New().Foreground(tcolor.BrightRed)))
fmt.Println(formatProp("Description"), *md.Description)
fmt.Println(formatProp("Created"), *md.CreationDate)
fmt.Println(formatProp("Enabled"), *md.Enabled)
fmt.Println(formatProp("Usage"), *md.KeyUsage)
fmt.Println()
}
}
func formatProp(prop string) string {
return colors.Property(fmt.Sprintf(" %-15s", prop+":"))
}