-
Notifications
You must be signed in to change notification settings - Fork 18
/
ec2_ssh.go
82 lines (67 loc) · 1.8 KB
/
ec2_ssh.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
package aws
import (
"github.com/Qovery/pleco/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
"time"
)
type KeyPair struct {
KeyName string
KeyId string
CreationDate time.Time
Tag string
ttl int64
IsProtected bool
}
func getSshKeys(ec2session *ec2.EC2, tagName string) []KeyPair {
result, err := ec2session.DescribeKeyPairs(
&ec2.DescribeKeyPairsInput{})
if err != nil {
log.Error(err)
return nil
}
var keys []KeyPair
for _, key := range result.KeyPairs {
creationTime, ttl, isProtected, _, _ := utils.GetEssentialTags(key.Tags, tagName)
newKey := KeyPair{
KeyName: *key.KeyName,
KeyId: *key.KeyPairId,
CreationDate: creationTime,
ttl: ttl,
IsProtected: isProtected,
}
keys = append(keys, newKey)
}
return keys
}
func deleteKeyPair(ec2session *ec2.EC2, keyId string) error {
_, err := ec2session.DeleteKeyPair(
&ec2.DeleteKeyPairInput{
KeyPairId: aws.String(keyId),
})
return err
}
func DeleteExpiredKeyPairs(ec2session *ec2.EC2, tagName string, dryRun bool) {
keys := getSshKeys(ec2session, tagName)
region := ec2session.Config.Region
var expiredKeys []KeyPair
for _, key := range keys {
if utils.CheckIfExpired(key.CreationDate, key.ttl, "ec2 key pair: "+key.KeyId) && !key.IsProtected {
expiredKeys = append(expiredKeys, key)
}
}
count, start := utils.ElemToDeleteFormattedInfos("expired ELB load balancer", len(expiredKeys), *region)
log.Debug(count)
if dryRun || len(expiredKeys) == 0 {
return
}
log.Debug(start)
for _, key := range expiredKeys {
deletionErr := deleteKeyPair(ec2session, key.KeyId)
if deletionErr != nil {
log.Errorf("Deletion EC2 key pair error %s/%s: %s",
key.KeyName, *region, deletionErr)
}
}
}