From ad29ed90baff4241879cc53a5ed28a6fc23724da Mon Sep 17 00:00:00 2001 From: Ryan Swanson Date: Sat, 1 Aug 2020 03:25:15 -0600 Subject: [PATCH] add iam user SSH public key support (#533) Co-authored-by: Tom Neyland Co-authored-by: Matt Shipman Co-authored-by: Tom Neyland Co-authored-by: Matt Shipman Co-authored-by: Joey Davenport --- resources/iam-user-ssh-keys.go | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 resources/iam-user-ssh-keys.go diff --git a/resources/iam-user-ssh-keys.go b/resources/iam-user-ssh-keys.go new file mode 100644 index 000000000..24fbd1344 --- /dev/null +++ b/resources/iam-user-ssh-keys.go @@ -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 +}