Skip to content

Commit

Permalink
Merge pull request #68 from appbaseio/feat/function_middleware
Browse files Browse the repository at this point in the history
feat: Open-Faas functions integration
  • Loading branch information
lakhansamani authored Jan 31, 2020
2 parents 1d1c5f0 + 0b3de60 commit d74aa9c
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 26 deletions.
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antlr/antlr4 v0.0.0-20191011202612-ad2bd05285ca h1:QHbltbNkVcw97h4zA/L8gA4o3dJiFvBZ0gyZHrYXHbs=
github.com/antlr/antlr4 v0.0.0-20191011202612-ad2bd05285ca/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antonmedv/expr v1.4.2 h1:88UiG54tE+9QaqwasWcvUCGWYVOmqdJMzBTSGNkCZPA=
github.com/antonmedv/expr v1.4.2/go.mod h1:xesgliOuukGf21740qhh8PvFdN66yZ9lJJ/PzSFAmzI=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aws/aws-sdk-go v1.19.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
Expand Down Expand Up @@ -36,6 +45,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.1.2/go.mod h1:h3kq4HO9l2On+V9ed8w8ewqQEmGCSSHOgQ+2h8uzurE=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
Expand Down Expand Up @@ -274,6 +285,7 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe h1:W/GaMY0y69G4cFlmsC6B9sbuo2fP8OFP1ABjt4kPz+w=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -302,6 +314,7 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
Expand Down Expand Up @@ -334,10 +347,13 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.2.0 h1:kUZDBDTdBVBYBj5Tmh2NZLlF60mfjA27rM34b+cVwNU=
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rivo/tview v0.0.0-20190515161233-bd836ef13b4b/go.mod h1:+rKjP5+h9HMwWRpAfhIkkQ9KE3m3Nz5rwn7YtUpwgqk=
github.com/rivo/uniseg v0.0.0-20190513083848-b9f5b9457d44/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron v1.1.0 h1:jk4/Hud3TTdcrJgUOBgsqrZBarcxl6ADIjSC2iniwLY=
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand All @@ -346,6 +362,7 @@ github.com/rogpeppe/go-internal v1.2.2 h1:J7U/N7eRtzjhs26d6GqMh2HBuXP8/Z64Densii
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/sanity-io/litter v1.1.0/go.mod h1:CJ0VCw2q4qKU7LaQr3n7UOSHzgEMgcGco7N/SkZQPjw=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand All @@ -361,6 +378,7 @@ github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjM
github.com/sirupsen/logrus v1.1.0/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down Expand Up @@ -468,6 +486,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181003024731-2f84ea8ef872/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -505,6 +525,7 @@ google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
26 changes: 22 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func main() {
// ES client instantiation
// ES v7 and v6 clients
util.NewClient()
// map of specific plugins
sequencedPlugins := []string{"rules.so", "functions.so", "querytranslate.so", "analytics.so"}
sequencedPluginsByPath := make(map[string]string)

var elasticSearchPath string
elasticSearchMiddleware := make([]middleware.Middleware, 0)
Expand All @@ -179,16 +182,31 @@ func main() {
return err
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".so" && info.Name() != "elasticsearch.so" {
mw, err1 := LoadPluginFromFile(router, path)
if err1 != nil {
return err1
if util.IsExists(info.Name(), sequencedPlugins) {
sequencedPluginsByPath[info.Name()] = path
} else {
mw, err1 := LoadPluginFromFile(router, path)
if err1 != nil {
return err1
}
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
}
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
} else if info.Name() == "elasticsearch.so" {
elasticSearchPath = path
}
return nil
})
// load plugins in a sequence
for _, pluginName := range sequencedPlugins {
path, _ := sequencedPluginsByPath[pluginName]
if path != "" {
mw, err := LoadPluginFromFile(router, path)
if err != nil {
log.Fatal("error loading plugins: ", err)
}
elasticSearchMiddleware = append(elasticSearchMiddleware, mw...)
}
}
LoadESPluginFromFile(router, elasticSearchPath, elasticSearchMiddleware)
if err != nil {
log.Fatal("error loading plugins: ", err)
Expand Down
6 changes: 6 additions & 0 deletions model/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
Templates
Suggestions
Auth
Functions
)

// String is an implementation of Stringer interface that returns the string representation of category.Categories.
Expand All @@ -52,6 +53,7 @@ func (c Category) String() string {
"templates",
"suggestions",
"auth",
"functions",
}[c]
}

Expand Down Expand Up @@ -91,6 +93,8 @@ func (c *Category) UnmarshalJSON(bytes []byte) error {
*c = Suggestions
case Auth.String():
*c = Auth
case Functions.String():
*c = Functions
default:
return fmt.Errorf("invalid category encountered: %v", category)
}
Expand Down Expand Up @@ -129,6 +133,8 @@ func (c Category) MarshalJSON() ([]byte, error) {
category = Suggestions.String()
case Auth:
category = Auth.String()
case Functions:
category = Functions.String()
default:
return nil, fmt.Errorf("invalid category encountered: %v" + c.String())
}
Expand Down
3 changes: 3 additions & 0 deletions model/permission/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
category.Templates,
category.Suggestions,
category.Auth,
category.Functions,
}

