-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfunction.go
424 lines (382 loc) · 13.4 KB
/
function.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package function
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"time"
"cloud.google.com/go/pubsub"
secretmanager "cloud.google.com/go/secretmanager/apiv1"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/google/cel-go/cel"
celtypes "github.com/google/cel-go/common/types"
"google.golang.org/api/option"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var VERSION string = "0.2.0"
var requestControlExpr cel.Program
var extractMessage cel.Program
var responseBody cel.Program
var defaultResponseBody = true
var pubsubTopic string
var cloudProjectId string
var userAgent string
type GetPubsubTopicFunc func(ctx context.Context, cloudProjectId string, userAgent string, pubsubTopic string) (*pubsub.Topic, error)
func getPubsubTopicReal(ctx context.Context, cloudProjectId string, userAgent string, pubsubTopic string) (*pubsub.Topic, error) {
client, err := pubsub.NewClient(ctx, cloudProjectId, option.WithUserAgent(userAgent))
if err != nil {
return nil, err
}
topic := client.Topic(pubsubTopic)
return topic, nil
}
var GetPubsubTopic GetPubsubTopicFunc = getPubsubTopicReal
func getEnvironmentVariable(name string, required bool) (string, error) {
envVar := os.Getenv(name)
if envVar == "" && required {
log.Fatal().Msgf("Environment variable %s is required.", name)
}
// Check if this is a Secret Manager secret
if strings.HasPrefix(envVar, "gsm:") {
ctx := context.Background()
client, err := secretmanager.NewClient(ctx, option.WithUserAgent(userAgent))
if err != nil {
log.Fatal().Err(err).Msgf("Failed to setup Secret Manager client: %v", err)
}
defer client.Close()
accessRequest := &secretmanagerpb.AccessSecretVersionRequest{
Name: envVar[4:],
}
result, err := client.AccessSecretVersion(ctx, accessRequest)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to access Secret %s: %v", envVar[4:], err)
}
return string(result.Payload.Data), nil
}
return envVar, nil
}
func setControlCel(celEnv *cel.Env, controlCel string) (err error) {
requestControlExpr, err = GetCelProgram(celEnv, controlCel, true)
if err != nil {
return err
}
return nil
}
func setMessageCel(celEnv *cel.Env, messageCel string) (err error) {
extractMessage, err = GetCelProgram(celEnv, messageCel, false)
if err != nil {
return err
}
return nil
}
func setResponseBodyCel(celEnv *cel.Env, bodyCel string) (err error) {
if bodyCel != "" {
responseBody, err = GetCelProgram(celEnv, bodyCel, false)
if err != nil {
return err
}
defaultResponseBody = false
} else {
defaultResponseBody = true
}
return nil
}
func Startup() (port string) {
port = os.Getenv("PORT")
if port == "" {
// Probably not running as function, configure a more console friendly logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
port = "8080"
}
log.Info().Msgf("Starting Json2Pubsub server, version %s...", VERSION)
userAgent = fmt.Sprintf("google-pso-tool/json2pubsub/%s", VERSION)
cloudProjectId = os.Getenv("GOOGLE_CLOUD_PROJECT")
if cloudProjectId == "" {
log.Fatal().Msg("Environment variable GOOGLE_CLOUD_PROJECT is not set.")
}
log.Info().Msgf("Using cloud project: %s", cloudProjectId)
var err error
pubsubTopic, err = getEnvironmentVariable("PUBSUB_TOPIC", true)
if err != nil {
log.Fatal().Msgf("Failed to get PUBSUB_TOPIC: %v", err)
}
log.Info().Msgf("Using Pub/Sub topic: %s", pubsubTopic)
handlerUrl, err := getEnvironmentVariable("CUSTOM_HANDLER", false)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to get CUSTOM_HANDLER: %v", err)
}
if handlerUrl == "" {
handlerUrl = "/"
} else {
log.Info().Msgf("Using custom handler location: %s", handlerUrl)
}
http.HandleFunc("/", RequestHandler)
celEnv, err := GetCelEnv()
if err != nil {
log.Fatal().Err(err).Msgf("Failed to setup CEL environment: %v", err)
}
controlCel, err := getEnvironmentVariable("CONTROL_CEL", true)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to get CONTROL_CEL: %v", err)
}
err = setControlCel(celEnv, controlCel)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to compile request control CEL program (%s): %v", controlCel, err)
}
messageCel, err := getEnvironmentVariable("MESSAGE_CEL", true)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to get MESSAGE_CEL: %v", err)
}
err = setMessageCel(celEnv, messageCel)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to compile message extract CEL program (%s): %v", messageCel, err)
}
responseCel, err := getEnvironmentVariable("RESPONSE_CEL", false)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to get RESPONSE_CEL: %v", err)
}
err = setResponseBodyCel(celEnv, responseCel)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to compile message extract CEL program (%s): %v", messageCel, err)
}
if responseCel == "" {
log.Info().Msg("No response body CEL specified, using empty responses.")
}
return port
}
func init() {
// Slightly ugly hack for testing
if !strings.HasSuffix(os.Args[0], ".test") {
Startup()
} else {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
functions.HTTP("Json2Pubsub", RequestHandler)
}
func RequestHandler(w http.ResponseWriter, r *http.Request) {
originIp, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Warn().Err(err).Str("RemoteAddr", r.RemoteAddr).Msg("Unable to parse remote address")
w.WriteHeader(http.StatusBadRequest)
return
}
// Only accept POST requests
if r.Method != "POST" {
log.Warn().Str("RemoteAddr", r.RemoteAddr).Msg("Not a POST request.")
w.WriteHeader(http.StatusBadRequest)
return
}
postBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Warn().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to read request body: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(postBody) == 0 {
log.Warn().Str("RemoteAddr", r.RemoteAddr).Msg("Empty request body.")
w.WriteHeader(http.StatusBadRequest)
return
}
requestHeaders := make(map[string]string, 0)
for key := range r.Header {
requestHeaders[strings.ToLower(key)] = r.Header.Get(key)
}
var postValues url.Values
contentType := r.Header.Get("Content-Type")
if contentType == "application/x-www-form-urlencoded" {
postValues, err = url.ParseQuery(string(postBody))
if err != nil {
log.Warn().Str("RemoteAddr", r.RemoteAddr).Msg("Failed to parse post body for content type application/x-www-form-urlencoded")
w.WriteHeader(http.StatusBadRequest)
return
}
}
var jsonBody interface{}
if contentType == "application/json" || contentType == "text/json" {
if json.Valid([]byte(postBody)) {
json.Unmarshal([]byte(postBody), &jsonBody)
} else {
log.Warn().Str("RemoteAddr", r.RemoteAddr).Msg("Invalid JSON body.")
w.WriteHeader(http.StatusBadRequest)
return
}
} else {
jsonBody = postValues
}
// Set up request structure
currentTime := time.Now()
currentTimeUTC := currentTime.UTC()
celParams := map[string]interface{}{
"origin": map[string]interface{}{
"ip": originIp,
},
"request": map[string]interface{}{
"body": string(postBody),
"method": r.Method,
"path": r.URL.RawPath,
"scheme": r.URL.Scheme,
"query": r.URL.RawQuery,
"json": jsonBody,
"post": postValues,
"headers": requestHeaders,
"unixtime": currentTime.Unix(),
"time": map[string]int{
"year": currentTimeUTC.Year(),
"month": int(currentTimeUTC.Month()),
"day": currentTimeUTC.Day(),
"hour": currentTimeUTC.Hour(),
"minute": currentTimeUTC.Minute(),
"second": currentTimeUTC.Second(),
},
},
}
out, _, err := requestControlExpr.Eval(celParams)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to evaluate request control check: %v", out)
w.WriteHeader(http.StatusInternalServerError)
return
}
result, ok := out.(celtypes.Bool)
if !ok || result.Equal(celtypes.False).Value().(bool) {
log.Error().Str("RemoteAddr", r.RemoteAddr).Msgf("Request control check failed: %v", out)
w.WriteHeader(http.StatusForbidden)
return
}
messageOut, _, err := extractMessage.Eval(celParams)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to evaluate message extraction: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var messageJson interface{}
if messageOut.Type() == celtypes.StringType {
_messageOut, err := messageOut.ConvertToNative(reflect.TypeOf(""))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert message output to string: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if json.Valid([]byte(_messageOut.(string))) {
json.Unmarshal([]byte(_messageOut.(string)), &messageJson)
} else {
log.Error().Str("RemoteAddr", r.RemoteAddr).Str("json", _messageOut.(string)).Msg("Invalid JSON string from message CEL.")
w.WriteHeader(http.StatusBadRequest)
return
}
} else if messageOut.Type() == celtypes.MapType {
_messageOut, err := messageOut.ConvertToNative(reflect.TypeOf(map[string]interface{}{}))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert message output to map: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
messageJson = _messageOut.(map[string]interface{})
} else if messageOut.Type() == celtypes.ListType {
_messageOut, err := messageOut.ConvertToNative(reflect.TypeOf([]interface{}{}))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert message output to list: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
messageJson = _messageOut.([]interface{})
}
if messageJson == nil {
log.Error().Str("RemoteAddr", r.RemoteAddr).Msg("Failed to turn request into a Pub/Sub message.")
w.WriteHeader(http.StatusBadRequest)
return
}
messageData, err := json.Marshal(messageJson)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Unable to marshal message data to JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
msg := &pubsub.Message{
Data: messageData,
}
topic, err := GetPubsubTopic(r.Context(), cloudProjectId, userAgent, pubsubTopic)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msg("Unable to get Pub/Sub client for topic.")
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Info().Str("RemoteAddr", r.RemoteAddr).Msgf("Publishing a message to topic %s, len=%d bytes", topic.String(), len(msg.Data))
if _, err := topic.Publish(r.Context(), msg).Get(r.Context()); err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Unable to publish message to Pub/Sub topic: %v", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
if !defaultResponseBody {
bodyOut, _, err := responseBody.Eval(celParams)
if err != nil || bodyOut == nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to evaluate response boy: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if bodyOut.Type() == celtypes.StringType {
_bodyOut, err := bodyOut.ConvertToNative(reflect.TypeOf(""))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert response body to string: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(_bodyOut.(string)))
} else if bodyOut.Type() == celtypes.MapType {
_bodyOut, err := bodyOut.ConvertToNative(reflect.TypeOf(map[string]interface{}{}))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert response body to map: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_bodyAsMap := _bodyOut.(map[string]interface{})
_bodyJson, err := json.Marshal(_bodyAsMap)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert response body map to JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(_bodyJson)
} else if bodyOut.Type() == celtypes.ListType {
_bodyOut, err := bodyOut.ConvertToNative(reflect.TypeOf([]interface{}{}))
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert response body to list: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_bodyAsList := _bodyOut.([]interface{})
_bodyJson, err := json.Marshal(_bodyAsList)
if err != nil {
log.Error().Err(err).Str("RemoteAddr", r.RemoteAddr).Msgf("Failed to convert response body list to JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(_bodyJson)
}
} else {
w.WriteHeader(http.StatusOK)
}
}