-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh_keys.go
50 lines (35 loc) · 1.01 KB
/
ssh_keys.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
package directory
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
)
func onlyActiveKeys(keys []*iam.SSHPublicKeyMetadata) []*iam.SSHPublicKeyMetadata {
var active []*iam.SSHPublicKeyMetadata
for _, key := range keys {
if aws.StringValue(key.Status) != iam.StatusTypeActive {
continue
}
active = append(active, key)
}
return active
}
func GetActiveSshPublicKeys(username string) ([]*iam.SSHPublicKeyMetadata, error) {
svc := getIamService()
resp, err := svc.ListSSHPublicKeys(&iam.ListSSHPublicKeysInput{UserName: &username})
if err != nil {
return nil, err
}
return onlyActiveKeys(resp.SSHPublicKeys), nil
}
func GetSshEncodedPublicKey(username, keyID *string) (*string, error) {
svc := getIamService()
keyref, err := svc.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{
SSHPublicKeyId: keyID,
UserName: username,
Encoding: aws.String(iam.EncodingTypeSsh),
})
if err != nil {
return nil, err
}
return keyref.SSHPublicKey.SSHPublicKeyBody, nil
}