-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
139 lines (119 loc) · 3.39 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"strings"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/hamstah/awstools/common"
log "github.com/sirupsen/logrus"
)
var (
username = kingpin.Flag("username", "Username to fetch the keys for, otherwise default to the logged in user.").Short('u').String()
encoding = kingpin.Flag("key-encoding", "Encoding of the key to return (SSH or PEM)").Default("SSH").Enum("PEM", "SSH")
groups = kingpin.Flag("allowed-group", "Fetch the keys only if the user is in this group. You can use --allowed-group multiple times.").Strings()
)
func main() {
kingpin.CommandLine.Name = "iam-public-ssh-keys"
kingpin.CommandLine.Help = "Return public SSH keys for an IAM user."
flags := common.HandleFlags()
session, conf := common.OpenSession(flags)
iamClient := iam.New(session, conf)
if len(*username) == 0 {
stsClient := sts.New(session, conf)
res, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
common.FatalOnError(err)
p := strings.Split(*res.Arn, "/")
username = aws.String(p[len(p)-1])
}
if len(*groups) != 0 {
found, err := checkGroups(iamClient, username, groups)
common.FatalOnError(err)
if !found {
common.Fatalln("User is not part of the allowed group")
}
}
sshKeys, err := getSSHKeys(iamClient, username)
common.FatalOnError(err)
for _, sshKey := range sshKeys {
fmt.Println(*sshKey)
}
}
func getSSHKeys(iamClient *iam.IAM, username *string) ([]*string, error) {
sshParams := &iam.ListSSHPublicKeysInput{
UserName: username,
}
sshKeyIDs := make(chan *string, 100)
sshKeys := make(chan *string, 100)
for w := 0; w < 5; w++ {
go worker(iamClient, encoding, username, sshKeyIDs, sshKeys)
}
maxPage := 10
pageNum := 0
total := 0
err := iamClient.ListSSHPublicKeysPages(sshParams,
func(page *iam.ListSSHPublicKeysOutput, lastPage bool) bool {
pageNum++
for _, sshKey := range page.SSHPublicKeys {
if *sshKey.Status == "Active" {
sshKeyIDs <- sshKey.SSHPublicKeyId
total++
}
}
return pageNum <= maxPage
})
if err != nil {
return nil, err
}
result := make([]*string, 0, total)
for a := 0; a < total; a++ {
res := <-sshKeys
if res != nil {
result = append(result, res)
}
}
return result, nil
}
func checkGroups(iamClient *iam.IAM, username *string, groups *[]string) (bool, error) {
maxPage := 10
lookup := make(map[string]bool, len(*groups))
for _, group := range *groups {
lookup[group] = true
}
groupsParams := &iam.ListGroupsForUserInput{
UserName: username,
}
pageNum := 0
found := false
err := iamClient.ListGroupsForUserPages(groupsParams,
func(page *iam.ListGroupsForUserOutput, lastPage bool) bool {
pageNum++
for _, group := range page.Groups {
if lookup[*group.GroupName] {
found = true
return false
}
}
return pageNum <= maxPage
})
if err != nil {
return false, err
}
return found, nil
}
func worker(iamClient *iam.IAM, encoding, username *string, sshKeysIDs <-chan *string, sshKeys chan<- *string) {
for sshKeyID := range sshKeysIDs {
result, err := iamClient.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{
SSHPublicKeyId: sshKeyID,
Encoding: encoding,
UserName: username,
})
if err != nil {
log.Error(err)
sshKeys <- nil
} else {
sshKeys <- result.SSHPublicKey.SSHPublicKeyBody
}
}
}