defaultOps = []op.Operation{
Expand Down Expand Up @@ -55,6 +56,7 @@ var (
SuggestionsLimit: 10,
StreamsLimit: 10,
AuthLimit: 10,
FunctionsLimit: 10,
}

defaultAdminLimits = Limits{
Expand All @@ -73,5 +75,6 @@ var (
SuggestionsLimit: 30,
StreamsLimit: 30,
AuthLimit: 30,
FunctionsLimit: 30,
}
)
8 changes: 7 additions & 1 deletion model/permission/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Limits struct {
SuggestionsLimit int64 `json:"suggestions_limit"`
StreamsLimit int64 `json:"streams_limit"`
AuthLimit int64 `json:"auth_limit"`
FunctionsLimit int64 `json:"functions_limit"`
}

// Options is a function type used to define a permission's properties.
Expand Down Expand Up @@ -465,6 +466,8 @@ func (p *Permission) GetLimitFor(c category.Category) (int64, error) {
return p.Limits.AuthLimit, nil
case category.Streams:
return p.Limits.StreamsLimit, nil
case category.Functions:
return p.Limits.FunctionsLimit, nil
default:
return -1, fmt.Errorf(`we do not rate limit "%s" category`, c)
}
Expand Down Expand Up @@ -575,7 +578,10 @@ func (p *Permission) GetPatch(rolePatched bool) (map[string]interface{}, error)
limits["streams_limit"] = p.Limits.StreamsLimit
}
if p.Limits.AuthLimit != 0 {
limits["streams_limit"] = p.Limits.AuthLimit
limits["auth_limit"] = p.Limits.AuthLimit
}
if p.Limits.FunctionsLimit != 0 {
limits["functions_limit"] = p.Limits.FunctionsLimit
}

patch["limits"] = limits
Expand Down
1 change: 1 addition & 0 deletions model/user/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
category.Rules,
category.Templates,
category.Suggestions,
category.Functions,
category.Auth,
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/auth/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var adminCategories = []category.Category{
category.Templates,
category.Suggestions,
category.Auth,
category.Functions,
}

var adminOps = []op.Operation{
Expand All @@ -50,6 +51,7 @@ var defaultAdminLimits = permission.Limits{
SuggestionsLimit: 30,
StreamsLimit: 30,
AuthLimit: 30,
FunctionsLimit: 30,
}

var createPermissionResponse = map[string]interface{}{
Expand Down
1 change: 1 addition & 0 deletions plugins/elasticsearch/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func list() []middleware.Middleware {
validate.ACL(),
validate.Operation(),
validate.PermissionExpiry(),
// TODO: move transform request logic to querytranslate plugin
transformRequest,
}
}
Expand Down
56 changes: 35 additions & 21 deletions plugins/logs/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,26 @@ func classifyCategory(h http.HandlerFunc) http.HandlerFunc {
}
}

type Request struct {
URI string `json:"uri"`
Method string `json:"method"`
Headers map[string][]string `json:"header"`
Body string `json:"body"`
}

type Response struct {
Code int `json:"code"`
Status string `json:"status"`
Headers map[string][]string
Body string `json:"body"`
}

type record struct {
Indices []string `json:"indices"`
Category category.Category `json:"category"`
Request struct {
URI string `json:"uri"`
Method string `json:"method"`
Headers map[string][]string `json:"header"`
Body string `json:"body"`
} `json:"request"`
Response struct {
Code int `json:"code"`
Status string `json:"status"`
Headers map[string][]string
Body string `json:"body"`
} `json:"response"`
Timestamp time.Time `json:"timestamp"`
Indices []string `json:"indices"`
Category category.Category `json:"category"`
Request Request `json:"request"`
Response Response `json:"response"`
Timestamp time.Time `json:"timestamp"`
}

// Recorder records a log "record" for every request.
Expand All @@ -88,8 +92,21 @@ func (l *Logs) recorder(h http.HandlerFunc) http.HandlerFunc {
util.WriteBackError(w, "Can't read request body", http.StatusInternalServerError)
return
}

r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))

var headers = make(map[string][]string)

for key, values := range r.Header {
headers[key] = values
}

request := Request{
URI: r.URL.Path,
Headers: headers,
Body: string(reqBody),
Method: r.Method,
}
// Serve using response recorder
respRecorder := httptest.NewRecorder()
h(respRecorder, r)
Expand All @@ -102,11 +119,11 @@ func (l *Logs) recorder(h http.HandlerFunc) http.HandlerFunc {
w.Write(respRecorder.Body.Bytes())

// Record the document
go l.recordResponse(reqBody, respRecorder, r)
go l.recordResponse(&request, respRecorder, r)
}
}

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

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

// record request
rec.Request.URI = req.URL.Path
rec.Request.Headers = req.Header
rec.Request.Method = req.Method
rec.Request.Body = string(reqBody)
rec.Request = *request

// record response
response := w.Result()
Expand Down
2 changes: 2 additions & 0 deletions plugins/permissions/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var adminCategories = []category.Category{
category.Rules,
category.Templates,
category.Suggestions,
category.Functions,
category.Auth,
}

Expand All @@ -51,6 +52,7 @@ var defaultAdminLimits = permission.Limits{
SuggestionsLimit: 30,
StreamsLimit: 30,
AuthLimit: 30,
FunctionsLimit: 30,
}

var createPermissionResponse = map[string]interface{}{
Expand Down
Loading

0 comments on commit d74aa9c

Please sign in to comment.