-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (58 loc) · 1.59 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
package main
import (
"bytes"
"context"
b64 "encoding/base64"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"os"
)
type Response events.APIGatewayProxyResponse
type Event struct {
ControlValue string `json:"control_value"`
}
func GuessSecret(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {
apiEvent := Event{}
if err := json.Unmarshal([]byte(request.Body), &apiEvent); err != nil {
return Response{StatusCode: 400}, err
}
kmsSvc := kms.New(session.Must(session.NewSession()))
//Get encrypted password from environment
encryptedPassword, err := b64.URLEncoding.DecodeString(os.Getenv("password"))
if err != nil {
return Response{StatusCode: 400}, err
}
input := &kms.DecryptInput{
CiphertextBlob: encryptedPassword,
}
//Decode encrypted password
result, err := kmsSvc.Decrypt(input)
if err != nil {
return Response{StatusCode: 500}, err
}
//Do something with plain text password value
isSecretValueCorrect := apiEvent.ControlValue == string(result.Plaintext)
body, err := json.Marshal(map[string]interface{}{
"is_secret_correct": isSecretValueCorrect,
})
if err != nil {
return Response{StatusCode: 500}, err
}
var buf bytes.Buffer
json.HTMLEscape(&buf, body)
resp := Response{
StatusCode: 200,
IsBase64Encoded: false,
Body: buf.String(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return resp, nil
}
func main() {
lambda.Start(GuessSecret)
}