Skip to content

Commit d74aa9c

Browse files
authored
Merge pull request #68 from appbaseio/feat/function_middleware
feat: Open-Faas functions integration
2 parents 1d1c5f0 + 0b3de60 commit d74aa9c

11 files changed

Lines changed: 134 additions & 26 deletions

File tree

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
22
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
33
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
44
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
5+
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
6+
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
7+
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
58
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
69
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
710
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
811
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
12+
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
913
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
14+
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
1015
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
16+
github.com/antlr/antlr4 v0.0.0-20191011202612-ad2bd05285ca h1:QHbltbNkVcw97h4zA/L8gA4o3dJiFvBZ0gyZHrYXHbs=
17+
github.com/antlr/antlr4 v0.0.0-20191011202612-ad2bd05285ca/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
18+
github.com/antonmedv/expr v1.4.2 h1:88UiG54tE+9QaqwasWcvUCGWYVOmqdJMzBTSGNkCZPA=
19+
github.com/antonmedv/expr v1.4.2/go.mod h1:xesgliOuukGf21740qhh8PvFdN66yZ9lJJ/PzSFAmzI=
1120
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
1221
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
1322
github.com/aws/aws-sdk-go v1.19.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
@@ -36,6 +45,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
3645
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
3746
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
3847
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
48+
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
49+
github.com/gdamore/tcell v1.1.2/go.mod h1:h3kq4HO9l2On+V9ed8w8ewqQEmGCSSHOgQ+2h8uzurE=
3950
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
4051
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
4152
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
@@ -274,6 +285,7 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
274285
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
275286
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
276287
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
288+
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
277289
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
278290
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe h1:W/GaMY0y69G4cFlmsC6B9sbuo2fP8OFP1ABjt4kPz+w=
279291
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
@@ -302,6 +314,7 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
302314
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
303315
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
304316
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
317+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
305318
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
306319
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
307320
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
@@ -334,10 +347,13 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
334347
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
335348
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
336349
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
350+
github.com/prometheus/common v0.2.0 h1:kUZDBDTdBVBYBj5Tmh2NZLlF60mfjA27rM34b+cVwNU=
337351
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
338352
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
339353
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
340354
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
355+
github.com/rivo/tview v0.0.0-20190515161233-bd836ef13b4b/go.mod h1:+rKjP5+h9HMwWRpAfhIkkQ9KE3m3Nz5rwn7YtUpwgqk=
356+
github.com/rivo/uniseg v0.0.0-20190513083848-b9f5b9457d44/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
341357
github.com/robfig/cron v1.1.0 h1:jk4/Hud3TTdcrJgUOBgsqrZBarcxl6ADIjSC2iniwLY=
342358
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
343359
github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -346,6 +362,7 @@ github.com/rogpeppe/go-internal v1.2.2 h1:J7U/N7eRtzjhs26d6GqMh2HBuXP8/Z64Densii
346362
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
347363
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
348364
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
365+
github.com/sanity-io/litter v1.1.0/go.mod h1:CJ0VCw2q4qKU7LaQr3n7UOSHzgEMgcGco7N/SkZQPjw=
349366
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
350367
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
351368
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
@@ -361,6 +378,7 @@ github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjM
361378
github.com/sirupsen/logrus v1.1.0/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
362379
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
363380
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
381+
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
364382
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
365383
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
366384
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
@@ -468,6 +486,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
468486
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
469487
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
470488
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
489+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
490+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
471491
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
472492
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
473493
golang.org/x/tools v0.0.0-20181003024731-2f84ea8ef872/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -505,6 +525,7 @@ google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRn
505525
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
506526
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
507527
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
528+
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
508529
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
509530
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
510531
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

