Skip to content

Commit 92ae88f

Browse files
authored
Merge pull request #47 from appbaseio/feat/source_filter
feat: add support for source_filtering
2 parents 7531873 + 517d6f9 commit 92ae88f

3 files changed

Lines changed: 106 additions & 5 deletions

File tree

model/permission/permission.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type Permission struct {
4646
TTL time.Duration `json:"ttl"`
4747
Limits *Limits `json:"limits"`
4848
Description string `json:"description"`
49+
Includes []string `json:"include_fields"`
50+
Excludes []string `json:"exclude_fields"`
4951
}
5052

5153
// Limits defines the rate limits for each category.
@@ -159,6 +161,22 @@ func SetSources(sources []string) Options {
159161
}
160162
}
161163

164+
// SetIncludes sets the includes fields
165+
func SetIncludes(includes []string) Options {
166+
return func(p *Permission) error {
167+
p.Includes = includes
168+
return nil
169+
}
170+
}
171+
172+
// SetExcludes sets the excludes fields
173+
func SetExcludes(excludes []string) Options {
174+
return func(p *Permission) error {
175+
p.Excludes = excludes
176+
return nil
177+
}
178+
}
179+
162180
func validateSources(sources []string) error {
163181
for _, source := range sources {
164182
_, _, err := net.ParseCIDR(source)
@@ -557,6 +575,12 @@ func (p *Permission) GetPatch(rolePatched bool) (map[string]interface{}, error)
557575
if p.Description != "" {
558576
patch["description"] = p.Description
559577
}
578+
if p.Includes != nil {
579+
patch["include_fields"] = p.Includes
580+
}
581+
if p.Excludes != nil {
582+
patch["exclude_fields"] = p.Excludes
583+
}
560584

561585
return patch, nil
562586
}

plugins/elasticsearch/middleware.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package elasticsearch
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
7+
"io/ioutil"
58
"log"
69
"net/http"
10+
"strings"
711

812
"github.com/appbaseio/arc/middleware"
913
"github.com/appbaseio/arc/middleware/classify"
@@ -13,6 +17,7 @@ import (
1317
"github.com/appbaseio/arc/model/acl"
1418
"github.com/appbaseio/arc/model/category"
1519
"github.com/appbaseio/arc/model/op"
20+
"github.com/appbaseio/arc/model/permission"
1621
"github.com/appbaseio/arc/plugins/auth"
1722
"github.com/appbaseio/arc/plugins/logs"
1823
"github.com/appbaseio/arc/util"
@@ -126,7 +131,73 @@ func transformRequest(h http.HandlerFunc) http.HandlerFunc {
126131
}
127132
// transform POST request(search) to GET
128133
if *reqACL == category.Search {
129-
req.Method = http.MethodGet
134+
isMsearch := strings.HasSuffix(req.URL.String(), "/_msearch")
135+
// Apply source filters
136+
reqPermission, err := permission.FromContext(ctx)
137+
if err != nil {
138+
log.Printf("%s: %v\n", logTag, err)
139+
h(w, req)
140+
return
141+
}
142+
sources := make(map[string]interface{})
143+
var Includes, Excludes []string
144+
Includes = reqPermission.Includes
145+
Excludes = reqPermission.Excludes
146+
if len(Includes) > 0 {
147+
sources["includes"] = Includes
148+
}
149+
if len(Excludes) > 0 {
150+
sources["excludes"] = Excludes
151+
}
152+
_, isExcludesPresent := sources["excludes"]
153+
isDefaultInclude := len(Includes) > 0 && Includes[0] == "*"
154+
shouldApplyFilters := !isDefaultInclude || isExcludesPresent
155+
if shouldApplyFilters {
156+
if isMsearch {
157+
// Handle the _msearch requests
158+
body, err := ioutil.ReadAll(req.Body)
159+
if err != nil {
160+
log.Printf("%s: %v\n", logTag, err)
161+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
162+
return
163+
}
164+
var reqBodyString = string(body)
165+
splitReq := strings.Split(reqBodyString, "\n")
166+
var modifiedBodyString string
167+
for index, element := range splitReq {
168+
if index%2 == 1 { // even lines
169+
var reqBody = make(map[string]interface{})
170+
err := json.Unmarshal([]byte(element), &reqBody)
171+
if err != nil {
172+
log.Printf("%s: %v\n", logTag, err)
173+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
174+
return
175+
}
176+
reqBody["_source"] = sources
177+
raw, _ := json.Marshal(reqBody)
178+
modifiedBodyString += string(raw)
179+
} else {
180+
modifiedBodyString += element
181+
}
182+
modifiedBodyString += "\n"
183+
}
184+
modifiedBody := []byte(modifiedBodyString)
185+
req.Body = ioutil.NopCloser(bytes.NewReader(modifiedBody))
186+
} else {
187+
body, err := ioutil.ReadAll(req.Body)
188+
if err != nil {
189+
log.Printf("%s: %v\n", logTag, err)
190+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
191+
return
192+
}
193+
d := json.NewDecoder(ioutil.NopCloser(bytes.NewReader(body)))
194+
reqBody := make(map[string]interface{})
195+
d.Decode(&reqBody)
196+
reqBody["_source"] = sources
197+
modifiedBody, _ := json.Marshal(reqBody)
198+
req.Body = ioutil.NopCloser(bytes.NewReader(modifiedBody))
199+
}
200+
}
130201
}
131202
h(w, req)
132203
}

plugins/permissions/handlers.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ func (p *permissions) postPermission(opts ...permission.Options) http.HandlerFun
7878
if permissionBody.Referers != nil {
7979
opts = append(opts, permission.SetReferers(permissionBody.Referers))
8080
}
81+
if permissionBody.Includes != nil {
82+
opts = append(opts, permission.SetIncludes(permissionBody.Includes))
83+
}
84+
if permissionBody.Excludes != nil {
85+
opts = append(opts, permission.SetExcludes(permissionBody.Excludes))
86+
}
8187
if permissionBody.Indices != nil {
8288
opts = append(opts, permission.SetIndices(permissionBody.Indices))
8389
}
@@ -270,7 +276,7 @@ func (p *permissions) getUserPermissions() http.HandlerFunc {
270276
}
271277

272278
func (p *permissions) role() http.HandlerFunc {
273-
return func (w http.ResponseWriter, req *http.Request) {
279+
return func(w http.ResponseWriter, req *http.Request) {
274280
vars := mux.Vars(req)
275281
role := vars["name"]
276282

@@ -290,7 +296,7 @@ func (p *permissions) role() http.HandlerFunc {
290296
msg := fmt.Sprintf(`an error occurred while fetching permissions for role=%s`, role)
291297
log.Printf("%s: %s: %v\n", logTag, msg, err)
292298
util.WriteBackError(w, msg, http.StatusInternalServerError)
293-
return
299+
return
294300
}
295301
}
296302

@@ -301,10 +307,10 @@ func (p *permissions) role() http.HandlerFunc {
301307
p.postPermission(permission.SetRole(role))(w, req)
302308
return
303309
case http.MethodPatch:
304-
http.Redirect(w, req, "/_permission/" + perm.Username, 308)
310+
http.Redirect(w, req, "/_permission/"+perm.Username, 308)
305311
return
306312
case http.MethodDelete:
307-
http.Redirect(w, req, "/_permission/" + perm.Username, 308)
313+
http.Redirect(w, req, "/_permission/"+perm.Username, 308)
308314
return
309315
}
310316
}

0 commit comments

Comments
 (0)