-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_lambda_linux_amd64.go
157 lines (129 loc) · 4.14 KB
/
main_lambda_linux_amd64.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
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/cockroachdb/errors"
"github.com/sue445/gitpanda/gitlab"
"github.com/sue445/gitpanda/webhook"
"net/http"
"os"
"strings"
)
func startLambda() {
lambda.Start(lambdaHandler)
}
func lambdaHandler(_ context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
body := strings.TrimSpace(request.Body)
if isDebugLogging {
fmt.Printf("[DEBUG] lambdaHandler: body=%s\n", body)
}
sentryClient, releaseSentry, err := NewSentryClient(sentryDsn, isDebugLogging)
if err != nil {
fmt.Printf("Sentry initialization failed: %v\n", err)
}
defer releaseSentry()
response, err := lambdaMain(body)
if err != nil {
sentryClient.CaptureException(err)
fmt.Printf("[ERROR] %s, error=%v\n", response, err)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: response,
}, errors.WithStack(err)
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: response,
}, nil
}
func lambdaMain(body string) (string, error) {
slackOAuthAccessToken, err := GetParameterStoreOrEnv("SLACK_OAUTH_ACCESS_TOKEN", os.Getenv("SLACK_OAUTH_ACCESS_TOKEN_KEY"), true)
if err != nil {
return "Failed: slackOAuthAccessToken", errors.WithStack(err)
}
slackVerificationToken, err := GetParameterStoreOrEnv("SLACK_VERIFICATION_TOKEN", os.Getenv("SLACK_VERIFICATION_TOKEN_KEY"), false)
if err != nil {
return "Failed: slackVerificationToken", errors.WithStack(err)
}
gitlabAPIEndpoint, err := GetParameterStoreOrEnv("GITLAB_API_ENDPOINT", os.Getenv("GITLAB_API_ENDPOINT_KEY"), true)
if err != nil {
return "Failed: gitlabAPIEndpoint", errors.WithStack(err)
}
gitlabBaseURL, err := GetParameterStoreOrEnv("GITLAB_BASE_URL", os.Getenv("GITLAB_BASE_URL_KEY"), true)
if err != nil {
return "Failed: gitlabBaseURL", errors.WithStack(err)
}
gitlabPrivateToken, err := GetParameterStoreOrEnv("GITLAB_PRIVATE_TOKEN", os.Getenv("GITLAB_PRIVATE_TOKEN_KEY"), true)
if err != nil {
return "Failed: gitlabPrivateToken", errors.WithStack(err)
}
s := webhook.NewSlackWebhook(
slackOAuthAccessToken,
slackVerificationToken,
&gitlab.URLParserParams{
APIEndpoint: gitlabAPIEndpoint,
BaseURL: gitlabBaseURL,
PrivateToken: gitlabPrivateToken,
GitPandaVersion: Version,
IsDebugLogging: isDebugLogging,
},
)
response, err := s.Request(body, truncateLines)
if err != nil {
return response, errors.WithStack(err)
}
return response, nil
}
// GetParameterStoreOrEnv returns Environment variable or Parameter Store variable
func GetParameterStoreOrEnv(envKey string, parameterStoreKey string, required bool) (string, error) {
if parameterStoreKey != "" {
// Get from Parameter Store
d, err := NewSsmDecrypter()
if err != nil {
return "", errors.WithStack(err)
}
decryptedValue, err := d.Decrypt(parameterStoreKey)
if err != nil {
return fmt.Sprintf("Failed: Decrypt %s", parameterStoreKey), errors.WithStack(err)
}
return decryptedValue, nil
}
// Get from env
if os.Getenv(envKey) != "" {
return os.Getenv(envKey), nil
}
if required {
return "", fmt.Errorf("Either %s or %s is required", envKey, parameterStoreKey)
}
return "", nil
}
// SsmDecrypter stores the AWS Session used for SSM decrypter.
type SsmDecrypter struct {
client *ssm.Client
}
// NewSsmDecrypter returns a new SsmDecrypter.
func NewSsmDecrypter() (*SsmDecrypter, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, errors.WithStack(err)
}
client := ssm.NewFromConfig(cfg)
return &SsmDecrypter{client}, nil
}
// Decrypt decrypts string.
func (d *SsmDecrypter) Decrypt(encrypted string) (string, error) {
params := &ssm.GetParameterInput{
Name: aws.String(encrypted),
WithDecryption: aws.Bool(true),
}
resp, err := d.client.GetParameter(context.TODO(), params)
if err != nil {
return "", errors.WithStack(err)
}
return aws.ToString(resp.Parameter.Value), nil
}