-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathkey.go
47 lines (42 loc) · 970 Bytes
/
key.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
package commands
import (
"fmt"
"github.com/abiosoft/ishell"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
saws "github.com/bwhaley/ssmsh/aws"
)
const keyUsage string = `
key ARN|ID
Set the KMS key ARN (or ID) to use with SecureString parameters
`
func key(c *ishell.Context) {
if len(c.Args) != 1 {
shell.Println(keyUsage)
return
}
if err := checkKey(c.Args[0]); err != nil {
shell.Println(err)
}
ps.Key = c.Args[0]
}
func checkKey(key string) (err error) {
client := kms.New(saws.NewSession(ps.Region, ps.Profile))
input := kms.ListKeysInput{}
for {
resp, err := client.ListKeys(&input)
if err != nil {
return err
}
for _, keyEntry := range resp.Keys {
if aws.StringValue(keyEntry.KeyId) == key || aws.StringValue(keyEntry.KeyArn) == key {
return nil
}
}
if resp.NextMarker == nil {
break
}
input.Marker = resp.NextMarker
}
return fmt.Errorf("key %s not found in this region", key)
}