main.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ func main() {
171171
// ES client instantiation
172172
// ES v7 and v6 clients
173173
util.NewClient()
174+
// map of specific plugins
175+
sequencedPlugins := []string{"rules.so", "functions.so", "querytranslate.so", "analytics.so"}
176+
sequencedPluginsByPath := make(map[string]string)
174177

175178
var elasticSearchPath string
176179
elasticSearchMiddleware := make([]middleware.Middleware, 0)
@@ -179,16 +182,31 @@ func main() {
179182
return err
180183
}
181184
if !info.IsDir() && filepath.Ext(info.Name()) == ".so" && info.Name() != "elasticsearch.so" {
182-
mw, err1 := LoadPluginFromFile(router, path)
183-
if err1 != nil {
184-
return err1
185+
if util.IsExists(info.Name(), sequencedPlugins) {
186+
sequencedPluginsByPath[info.Name()] = path
187+
} else {
188+
mw, err1 := LoadPluginFromFile(router, path)
189+
if err1 != nil {
190+
return err1
191+
}
192+
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
185193
}
186-
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
187194
} else if info.Name() == "elasticsearch.so" {
188195
elasticSearchPath = path
189196
}
190197
return nil
191198
})
199+
// load plugins in a sequence
200+
for _, pluginName := range sequencedPlugins {
201+
path, _ := sequencedPluginsByPath[pluginName]
202+
if path != "" {
203+
mw, err := LoadPluginFromFile(router, path)
204+
if err != nil {
205+
log.Fatal("error loading plugins: ", err)
206+
}
207+
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
208+
}
209+
}
192210
LoadESPluginFromFile(router, elasticSearchPath, elasticSearchMiddleware)
193211
if err != nil {
194212
log.Fatal("error loading plugins: ", err)

model/category/category.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
Templates
3434
Suggestions
3535
Auth
36+
Functions
3637
)
3738

3839
// String is an implementation of Stringer interface that returns the string representation of category.Categories.
@@ -52,6 +53,7 @@ func (c Category) String() string {
5253
"templates",
5354
"suggestions",
5455
"auth",
56+
"functions",
5557
}[c]
5658
}
5759

@@ -91,6 +93,8 @@ func (c *Category) UnmarshalJSON(bytes []byte) error {
9193
*c = Suggestions
9294
case Auth.String():
9395
*c = Auth
96+
case Functions.String():
97+
*c = Functions
9498
default:
9599
return fmt.Errorf("invalid category encountered: %v", category)
96100
}
@@ -129,6 +133,8 @@ func (c Category) MarshalJSON() ([]byte, error) {
129133
category = Suggestions.String()
130134
case Auth:
131135
category = Auth.String()
136+
case Functions:
137+
category = Functions.String()
132138
default:
133139
return nil, fmt.Errorf("invalid category encountered: %v" + c.String())
134140
}

model/permission/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
category.Templates,
2828
category.Suggestions,
2929
category.Auth,
30+
category.Functions,
3031
}
3132

3233
defaultOps = []op.Operation{
@@ -55,6 +56,7 @@ var (
5556
SuggestionsLimit: 10,
5657
StreamsLimit: 10,
5758
AuthLimit: 10,
59+
FunctionsLimit: 10,
5860
}
5961

6062
defaultAdminLimits = Limits{
@@ -73,5 +75,6 @@ var (
7375
SuggestionsLimit: 30,
7476
StreamsLimit: 30,
7577
AuthLimit: 30,
78+
FunctionsLimit: 30,
7679
}
7780
)

model/permission/permission.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type Limits struct {
6969
SuggestionsLimit int64 `json:"suggestions_limit"`
7070
StreamsLimit int64 `json:"streams_limit"`
7171
AuthLimit int64 `json:"auth_limit"`
72+
FunctionsLimit int64 `json:"functions_limit"`
7273
}
7374

7475
// Options is a function type used to define a permission's properties.
@@ -465,6 +466,8 @@ func (p *Permission) GetLimitFor(c category.Category) (int64, error) {
465466
return p.Limits.AuthLimit, nil
466467
case category.Streams:
467468
return p.Limits.StreamsLimit, nil
469+
case category.Functions:
470+
return p.Limits.FunctionsLimit, nil
468471
default:
469472
return -1, fmt.Errorf(`we do not rate limit "%s" category`, c)
470473
}
@@ -575,7 +578,10 @@ func (p *Permission) GetPatch(rolePatched bool) (map[string]interface{}, error)
575578
limits["streams_limit"] = p.Limits.StreamsLimit
576579
}
577580
if p.Limits.AuthLimit != 0 {
578-
limits["streams_limit"] = p.Limits.AuthLimit
581+
limits["auth_limit"] = p.Limits.AuthLimit
582+
}
583+
if p.Limits.FunctionsLimit != 0 {
584+
limits["functions_limit"] = p.Limits.FunctionsLimit
579585
}
580586

581587
patch["limits"] = limits

model/user/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
category.Rules,
2727
category.Templates,
2828
category.Suggestions,
29+
category.Functions,
2930
category.Auth,
3031
}
3132

plugins/auth/e2e_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var adminCategories = []category.Category{
2626
category.Templates,
2727
category.Suggestions,
2828
category.Auth,
29+
category.Functions,
2930
}
3031

