Skip to content

Commit dab7b87

Browse files
committed
feat: add support for source_filtering
1 parent 7531873 commit dab7b87

3 files changed

Lines changed: 71 additions & 4 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package elasticsearch
22

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

@@ -13,6 +16,7 @@ import (
1316
"github.com/appbaseio/arc/model/acl"
1417
"github.com/appbaseio/arc/model/category"
1518
"github.com/appbaseio/arc/model/op"
19+
"github.com/appbaseio/arc/model/permission"
1620
"github.com/appbaseio/arc/plugins/auth"
1721
"github.com/appbaseio/arc/plugins/logs"
1822
"github.com/appbaseio/arc/util"
@@ -127,6 +131,39 @@ func transformRequest(h http.HandlerFunc) http.HandlerFunc {
127131
// transform POST request(search) to GET
128132
if *reqACL == category.Search {
129133
req.Method = http.MethodGet
134+
// Apply source filters
135+
reqPermission, err := permission.FromContext(ctx)
136+
if err != nil {
137+
log.Printf("%s: %v\n", logTag, err)
138+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
139+
return
140+
}
141+
sources := make(map[string]interface{})
142+
var Includes, Excludes []string
143+
Includes = reqPermission.Includes
144+
Excludes = reqPermission.Excludes
145+
if len(Includes) > 0 {
146+
sources["includes"] = Includes
147+
}
148+
if len(Excludes) > 0 {
149+
sources["excludes"] = Excludes
150+
}
151+
body, err := ioutil.ReadAll(req.Body)
152+
if err != nil {
153+
log.Printf("%s: %v\n", logTag, err)
154+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
155+
return
156+
}
157+
d := json.NewDecoder(ioutil.NopCloser(bytes.NewReader(body)))
158+
reqBody := make(map[string]interface{})
159+
d.Decode(&reqBody)
160+
_, isExcludesPresent := sources["excludes"]
161+
isDefaultInclude := len(Includes) > 0 && Includes[0] == "*"
162+
if !isDefaultInclude || isExcludesPresent {
163+
reqBody["_source"] = sources
164+
}
165+
modifiedBody, _ := json.Marshal(reqBody)
166+
req.Body = ioutil.NopCloser(bytes.NewReader(modifiedBody))
130167
}
131168
h(w, req)
132169
}

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)