-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
304 lines (264 loc) · 8.36 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/joncalhoun/qson"
"github.com/openfaas-incubator/connector-sdk/types"
"github.com/sirupsen/logrus"
)
// storageGridConfiguration holds the configuration required to consume and process storage grid events.
type storageGridConfiguration struct {
// routes are REST endpoints for incoming events
// +required
routes map[string]*routeConfiguration
// port to run the http server on
// +required
port string
// logger
logger *logrus.Logger
// controller is a connector SDK controller
controller *types.Controller
}
// routeConfiguration configures a REST endpoint that consumes StorageGrid events
type routeConfiguration struct {
// REST endpoint
endpoint string
// prefixFilter is a filter applied as prefix on object key which caused the notification.
// +optional
prefixFilter string
// suffixFilters is a filter applied as suffix on object key which caused the notification.
// +optional
suffixFilter string
}
// storageGridNotification is the bucket notification received from storage grid
type storageGridNotification struct {
Action string `json:"Action"`
Message struct {
Records []struct {
EventVersion string `json:"eventVersion"`
EventSource string `json:"eventSource"`
EventTime time.Time `json:"eventTime"`
EventName string `json:"eventName"`
UserIdentity struct {
PrincipalID string `json:"principalId"`
} `json:"userIdentity"`
RequestParameters struct {
SourceIPAddress string `json:"sourceIPAddress"`
} `json:"requestParameters"`
ResponseElements struct {
XAmzRequestID string `json:"x-amz-request-id"`
} `json:"responseElements"`
S3 struct {
S3SchemaVersion string `json:"s3SchemaVersion"`
ConfigurationID string `json:"configurationId"`
Bucket struct {
Name string `json:"name"`
OwnerIdentity struct {
PrincipalID string `json:"principalId"`
} `json:"ownerIdentity"`
Arn string `json:"arn"`
} `json:"bucket"`
Object struct {
Key string `json:"key"`
Size int `json:"size"`
ETag string `json:"eTag"`
Sequencer string `json:"sequencer"`
} `json:"object"`
} `json:"s3"`
} `json:"Records"`
} `json:"Message"`
TopicArn string `json:"TopicArn"`
Version string `json:"Version"`
}
var (
respBody = `
<PublishResponse xmlns="http://argoevents-sns-server/">
<PublishResult>
<MessageId>` + generateUUID().String() + `</MessageId>
</PublishResult>
<ResponseMetadata>
<RequestId>` + generateUUID().String() + `</RequestId>
</ResponseMetadata>
</PublishResponse>` + "\n"
)
// generateUUID returns a new uuid
func generateUUID() uuid.UUID {
return uuid.New()
}
func main() {
creds := types.GetCredentials()
gatewayURL, ok := os.LookupEnv("GATEWAY_URL")
if !ok {
panic("gateway url is not specified")
}
upstreamTimeout := time.Second * 30
rebuildInterval := time.Second * 3
if val, exists := os.LookupEnv("upstream_timeout"); exists {
parsedVal, err := time.ParseDuration(val)
if err == nil {
upstreamTimeout = parsedVal
}
}
if val, exists := os.LookupEnv("rebuild_interval"); exists {
parsedVal, err := time.ParseDuration(val)
if err == nil {
rebuildInterval = parsedVal
}
}
printResponse := false
if val, exists := os.LookupEnv("print_response"); exists {
printResponse = val == "1" || val == "true"
}
printResponseBody := false
if val, exists := os.LookupEnv("print_response_body"); exists {
printResponseBody = val == "1" || val == "true"
}
delimiter := ","
if val, exists := os.LookupEnv("topic_delimiter"); exists {
if len(val) > 0 {
delimiter = val
}
}
asynchronousInvocation := false
if val, exists := os.LookupEnv("asynchronous_invocation"); exists {
asynchronousInvocation = val == "1" || val == "true"
}
config := &types.ControllerConfig{
UpstreamTimeout: upstreamTimeout,
GatewayURL: gatewayURL,
PrintResponse: printResponse,
PrintResponseBody: printResponseBody,
RebuildInterval: rebuildInterval,
TopicAnnotationDelimiter: delimiter,
AsyncFunctionInvocation: asynchronousInvocation,
}
controller := types.NewController(creds, config)
controller.BeginMapBuilder()
r := mux.NewRouter()
sgCfg, err := newStorageGridConfiguration()
if err != nil {
panic(err)
}
sgCfg.controller = controller
for endpoint, _ := range sgCfg.routes {
r.HandleFunc(endpoint, sgCfg.handler).Methods(http.MethodHead, http.MethodPost)
}
server := http.Server{
Addr: fmt.Sprintf(":%s", sgCfg.port),
Handler: r,
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}
func (sgCfg *storageGridConfiguration) handler(writer http.ResponseWriter, request *http.Request) {
sgCfg.logger.WithField("endpoint", request.URL.Path).Infoln("received a notification")
if _, ok := sgCfg.routes[request.URL.Path]; !ok {
sgCfg.logger.WithField("endpoint", request.URL.Path).Errorln("unknown route")
writer.WriteHeader(http.StatusNotFound)
writer.Write([]byte("unknown route"))
return
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
sgCfg.logger.WithError(err).Errorln("failed to parse request body")
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte("malformed request body"))
return
}
switch request.Method {
case http.MethodHead:
respBody = ""
}
writer.WriteHeader(http.StatusOK)
writer.Header().Add("Content-Type", "text/plain")
writer.Write([]byte(respBody))
// notification received from storage grid is url encoded.
parsedURL, err := url.QueryUnescape(string(body))
if err != nil {
sgCfg.logger.WithError(err).Errorln("failed to unescape request body url")
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte("malformed query url"))
return
}
b, err := qson.ToJSON(parsedURL)
if err != nil {
sgCfg.logger.WithError(err).Errorln("failed to convert request body in JSON format")
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("failed to parse request body"))
return
}
var notification *storageGridNotification
err = json.Unmarshal(b, ¬ification)
if err != nil {
sgCfg.logger.WithError(err).Errorln("failed to unmarshal request body")
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("failed to construct the notification"))
return
}
if !filterName(notification, sgCfg.routes[request.URL.Path]) {
sgCfg.logger.Warn("discarding notification since it did not pass all filters")
}
writer.WriteHeader(http.StatusOK)
writer.Write([]byte("ok"))
sgCfg.controller.Invoke(strings.TrimLeft(request.URL.Path, "/"), &body)
}
// filterName filters object key based on configured prefix and/or suffix
func filterName(notification *storageGridNotification, rc *routeConfiguration) bool {
if rc.prefixFilter != "" && rc.suffixFilter != "" {
return strings.HasPrefix(notification.Message.Records[0].S3.Object.Key, rc.prefixFilter) && strings.HasSuffix(notification.Message.Records[0].S3.Object.Key, rc.suffixFilter)
}
if rc.prefixFilter != "" {
return strings.HasPrefix(notification.Message.Records[0].S3.Object.Key, rc.prefixFilter)
}
if rc.suffixFilter != "" {
return strings.HasSuffix(notification.Message.Records[0].S3.Object.Key, rc.suffixFilter)
}
return true
}
func newStorageGridConfiguration() (*storageGridConfiguration, error) {
routesStr, ok := os.LookupEnv("STORAGE_GRID_ROUTES")
if !ok {
return nil, fmt.Errorf("route configuration is not specified")
}
port, ok := os.LookupEnv("PORT")
if !ok {
return nil, fmt.Errorf("port is not specified")
}
routes, err := parseRoutes(routesStr)
if err != nil {
return nil, err
}
return &storageGridConfiguration{
port: port,
routes: routes,
}, nil
}
// parseRoutes constructs a route configurations.
func parseRoutes(routesStr string) (map[string]*routeConfiguration, error) {
routes := strings.Split(routesStr, ";")
if len(routes) == 0 {
return nil, fmt.Errorf("no routes are configured")
}
routeCfgs := map[string]*routeConfiguration{}
for _, routeCfg := range routes {
cfg := strings.Split(routeCfg, ",")
if len(cfg) != 3 {
return nil, fmt.Errorf("route %s is misconfigured", routeCfg)
}
routeCfgs[cfg[0]] = &routeConfiguration{
endpoint: cfg[0],
prefixFilter: cfg[2],
suffixFilter: cfg[3],
}
}
return routeCfgs, nil
}