-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (212 loc) · 6.44 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
//Required ENV VARS
//DD_API_KEY
//DD_APP_KEY
//AWS_ACCOUNT_NAME
import (
"fmt"
"time"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/iam"
"encoding/json"
"github.com/zorkian/go-datadog-api"
"os"
"errors"
"github.com/aws/aws-lambda-go/lambda"
)
var awsRegions = []string{
"us-east-1",
"us-west-1",
"us-west-2",
"eu-west-1",
"eu-central-1",
"sa-east-1",
}
var debug = false
type awsELB struct {
Name string
CertID string
}
type certDetails struct {
Arn string
ExpirationDate time.Time
Name string
AttachedELBs []awsELB
ExpirationStatus string
Daysleft float64
}
func listCerts() []*iam.ServerCertificateMetadata {
log.Println("Processing IAM Certs")
session,err := session.NewSession()
if err != nil {
log.Fatalf("Could not create new AWS session, check credentials")
}
svc := iam.New(session)
params := &iam.ListServerCertificatesInput{}
resp, err := svc.ListServerCertificates(params)
if err != nil {
log.Fatal("there was an error listing certificates from AWS", err.Error())
}
return resp.ServerCertificateMetadataList
}
func listElbs() (elbList []*elb.LoadBalancerDescription) {
log.Println("Processing ELBs")
params := &elb.DescribeLoadBalancersInput{}
for _, region := range awsRegions {
svc := elb.New(session.New(&aws.Config{Region: aws.String(region)}))
log.Println("enumerating ELBs in:", region)
resp, err := svc.DescribeLoadBalancers(params)
if err != nil {
log.Fatal("there was an error listing ELBS in", region, err.Error())
}
for _, elb := range resp.LoadBalancerDescriptions {
elbList = append(elbList, elb)
}
}
return
}
func listELBsWithSSL(elbList []*elb.LoadBalancerDescription) (ELBsWithSSl []awsELB) {
for _, elb := range elbList {
for _, elbListener := range elb.ListenerDescriptions {
if *elbListener.Listener.Protocol == "HTTPS" || *elbListener.Listener.Protocol == "SSL" {
matchedElb := awsELB{
Name: *elb.DNSName,
CertID: *elbListener.Listener.SSLCertificateId,
}
ELBsWithSSl = append(ELBsWithSSl, matchedElb)
}
}
}
return
}
func dedupStringArray(stringArray []string) []string {
var DedupedArray []string //will hold the final results of the dedup process
//use hash map to dedup strings based on the fact that it does not allow duplicate keys.
//also avoids us having to iterate over the whole slice each time for existence checks
var a = make(map[string]struct{})
for _, s := range stringArray { //each unique string will only show once in this map
a[s] = struct{}{}
}
for key := range a { //take unique keys and append them to slice, slice only contains uniq values now
DedupedArray = append(DedupedArray,key)
}
return DedupedArray
}
func extractUniqueELBCerts(elbList *[]awsELB) []string {
var ELBCerts []string
for _, elb := range *elbList {
ELBCerts = append(ELBCerts, elb.CertID)
}
dedupedCertsList := dedupStringArray(ELBCerts)
return dedupedCertsList
}
func selectCertByArn(certList []*iam.ServerCertificateMetadata, certArn string) (iam.ServerCertificateMetadata,error) {
Certificate := iam.ServerCertificateMetadata{}
for _, certDetail := range certList {
if *certDetail.Arn == certArn {
return *certDetail,nil
}
}
return Certificate,errors.New("could not find cert")
}
func groupELBsWithCerts(elbList []awsELB, certList []*iam.ServerCertificateMetadata) []certDetails {
var CertDetailsList []certDetails
usedCerts := extractUniqueELBCerts(&elbList)
for _, certArn := range usedCerts {
var elbCollection []awsELB
for _, elb := range elbList {
if elb.CertID == certArn {
elbCollection = append(elbCollection, elb)
}
}
Details,err := selectCertByArn(certList, certArn)
if err != nil {
continue
}
certDetail := certDetails{Arn: certArn,
ExpirationDate: *Details.Expiration,
Name: *Details.ServerCertificateName,
AttachedELBs: elbCollection}
CertDetailsList = append(CertDetailsList,certDetail)
}
return CertDetailsList
}
func checkExpirationAndTriggerAlert(CertDetailsList []certDetails) []certDetails {
Out45 := float64(45)
Out30 := float64(30)
Out20 := float64(20)
Out10 := float64(10)
Out5 := float64(5)
Outnow := float64(1)
for index, certDetail := range CertDetailsList {
expiresInDays := certDetail.ExpirationDate.Sub(time.Now()).Hours() / 24
CertDetailsList[index].Daysleft = expiresInDays
switch {
case expiresInDays <= Outnow:
CertDetailsList[index].ExpirationStatus = "error"
case expiresInDays <= Out5:
CertDetailsList[index].ExpirationStatus = "error"
case expiresInDays <= Out10:
CertDetailsList[index].ExpirationStatus = "warning"
case expiresInDays <= Out20:
CertDetailsList[index].ExpirationStatus = "warning"
case expiresInDays <= Out30:
CertDetailsList[index].ExpirationStatus = "info"
case expiresInDays <= Out45:
CertDetailsList[index].ExpirationStatus = "info"
default:
CertDetailsList[index].ExpirationStatus = "GTG"
}
if CertDetailsList[index].ExpirationStatus != "GTG" {
postAlertEventDD(CertDetailsList[index])
}
}
return CertDetailsList
}
func postAlertEventDD(certInfo certDetails) {
certJSON, err := json.MarshalIndent(certInfo, "", " ")
if err != nil {
log.Println("Could not marshall cert info to json", err.Error())
}
description := fmt.Sprintf("Certificate: %v expiring in %0.f day(s).\n There are currently %v ELB(s) using this certificate. \n Details: %+v \n",
certInfo.Arn,
certInfo.Daysleft,
len(certInfo.AttachedELBs),
string(certJSON))
accountTag := fmt.Sprintf("aws_account:%s", os.Getenv("AWS_ACCOUNT_NAME"))
var tags = []string{accountTag}
if debug {
fmt.Println(description)
return
}
event := datadog.Event{
Title: "Certificate expiration notice",
Text: description,
Priority: "normal",
AlertType: certInfo.ExpirationStatus,
Aggregation: certInfo.Arn,
SourceType: "certificate_checker",
Tags: tags,
}
ddClient := datadog.NewClient(os.Getenv("DD_API_KEY"), os.Getenv("DD_APP_KEY"))
res, err := ddClient.PostEvent(&event)
if err != nil {
log.Println("Could not post event to DD", err.Error())
os.Exit(1)
}
log.Printf("Posted event for %s successfully. Event ID: %x", certInfo.Arn, res.Id)
}
func Handler() {
certs := listCerts()
elb := listElbs()
matching := listELBsWithSSL(elb)
groupedCerts := groupELBsWithCerts(matching, certs)
checkExpirationAndTriggerAlert(groupedCerts)
log.Println("Completed", time.Now())
}
func main() {
lambda.Start(Handler)
}