3132
var adminOps = []op.Operation{
@@ -50,6 +51,7 @@ var defaultAdminLimits = permission.Limits{
5051
SuggestionsLimit: 30,
5152
StreamsLimit: 30,
5253
AuthLimit: 30,
54+
FunctionsLimit: 30,
5355
}
5456

5557
var createPermissionResponse = map[string]interface{}{

plugins/elasticsearch/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func list() []middleware.Middleware {
4949
validate.ACL(),
5050
validate.Operation(),
5151
validate.PermissionExpiry(),
52+
// TODO: move transform request logic to querytranslate plugin
5253
transformRequest,
5354
}
5455
}

plugins/logs/middleware.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,26 @@ func classifyCategory(h http.HandlerFunc) http.HandlerFunc {
5050
}
5151
}
5252

53+
type Request struct {
54+
URI string `json:"uri"`
55+
Method string `json:"method"`
56+
Headers map[string][]string `json:"header"`
57+
Body string `json:"body"`
58+
}
59+
60+
type Response struct {
61+
Code int `json:"code"`
62+
Status string `json:"status"`
63+
Headers map[string][]string
64+
Body string `json:"body"`
65+
}
66+
5367
type record struct {
54-
Indices []string `json:"indices"`
55-
Category category.Category `json:"category"`
56-
Request struct {
57-
URI string `json:"uri"`
58-
Method string `json:"method"`
59-
Headers map[string][]string `json:"header"`
60-
Body string `json:"body"`
61-
} `json:"request"`
62-
Response struct {
63-
Code int `json:"code"`
64-
Status string `json:"status"`
65-
Headers map[string][]string
66-
Body string `json:"body"`
67-
} `json:"response"`
68-
Timestamp time.Time `json:"timestamp"`
68+
Indices []string `json:"indices"`
69+
Category category.Category `json:"category"`
70+
Request Request `json:"request"`
71+
Response Response `json:"response"`
72+
Timestamp time.Time `json:"timestamp"`
6973
}
7074

7175
// Recorder records a log "record" for every request.
@@ -88,8 +92,21 @@ func (l *Logs) recorder(h http.HandlerFunc) http.HandlerFunc {
8892
util.WriteBackError(w, "Can't read request body", http.StatusInternalServerError)
8993
return
9094
}
95+
9196
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
9297

98+
var headers = make(map[string][]string)
99+
100+
for key, values := range r.Header {
101+
headers[key] = values
102+
}
103+
104+
request := Request{
105+
URI: r.URL.Path,
106+
Headers: headers,
107+
Body: string(reqBody),
108+
Method: r.Method,
109+
}
93110
// Serve using response recorder
94111
respRecorder := httptest.NewRecorder()
95112
h(respRecorder, r)
@@ -102,11 +119,11 @@ func (l *Logs) recorder(h http.HandlerFunc) http.HandlerFunc {
102119
w.Write(respRecorder.Body.Bytes())
103120

104121
// Record the document
105-
go l.recordResponse(reqBody, respRecorder, r)
122+
go l.recordResponse(&request, respRecorder, r)
106123
}
107124
}
108125

109-
func (l *Logs) recordResponse(reqBody []byte, w *httptest.ResponseRecorder, req *http.Request) {
126+
func (l *Logs) recordResponse(request *Request, w *httptest.ResponseRecorder, req *http.Request) {
110127
ctx := req.Context()
111128

112129
reqCategory, err := category.FromContext(ctx)
@@ -127,10 +144,7 @@ func (l *Logs) recordResponse(reqBody []byte, w *httptest.ResponseRecorder, req
127144
rec.Timestamp = time.Now()
128145

129146
// record request
130-
rec.Request.URI = req.URL.Path
131-
rec.Request.Headers = req.Header
132-
rec.Request.Method = req.Method
133-
rec.Request.Body = string(reqBody)
147+
rec.Request = *request
134148

135149
// record response
136150
response := w.Result()

plugins/permissions/e2e_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var adminCategories = []category.Category{
2626
category.Rules,
2727
category.Templates,
2828
category.Suggestions,
29+
category.Functions,
2930
category.Auth,
3031
}
3132

@@ -51,6 +52,7 @@ var defaultAdminLimits = permission.Limits{
5152
SuggestionsLimit: 30,
5253
StreamsLimit: 30,
5354
AuthLimit: 30,
55+
FunctionsLimit: 30,
5456
}
5557

5658
var createPermissionResponse = map[string]interface{}{

0 commit comments

Comments
 (0)