11package elasticsearch
22
33import (
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 }
0 commit comments