-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
172 lines (142 loc) · 4.32 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
var (
metricNamespace string
metricName string
url string
secretName string
)
type request struct{}
type httpBasicAuth struct {
Username string
Password string
}
func handleRequest(ctx context.Context, req request) (string, error) {
metricNamespace = os.Getenv("CW_METRIC_NAMESPACE")
url = os.Getenv("TARGET_URL")
metricName = os.Getenv("CW_METRIC_NAME")
result := webIsReachable(url)
pushMetric(float64(result))
return "Finished", nil
}
func pushMetric(value float64) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(os.Getenv("REGION"))},
)
if err != nil {
log.Fatal("Error creating session")
}
svc := cloudwatch.New(sess)
now := time.Now()
_, err = svc.PutMetricData(&cloudwatch.PutMetricDataInput{
MetricData: []*cloudwatch.MetricDatum{
&cloudwatch.MetricDatum{
MetricName: &metricName,
Value: &value,
Timestamp: &now,
Dimensions: []*cloudwatch.Dimension{
&cloudwatch.Dimension{
Name: aws.String("HealthcheckTarget"),
Value: &url,
},
},
},
},
Namespace: &metricNamespace,
})
if err != nil {
log.Fatal(err)
}
}
func webIsReachable(web string) int {
var auth *httpBasicAuth
auth = getSecret()
client := &http.Client{}
req, err := http.NewRequest("GET", web, nil)
if err != nil {
panic(err)
}
if auth != nil {
req.SetBasicAuth(auth.Username, auth.Password)
}
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if err != nil {
_, netErrors := http.Get("https://www.google.com")
if netErrors != nil {
fmt.Fprintf(os.Stderr, "no internet\n")
os.Exit(1)
}
}
log.Print("Target website returned ", response.Status)
return response.StatusCode
}
func getSecret() *httpBasicAuth {
region := os.Getenv("REGION")
secretName = os.Getenv("SECRET_NAME")
//Create a Secrets Manager client
svc := secretsmanager.New(session.New(),
aws.NewConfig().WithRegion(region))
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified
}
// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
result, err := svc.GetSecretValue(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeDecryptionFailure:
// Secrets Manager can't decrypt the protected secret text using the provided KMS key.
fmt.Println(secretsmanager.ErrCodeDecryptionFailure, aerr.Error())
case secretsmanager.ErrCodeInternalServiceError:
// An error occurred on the server side.
fmt.Println(secretsmanager.ErrCodeInternalServiceError, aerr.Error())
case secretsmanager.ErrCodeInvalidParameterException:
// You provided an invalid value for a parameter.
fmt.Println(secretsmanager.ErrCodeInvalidParameterException, aerr.Error())
case secretsmanager.ErrCodeInvalidRequestException:
// You provided a parameter value that is not valid for the current state of the resource.
fmt.Println(secretsmanager.ErrCodeInvalidRequestException, aerr.Error())
case secretsmanager.ErrCodeResourceNotFoundException:
// We can't find the resource that you asked for.
fmt.Println(secretsmanager.ErrCodeResourceNotFoundException, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
panic(err)
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
var secretString string
if result.SecretString != nil { //pragma: allowlist secret
secretString = *result.SecretString
}
var httpBasicAuth httpBasicAuth
json.Unmarshal([]byte(secretString), &httpBasicAuth)
return &httpBasicAuth
}
func main() {
// handleRequest(nil, request{})
lambda.Start(handleRequest)
}