Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
add iam user SSH public key support (#533)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Neyland <tcneyland@gmail.com>
Co-authored-by: Matt Shipman <undermybed@undermybed.info>

Co-authored-by: Tom Neyland <tcneyland@gmail.com>
Co-authored-by: Matt Shipman <undermybed@undermybed.info>
Co-authored-by: Joey Davenport <jrsdav@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 1, 2020
1 parent cedff5e commit ad29ed9
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions resources/iam-user-ssh-keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type UserSSHKey struct {
svc *iam.IAM
userName string
sshKeyID string
}

func init() {
register("IAMUserSSHPublicKey", ListIAMUserSSHPublicKeys)
}

func ListIAMUserSSHPublicKeys(sess *session.Session) ([]Resource, error) {
svc := iam.New(sess)

usersOutput, err := svc.ListUsers(nil)
if err != nil {
return nil, err
}

var resources []Resource
for _, user := range usersOutput.Users {
listOutput, err := svc.ListSSHPublicKeys(&iam.ListSSHPublicKeysInput{
UserName: user.UserName,
})

if err != nil {
return nil, err
}

for _, publicKey := range listOutput.SSHPublicKeys {
resources = append(resources, &UserSSHKey{
svc: svc,
userName: *user.UserName,
sshKeyID: *publicKey.SSHPublicKeyId,
})
}
}

return resources, nil
}

func (u *UserSSHKey) Properties() types.Properties {
return types.NewProperties().
Set("UserName", u.userName).
Set("SSHKeyID", u.sshKeyID)
}

func (u *UserSSHKey) String() string {
return fmt.Sprintf("%s -> %s", u.userName, u.sshKeyID)
}

func (u *UserSSHKey) Remove() error {
_, err := u.svc.DeleteSSHPublicKey(&iam.DeleteSSHPublicKeyInput{
UserName: &u.userName,
SSHPublicKeyId: &u.sshKeyID,
})

return err
}

0 comments on commit ad29ed9

Please sign in to comment.