This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add iam user SSH public key support (#533)
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
1 parent
cedff5e
commit ad29ed9
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |